碰撞检测无法正常运行

时间:2018-07-19 15:56:59

标签: python pygame collision rectangles

我正在尝试使用以下代码在岩石精灵上添加碰撞检测;

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)


class Player(pygame.sprite.Sprite):


    def __init__(self, filename, x, y):

        super().__init__()

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y


        self.change_x = 0
        self.change_y = 0

    def changespeed(self, x, y):
        self.change_x += x
        self.change_y += y

    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y


    def rockCollision(self, rock):

        block_hit_list = pygame.sprite.spritecollide(self, rock, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                self.rect.left = block.rect.right


        block_hit_list = pygame.sprite.spritecollide(self, rock, False)
        for block in block_hit_list:

            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom


class Rock(pygame.sprite.Sprite):

    def __init__(self, filename, x, y):

        super().__init__()    

        self.image = pygame.image.load(filename).convert()
        self.image.set_colorkey(BLACK)

        self.rect = self.image.get_rect().inflate(1, 1)
        self.rect.y = y
        self.rect.x = x



pygame.init()

screen_width = 1080
screen_height = 607
screen = pygame.display.set_mode([screen_width, screen_height])

pygame.display.set_caption('Labyrinth')

block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
rock_list = pygame.sprite.Group()

rock = Rock("Rock.png", 380, 280)
rock_list.add(rock)

player = Player("Isaac.png", 420, 150)
all_sprites_list.add(player)


clock = pygame.time.Clock()

background_position = [0, 0]
background_image = pygame.image.load("Floor.png").convert()

done = False


# -- MAIN PROGRAM LOOP -- #

# -- Event processing --

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

# Player controls

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-7, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(7, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, -7)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 7)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(7, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(-7, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, 7)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, -7)

# -- Game Logic --

    all_sprites_list.update()

# Hit detection 

    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
    player.rockCollision(rock_list)



    screen.blit(background_image, background_position)

    all_sprites_list.draw(screen)
    rock_list.draw(screen)

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

向左或向右移动到岩石中,碰撞效果完美。但是,向上或向下移动到岩石中会将我的播放器夹在岩石的右边(我永远无法真正走进岩石并卡在那儿,所以我会立即被夹在右边)。

我的问题是为什么会这样?我看不出为什么只有在我上下走入岩石时才发生这种情况的原因,而对于每个人来说,它们的代码都是相同的。

我希望能够在上,下行走时走进岩石,并能真正卡在岩石上而不是围住岩石

1 个答案:

答案 0 :(得分:4)

您的碰撞检测会检查self.change_x > 0self.change_y > 0,但不能处理这些值等于零的情况。这意味着您假设如果玩家不向上移动,则他必须向下移动,而实际上,在侧向移动时可能既不向上也不向下。同样,在垂直移动时也可以左右移动。

这只会在垂直行走时引起问题,因为在发生水平碰撞时,第一段代码将解决此问题,因此当您第二次调用spritecollide()时,block_hit_list为空,因此将跳过第二个for循环。

要进行改进,可以按以下方式更改rockCollision()方法:

def rockCollision(self, rock):
    block_hit_list = pygame.sprite.spritecollide(self, rock, False)
    for block in block_hit_list:
        if self.change_x > 0:
            self.rect.right = block.rect.left
        elif self.change_x < 0:
            self.rect.left = block.rect.right
        # else not moving sideways, so don't change our x position

    block_hit_list = pygame.sprite.spritecollide(self, rock, False)
    for block in block_hit_list:
        if self.change_y > 0:
            self.rect.bottom = block.rect.top
        elif self.change_y < 0:
            self.rect.top = block.rect.bottom
        # else not moving vertically, so don't change our y position

这解决了左右或上下移动时的问题。但是您会注意到,如果您沿对角线移动,它仍然会失败:即使self.change_xself.change_y都不为零,您的代码也无法判断这两个运动中的哪一个引起了碰撞

有一个教程here展示了如何通过将对角线运动分解为2个步骤(一个垂直方向和一个水平方向),并针对每个步骤进行碰撞检查,以更强大的方式处理碰撞。