为什么我的障碍物在我编码时仍然会彼此相邻产生,所以它们不会?
Why will my obstacles still spawn next to each other when I coded it so they won't?
我正在努力做到这一点,如果我的两个障碍物彼此靠近生成,它们就会移动,但它不起作用。为什么?
if obstacle2.rect.x - obstacle1.rect.x > 0:
while obstacle1.rect.x - obstacle2 .rect.x < 100:
obstacle1.rect.x = (random.randint(300, 900))
obstacle2.rect.x = (random.randint(300, 900))
if obstacle1.rect.x - obstacle2.rect.x > 0:
while obstacle2.rect.x - obstacle1 .rect.x < 100:
obstacle1.rect.x = (random.randint(300, 900))
obstacle2.rect.x = (random.randint(300, 900))
while循环只检查对象是否在一个方向上重叠,当你移动它们时它们可能会在另一个方向上重叠但满足while循环条件并退出。
删除您的 if
语句并创建一个 while
循环来检查它们是否在任一方向重叠
while abs(obstacle1.rect.x - obstacle2.rect.x) < 100:
obstacle1.rect.x = random.randint(300, 900)
obstacle2.rect.x = random.randint(300, 900)
我正在努力做到这一点,如果我的两个障碍物彼此靠近生成,它们就会移动,但它不起作用。为什么?
if obstacle2.rect.x - obstacle1.rect.x > 0:
while obstacle1.rect.x - obstacle2 .rect.x < 100:
obstacle1.rect.x = (random.randint(300, 900))
obstacle2.rect.x = (random.randint(300, 900))
if obstacle1.rect.x - obstacle2.rect.x > 0:
while obstacle2.rect.x - obstacle1 .rect.x < 100:
obstacle1.rect.x = (random.randint(300, 900))
obstacle2.rect.x = (random.randint(300, 900))
while循环只检查对象是否在一个方向上重叠,当你移动它们时它们可能会在另一个方向上重叠但满足while循环条件并退出。
删除您的 if
语句并创建一个 while
循环来检查它们是否在任一方向重叠
while abs(obstacle1.rect.x - obstacle2.rect.x) < 100:
obstacle1.rect.x = random.randint(300, 900)
obstacle2.rect.x = random.randint(300, 900)