碰撞检测无法正常工作
collision detection not functioning properly
我正在尝试使用以下代码在岩石精灵上添加碰撞检测;
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()
向左或向右移动到岩石中,碰撞效果完美。但是,向上或向下移动到岩石中会使我的玩家夹在岩石的右侧(我永远无法真正走进岩石并卡在那里,我会立即被夹在右侧)。
我的问题是为什么会这样?我看不出为什么只有当我向上或向下走进岩石时才会发生这种情况,而不是向左或向右走,因为每个代码都是相同的。
我希望能够在上下行走时走进岩石,并且能够真正卡在岩石上而不是夹在岩石周围
您的碰撞检测检查 self.change_x > 0
和 self.change_y > 0
,但不处理这些值等于零的情况。这意味着您假设如果玩家没有向上移动,他一定是向下移动,而实际上,有可能在横向移动时既不向上也不向下移动。垂直移动时 left/right 移动也类似。
这只会在垂直行走时导致问题,因为当发生水平碰撞时,问题会被第一段代码清除,所以当您第二次调用 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_x
和 self.change_y
都不为零,您的代码也无法判断这两个运动中的哪一个导致了碰撞.
有一个教程 here 展示了如何以更稳健的方式处理碰撞,方法是将对角线运动分解为 2 个步骤(一个垂直和一个水平),并对每个步骤进行碰撞检查。
我正在尝试使用以下代码在岩石精灵上添加碰撞检测;
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()
向左或向右移动到岩石中,碰撞效果完美。但是,向上或向下移动到岩石中会使我的玩家夹在岩石的右侧(我永远无法真正走进岩石并卡在那里,我会立即被夹在右侧)。
我的问题是为什么会这样?我看不出为什么只有当我向上或向下走进岩石时才会发生这种情况,而不是向左或向右走,因为每个代码都是相同的。
我希望能够在上下行走时走进岩石,并且能够真正卡在岩石上而不是夹在岩石周围
您的碰撞检测检查 self.change_x > 0
和 self.change_y > 0
,但不处理这些值等于零的情况。这意味着您假设如果玩家没有向上移动,他一定是向下移动,而实际上,有可能在横向移动时既不向上也不向下移动。垂直移动时 left/right 移动也类似。
这只会在垂直行走时导致问题,因为当发生水平碰撞时,问题会被第一段代码清除,所以当您第二次调用 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_x
和 self.change_y
都不为零,您的代码也无法判断这两个运动中的哪一个导致了碰撞.
有一个教程 here 展示了如何以更稳健的方式处理碰撞,方法是将对角线运动分解为 2 个步骤(一个垂直和一个水平),并对每个步骤进行碰撞检查。