这是代码:
# Initialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((800, 600))
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# Player
playerImg = pygame.image.load("player.png")
playerX = 370
playerY = 480
def player(x, y):
screen.blit(playerImg, (x, y))
# Run game until x is pressed
running = True
while running:
for event in pygame.event.get():
screen.fill((0, 0, 0))
playerX += 0.1
print(playerX)
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
sys.exit()
player(playerX, playerY)
pygame.display.update()
由于某些原因,只有在将鼠标移到屏幕上或向键盘发送垃圾邮件时,屏幕才会更新。我没有要添加的更多详细信息,所以我必须输入此内容,否则我将无法发布我的问题。
答案 0 :(得分:1)
修复缩进。您只需要在事件循环中检查事件类型。绘制代码位于while
循环下。
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
sys.exit()
screen.fill((0, 0, 0))
playerX += 0.1
print(playerX)
player(playerX, playerY)
pygame.display.update()
答案 1 :(得分:1)
这是Indentation的问题。您必须更新屏幕应用程序循环而不是事件循环:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#<--| INDENTATION
screen.fill((0, 0, 0))
playerX += 0.1
print(playerX)
player(playerX, playerY)
pygame.dispaly.flip()