我的精灵不会向右移动pygame

时间:2012-05-12 18:30:44

标签: python pygame sprite

早些时候我的精灵根本不动,所以我发布了代码并且我已经修复了大部分但是现在我的向上/向下箭头工作正常但我的右键不起作用。 (此外,当你按下两个键,然后放开一个,步行动画不起作用,但我现在并不急于修复。)此外,我宁愿不使用用户制作的类。提前致谢。 这是代码:

from pygame.locals import *
import pygame._view

pygame.init()
clock = pygame.time.Clock()

height = 500
width = 500
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('placeholder text')

photo = 'grassbackground.png'
background = pygame.image.load(photo).convert()

rectexist = False

photo1 = 1

user = pygame.sprite.Sprite()

change = False

up = False
down = False
left = False
right = False

speed = 5

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                up = True
                change = True
            if event.key == K_DOWN:
                down = True
                change = True
            if event.key == K_LEFT:
                left = True
                change = True
            if event.type == K_RIGHT:
                right = True
                change = True

        if event.type == KEYUP:
            if event.key == K_UP:
                up = False
                change = False
            if event.key == K_DOWN:
                down = False
                change = False
            if event.key == K_LEFT:
                left = False
                change = False
            if event.key == K_RIGHT:
                right = False
                change = False

    if down and user.rect.bottom < height:
        user.rect.top += speed
    if up and user.rect.top > 0:
        user.rect.top -= speed
    if left and user.rect.left > 0:
        user.rect.left -= speed
    if right and user.rect.right < width:
        user.rect.right += speed

   if change == True:
        pygame.time.wait(110)
        photo1 += 1

    if change == False:
        photo1 = 1

    if photo1 == 1:
        user.image = pygame.image.load("1.png").convert()
        if rectexist == False:
            user.rect = user.image.get_rect()
        rectexist = True
        screen.blit(user.image, user.rect)

    if photo1 == 2:
        user.image = pygame.image.load("2.png").convert()
        screen.blit(user.image, user.rect)

    if photo1 == 3:
        user.image = pygame.image.load("3.png").convert()
        screen.blit(user.image, user.rect)

    if photo1 >= 4:
        photo1 = 1

    thesprites = pygame.sprite.RenderPlain((user))
    thesprites.update()

    screen.blit(background, (0, 0))
    thesprites.draw(screen)

    pygame.display.update()
    clock.tick(60)

2 个答案:

答案 0 :(得分:2)

在你的代码中说:

if event.type == K_RIGHT:

它应该是:

if event.key == K_RIGHT:

要保持动画效果,您需要更多地更改代码,请添加:

key_pressed = []

一开始。然后为每个按键块执行:

key_pressed.append(event.key)

和key-release do:

key_pressed = [k for k in key_pressed if k != event.key]

而不是change=Truechange=False。然后,在检查按下和释放的内容的两个部分之后添加以下行:

if len(key_pressed) > 0:
    change = True
else:
    change = False

这应该解决你的大部分问题...

答案 1 :(得分:0)

使用pressed = pygame.key.get_pressed()获取当前按下的所有键的字典(实际上是一个元组,但是结构的方式,你可以把它想象成字典)。你会这样使用它:

pressed = pygame.key.get_pressed()
if pressed[K_LEFT]:
    # move left
elif pressed[K_RIGHT]:
    # move right
# etc