如何在Python/Pygame中实现跳墙

如何在Python/Pygame中实现跳墙,python,pygame,Python,Pygame,我只是想知道是否有人能帮我让我的角色墙在我用Pygame制作的游戏中跳跃。到目前为止,我已经尝试了多种碰撞功能,但没有任何效果,我需要的是角色在地面或平台或墙壁一侧跳跃。感谢所有帮助,以下是代码: def jump(self): """ Called when user hits 'jump' button. """ # move down a bit and see if there is a platform below us. # Move

我只是想知道是否有人能帮我让我的角色墙在我用Pygame制作的游戏中跳跃。到目前为止,我已经尝试了多种碰撞功能,但没有任何效果,我需要的是角色在地面或平台或墙壁一侧跳跃。感谢所有帮助,以下是代码:

def jump(self):
        """ Called when user hits 'jump' button. """

        # move down a bit and see if there is a platform below us.
        # Move down 2 pixels because it doesn't work well if we only move down 1
        # when working with a platform moving down.
        self.rect.y += 2
        platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)

        for block in platform_hit_list:

            if pygame.sprite.collide_rect(self,block) or pygame.sprite.collide_rect(block,self):

                self.change_y = -8.5
                print("I am reading this!")

        self.rect.y -= 2

        # If it is ok to jump, set our speed upwards
        if len(platform_hit_list) > 0 or self.rect.bottom >= constants.SCREEN_HEIGHT:
            self.change_y = -8.5
对于if语句,我尝试了以下条件:

if pygame.sprite.collide_rect(self.rect,block.rect) or 
pygame.sprite.collide_rect(self.rect,block.rect)


到底出了什么问题?当在墙上输入跳转按钮时,代码会打印出消息,但看不到高度增加。即使在地面上执行跳跃,代码也会被读取。如果不检查x方向的碰撞,你怎么知道你旁边是否有墙?这就是我想做的,但它告诉我int是不可编辑的错误。你知道为什么在检查碰撞之前调整了精灵的
y
值吗?您认为在检查碰撞之前对
x
执行同样的操作会有帮助吗?想一想,如果你站在一堵墙旁边,你会撞到它吗?
if pygame.sprite.collide_rect(self.rect.right,block.rect.left) or
 pygame.sprite.collide_rect(self.rect.right,block.rect.left)