pygame - Pygame如何使用相同的键启用和禁用功能?
问题描述
在制作暂停菜单时,我注意到我无法使用相同的键暂停和继续游戏。
假设我想用转义键做到这一点。然后,如果我只是按下它,游戏将暂停几微秒,但会继续,因为pause()
功能也用退出键结束。
我还注意到,如果我将用于结束 pause() 函数执行的键更改为与暂停游戏不同的键,一切都会正常工作,但我不希望这样。
那么我应该怎么做才能防止这种情况并能够一键暂停和继续游戏呢?
解决方案
添加一个paused
状态。实现依赖于状态的事件处理paused
。
用来pygame.time.get_ticks()
测量时间。计算暂停模式应该结束的时间。设置paused = False
时间到:
paused = False
pause_end_time = 0
while running:
current_time = pygame.time.get_ticks()
if current_time > pause_end_time:
paused = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
paused = not paused
pause_end_time = current_time + 3000 # pause for 3 seconds
if not paused:
# game event handling
if event.type == pygame.KEYDOWN:
# [...]
# [...]
推荐阅读
- text - tr -d 或 sed -e 's/\r//g' 无法从 MINGW32 中的文件中删除 CRLF
- c# - 将值添加到
- 来自excel单元格的范围
- vue.js - VueJS。模块解析失败:意外的令牌 <
- mariadb - 如何在 DataGrip 中设置 LOAD DATA 工作目录?
- c# - 将具有重复基础值的枚举转换为字符串和枚举的字典
- android-emulator - Windows 7 笔记本电脑上的 VS 2019 出现 Android 模拟器错误
- javascript - 状态更改时的 FlatList 问题 [React Native]
- excel - Excel UserForm 最重要的重置问题
- java - 如何在魅力报告中获取失败的测试类计数而不是@Test方法失败计数
- java - 等效于 C++ 中的 Java 的 String.getBytes()?