pygame 撞到天花板?
pygame collision with ceiling?
我只在此处看到有关与墙壁碰撞的信息,但我无法让玩家 "bump" 的头靠在天花板上。物体没有撞到它的头,速度被设置为 0 并且它由于重力而加速回落,而是被吸到它上面的平台上。为什么这不起作用,我该如何解决?我正在学习一个教程并逐渐进行我自己的改编,内容创建者说这很难实施。貌似对角碰撞也有可能出错? (根据内容创作者)
提前谢谢大家,老师要我在没有任何游戏引擎经验的情况下,在短短几天内制作一些东西展示给大家。
def update(self):
# Game Loop - Update
self.all_sprites.update()
# check if player hits a platform
hits = pygame.sprite.spritecollide(self.player,self.platforms , False)
if self.player.vel.y > 0:
if hits:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = 0
if self.player.vel.y < 0:
if hits:
self.player.rect.top = hits[0].rect.bottom
self.player.vel.y = 0
您可能希望将速度设置为 -1 或其他负值以强制移动 'downward' 超出边界条件,而不是将速度设置为 0。如果需要,您还可以在相同条件下添加加速度:
if hits:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = -1 # example velocity
self.player.acc.y = 2 # example acceleration
我只在此处看到有关与墙壁碰撞的信息,但我无法让玩家 "bump" 的头靠在天花板上。物体没有撞到它的头,速度被设置为 0 并且它由于重力而加速回落,而是被吸到它上面的平台上。为什么这不起作用,我该如何解决?我正在学习一个教程并逐渐进行我自己的改编,内容创建者说这很难实施。貌似对角碰撞也有可能出错? (根据内容创作者)
提前谢谢大家,老师要我在没有任何游戏引擎经验的情况下,在短短几天内制作一些东西展示给大家。
def update(self):
# Game Loop - Update
self.all_sprites.update()
# check if player hits a platform
hits = pygame.sprite.spritecollide(self.player,self.platforms , False)
if self.player.vel.y > 0:
if hits:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = 0
if self.player.vel.y < 0:
if hits:
self.player.rect.top = hits[0].rect.bottom
self.player.vel.y = 0
您可能希望将速度设置为 -1 或其他负值以强制移动 'downward' 超出边界条件,而不是将速度设置为 0。如果需要,您还可以在相同条件下添加加速度:
if hits:
self.player.pos.y = hits[0].rect.top
self.player.vel.y = -1 # example velocity
self.player.acc.y = 2 # example acceleration