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

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

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()

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:

确定