我想让一个角色四处移动。 我的问题是,当我运行程序,它立即停止响应,所以我甚至不知道问题是什么。 这是我的密码。你知道吗
import pygame, sys
from pygame.locals import*
pygame.init()
DISPLAYSURF = pygame.display.set_mode((780, 500), 0, 32)
FPS = 30
fpsClock = pygame.time.Clock()
sprite = pygame.image.load('CharacterFront.png')
spritex = 50
spritey = 50
charLeft = False
charRight = False
charUp = False
charDown = False
while True:
DISPLAYSURF.blit(sprite,(spritex,spritey))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if (event.key == K_LEFT):
charLeft = True
elif (event.key == K_d):
charRight = True
elif (event.key == K_w):
charUp = True
elif (event.key == K_s):
charDown = True
if event.type == KEYUP:
if (event.key == K_LEFT):
charLeft = False
elif (event.key == K_d):
charRight = False
elif (event.key == K_w):
charUp = False
elif (event.key == K_s):
charDown = False
while charLeft == True:
spritex -= 10
sprite=pygame.image.load('CharacterLeft.png')
while charRight == True:
spritex += 10
sprite=pygame.image.load('CharacterRight.png')
while charUp == True:
spritey -= 10
sprite=pygame.image.load('CharacterBack.png')
while charDown == True:
spritey += 10
sprite=pygame.image.load('CharacterFront.png')
pygame.display.update()
fpsClock.tick(FPS)
我已经尝试了很多不同的方法来做到这一点,但最近我得到了导致字符被粘贴了一遍又一遍,我不得不垃圾邮件的方向,实际上移动超过10像素。你知道吗
你的
while char..
循环永远不会结束。您已经在循环(while True:
在顶部)。只需移动一步(例如spritey -= 10
),让外循环继续运行。你知道吗有关如何在按住键时保持角色移动的想法,请参见this question。你知道吗
除了jornsharpe所说的,你不应该每次按键都加载精灵。 取而代之的是在加载之前加载所有的图像,并在必要时进行blit。你知道吗
因此,您的代码如下所示:
相关问题 更多 >
编程相关推荐