【问题标题】:Python doesn't detect a colliderect for one of my objects [closed]Python没有检测到我的一个对象的碰撞[关闭]
【发布时间】:2020-11-03 12:15:14
【问题描述】:

我有这个代码

if ball.colliderect(border_up_rect):
    border_up_collide = True
elif ball.colliderect(border_down_rect):
    border_down_collide = True
elif ball.colliderect(goal_attack_rect):
    my_score += 1
elif ball.colliderect(goal_def_rect):
    ai_score += 1


if border_up_collide == True:
    if where_did_it_come_from == True:
        ball_x -= 7
        ball_y += 2
        pboard_shoot1 = False
        pboard_shoot2 = False
        pboard_shoot3 = False
    elif where_did_it_come_from == False:
        ball_x -= 3
        ball_y += 2
        ai_shoot1 = False
        ai_shoot2 = False
        ai_shoot3 = False
elif border_down_collide == True:
    if where_did_it_come_from == True:
        ball_x -= 7
        ball_y -= 2
        pboard_shoot1 = False
        pboard_shoot2 = False
        pboard_shoot3 = False
    elif where_did_it_come_from == False:
        ball_x -= 3
        ball_y -= 2
        ai_shoot1 = False
        ai_shoot2 = False
        ai_shoot3 = False
elif goal_attack_collide == True:
    if where_did_it_come_from == True:
        ball_x = 600
        ball_y = 220
        pboard_shoot1 = False
        pboard_shoot2 = False
        pboard_shoot3 = False
    elif where_did_it_come_from == False:
        ball_x = 600
        ball_y = 220
        ai_shoot1 = False
        ai_shoot2 = False
        ai_shoot3 = False

这里它适用于border_up_rect 以及其他所有collide.rect,但不适用于border_down_rect。它只是让球穿过它。

我该如何解决这个问题?

【问题讨论】:

    标签: python pygame rectangles


    【解决方案1】:

    border_down_rect 不起作用的原因是因为border_up_rect 仍然为真,并且在border_up_rect 为真之前它不会切换elif 语句。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多