销毁并重新启动PyGame对象

2024-04-27 03:58:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python脚本,它可以打开pygame窗口。如果此窗口已关闭,则可以重新打开。如果pygame窗口再次打开,我会在后台看到一个pygame.error: display Surface quit。一切都很好,当我最终退出父脚本时,我可以看到所有的错误。你知道吗

你知道吗父.py地址:

from game import game

def launch_game():
    game = Game() # Now I am in the pygame window until I quit it

# Do a bunch of other stuff
# At some point
launchGame()

你知道吗游戏.py你知道吗

class Game():
    def __init__(self):
        pygame.display.init()
        self.screen = pygame.display.set_mode((640, 480))

    # Set up a bunch of GPIO Controls including a quit button

        self.playing = True
        while self.playing:
            self.check_events()
            sleep(.1)
        pygame.quit()


    def check_events(self, current_station):
        if GPIO.input(self.quitButton) == 0:
            self.playing = False
        if GPIO.input(self.upButton) == 0:
            self.playing = moveUp()
        if GPIO.input(self.downButton) == 0:
            self.playing = moveDown()

所以我第一次启动game()的时候一切都正常。下一次我开始看到显示表面退出的背景。你知道吗

每次我按下其他按钮都会出现一个错误。moveUp()和moveDown()函数都以screen drawing函数结束,并且以self.screen.fill(bg_color)开始。这就是引发错误的原因。你知道吗

如果我打开可以关闭游戏两次,我会得到两个错误消息每个按钮按下,三次,三个错误消息。这就好像屏幕对象即使在调用pygame.quit()之后仍然保存在内存中的某个地方。你知道吗


Tags: pyself脚本gameinputgpioifdef