如果有一个能射击的小机器人,我正在尝试制作一个游戏。问题在于它仅在不移动时,在向左或向右移动或跳跃时才拍摄。当我按下其他键时,可以做些什么让我的Barspace键起作用吗?我试图将另一个if关键语句放在已经存在但不起作用的关键语句中,就像这样:
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留在这里,因此您可以运行该程序。谢谢您的帮助,对不起我的代码。
答案 0 :(得分:1)
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_LEFT
和K_RIGHT
的情况下才会执行该命令,因为这些语句的顺序相同。if
... elif
。 / p>
此版本对我有用:
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