基本上,我需要在我的游戏中执行暂停功能(游戏邦注:这是《蛙人》的简化版本),停止日志滚动,并忽略任何其他输入,直到字符p再次被按下)。我开始在while循环中实现这一点的方式是在另一个p被按下时结束它。
if(serial_input == 'p' || serial_input == 'P') {
while(1){
//need to pause the game
if(serial_input == 'p' || serial_input == 'P')
break;
}
这是我的日志当前滚动的方式:
/* The following statements change the scrolling speeds of the individual logs */
current_time = get_clock_ticks();
if(is_frog_alive() && current_time >= last_move_time1 + 1000) {
scroll_lane(0, 1);
last_move_time1 = current_time;
} else if(is_frog_alive() && current_time >= last_move_time2 + 600) {
scroll_lane(1, -1);
last_move_time2 = current_time;
} else if(is_frog_alive() && current_time >= last_move_time3 + 800) {
scroll_lane(2, 1);
last_move_time3 = current_time;
} else if(is_frog_alive() && current_time >= last_move_time4 + 900) {
scroll_log_channel(0, -1);
last_move_time4 = current_time;
} else if(is_frog_alive() && current_time >= last_move_time5 + 1200) {
scroll_log_channel(1, 1);
last_move_time5 = current_time;
这是通过定时器实现的,如下所述:
* We update a global clock tick variable - whose value
* can be retrieved using the get_clock_ticks() function.
*/
如有任何建议,不胜感激
最佳实践将取决于您正在使用的库和一般体系结构。话虽如此,我有时使用的一个简单的实现是这样的:
while( playing) {
if( !paused) {
logic();
}
rendering();
input();
}
在制作小型游戏项目时,我会在主while循环中分散逻辑、渲染和输入到不同的部分。在输入部分有一个按钮用于切换暂停标志。在主循环中,逻辑被简单地括在if语句中。
如果您仍然需要在逻辑内部做一些事情,您可以将其作为参数传递或以其他方式使其可见。此外,您可以在渲染部分中设置一些特殊的暂停时图形。
细节会有所不同,但我希望这至少能给你一个正确的方向。也就是说,这是一件很常见的事情,应该不会太难谷歌。