我正在尝试让云从左到右动画并返回基本屏幕,当我不使用定义并将其放入游戏循环时,它会起作用,那为什么它会停止工作里面的定义?
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
width = 500
length = 300
DISPLAYSURF = pygame.display.set_mode((width, length), 0, 32)
pygame.display.set_caption('Test')
def Clouds():
BROWN = ( 139, 69, 19)
GREEN = ( 0, 255, 0)
LIGHT_BLUE = ( 135, 206, 250)
global cloudimg, cloudx, cloudy
cloudimg = pygame.image.load('Cartoon_cloud.png')
direction = 'left'
cloudx = 100
cloudy = 70
DISPLAYSURF.fill(LIGHT_BLUE)
pygame.draw.rect(DISPLAYSURF, BROWN, (0, 285, width, 15))
pygame.draw.rect(DISPLAYSURF, GREEN, (0, 280, width, 5))
if direction == 'left':
cloudx += 1
if cloudx == 450:
direction = 'right'
elif direction == 'right':
cloudx -= 1
if cloudx == 0:
direction = 'left'
while True:
Clouds()
DISPLAYSURF.blit(cloudimg, (cloudx, cloudy))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)