Pygame精灵向左移动时表现不正常

3

好的,跟进我的上一个问题后,我取得了一些进展。游戏现在拥有左右移动和简单跳跃脚本,进化哦 :D

我最新加入的是一个空闲动画,现在这个角色无法向左移动 :D 我希望这不会太愚蠢,我检查了所有东西,但找不出问题 >.> ..

无论如何,这里是代码,请提前致谢,非常感激!(原始代码太糟糕了,抱歉 >.<):

import pygame

pygame.init()

path = "C:/Users/user/Documents/Le game/"
win = pygame.display.set_mode((800, 700))
pygame.display.set_caption("Potato ultra")
bg = pygame.image.load(path + "BG.jpg")
walk_right = [pygame.image.load("C:/Users/user/Documents/Le game/R2.png"), pygame.image.load("C:/Users/user/Documents/Le game/R3.png"), pygame.image.load("C:/Users/user/Documents/Le game/R4.png"), pygame.image.load("C:/Users/user/Documents/Le game/R5.png"), pygame.image.load("C:/Users/user/Documents/Le game/R6.png"), pygame.image.load("C:/Users/user/Documents/Le game/R7.png"), pygame.image.load("C:/Users/user/Documents/Le game/R8.png"), pygame.image.load("C:/Users/user/Documents/Le game/R9.png")]
walk_left = [pygame.image.load("C:/Users/user/Documents/Le game/L2.png"), pygame.image.load("C:/Users/user/Documents/Le game/L3.png"), pygame.image.load("C:/Users/user/Documents/Le game/L4.png"), pygame.image.load("C:/Users/user/Documents/Le game/L5.png"), pygame.image.load("C:/Users/user/Documents/Le game/L6.png"), pygame.image.load("C:/Users/user/Documents/Le game/L7.png"), pygame.image.load("C:/Users/user/Documents/Le game/L8.png"), pygame.image.load("C:/Users/user/Documents/Le game/L9.png")]
Static = pygame.image.load("C:/Users/user/Documents/Le game/Idle.png")

SW = 800
SH = 700
x = 0
y = 480
width = 64
height = 64
vel = 20
isJump = False
MoveLeft = False
MoveRight = False
Idle = False
JumpCount = 10
walkCount = 0

def redrawGameWindow():

    win.blit(bg, (0,0))
    global walkCount
    if not Idle:
        if MoveRight:
            if walkCount <= 7:
                win.blit(walk_right[walkCount], (x, y))
            elif walkCount > 7:
                walkCount = 0
                win.blit(walk_right[walkCount], (x, y))
        if MoveLeft:
            if walkCount <= 7:
                win.blit(walk_left[walkCount], (x, y))
            elif walkCount > 7:
                walkCount = 0
                win.blit(walk_left[walkCount], (x, y))
    else:
        win.blit(Static, (x, y))

    pygame.display.update()


run = True
while run:
    pygame.time.delay(50)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > 0:
        Idle = False
        MoveRight = False
        MoveLeft = True
        x -= vel
        walkCount += 1
    if keys[pygame.K_RIGHT] and x < SW - width:
        Idle = False
        MoveRight = True
        MoveLeft = False
        x += vel
        walkCount += 1
    else:
        Idle = True

    if not isJump:
        if keys[pygame.K_UP] and y > 0:
            y -= vel
            if y < 0:
                y = 0
        if keys[pygame.K_DOWN] and y < SH - height:
            y += vel
            if y > 636:
                y = 636
        if keys[pygame.K_SPACE]:
            isJump = True
    else:
        if JumpCount >= -10:
            y -= (JumpCount * 4)
            if y < 0:
                y = 0
            JumpCount -= 2
        else:
            isJump = False
            JumpCount = 10

    redrawGameWindow()

pygame.quit()

你有一款叫做“Le game”的游戏吗?如果你想制作另一款游戏,你会怎么做?我们能期待“Le game bis”吗?</humour> - paxdiablo
1个回答

3

好的,我会先添加以下代码(在下面的最后一行之前,你已经有了):

print("DEBUG1 keyl =", keys[pygame.K_LEFT], "x =", x)
print("DEBUG2 keyr =", keys[pygame.K_RIGHT], "SW =", Sw, "wid =", width)
print("DEBUG3 mover =", MoveRight, "movel =", MoveLeft)
print("DEBUG4 vel =", vel, "walkc =", walkCount)
print("DEBUG5 ==========")
if keys[pygame.K_LEFT] and x > 0:

那么,您将会看到所有参与决定左右移动的变量,希望很明显可以发现是什么阻止了左移动的功能。


通过更深入地查看您的代码,似乎是这一部分:

if keys[pygame.K_LEFT] and x > 0:
    Idle = False
    MoveRight = False
    MoveLeft = True
    x -= vel
    walkCount += 1
if keys[pygame.K_RIGHT] and x < SW - width:
    Idle = False
    MoveRight = True
    MoveLeft = False
    x += vel
    walkCount += 1
else:
    Idle = True

由于else仅属于第二个if语句,因此无论您是否按下左键,只要您没有按下正确的键,它就会触发。

我猜您可以通过将其从if,if,else序列更改为if,elif,else序列来简单地解决这个问题,因此else仅在没有任何一个键被按下时才会触发。


对您的代码进行了几点改进:

  • 您可以通过在事件循环中使用walkCount =(walkCount + 1)%8 来消除在重绘函数中检查和调整所有walkCount的代码,这将确保它从七转到零而不需要进一步努力。

  • 您好像没有对x值进行限制。例如,如果x == 5并且vel == 10,则向左移动可能会将x = -5设置为不可取的值。您比我更了解游戏玩法,所以我可能是错误的,但值得检查。

  • 可能不需要同时使用MoveLeft MoveRightIdle标志决定您是否移动,因此如果您正在移动,则要么向左或向右。因此,Idle / MoveRight组合应足以让代码弄清楚要做什么。同样,这是一个游戏玩法问题,值得研究,但我可能不正确。

  • 不确定当它们跳跃时您的精灵看起来如何,但是使用计算y位置的恒定加速度公式可能更好。我之前回答过类似的问题,因此您可以查看这个答案以获得指导。


嘿,if语句很对头,空闲动画一直被设置为True,这确实允许角色移动,但它总是显示他像个瘫子一样被冻结在空闲动画中(哈哈),我喜欢第二部分,你提出了建议,第一个建议确实缩短了我的代码,现在函数内只有8行,看起来更干净了,第二个建议我目前还没有遇到这个问题,但为了未来的考虑,你是正确的,我会改变它,第三个也是正确的,但我决定保留它,因为我可能需要这个变量用于将来,第四个我会去检查一下,非常感谢。 - Zelix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接