为什么我的运动代码不起作用?

时间:2014-01-13 20:05:41

标签: python pygame game-physics

我想让角色四处走动。 我的问题是,当我运行程序时,它会立即停止响应,所以我甚至不知道问题是什么。 这是我的代码。

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个像素。

2 个答案:

答案 0 :(得分:3)

你的while char..循环永远不会结束。您已经循环(while True:位于顶部)。只需进行一次移动(例如spritey -= 10)并允许外循环继续运行。

有关如何在按住键时让角色保持移动的想法,请参阅this question

答案 1 :(得分:1)

除了jonrsharpe所说的,每次按键完成时都不应加载精灵。 而是在之前加载所有图像,并在必要时将它们加入。

所以你的代码看起来像这样:

sprite_back = pygame.image.load('CharacterBack.png')
sprite_front = pygame.image.load('CharacterFront.png')
sprite_right = pygame.image.load('CharacterRight.png')
sprite_left = pygame.image.load('CharacterLeft.png')

sprite = sprite_front

while True:

    DISPLAYSURF.blit(sprite,(spritex,spritey))

    if charLeft == True:
        spritex -= 10
    elif charRight == True:
        spritex += 10
    elif charUp == True:
        spritey -= 10
    elif charDown == True:
        spritey += 10

    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
                sprite=sprite_left
            elif (event.key == K_d):
                charRight = True
                sprite=sprite_right
            elif (event.key == K_w):
                charUp = True
                sprite=sprite_back
            elif (event.key == K_s):
                charDown = True
                sprite=sprite_front
        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