找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 46|回复: 2

[Raylib项目设计/] Raylib中如何进行延时操作

[复制链接]

3

主题

12

回帖

230

积分

蓝锐

积分
230
发表于 7 天前 | 显示全部楼层 |阅读模式
本帖最后由 xinhaitianze 于 2025-4-19 16:44 编辑

延时操作
在软件、游戏领域中很多情况下都需要延时操作
例如:一个游戏的目标需要在限定时间内完成(这需要计时操作),或者一个游戏中的宝箱需要“解锁”这需要几秒钟的时间(这同样需要进行等待)
因此,延时操作是有必要的

如何实现?
延时操作最常见的方式便是直接调用函数:
Sleep()           <--- C中
time.sleep()    <--- Python的time模块中

但是,如果你真的在raylib中这么做了,你将会发现这似乎让整个主进程都停止了   它会阻塞你的整个主循环,操作系统可能会认为你的程序崩溃了
这断然不是我们所期望的效果,也没见过哪个公司的软件用着用着就出现“未响应”的情况

SO,我们如何避免?
在raylib的Wiki(https://github.com/raysan5/raylib/wiki/Frequently-Asked-Questions#how-do-i-make-a-timer)中有这么一段:
raylib has no built in timer system. You are expected to keep track of time in your own code. You can do with with the and functions. Below is an example of a simple timer struct and functions to use it.
raylib 没有内置的计时器系统。您需要在自己的代码中跟踪时间。您可以使用 and 函数。下面是一个简单的计时器结构和使用它的函数的示例
  1. typedef struct Timer {

  2.     double startTime;   // Start time (seconds)

  3.     double lifeTime;    // Lifetime (seconds)

  4. } Timer;



  5. void StartTimer(Timer *timer, double lifetime)

  6. {

  7.     timer->startTime = GetTime();

  8.     timer->lifeTime = lifetime;

  9. }



  10. bool TimerDone(Timer timer)

  11. {

  12.     return GetTime() - timer.startTime >= timer.lifeTime;

  13. }



  14. double GetElapsed(Timer timer)

  15. {

  16.     return GetTime() - timer.startTime;

  17. }
复制代码

这似乎就是我们寻求的答案。
而这些函数则是我们的突破口
于是,事情就变得简单了,我们只需理解并使用这些给出的函数即可,当然,它们大概只适用于raylib项目之中。

此程序的Hello world将在3秒后消失
  1. #include <raylib.h>

  2. // 定义圆角矩形函数
  3. void yuan_fang(float width, float height, float x, float y, float roundness, int segments, Color color) {
  4.     DrawRectangleRounded((Rectangle) { x, y, width, height }, roundness, segments, color);
  5. }

  6. typedef struct Timer {
  7.     double startTime;   // Start time (seconds)
  8.     double lifeTime;    // Lifetime (seconds)
  9. } Timer;

  10. void StartTimer(Timer *timer, double lifetime) {
  11.     timer->startTime = GetTime();
  12.     timer->lifeTime = lifetime;
  13. }

  14. bool TimerDone(Timer timer) {
  15.     return GetTime() - timer.startTime >= timer.lifeTime;
  16. }

  17. double GetElapsed(Timer timer) {
  18.     return GetTime() - timer.startTime;
  19. }

  20. int main(void) {
  21.     // 初始化窗口
  22.     InitWindow(200, 200, "Time");
  23.     SetTargetFPS(60);
  24.     // 初始化定时器
  25.     Timer timer;
  26.     StartTimer(&timer, 3.0); // 设置定时器持续时间为3秒

  27.     while (!WindowShouldClose()) {
  28.         BeginDrawing();
  29.         ClearBackground(WHITE);

  30.         if (!TimerDone(timer)) {
  31.             // 绘制文本
  32.           DrawText("Hello, World!", 10, 10, 20, BLACK); // 在 (10, 10) 位置绘制黑色文本,字体大小为 20
  33.         }

  34.         EndDrawing();
  35.     }

  36.     CloseWindow();

  37.     return 0;
  38. }
复制代码

Make Programming Great Again

18

主题

85

回帖

1414

积分

管理员

积分
1414

一周年纪念

发表于 7 天前 | 显示全部楼层
哇啊啊啊啊啊啊啊唉唉唉唉唉唉唉唉唉太厉害了

以前也有这个问题,居然做出来了

忒厉害了
回复

使用道具 举报

3

主题

12

回帖

230

积分

蓝锐

积分
230
 楼主| 发表于 7 天前 | 显示全部楼层
Nixx0328 发表于 2025-4-19 16:47
哇啊啊啊啊啊啊啊唉唉唉唉唉唉唉唉唉太厉害了

以前也有这个问题,居然做出来了

Thank you so much
Make Programming Great Again
回复

使用道具 举报

Archiver|手机版|小黑屋|CZLJ

GMT+8, 2025-4-26 17:33 , Processed in 0.059471 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表