Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么PyGame动画会闪烁_Python_Pygame_Pycharm - Fatal编程技术网

Python 为什么PyGame动画会闪烁

Python 为什么PyGame动画会闪烁,python,pygame,pycharm,Python,Pygame,Pycharm,所以我运行代码,它就开始出错了。我是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("

所以我运行代码,它就开始出错了。我是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()

这张图片不是有瑕疵的,因为我无法发布视频,但这是我的代码所做的


该问题是由对的多个调用引起的。应用程序循环结束时的显示更新就足够了。多次调用
pygame.display.update()
pygame.display.flip()
会导致闪烁

从代码中删除对
pygame.display.update()
的所有调用,但在应用程序循环结束时调用一次:

运行时:
屏幕填充((225,0,0))

#pygame.display.update()您能帮我解决另一个问题吗?这里是错误:参数1必须是pygame.Surface,而不是function这里是它给我错误的代码:def敌方(x,y):screen.blit(敌方,(x,y))enemyimg=pygame.image.load('space-invaders.png')enemyX=370 enemyY=480敌方(enemyX,enemyY)@piddi24 name
敌方
是函数的名称,不是曲面的名称。它必须是
screen.blit(enemyimg,(x,y))
而不是
screen.blit(敌人,(x,y))