Python 3.x Pygame-斜行一个精灵

Python 3.x Pygame-斜行一个精灵,python-3.x,pygame,Python 3.x,Pygame,我是python新手,正在阅读Craig Richardson的《python冒险》一书 这是我的代码,我的挑战是当按下两个键时让精灵斜行 import pygame pygame.init() def move(image1, image2): global count if count < 5: image = image1 elif count >= 5: image = image2 if count >

我是python新手,正在阅读Craig Richardson的《python冒险》一书

这是我的代码,我的挑战是当按下两个键时让精灵斜行

import pygame
pygame.init()

def move(image1, image2):
    global count
    if count < 5:
        image = image1
    elif count >= 5:
        image = image2

    if count >= 10:
        count = 0
    else:
        count += 1
    return image

windowSize = [400, 300]
screen = pygame.display.set_mode(windowSize)
clock = pygame.time.Clock()

standing = pygame.image.load('standing.png')

down1 = pygame.image.load('down1.png')
down2 = pygame.image.load('down2.png')

up1 = pygame.image.load('up1.png')
up2 = pygame.image.load('up2.png')

left1 = pygame.image.load('side1.png')
left2 = pygame.image.load('side2.png')

right1 = pygame.transform.flip(left1, True, False)
right2 = pygame.transform.flip(left2, True, False)

white = pygame.color.Color("#FFFFFF")

count = 0
x = 0
y = 0

done = False
while not done:
    screen.fill(white)
    keys = pygame.key.get_pressed()

    #player movement
    if keys[pygame.K_d]:
        image = move(right1, right2)
        x += 1
    elif keys[pygame.K_s]:
        image = move(down1, down2)
        y += 1
    elif keys[pygame.K_w]:
        image = move(up1, up2)
        y -= 1
    elif keys[pygame.K_a]:
        image = move(left1, left2)
        x -= 1
    else:
        image = standing


    screen.blit(image, (x, y))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.display.flip()
    clock.tick(32)

pygame.quit()

您需要以不同的方式构造if-elif子句。将水平运动与垂直运动分开。使用if elif块检查播放器是否向左或向右移动,如果是,则水平移动播放器,然后对y轴执行相同操作

if keys[pygame.K_d]:
    image = move(right1, right2)
    x += 1
elif keys[pygame.K_a]:
    image = move(left1, left2)
    x -= 1

if keys[pygame.K_s]:
    image = move(down1, down2)
    y += 1
elif keys[pygame.K_w]:
    image = move(up1, up2)
    y -= 1

# Set the `image` to `standing` if none of the keys is pressed.
if not any((keys[pygame.K_d], keys[pygame.K_a], keys[pygame.K_w], keys[pygame.K_s])):
    image = standing

问题应该与if语句有关。如果你使用了所有的ifs,一旦你按下一个键,它就会一直执行,直到它被释放。试着这样做:

if keys[pygame.K_d]:
    image = move(right1, right2)
    x += 1
elif keys[pygame.K_a]:  
    image = move(left1, left2)
    x -= 1
if keys[pygame.K_s]:
    image = move(down1, down2)
    y += 1
elif keys[pygame.K_w]:
    image = move(up1, up2)
    y -= 1

希望这有帮助

好的。我理解。这样,当按下两个键时,两个ifs被激活。此外,为什么else条款不起作用?因为我的下一个挑战是如何让精灵保持在它移动的同一方向,而不是在没有按键的情况下站着。在发布之前,我会自己解决这个问题。只需在底部添加一个else子句(如果没有(…)来代替
),然后看看会发生什么。如果既没有按下
s
也没有按下
w
,它总是会将图像更改为
站着。
if keys[pygame.K_d]:
    image = move(right1, right2)
    x += 1
elif keys[pygame.K_a]:  
    image = move(left1, left2)
    x -= 1
if keys[pygame.K_s]:
    image = move(down1, down2)
    y += 1
elif keys[pygame.K_w]:
    image = move(up1, up2)
    y -= 1