簡體   English   中英

多鍵按下 Pygame

[英]Multiple Keys press Pygame

我正在嘗試制作一個游戲,如果有這個會射擊的小機器人。 問題是它只有在它不動時才會射擊,當我向左或向右移動或跳躍時它不會射擊。 當我按下其他鍵時,我可以做些什么讓我的 barspace 鍵工作? 我試圖將另一個 if key 語句放在已經存在的 key 語句中,但它不起作用,就像我的意思:

elif keys[py.K_LEFT] and man.x >= 0:
    man.x -= man.vel
    man.right = False
    man.left = True
    man.standing = False
    man.idlecount = 0
    man.direction = -1

    if keys [py.K_SPACE] and shootloop == 0:
        if man.left:
            facing = -1

        elif man.right:
            facing = 1

        if len(bullets) < 5:
            man.standing = True
            man.shooting = True
            bullets.append(bulletss(round(man.x + man.lenght//2), round(man.y + man.lenght//2), facing))

        shootloop = 1

我把我的github留在這里,這樣你就可以運行程序了。 感謝您的幫助,並為我的代碼一團糟而感到抱歉。

https://github.com/20nicolas/Game.git

if keys [py.K_SPACE] and shootloop == 0:語句不應在elif keys[py.K_LEFT] and man.x >= 0:子句內,否則只能按左箭頭鍵才能射擊.

此外,在你的回購中,它實際上是,

if keys[py.K_RIGHT] and man.x <= 700:
    # ...
elif keys[py.K_LEFT] and man.x >= 0:
    # ...       
elif keys [py.K_SPACE] and shootloop == 0:

這意味着它只會在K_LEFTK_RIGHT都沒有按下時才會執行,因為這些語句在相同的if ... elif序列中。

這個版本對我有用:

elif keys[py.K_LEFT] and man.x >= 0:
    man.x -= man.vel
    man.right = False
    man.left = True
    man.standing = False
    man.idlecount = 0
    man.direction = -1
else:
    man.standing = True

if keys [py.K_SPACE] and shootloop == 0:
    if man.left:
        facing = -1

    elif man.right:
        facing = 1

    if len(bullets) < 5:
        man.standing = True
        man.shooting = True
        bullets.append(bulletss(round(man.x + man.lenght//2), round(man.y + man.lenght//2), 1))

    shootloop = 1
else:
    man.shooting = False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM