简体   繁体   English

如果那段时间条件发生变化(不再成立),我应该用什么来停止 C 中的 sleep() function?

[英]What should I use to stop the sleep() function in C if a condition changes in that period of time (doesn't hold anymore)?

I'm an amateur in C and I have the following task: We are given a two-dimensional table with the player controlling an 'X' in the middle.我是 C 的业余爱好者,我有以下任务:给我们一张二维桌子,玩家在中间控制一个“X”。 The player should have the possibility to move the 'X' around.玩家应该可以移动“X”。 However, every 2 seconds a random character appears in a specific point near a border of the table, such that if all the boxes near the border have a character in them, the player loses.然而,每 2 秒一个随机角色会出现在靠近桌子边界的特定点,这样如果靠近边界的所有方块中都有一个角色,则玩家输了。

I'm having problems with moving the 'X' around and at the same time random characters appearing every 2 seconds.我在移动“X”时遇到问题,同时随机字符每 2 秒出现一次。 If I press the button to move 'X' in any direction but the code is still 'sleeping' , the change will happen only after the certain remaining time has passed which is nearly not good enough - 1 move per 2 seconds .如果我按下按钮向任何方向移动“X”但代码仍在“休眠” ,则更改只会在一定的剩余时间过去后发生,这几乎不够好 -每 2 秒移动 1 次

I want the sleep() function to stop when there is a keyboard input , print the changed position on the table and start again with the remaining time after that or at least just sleep again for 2 seconds.我希望 sleep() function 在有键盘输入停止,在桌子上打印更改后的 position 并在此之后的剩余时间重新开始,或者至少再次睡眠 2 秒。 However, I am unable to understand all the complex stuff like multi-threading at the moment so could you please elaborate in simplistic terms your answer?但是,我目前无法理解所有复杂的东西,比如多线程,所以你能用简单的术语详细说明你的答案吗? My PC is using Windows .我的电脑正在使用Windows

Here is a part of my code:这是我的代码的一部分:

void move()
{
    if (kbhit())
    {
        chkb = getch();//character from keyboard
        if ((int)chkb == 27)//if chkb is ESC
        {
            printf("\nGame Exited. We already miss you!");
            stop();
        }
        else
        {
            changeposition();
            printTable();
        }
    }
}

while (1)
    {
        move();
        switch (rand() % 4)
        {
        case 0:
            balls[p1][p2] = 'R';//array of chars
            break;
        case 1:
            balls[p1][p2] = 'G';
            break;
        case 2:
            balls[p1][p2] = 'B';
            break;
        case 3:
            balls[p1][p2] = 'Y';
            break;
        default:
            balls[p1][p2] = '?';
        }

        printTable();
        verifyLose();
        Sleep(2000);
        }
    }
    printTable();

I'm looking for something like:我正在寻找类似的东西:

Sleep(for a time, if some condition is true);
if(condition is false){
    ->make it true
    ->start sleep again with remaining time
}

Or something similar to that.或者类似的东西。 Any ideas?有任何想法吗? Thanks in advance!提前致谢!

I would not call either MS Sleep() or gcc sleep() but go for a non-blocking function, as you have with MS kbhit() .我不会调用 MS Sleep()或 gcc sleep() ,而是调用 go 作为非阻塞 function,就像您使用 MS kbhit()

For a 1 second timer an example use is对于 1 秒计时器,示例使用是

clock_t target = clock() + CLOCKS_PER_SEC;
while(!kbhit() && clock() < target) {
    // some code if needed
}

The loop will end when a key is pressed, or after 1 second.按下某个键或 1 秒后,循环将结束。 You can check kbhit() again to find out which.您可以再次检查kbhit()以找出是哪个。

Notice that the sleep function is not part of the C11 standard.请注意, sleep function不是C11 标准的一部分。 You could check by reading n1570 , the draft C11 standard.您可以通过阅读 C11 标准草案n1570进行检查。 See also this C reference website.另请参阅此C 参考网站。

But sleep is part of POSIX.但是sleep是 POSIX 的一部分。 Read for example sleep(3) .阅读例如sleep(3)

You could want to use some event looping library, such as Glib from GTK (cross-platform on Windows, Linux, MacOSX), or libev or libevent .您可能想要使用一些事件循环库,例如来自 GTK 的Glib (在 Windows、Linux、MacOSX 上跨平台)或libevlibevent

You could want to use a terminal interface library, such as ncurses .您可能想要使用终端界面库,例如ncurses

You could consider writing a graphical application above cross-platform libraries such as GTK (or Qt , or FLTK ; both requiring learning programming in C++ )您可以考虑在跨平台库之上编写图形应用程序,例如GTK (或QtFLTK ;两者都需要在 C++ 中学习编程

You could decide to code a Windows-specific C program.您可以决定编写特定于 Windows 的 C 程序。

Be sure to then read the documentation of the WinAPI , eg here请务必阅读WinAPI的文档,例如此处

I never coded on Windows myself, but I believe you could be interested in the Windows specific Sleep function .我自己从未编写过 Windows 代码,但我相信您可能会对 Windows 特定的Sleep function 感兴趣

I guess that for a game, you need some input-multiplexing function. On Linux, select(2) or poll(2) comes to mind.我想对于游戏,您需要一些输入多路复用 function。在 Linux 上,会想到select(2)poll(2) Perhaps you might be interested by Socket.Poll on Windows (but polling the keyboard or the mouse should be done otherwise).也许您可能对Windows上的 Socket.Poll 感兴趣(但轮询键盘或鼠标应该以其他方式完成)。

For a game, I would recommend using libSFML .对于游戏,我建议使用libSFML

Whatever API or library you would use, be sure to read its documentation .无论您使用什么 API 或库,请务必阅读其文档

Look also for existing open-source examples on github .另请在github上查找现有的开源示例。

Read also the documentation of your C compiler (perhaps GCC ) and of your debugger (maybe GDB ).另请阅读 C 编译器(可能是GCC )和调试器(可能是GDB )的文档。 Enable all warnings and debug info in your compiler (with GCC, compile with gcc -Wall -Wextra -g ).在编译器中启用所有警告和调试信息(使用 GCC,使用gcc -Wall -Wextra -g编译)。 Consider using static analysis tools like Clang static analyzer or Frama-C .考虑使用 static 分析工具,例如Clang static 分析器Frama-C Read perhaps this draft report, and look (at end of 2020) into the DECODER European project, and recent papers to ACM SIGPLAN sponsored conferences (about compilers and static analysis).或许阅读这份报告草案,并查看(2020 年底) DECODER欧洲项目,以及ACM SIGPLAN赞助会议的最新论文(关于编译器和 static 分析)。

Be aware that PC keyboards have various layouts (mine is AZERTY ).请注意,PC 键盘有多种布局(我的是AZERTY )。 You may want to use some library abstracting these.你可能想使用一些库来抽象这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM