pygame等待屏幕更新

时间:2020-02-02 19:10:01

标签: python pygame

我刚开始使用pygame,我只是想在屏幕上移动点。问题是它以快速的方式发生,并且在循环运行时我的pygame屏幕冻结(不响应),然后仅显示点的最后迭代位置。 我认为更新发生得很快。

当我包含pygame.event.wait()时,当我输入内容时,循环将继续进行,我可以在窗口中实时跟踪点如何在屏幕上移动。但是,我希望它们在屏幕上移动而无需输入。

这是我的主循环:

def run(self):
    self.food_spread()
    self.spawn_animal()

    for k in range(20000):
        print(k)
        for member in self.zoo: 
            self.move(member)

        self.screen.fill(black)
        for i in range(self.food_locations.shape[0]):
            pygame.draw.rect(self.screen, white, (self.food_locations[i,1], self.food_locations[i,2],1,1))

        for member in self.zoo:
            pygame.draw.circle(self.screen, green,(member.location[0], member.location[1]), 2,1)
            pygame.display.update()
        pygame.event.wait() 

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                 pygame.quit()
                 sys.exit()

1 个答案:

答案 0 :(得分:1)

您有一个应用程序循环,请使用if。使用pygame.time.Clock()控制帧频。应用程序循环必须

  • 控制帧速率(clock.tick(60)
  • 处理事件并移动对象
  • 清除显示
  • 绘制场景
  • 更新显示

例如:

class App:

    def __init__(self):
        # [...]

        self.clock = pygame.time.Clock()

    def run(self):
        self.food_spread()
        self.spawn_animal()

        run = True
        while run:

            # control the framerate
            self.clock.tick(60) # 60 FPS

            # handel the events
            for event in pygame.event.get():
                if event.type == pygame.QUIT: 
                    run = False

            # move the objects
            for member in self.zoo: 
                self.move(member)

            # clear the display
            self.screen.fill(black)

            # draw the scene
            for i in range(self.food_locations.shape[0]):
                pygame.draw.rect(self.screen, white, (self.food_locations[i,1], self.food_locations[i,2],1,1))
            for member in self.zoo:
                pygame.draw.circle(self.screen, green,(member.location[0], member.location[1]), 2,1)

            # update the display
            pygame.display.update()
相关问题