繁体   English   中英

如何阻止我在 pygame 中的矩形认为它们正在相互碰撞?

[英]How can I stop my rectangles in pygame thinking they are colliding into themselves?

所以我目前正在 pygame 中创建一个游戏,其中矩形在游戏 window 的随机部分产生,并沿随机方向移动。

对于这个游戏的一个功能,我必须尝试让每个矩形都可以检测到另一个矩形是否靠近它。 为此,我在跟随它们的每个矩形周围添加了第二个矩形,因此我可以检测这些第二个矩形是否发生碰撞,然后向主矩形发出下一步移动的命令。

然而,每当我运行下面的代码时,第二个矩形(在代码中写为“rect_anti_collsion”)似乎相信它们会不断地与自己发生碰撞,即使这是不可能的。

这段代码似乎有一个错误,虽然我不知道是什么:

    while loop1 < len(rects):
        try:
            if rects[-1][1].colliderect(rects[loop1][1]):
                print("collided")
        except IndexError:
            print("INDEX ERROR")
        loop1 += 1

完整代码(如果这个游戏看起来有点奇怪,那是因为我已经将代码简化为对这个 bug 很重要的所有内容):

import pygame
import time
import random
import sys
import os

def rect_animation(given_rect, given_rect_anti_collision):
    global rect_movement_counter, can_move_left, can_move_right, can_move_up, can_move_down

    del rects[rect_loop]
    rects.append([given_rect, given_rect_anti_collision])
    given_rect_anti_collision = rects[-1][1]
    given_rect = rects[-1][0]

    if given_rect.x <= 10:
        can_move_left = False

    if given_rect.x >= 1100:
        can_move_right = False

    if given_rect.y <= 10:
        can_move_up = False

    if given_rect.y >= 600:
        can_move_down = False

    direction = random.randint(1, 4)

    if direction == 1 and rect_movement_counter <= 0 and can_move_right:


        given_rect.x += 30

        given_rect_anti_collision.x += 30
        rect_movement_counter += 60


    elif direction == 2 and rect_movement_counter <= 0 and can_move_up:  # UP

        given_rect.y -= 30

        given_rect_anti_collision.y -= 30
        rect_movement_counter += 60

    elif direction == 3 and rect_movement_counter <= 0 and can_move_down:  # DOWN

        given_rect.y += 30

        given_rect_anti_collision.y += 30
        rect_movement_counter += 60

    elif direction == 4 and rect_movement_counter <= 0 and can_move_left:  # LEFT

        given_rect.x -= 30

        given_rect_anti_collision.x -= 30
        rect_movement_counter += 60

    else:
        rect_movement_counter -= 1
    loop1 = 0

    while loop1 < len(rects):
        try:
            if rects[-1][1].colliderect(rects[loop1][1]):
                print("collided")
        except IndexError:
            print("INDEX ERROR")
        loop1 += 1


    pygame.display.update()



pygame.init()

clock = pygame.time.Clock()

FPS = 10

screen_width, screen_height = 1160, 640

screen = pygame.display.set_mode((screen_width, screen_height))


rects = []

add_rect = True
for i in range(20):
    random_x = random.randint(1,18)
    random_y = random.randint(1,10)
    rect_x = 60 * random_x
    rect_y = 60 * random_y

    rect = pygame.Rect(rect_x, rect_y, 30,30)
    rect_anti_collision = pygame.Rect(rect_x - 1, rect_y - 1, 32,32)

    rects.append([rect, rect_anti_collision])

x_location = 0
y_location = 0

rect_movement_counter = 30

while True:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((20,140,20))

    for certain_rect_anti_collision in range(len(rects)):
        pygame.draw.rect(screen, (200, 200, 20), rects[certain_rect_anti_collision][1])
    for certain_rect in range(len(rects)):
        pygame.draw.rect(screen, (200, 200, 200), rects[certain_rect][0])



    rect_loop = 0

    while rect_loop < len(rects):
        can_move_right = True
        can_move_left = True
        can_move_up = True
        can_move_down = True

        rect_animation(rects[rect_loop][0],rects[rect_loop][1])
        rect_loop += 1

    pygame.display.flip()

只是不要测试列表中的最后一个矩形:

while loop1 < len(rects):

while loop1 < len(rects)-1:
    try:
        if rects[-1][1].colliderect(rects[loop1][1]):
        print("collided")
    except IndexError:
        print("INDEX ERROR")
    loop1 += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM