Pygame 等待屏幕更新 [英] Pygame wait for screen to update

查看:47
本文介绍了Pygame 等待屏幕更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just started with pygame, and I am just trying to move points across my screen. The problem is that it happens way to fast and my pygame screen freezes (Not Responding) while the loop runs and then only shows the last iterations position of dots. I am thinking the updating happens to fast.

当我包含 pygame.event.wait() 时,当我提供输入时,循环会进行,我可以在窗口中实时跟踪点如何在屏幕上移动.但是,我希望它们无需输入即可在屏幕上移动.

When I include pygame.event.wait() then as I give an input the loop progresses and I can follow live in the window how the dots are moving across the screen. However, I would like to have it that they move across the screen without an input required.

这是我的主循环:

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

推荐答案

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

You have an application loop, use if. Use pygame.time.Clock() to control the framerate . The application loop has to

  • 控制帧率 (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()

这篇关于Pygame 等待屏幕更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