gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:36:44 24 4
gpt4 key购买 nike

我最近在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com