微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从上掉下来的直肠只与列表中的一个碰撞?

如何解决从上掉下来的直肠只与列表中的一个碰撞?

我正在尝试使下降的rect与两个静态rect发生冲突,但它仅与列表中的第二个发生冲突。我怎样才能做到这一点?我尝试使用rect.coliderect,但它使rects重叠。但是我不希望它们重叠。我什至使用rect.bottom = tile.top,但是没有用。请帮忙


    import pygame
    
    dis = pygame.display.set_mode((500,500))
    clock = pygame.time.Clock()
    x = 200
    y = 10
    gra = 10
    air_timer = 0
    jumped = False
    
    
    def collisions(rect,tiles):
        collide = {
            'top': False,'bottom': False,'left': False,'right': False
        }
    
        for tile in tiles:
            # if tile.top <= rect.top <= tile.bottom:
            #     if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
            #         collide["top"] = True
            #     else:
            #         collide["top"] = False
            # else:
            #     collide["top"] = False
    
            if tile.top <= rect.bottom <= tile.bottom:
                if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
                    collide["bottom"] = True
                else:
                    collide["bottom"] = False
            else:
                collide["bottom"] = False
    
            # if tile.top <= rect.bottom <= tile.bottom:
            #     if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
            #         collide['bottom'] = True
            #     else:
            #         collide['bottom'] = False
            # else:
            #     collide['bottom'] = False
            #
            # if tile.top <= rect.bottom <= tile.bottom:
            #     if tile.left <= rect.right <= tile.right or tile.left <= rect.left <= tile.right:
            #         collide['bottom'] = True
            #     else:
            #         collide['bottom'] = False
            # else:
            #     collide['bottom'] = False
    
        return collide
    
    
    run = True
    while run:
        clock.tick(30)
        dis.fill((255,255,255))
        y += gra
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        pygame.draw.rect(dis,(16,50,255),(x,y,25,50),1)
        fall = pygame.Rect(x,50)
    
        pygame.draw.rect(dis,(200,400,1)
        fix = pygame.Rect(200,(300,350,1)
        fix2 = pygame.Rect(300,50)
    
        fixed = [fix2,fix]
        collision = collisions(fall,fixed)
        if collision["bottom"]:
            gra = 0
            air_timer = 0
        else:
            gra = 10
            air_timer += 1
    
        key = pygame.key.get_pressed()
    
        if key[pygame.K_LEFT]:
            x -= 5
        if key[pygame.K_RIGHT]:
            x += 5
    
        if key[pygame.K_UP]:
            if air_timer < 6:
                gra = -30
            elif air_timer >= 6:
                gra = 10
    
        pygame.display.update()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。