【问题标题】:game crashes when bullet hits the wall子弹击中墙壁时游戏崩溃
【发布时间】:2020-05-18 03:18:13
【问题描述】:

在我的 pygame 游戏中,我的玩家射击“子弹”,当它们到达屏幕边缘时,它们是“pop()”-ed,但如果只有 1 颗子弹到达末端,而数组中没有其他东西,游戏立即崩溃。

    for i in reversed(range(len(shots))):
    shots[i].shoot()
    shots[i].drawBullet()


    if shots[i].x > swidth or shots[i].x < 0:
        shots.pop(i)

    if shots[i].y > sheight or shots[i].y < 0:
        shots.pop(i)

【问题讨论】:

  • 我认为这是格式错误,并且所有其余的行都应该在 fr 循环内?
  • 请格式化您的代码。您的问题的答案可能取决于缩进。

标签: python-3.x pygame


【解决方案1】:

假设您的意思是从 python 核心列表中弹出:

在调用 pop 之前在列表中使用“价值测试中的真理”。即“如果拍摄:”。

此外,我强烈建议在对镜头调用 pop(i) 之前,在某个时候也使用类似于“if len(shots)

Recall pop 返回删除的元素,您的代码没有反映或评论它的避免。我使用一个变量,不管像“discarded_i = shot.pop(i)”还是评论“#肯定永远不需要,因为xyz”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多