Pygame Collide Rect精度问题

2024-04-18 22:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

rectGhost.left = blockRect.right

但是在多次碰撞(在墙的左侧)之后,鬼影越过墙,就好像鬼影矩形通过运动越过墙,并且在墙的左侧检测到碰撞,然后移动到墙的右侧的鬼影矩形的左侧。你知道吗

Collision issue example

这是幽灵的职业:

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)

有什么想法吗? 非常感谢您的关注!你知道吗


Tags: selfrightifdefrandomleftdownghost
2条回答

正如大卫·杰伊·布雷迪所解释的,解决方案就在这里。你知道吗

与墙壁相撞

        #pdb.set_trace() # Debugging

        for blockRect in wall:
            if rectGhost.colliderect(blockRect): 

                while 1:# Cycle that solve the problem
                    if self._direction ==  LEFT:
                        rectGhost.left = blockRect.right
                        self._direction = random.choice([RIGHT, UP, DOWN])
                        break

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

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

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

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

# Collision with wall
import pdb; pdb.set_trace()

这可能会给你更多的关于正在发生的事情的信息。你知道吗

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

相关问题 更多 >