即使在收到按键后 Pygame 也不会退出

所以我试图使用函数退出 pygame,但它不起作用。它肯定进入了功能块,但由于某种原因在按键后没有退出。请帮忙。

import pygame
from pygame import mixer
from random import *
from math import *

pygame.init()

screen = pygame.display.set_mode((1280,720))
pygame.display.set_caption("The Rake Game")
font = pygame.font.Font('freesansbold.ttf',32)

running = True

class Paths:
    def __init__(self):
        self.game_state = 'intro'

    def intro(self):
        screen.fill((120,120,120))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        pygame.display.update()

    def state_manager(self):
        if self.game_state == 'intro':
            self.intro()

a = Paths()

while running:
    a.state_manager()
stu5229 回答:即使在收到按键后 Pygame 也不会退出

running 是全局命名空间中的变量。如果要将变量解释为全局变量,则必须使用 global statement

class Paths:
    # [...]

    def intro(self):
        global running  # <--- add global statement

        screen.fill((120,120,120))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        pygame.display.update()
,

“跑步”与任何东西都没有关联。你需要一个while循环。

running = True
while running:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        screen.fill((120,120))
        pygame.display.update()

但是这不会像您那样退出:

while True:
    a.state_manager()

这总是正确的。从它周围删除 while true ,它应该退出

本文链接:https://www.f2er.com/337925.html

大家都在问