python 使用state_manager时屏幕不会更改

sg24os4d  于 2023-01-19  发布在  Python
关注(0)|答案(1)|浏览(101)

我尝试在pygame中创建一个状态管理器,这是我的代码:

class Main:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

        self.clock = pygame.time.Clock()

        self.state = "intro"
        self.font_one = pygame.font.SysFont("Comic Sans MS", 30)

    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()

            self.state_manager()

            self.clock.tick(30)

    def state_manager(self):
        if self.state == "intro":
            self.intro()
        elif self.state == "zgame":
            self.zgame()

    def intro(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    self.state = "zgame"

        self.screen.fill((0, 0, 0))

        text_surface = self.font_one.render(
            "Intro",
            True,
            (255, 255, 255),
        )
        self.screen.blit(text_surface, (100, 300))
        pygame.display.update()

    def zgame(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    self.state = "zgame"

        self.screen.fill((0, 0, 0))

        text_surface = self.font_one.render(
            "Game",
            True,
            (255, 255, 255),
        )
        self.screen.blit(text_surface, (100, 300))
        pygame.display.update()

if __name__ == "__main__":
    main = Main()
    main.run()

我没有得到任何错误,它只是不工作。当我点击回车的屏幕总是停留在什么是介绍功能。我不知道如何,但只有几次,而我一直点击回车它实际上工作。什么可能是问题?还有,有没有更好的方法来处理这样的事情?

a8jjtwal

a8jjtwal1#

pygame.event.get()获取所有消息并将其从队列中删除。请参阅文档:
这将获取所有消息并将其从队列中删除。[...]
如果pygame.event.get()在多个事件循环中被调用,那么只有一个循环接收事件,而不是所有循环都接收所有事件。因此,有些事件看起来会丢失。在中,每帧获取一次事件,将事件列表传递给处理它们的函数和方法,并在所有事件循环中使用事件列表f:

class Main:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.clock = pygame.time.Clock()
        self.state = "intro"
        self.font_one = pygame.font.SysFont("Comic Sans MS", 30)

    def run(self):
        while True:
            event_list = pygame.event.get()                   #<---
            for event in event_list:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit()
                        sys.exit()

            self.state_manager(event_list)                    #<---
            self.clock.tick(30)

    def state_manager(self, event_list):                      #<---
        if self.state == "intro":
            self.intro(event_list)                            
        elif self.state == "zgame":
            self.zgame(event_list)

    def intro(self, event_list):                              #<--- 
        for event in event_list:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    self.state = "zgame"

        self.screen.fill((0, 0, 0))

        text_surface = self.font_one.render(
            "Intro",
            True,
            (255, 255, 255),
        )
        self.screen.blit(text_surface, (100, 300))
        pygame.display.update()

    def zgame(self, event_list):                             #<---
        for event in event_list:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    self.state = "zgame"

        self.screen.fill((0, 0, 0))

        text_surface = self.font_one.render(
            "Game",
            True,
            (255, 255, 255),
        )
        self.screen.blit(text_surface, (100, 300))
        pygame.display.update()

相关问题