Pygame动画出现故障

问题描述 投票:2回答:1

所以我运行代码,它开始出现故障。请帮助我是pygame的新手这是代码:

import pygame

pygame.init()
# Screen (Pixels by Pixels (X and Y (X = right and left Y = up and down)))
screen = pygame.display.set_mode((1000, 1000))
running = True
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
# Player Icon/Image
playerimg = pygame.image.load('Player.png')
playerX = 370
playerY = 480

def player(x, y):
    # Blit means Draw
    screen.blit(playerimg, (x, y))


# Game loop (Put all code for pygame in this loop)
while running:
    screen.fill((225, 0, 0))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # if keystroke is pressed check whether is right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                print("Left arrow is pressed")

            if event.key == pygame.K_RIGHT:
                print("Right key has been pressed")


            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                print("kEYSTROKE RELEASED")

    # RGB (screen.fill) = red green blue

    player(playerX, playerY)
    pygame.display.update()

The image is not the glitching one as i was not able to post a video but it is what my code does

python pygame pycharm
1个回答
0
投票

在应用程序循环结束时仅进行一次显示更新。多次更新显示会导致闪烁:

while running:
    screen.fill((225, 0, 0))
    # pygame.display.update() <---- DELETE

    # [...]

    player(playerX, playerY)
    pygame.display.update()

如果在screen.fill()之后更新显示,则将在短时间内以背景颜色填充显示内容。然后,绘制播放器(blit),并在播放器位于背景上方的情况下显示显示。

© www.soinside.com 2019 - 2024. All rights reserved.