如何停止用户输入和在Pygame上显示之间的滞后
我是Pygame社区和Python的新手。对于我的计算机分配,我们必须制作一个视频游戏。即时通讯尝试做的是通过获取用户输入开始,然后将用户输入进行循环,然后根据该输入显示新视图。我试图制作一个主菜单屏幕,用户可以在其中按上下箭头以在屏幕上选择选项。但是,当我运行它时,它非常笨拙,响应用户输入的速度很慢。只有当您将其放下时,它才会移动,但它一直进入第四个选项。
while Loop_2 == True:
pygame.time.delay(75)
#Checks what keys have been pressed
keys = pygame.key.get_pressed()
#Checks if player has pressed the exit button
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#Checks if the player has pressed escape and will end python
if keys[pygame.K_ESCAPE]:
pygame.quit()
#The second main menu if statements for moving the curser and selecting
if keys[pygame.K_DOWN] and Position < 4:
#If the player presses the down arrow to move the pointer and its not in the 4th position at the bottom. it will move down
Position = Position + 1
else:
if keys[pygame.K_UP] and Position > 1:
#If the player presses the up arrow to move the pointer and its not in the 1st position at the top. it will move up
Position = Position - 1
else:
if keys[pygame.K_LSHIFT]:
Loop_3 = True
if Position == 1:
Play_Menu()
elif Position == 2:
Saves_Menu()
elif Position == 3:
Settings_Menu()
elif Position == 4:
Quit_Menu()
#Loads the second main Menu
window.blit(main_menu_2,(0,0))
if Position == 1:
window.blit(main_menu_pointer,(650,-525))
elif Position == 2:
window.blit(main_menu_pointer,(650,-385))
elif Position == 3:
window.blit(main_menu_pointer,(650,-240))
elif Position == 4:
window.blit(main_menu_pointer,(650,-100))
pygame.display.update()
我已经尝试将pygame.delay更改为不同的值,它将使其响应迅速或无反应。我只想要它,以便我可以向下或向上,当它释放时,它将移至下一个选项上,而不是一直向下底部。我在此网站上找不到有关此的信息。谢谢
Im a newbie in the pygame community and python in general. For my computers assignment we have to make a video game. What Im trying to do is have a loop where it starts off by getting user input, then it takes that user input and then displays a new view based on that. Im trying to make a main menu screen where the user can press the up and down arrows to select options on the screen. However, when I run it, its very glitchy and its slow to respond to user input. Only when you hold it down it will move, but it goes all the way to the 4th option.
while Loop_2 == True:
pygame.time.delay(75)
#Checks what keys have been pressed
keys = pygame.key.get_pressed()
#Checks if player has pressed the exit button
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#Checks if the player has pressed escape and will end python
if keys[pygame.K_ESCAPE]:
pygame.quit()
#The second main menu if statements for moving the curser and selecting
if keys[pygame.K_DOWN] and Position < 4:
#If the player presses the down arrow to move the pointer and its not in the 4th position at the bottom. it will move down
Position = Position + 1
else:
if keys[pygame.K_UP] and Position > 1:
#If the player presses the up arrow to move the pointer and its not in the 1st position at the top. it will move up
Position = Position - 1
else:
if keys[pygame.K_LSHIFT]:
Loop_3 = True
if Position == 1:
Play_Menu()
elif Position == 2:
Saves_Menu()
elif Position == 3:
Settings_Menu()
elif Position == 4:
Quit_Menu()
#Loads the second main Menu
window.blit(main_menu_2,(0,0))
if Position == 1:
window.blit(main_menu_pointer,(650,-525))
elif Position == 2:
window.blit(main_menu_pointer,(650,-385))
elif Position == 3:
window.blit(main_menu_pointer,(650,-240))
elif Position == 4:
window.blit(main_menu_pointer,(650,-100))
pygame.display.update()
I’ve tried changing the pygame.delay to different values and it will either make it too responsive or non-responsive. I just want it so that I can press down or up and when its released it will move onto the next option, not all the way down the bottom. I cant find anything about this on this website here. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
核心问题是,您希望能够循环浏览菜单选项,而不会对普通人过于快速。
目前,代码延迟整个程序以保持速度。但是,这里真正需要的是对选择下一个项目之间的时间限制。
这样做的一种方法是查看当选择“事件”发生时。也就是说,用户什么时候突出显示菜单项。时间的方便函数是
.get_ticks()
,它返回一个毫秒相对时间。这使我们能够创建一个未来的时间,然后允许新的选择。
现在,当用户试图更改选择时,直到当前时间已经过去,才允许它。以我们的示例,这将不适合另外300毫秒(这很慢)。
当然,我们应该为时间延迟设置一个常数,因此它很容易更改,而无需浏览常数的代码,
这是一些实现此类菜单的示例代码:
The core issue is that you want to be able to cycle through menu options without it being too-fast for a normal human.
Currently the code is delaying the entire program to maintain a speed. But what is really needed here is a restriction on the time between selecting a next item.
One way of doing this is looking at the Pygame Time when the selection "event" happens. That is, at what time did the user highlight the menu item. A handy function of the time is
.get_ticks()
, which returns a millisecond relative time.This allows us to create a future-time, after which a new selection is allowed.
Now when the user attempts to change the selection, don't allow it until the current time is past this time in the future. By our example, that wont be for another 300 milliseconds (which is quite slow).
Of course, we should set a constant for the time delay, so it's easily changed without having to hunt through the code for constants
Here's some example code which implements such a menu: