英文:
When R is pressed. Call a function that resets all variables to default
问题
我无法制作重新启动按钮,请帮助我,我想在我的项目中创建一个重置按钮,但我不知道如何做。我希望,当我按下"r"键时,所有游戏数据都会重置,也就是说,如果我有一个分数为1500,按下"r"键后分数会重新变为0。我的项目是使用Pygame制作的,如果需要代码,我可以在按下"R"键时提供它。调用一个重置所有变量为默认值的函数。
英文:
I can't make the restart button
please help me, please, I want to make a reset button in my project, but I do not know how, I want, provided that when I press r, all these games are reset, that is, if I have a record of 1500 and press r so that the record becomes 0 again, the project tetris works with pygame, if I need the code, I can throw it off when R is pressed. Call a function that resets all variables to their default values.
答案1
得分: 1
你可以使用键盘事件来检测按下'r'键。以下是一个小例子:
import pygame
gameDisplay = pygame.display.set_mode((800, 600))
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
print('按下'r'键!')
# 在此处重置变量或调用函数来重置它们
gameDisplay.fill((255, 255, 255))
pygame.display.update()
main()
英文:
You can detect when 'r' is pressed using keyboard events. Here is a small example:
import pygame
gameDisplay = pygame.display.set_mode((800, 600))
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
print('r pressed!')
# Reset variables here or call a function to reset them
gameDisplay.fill((255, 255, 255))
pygame.display.update()
main()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论