简体   繁体   English

C ++实时蛇状游戏控制台,无需多线程

[英]C++ real time snake like game console without multithreading

I want to make snake game in console using c++, but I don't want to wait every "frame" for user input. 我想使用c ++在控制台中制作蛇游戏,但是我不想等待每个“帧”都供用户输入。 If i'll use cin or getch (), program will wait for player input every frame: 如果我将使用cin或getch(),则程序将等待播放器每帧输入:

while (game)
{
    c = getch (); //input
    snake.move (c); //moving
    draw (); //drawing
}

How to move snake and draw it in console and don't waiting for input every frame? 如何移动蛇并在控制台中绘制它,而不等待每一帧的输入?

Is there any way to do that without multithreating? 有没有办法做到这一点? Maybe something like put something in cin buffer in code instead of using console? 也许像在代码中的cin缓冲区中放置某些内容而不是使用控制台? Is it doable? 可以吗

You can't use standard C++ because there is no guarantee that when the User presses a key, the key press will be acknowledged immediately. 您不能使用标准的C ++,因为不能保证当用户按下某个键时,会立即确认该按键。 Many implementations wait for the Enter key to be pressed, then process the input buffer. 许多实现都等待按下Enter键,然后处理输入缓冲区。

You will need an event driven system . 您将需要一个事件驱动系统 You want the OS or hardware to notify your program that a key press event has occurred. 您希望操作系统或硬件通知程序已发生按键事件。 In this kind of system, you would move the snake during the event handler. 在这种系统中,您将在事件处理程序期间移动蛇。

Another method is to poll the switch status. 另一种方法是轮询交换机状态。 When the key is released, you could move the snake. 释放钥匙后,您可以移动蛇。

All of the above solutions require platform or OS specific functionality. 以上所有解决方案都需要特定于平台或OS的功能。

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

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