Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame碰撞精度问题_Python_Pygame_Game Physics - Fatal编程技术网

Python Pygame碰撞精度问题

Python Pygame碰撞精度问题,python,pygame,game-physics,Python,Pygame,Game Physics,我正在为大学考试设计一个吃豆人克隆人。 我使用pygame,在处理鬼魂与墙的碰撞时遇到了一些问题 例如,当重影与左侧的墙碰撞时,重影矩形的左侧采用墙矩形右侧的值: rectGhost.left = blockRect.right 但在多次碰撞(在墙的左侧)后,重影会越过墙,就好像随着运动,重影矩形会越过墙,并在墙的左侧检测到碰撞,然后移动到墙右侧的重影矩形的左侧 这是幽灵的职业: import character, random from globalVar import * class

我正在为大学考试设计一个吃豆人克隆人。 我使用pygame,在处理鬼魂与墙的碰撞时遇到了一些问题

例如,当重影与左侧的墙碰撞时,重影矩形的左侧采用墙矩形右侧的值:

rectGhost.left = blockRect.right
但在多次碰撞(在墙的左侧)后,重影会越过墙,就好像随着运动,重影矩形会越过墙,并在墙的左侧检测到碰撞,然后移动到墙右侧的重影矩形的左侧

这是幽灵的职业:

import character, random
from globalVar import *

class ghost(character.character):

    def __init__(self, imgSprite):
        super(ghost, self).__init__(imgSprite)
        self._direction = random.choice([LEFT, RIGHT, UP, DOWN])

    def catch(self, wall):

        # Keep the direction 
        if self._direction ==  LEFT:
            self.moveToLeft()
        if self._direction ==  RIGHT:
            self.moveToRight()
        if self._direction ==  UP:
            self.moveToUp()
        if self._direction ==  DOWN:
            self.moveToDown()


        # Get ghost position
        rectGhost = self.getRectSprite()

        # Collision with wall
        for blockRect in wall:
            if rectGhost.colliderect(blockRect): # Check collision with the wall (wall contain a list of Rect object)

                if self._direction ==  LEFT:
                    rectGhost.left = blockRect.right 
                    self._direction = random.choice([RIGHT, UP, DOWN]) 

                if self._direction ==  RIGHT:
                    rectGhost.right = blockRect.left 
                    self._direction = random.choice([LEFT, UP, DOWN]) 

                if self._direction ==  UP:
                    rectGhost.top = blockRect.bottom 
                    self._direction = random.choice([LEFT, RIGHT, DOWN]) 

                if self._direction ==  DOWN:
                    rectGhost.bottom = blockRect.top 
                    self._direction = random.choice([LEFT, RIGHT, UP]) 
from globalVar import *

class character:

    def __init__(self, imgSprite):
        self._imageSprite = imgSprite
        self._spriteRect = self._imageSprite.get_rect() 
        self._dimStep = 10 
        self._direction = 0

    def setCharPos(self, charPos): 
        self._spriteRect.centerx = charPos[0]
        self._spriteRect.centery = charPos[1]


    def moveToLeft(self):
        self._spriteRect.x -= self._dimStep 


    def moveToRight(self):
        self._spriteRect.x += self._dimStep


    def moveToUp(self):
        self._spriteRect.y -= self._dimStep


    def moveToDown(self):
        self._spriteRect.y += self._dimStep


    def drawSprite(self):
        WINDOWSURF.blit(self._imageSprite, self._spriteRect)


    def getRectSprite(self):
        return self._spriteRect

    def setDirection(self, direction):
        self._direction = direction
def main():

    thisPacman = pacman.pacman(pacmanCImg) # Ghot obj

    thisGhost_a = ghost.ghost(ghostAImg) # Ghot obj

    thisGhost_o = ghost.ghost(ghostOImg) # Ghot obj

    thisGhost_p = ghost.ghost(ghostPImg) # Ghot obj

    thisGhost_r = ghost.ghost(ghostRImg) # Ghot obj

    # Coords of wall
    wall = thisLevel.getBlocksListPos()

    # Coords of Ghosts
    ghostsStartPos = thisLevel.getGhostsPos()

    # Start pos azure
    thisGhost_a.setCharPos(ghostsStartPos[0])

    # Start pos ghost orange
    thisGhost_o.setCharPos(ghostsStartPos[1])

    # Start pos ghost purple
    thisGhost_p.setCharPos(ghostsStartPos[2])

    # Start pos ghost red
    thisGhost_r.setCharPos(ghostsStartPos[3])

    while running:

        thisGhost_a.catch(wall)
        thisGhost_o.catch(wall)
        thisGhost_p.catch(wall)
        thisGhost_r.catch(wall)

        thisGhost_a.drawSprite()
        thisGhost_o.drawSprite()
        thisGhost_p.drawSprite()
        thisGhost_r.drawSprite()

        pygame.display.update()
        FPSCLOCK.tick(8)
