python - pygame“screen.fill command”,我把它放在哪里?

标签 python pygame

我最近在 screen.fill 命令中遇到了困难,我的程序运行良好,但特性不稳定,有时不会出现在屏幕上,我尝试将命令从代码中的不同位置放置。它有时也会留下痕迹
... 导入pygame

pygame.init()
pygame.display.init()
#creation of screen
screen = pygame.display.set_mode((800, 600))
#window title
# game.display.set_caption("space invaders")
#player coordinates
playerX = 360
playerY = 480
#player image
playerimg = pygame.image.load("spaceship.png")
#player change distance
playerX_change = 0

#drawing player on screen
def player(x, y):

pygame.display.flip()
pygame.display.update()
screen.blit(playerimg, (x, y))

#game loop
running = True

while running == True:
#event system
    for event in pygame.event.get():
        screen.fill((0, 0, 0))
#exit function
        if event.type == pygame.QUIT:
            pygame.quit()

            player(playerX, playerY)
#arrows key press detection or movement system
    if event.type == pygame.KEYDOWN:
        player(playerX,playerY)
        if event.key == pygame.K_LEFT:
            playerX_change = -0.3
        if event.key == pygame.K_RIGHT:
            playerX_change = 0.3

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

            playerX_change = 0
#movement
    playerX += playerX_change

...

最佳答案

在绘制屏幕上的对象之前先清除屏幕一次。实际上,您在事件循环中调用了 screen.fill。事件循环为每个事件执行一次。将 screen.fill 从事件循环移动到应用程序循环。应用程序循环每帧执行一次。

while running == True:
    screen.fill((0, 0, 0))        # insert
#event system
    for event in pygame.event.get():
        #screen.fill((0, 0, 0))    # delete
#exit function
        if event.type == pygame.QUIT:
            pygame.quit()
    # [...]

此外,在绘制所有对象后仅更新显示一次。参见 Why is the PyGame animation is flickering .

关于python - pygame“screen.fill command”,我把它放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66757382/

相关文章:

python - 如何在 Tornado 请求处理程序中访问我的部分 url/路径

python - 嵌套 tf.map_fn 性能缓慢

python - 当我不使用 screen.fill(color) 时 Pygame 文本重叠,为什么?

python - 按 SHIFT 中断在 pygame 中按住 W

python - 是否可以将 MySQL Server 与独立的客户端软件一起安装?

python - 添加 2 个不同长度的列表列表

python - 减去python中的匹配行

python - 如何使用类显示与矩形位置相关的字体?

python - GL_LINES 没有显示在立方体顶部?

python - 我无法让我的按钮在 pygame 中为我的程序工作