按下 R 键时,调用一个将所有变量重置为默认值的函数

huangapple go评论100阅读模式
英文:

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'键。以下是一个小例子:

  1. import pygame
  2. gameDisplay = pygame.display.set_mode((800, 600))
  3. def main():
  4. while True:
  5. for event in pygame.event.get():
  6. if event.type == pygame.QUIT:
  7. pygame.quit()
  8. quit()
  9. if event.type == pygame.KEYDOWN:
  10. if event.key == pygame.K_r:
  11. print('按下'r'键!')
  12. # 在此处重置变量或调用函数来重置它们
  13. gameDisplay.fill((255, 255, 255))
  14. pygame.display.update()
  15. main()
英文:

You can detect when 'r' is pressed using keyboard events. Here is a small example:

  1. import pygame
  2. gameDisplay = pygame.display.set_mode((800, 600))
  3. def main():
  4. while True:
  5. for event in pygame.event.get():
  6. if event.type == pygame.QUIT:
  7. pygame.quit()
  8. quit()
  9. if event.type == pygame.KEYDOWN:
  10. if event.key == pygame.K_r:
  11. print('r pressed!')
  12. # Reset variables here or call a function to reset them
  13. gameDisplay.fill((255, 255, 255))
  14. pygame.display.update()
  15. main()

huangapple
  • 本文由 发表于 2023年2月18日 07:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定