好的,所以我的代码在下面,现在我是 pygame 的新手,并试图弄清楚如何在屏幕上移动图像。根据我对 pygame 的了解,以下代码应该将图像向上移动(目前唯一可能的移动)。但是,当我向上推时,图像不会移动。Python 正在识别我正在向上推,因为它打印在系统托盘中,但是没有任何动作,有人可以帮忙吗?
import pygame, sys
from pygame.locals import *
FPS = 30
WIN_WIDTH = 800
WIN_HEIGHT = 600
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)
COLOR = (255, 0, 255)
IMAGE = pygame.image.load('squirrel.png')
STARTSIZE = 25
LEFT = 'left'
def main():
pygame.init()
FPS_CLOCK = pygame.time.Clock()
movement = 1
moveUp = False
DISPLAY_SURFACE = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
while True:
DISPLAY_SURFACE.fill(COLOR)
playerObj = {'surface': pygame.transform.scale(IMAGE, (STARTSIZE, STARTSIZE)),
'facing': LEFT,
'size': STARTSIZE,
'x': HALF_WIDTH,
'y': HALF_HEIGHT}
playerObj['rect'] = pygame.Rect( (playerObj['x'], playerObj['y'], playerObj['size'], playerObj['size']) )
DISPLAY_SURFACE.blit(playerObj['surface'], playerObj['rect'])
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:
moveUp = True
if event.type == KEYUP:
if event.key == K_UP:
moveUp = False
elif event.type == QUIT:
pygame.quit()
sys.exit()
if moveUp:
playerObj['y'] += movement
print("moving up")
else:
print('stopped')
pygame.display.update()
FPS_CLOCK.tick(FPS)
if __name__ == '__main__':
main()