这是超类字符:

import character, random
from globalVar import *

class ghost(character.character):

    def __init__(self, imgSprite):
        super(ghost, self).__init__(imgSprite)
        self._direction = random.choice([LEFT, RIGHT, UP, DOWN])

    def catch(self, wall):

        # Keep the direction 
        if self._direction ==  LEFT:
            self.moveToLeft()
        if self._direction ==  RIGHT:
            self.moveToRight()
        if self._direction ==  UP:
            self.moveToUp()
        if self._direction ==  DOWN:
            self.moveToDown()


        # Get ghost position
        rectGhost = self.getRectSprite()

        # Collision with wall
        for blockRect in wall:
            if rectGhost.colliderect(blockRect): # Check collision with the wall (wall contain a list of Rect object)

                if self._direction ==  LEFT:
                    rectGhost.left = blockRect.right 
                    self._direction = random.choice([RIGHT, UP, DOWN]) 

                if self._direction ==  RIGHT:
                    rectGhost.right = blockRect.left 
                    self._direction = random.choice([LEFT, UP, DOWN]) 

                if self._direction ==  UP:
                    rectGhost.top = blockRect.bottom 
                    self._direction = random.choice([LEFT, RIGHT, DOWN]) 

                if self._direction ==  DOWN:
                    rectGhost.bottom = blockRect.top 
                    self._direction = random.choice([LEFT, RIGHT, UP]) 
from globalVar import *

class character:

    def __init__(self, imgSprite):
        self._imageSprite = imgSprite
        self._spriteRect = self._imageSprite.get_rect() 
        self._dimStep = 10 
        self._direction = 0

    def setCharPos(self, charPos): 
        self._spriteRect.centerx = charPos[0]
        self._spriteRect.centery = charPos[1]


    def moveToLeft(self):
        self._spriteRect.x -= self._dimStep 


    def moveToRight(self):
        self._spriteRect.x += self._dimStep


    def moveToUp(self):
        self._spriteRect.y -= self._dimStep


    def moveToDown(self):
        self._spriteRect.y += self._dimStep


    def drawSprite(self):
        WINDOWSURF.blit(self._imageSprite, self._spriteRect)


    def getRectSprite(self):
        return self._spriteRect

    def setDirection(self, direction):
        self._direction = direction
def main():

    thisPacman = pacman.pacman(pacmanCImg) # Ghot obj

    thisGhost_a = ghost.ghost(ghostAImg) # Ghot obj

    thisGhost_o = ghost.ghost(ghostOImg) # Ghot obj

    thisGhost_p = ghost.ghost(ghostPImg) # Ghot obj

    thisGhost_r = ghost.ghost(ghostRImg) # Ghot obj

    # Coords of wall
    wall = thisLevel.getBlocksListPos()

    # Coords of Ghosts
    ghostsStartPos = thisLevel.getGhostsPos()

    # Start pos azure
    thisGhost_a.setCharPos(ghostsStartPos[0])

    # Start pos ghost orange
    thisGhost_o.setCharPos(ghostsStartPos[1])

    # Start pos ghost purple
    thisGhost_p.setCharPos(ghostsStartPos[2])

    # Start pos ghost red
    thisGhost_r.setCharPos(ghostsStartPos[3])

    while running:

        thisGhost_a.catch(wall)
        thisGhost_o.catch(wall)
        thisGhost_p.catch(wall)
        thisGhost_r.catch(wall)

        thisGhost_a.drawSprite()
        thisGhost_o.drawSprite()
        thisGhost_p.drawSprite()
        thisGhost_r.drawSprite()

        pygame.display.update()
        FPSCLOCK.tick(8)
这是主要内容:

import character, random
from globalVar import *

