简体   繁体   English

Pygame跳线碰撞检测不起作用

[英]Pygame Jumper collision detection not working

I am trying to make a game similar to what is seen in google chrome when the internet is down for my A Level computer science course work. 当我的A Level计算机科学课程工作中断时,我正在尝试制作类似于Google Chrome的游戏。 I have run into an issue regarding collision detection and any help would be greatly appreciated. 我遇到了有关碰撞检测的问题,任何帮助将不胜感激。

When i try to declare more than one platform to stop player movement (collision detection) all platforms stop stopping player movement. 当我尝试声明多个平台来停止玩家移动(冲突检测)时,所有平台都停止了玩家移动。 But when i have only one platform stopping player movement it will work. 但是,当我只有一个平台停止玩家移动时,它将起作用。

All of my code is located here: https://github.com/VincenzoLaRoche/ComputerScienceCourseWork 我所有的代码都位于这里: https : //github.com/VincenzoLaRoche/ComputerScienceCourseWork

Your problem is that your are handling the platforms completely separate. 您的问题是您要完全独立地处理平台。 So if you stand on one, you are not touching the other one so it makes you fall. 因此,如果您站在其中一个上,则不会碰到另一个,因此会使您跌倒。 For this to stop, you have to modify the t1o player methods collision_detect and do like so: 为此,您必须修改t1o播放器方法collision_detectdo

def collision_detect(self, platform):
    if self.x > platform.x and self.x < platform.x2:
        if self.y + 40 == platform.y:
            return True
        else:
            return False

def do(self):
    self.keys()
    self.move()
    self.draw()
    c1 = self.collision_detect(platform(0, 500, 800, 10))
    c2 = self.collision_detect(platform(0, 480, 400, 10))
    if c1 or c2:
        self.yVel = 0
        Constants.CANJUMP = True
    else:
        self.yVel = 5
        Constants.CANJUMP = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM