python - Pygame 没有响应

标签 python user-interface pygame

我正在学习 pygame,我刚刚输入了一些基本行来尝试在窗口上移动球,但在显示图像后,窗口将卡住。

import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
ball = pygame.image.load("ball.png").convert()
ball_rect = ball.get_rect()
white = (255,255,255)


frames = 100


for x in range(frames):
    screen.blit(ball, ball_rect) # display player
    ball_rect.move(2, 2) # move player
    pygame.display.update()
    pygame.time.delay(100)
    screen.fill(white) # erase player

最佳答案

窗口并没有卡住,它只是在您设置的 10 秒内在同一位置刷新同一图像。

这主要是由移动图像的方法引起的,您应该使用 move_ip 而不是 move 作为快速修复 ( doc )。

您可以做的另一个更改是将 for 循环替换一段时间,让玩家在需要时退出:

import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((640, 480))
ball = pygame.image.load("ball.png").convert()
ball_rect = ball.get_rect()
white = (255,255,255)


looping = True
while looping:

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

    screen.fill(white) # erase player            

    screen.blit(ball, ball_rect) # display player
    ball_rect.move_ip(2, 2) # move player
    pygame.display.update()
    clock.tick(10)  # to keep the same FPS, better increase  !
    # Thanks skrx !

关于python - Pygame 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45228170/

相关文章:

python-如何将值传递给类装饰器?

user-interface - 您可以在 Xamarin Android/iOS 中创建 UI 并在 Xamarin.Forms 中创建代码吗?

python - 添加每个元素后 Pygame 屏幕不更新

python-3.x - Pygame 菜单按钮无响应

c# - 将用户控件弹出到新窗口中

python - 无法在 pygame 中改变颜色?

根据多个函数的结果返回值的 Pythonic 方式

python - 为什么对 formdata 进行 urlencode 然后用 utf-8 再次编码,这里的逻辑是什么?

python - 用于在 Python 中解析复杂制表符分隔/csv 文件的循环

c++ - 将 C++ 函数添加到 C GUI