2016-05-19 10 views
1

大学の試験のためにPacmanクローンを設計しています。 私はパイゲームを使用しており、壁との幽霊の衝突の処理にはいくつか問題があります。例えば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) 

ご注意いただきありがとうございます!

+0

これはちょっとした作業でもよいでしょう。あなたが左をうまく打って、あなたのランダムな方向がまだ正しいとします。その位置を再割り当てした後で、あなたの幽霊の左側はまだ壁に触れているかもしれませんが、self._direcitonは右です。これで衝突し、左側に飛び出します。これが起こっていないかどうかを確認してください。壁の右側に移動するだけで問題を解決できます。また、繰り返し衝突が起きないようにもう一度確認してください。 –

+0

ええ、多くの感謝@DavidJayBrady!私はすでに解決しましたが、あなたの説明に感謝します。私は質問を更新する。 –

答えて

0

あなたのコードをよく知らなくても、私はあなたに答えを与えるのが難しいと思っています。ただし、catch()メソッドからコードをデバッグしてみるとよいでしょう:

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

これは、何が起こっているのかについてさらに情報を与えるかもしれません。

catch()メソッドのif文がになったことに気付きました。ゴーストの動きの後です。確かに、これらは移動が許可される前に衝突をチェックする必要がありますか?

+0

デバッグ用の回線をありがとう! はいブロック「壁との衝突」のif文は、ゴーストの矩形が壁のすべてのブロックと衝突するかどうかをチェックします。 もっとコードをお願いしたいのですが、ほとんどすべてを書いています。 –

0

解決策はここにあります。David Jay Bredyが説明したとおりです。

壁との衝突。

 #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