我正在使用pygame为一个项目编写平台游戏,之后与this tutorial并修改了他提供的文件here。我在平台的顶部设置了碰撞,但是当我尝试不通过平台跌落时,在侧面或底部移动时会弹出顶部。
我尝试使用此代码在各个方面进行碰撞,但最终在平台上出现故障。
hits = pg.sprite.spritecollide(self.player, self.platforms, False)
if hits:
if self.player.acc.x > 0: # Moving right; Hit the left side of the wall
self.player.right = hits[0].rect.left
self.player.vel.x = 0
if self.player.acc.x < 0: # Moving left; Hit the right side of the wall
self.player.left = hits[0].rect.right
self.player.vel.x = 0
if self.player.acc.y > 0: # Moving down; Hit the top side of the wall
self.player.bottom = hits[0].rect.top
self.player.vel.y = 0
if self.player.acc.y < 0: # Moving up; Hit the bottom side of the wall
self.player.top = hits[0].rect.bottom
self.player.vel.y = 0