class ghost(character.character):

    def __init__(self, imgSprite):
        super(ghost, self).__init__(imgSprite)
        self._direction = random.choice([LEFT, RIGHT, UP, DOWN])

    def catch(self, wall):

        # Keep the direction 
        if self._direction ==  LEFT:
            self.moveToLeft()
        if self._direction ==  RIGHT:
            self.moveToRight()
        if self._direction ==  UP:
            self.moveToUp()
        if self._direction ==  DOWN:
            self.moveToDown()


        # Get ghost position
        rectGhost = self.getRectSprite()

        # Collision with wall
        for blockRect in wall:
            if rectGhost.colliderect(blockRect): # Check collision with the wall (wall contain a list of Rect object)

                if self._direction ==  LEFT:
                    rectGhost.left = blockRect.right 
                    self._direction = random.choice([RIGHT, UP, DOWN]) 

                if self._direction ==  RIGHT:
                    rectGhost.right = blockRect.left 
                    self._direction = random.choice([LEFT, UP, DOWN]) 

                if self._direction ==  UP:
                    rectGhost.top = blockRect.bottom 
                    self._direction = random.choice([LEFT, RIGHT, DOWN]) 

                if self._direction ==  DOWN:
                    rectGhost.bottom = blockRect.top 
                    self._direction = random.choice([LEFT, RIGHT, UP]) 
from globalVar import *

class character:

    def __init__(self, imgSprite):
        self._imageSprite = imgSprite
        self._spriteRect = self._imageSprite.get_rect() 
        self._dimStep = 10 
        self._direction = 0

    def setCharPos(self, charPos): 
        self._spriteRect.centerx = charPos[0]
        self._spriteRect.centery = charPos[1]


    def moveToLeft(self):
        self._spriteRect.x -= self._dimStep 


    def moveToRight(self):
        self._spriteRect.x += self._dimStep


    def moveToUp(self):
        self._spriteRect.y -= self._dimStep


    def moveToDown(self):
        self._spriteRect.y += self._dimStep


    def drawSprite(self):
        WINDOWSURF.blit(self._imageSprite, self._spriteRect)


    def getRectSprite(self):
        return self._spriteRect

    def setDirection(self, direction):
        self._direction = direction
def main():

    thisPacman = pacman.pacman(pacmanCImg) # Ghot obj

    thisGhost_a = ghost.ghost(ghostAImg) # Ghot obj

    thisGhost_o = ghost.ghost(ghostOImg) # Ghot obj

    thisGhost_p = ghost.ghost(ghostPImg) # Ghot obj

    thisGhost_r = ghost.ghost(ghostRImg) # Ghot obj

    # Coords of wall
    wall = thisLevel.getBlocksListPos()

    # Coords of Ghosts
    ghostsStartPos = thisLevel.getGhostsPos()

    # Start pos azure
    thisGhost_a.setCharPos(ghostsStartPos[0])

    # Start pos ghost orange
    thisGhost_o.setCharPos(ghostsStartPos[1])

    # Start pos ghost purple
    thisGhost_p.setCharPos(ghostsStartPos[2])

    # Start pos ghost red
    thisGhost_r.setCharPos(ghostsStartPos[3])

    while running:

        thisGhost_a.catch(wall)
        thisGhost_o.catch(wall)
        thisGhost_p.catch(wall)
        thisGhost_r.catch(wall)

        thisGhost_a.drawSprite()
        thisGhost_o.drawSprite()
        thisGhost_p.drawSprite()
        thisGhost_r.drawSprite()

        pygame.display.update()
        FPSCLOCK.tick(8)
有什么想法吗?
非常感谢您的关注

如果不熟悉你的代码,我很难给你答案。但是,您可以尝试从catch()方法调试代码:

这可能会为您提供更多有关正在发生的事情的信息


我还注意到catch()方法中的if语句出现在ghost运动之后。当然,在允许移动之前,需要检查碰撞情况吗?

解决方案就在这里,正如戴维·杰伊·布雷迪(David Jay Bredy)所解释的那样

与墙壁碰撞。
感谢您的在线调试!是块“与墙碰撞”中的if语句检查重影矩形是否与墙的每个块碰撞,如果碰撞更改方向,否则保持方向。如果你想要更多的代码,可以问我,但我几乎写了所有的东西。可能是你的代码工作得有点太好了。假设你击中左边的井,然后你的随机方向仍然是正确的。在重新分配了位置后,你的鬼魂的左侧可能仍然触碰墙壁,而你的自我是对的。这会使它发生碰撞,然后弹出到左侧。检查是否发生了碰撞,如果发生了,只需将其移到墙的右侧即可修复,再加上一点,以确保不会再次发生碰撞是的,非常感谢@DavidJayBrady!我已经解决了,但是谢谢你的解释。我更新问题。