【问题标题】:Problems with character movement角色移动问题
【发布时间】:2019-07-07 23:17:28
【问题描述】:

我改变了我的敌人路径,但在 1 次尝试游戏后显示变黑,然后我改变了我的敌人路径恢复正常但我的角色无法移动......这可能是 pygame 有问题或pycharm

我尝试多次按“Ctrl + z”,但也没有用。

我的动作代码:

        if keys[pygame.K_LEFT] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False
    elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False
    else:
        man.standing = True
        man.walkCount = 0

    if not (man.isJump):
        if keys[pygame.K_UP]:
            man.isJump = True
            man.right = False
            man.left = False
            man.walkCount = 0
    else:
        if man.jumpCount >= -10:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.5 * neg
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 10
        man.jumpCount = 10

你可以在这里看到完整的代码:

主要:https://pastebin.com/pHgtUi5F

玩家:https://pastebin.com/C4kmU2Rn

弹道:https://pastebin.com/LgG5kCu3

敌人:https://pastebin.com/wsgcbtUG

【问题讨论】:

  • 你应该使用pygame.Rect()来保持对象的大小和位置——它具有检查两个或多个Rect()之间的碰撞的功能
  • 您可以使用print() 显示执行了哪部分代码以及变量中有什么。这样你可能会发现问题出在哪里。或者学习如何使用调试器。
  • 重绘所有元素后,您应该只在redrawGameWindow 中使用pygame.display.update()。不要在其他功能中使用它,因为它会降低动画的流畅度。
  • 我放了断点,它没有反应
  • 然后在其他地方放断点,看看执行了哪些部分。然后你可以检查为什么其他部分没有执行。也许某些 if/else 不能以预期的方式工作。也许你有错误的缩进,一个IF 在另一个IF 里面,但它不应该。在main.py 中,我看到if K_LEFTif K_SPACE 内部,所以您可能只有在按下SPACE 时才能移动。

标签: python python-3.x pygame pycharm


【解决方案1】:

好的,它开始工作了...所以我所做的是在主循环中添加了 keys = pygame.key.get_pressed() 并从 if K_SPACE 中删除了 if K_LEFTif K_RIGHT 等等...所以我的步行代码看起来像这样:

keys = pygame.key.get_pressed()

if keys[pygame.K_SPACE] and shootLoop == 0:
    if man.left:
        facing = -1
    else:
        facing = 1

    if len(bullets) < 5:
        bullets.append(
            projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))
        shootLoop = 1

if keys[pygame.K_LEFT] and man.x > man.vel:
    man.x -= man.vel
    man.left = True
    man.right = False
    man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
    man.x += man.vel
    man.right = True
    man.left = False
    man.standing = False
else:
    man.standing = True
    man.walkCount = 0

if not (man.isJump):
    if keys[pygame.K_UP]:
        man.isJump = True
        man.right = False
        man.left = False
        man.walkCount = 0
else:
    if man.jumpCount >= -10:
        neg = 1
        if man.jumpCount < 0:
            neg = -1
        man.y -= (man.jumpCount ** 2) * 0.5 * neg
        man.jumpCount -= 1
    else:
        man.isJump = False
        man.jumpCount = 10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    相关资源
    最近更新 更多