告诉我如何克服。当应用程序启动时,会显示一个黑色窗口而不是背景。关闭应用程序时,背景会出现半秒
import pygame
pygame.init()
x = 6
y = 440
width = 50
height = 37
speed = 5
isJump = False
jumpCount = 10
left = False
right = False
animationCount = 0
window = pygame.display.set_mode((512, 256))
pygame.display.set_caption('Firts Game')
clock = pygame.time.Clock()
walkRight = [pygame.image.load('assets\\player-run-right-01.png'),
pygame.image.load('assets\\player-run-right-02.png'), pygame.image.load('assets\\player-run-right-03.png'),
pygame.image.load('assets\\player-run-right-04.png'), pygame.image.load('assets\\player-run-right-05.png')]
walkLeft = [pygame.image.load('assets\\player-run-left-01.png'),
pygame.image.load('assets\\player-run-left-02.png'), pygame.image.load('assets\\player-run-left-03.png'),
pygame.image.load('assets\\player-run-left-04.png'), pygame.image.load('assets\\player-run-left-05.png')]
background = pygame.image.load('assets\\background.png')
playerStand = pygame.image.load('assets\\adventurer-idle-03.png')
def drawWindow():
global animationCount
window.blit(background, (0, 0))
if animationCount >= 25:
animationCount = 0
if left:
window.blit(walkLeft[animationCount // 5], (x, y))
animationCount += 1
elif right:
window.blit(walkRight[animationCount // 5], (x, y))
animationCount += 1
else:
window.blit(playerStand, (x, y))
pygame.display.update()
run = True
while run:
clock.tick(25)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > 5:
x -= speed
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 500 - width - 5:
x += speed
left = False
right = True
else:
left = False
right = False
animationCount = 0
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
y -= jumpCount * 2
jumpCount -= 1
else:
isJump = False
jumpCount = 10
drawWindow()
pygame.quit()
明白了。drawWindow() 函数必须从 while 循环中执行