Pacman在pygame - 碰撞

时间:2017-01-14 14:58:08

标签: python pygame

我正在尝试使用pygame在python中编写pacman克隆的东西,尽管我还在学习它们。

我对幽灵的碰撞检测有困难。目前只有一个鬼 - 它将地图视为数字矩阵,其中0表示墙。它应该在每次碰撞发生时随机选择方向(它的起始方向是UP)但是现在它只是忽略了所有的墙壁。

这是主要的游戏循环:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    pacman.change_direction(tiles)
    ghost.move(layout)
    for tile in tiles:
        pacman.collision(tile)
    screen.blit(background, (0,0))
    for tile in tiles:
        tile.draw(screen)
    ghost.draw(screen)
    pacman.draw(screen)
    pygame.display.update()

这是鬼类(请忽略波兰语中的评论):

class Ghost():
    def __init__(self, dimensions, coordinates=[0,0]): #mamy Blinky, Pinky, Inky, Clyde
        self.dimensions = dimensions
        self.coordinates = [13*16, 15*16]
        self.colour = (255,0,0)
        self.direction = 'UP'

    def draw(self, screen):
        pygame.draw.rect(screen, self.colour, (self.coordinates, self.dimensions))

    def choose_driection(self):
        a = random.randrange(1, 5)
        if (a==1):
            return 'UP'
        elif (a==2):
            return 'DOWN'
        elif (a==3):
            return 'RIGHT'
        elif (a==4):
            return 'LEFT'

    def collision(self, x, y, layout):
        if (layout[(x/16)-1][(y/16)-1]!=0):
            if (self.direction is 'LEFT'):
                self.coordinates[0] -= 1
            elif (self.direction is 'RIGHT'):
                self.coordinates[0] += 1
            elif (self.direction is 'UP'):
                self.coordinates[1] -= 1
            elif (self.direction is 'DOWN'):
                self.coordinates[1] += 1

        else: 
            xcollide = collide(self.coordinates[0], self.dimensions[0], x, 16)
            ycollide = collide(self.coordinates[1], self.dimensions[1], y, 16)

            if not (xcollide and ycollide):
                if (self.direction is 'LEFT'):
                    self.coordinates[0] -= 1
                elif (self.direction is 'RIGHT'):
                    self.coordinates[0] += 1
                elif (self.direction is 'UP'):
                    self.coordinates[1] -= 1
                elif (self.direction is 'DOWN'):
                    self.coordinates[1] += 1

            while xcollide and ycollide:
                if (self.direction is 'LEFT'):
                    self.coordinates[0] += 1
                elif (self.direction is 'RIGHT'):
                    self.coordinates[0] -= 1
                elif (self.direction is 'UP'):
                    self.coordinates[1] += 1
                elif (self.direction is 'DOWN'):
                    self.coordinates[1] -= 1

                self.direction = self.choose_direction()

                if (self.direction is 'LEFT'):
                    self.coordinates[0] -= 1
                    xcollide = collide(self.coordinates[0], self.dimensions[0], math.floor(self.coordinates[0]/16)*16-16, 16)
                elif (self.direction is 'RIGHT'):
                    self.coordinates[0] += 1
                    xcollide = collide(self.coordinates[0], self.dimensions[0], math.floor(self.coordinates[0]/16)*16+16, 16)
                elif (self.direction is 'UP'):
                    self.coordinates[1] -= 1
                    ycollide = collide(self.coordinates[1], self.dimensions[1], math.floor(self.coordinates[1]/16)*16-16, 16)
                elif (self.direction is 'DOWN'):
                    self.coordinates[1] += 1
                    ycollide = collide(self.coordinates[1], self.dimensions[1], math.floor(self.coordinates[1]/16)*16+16, 16)



    def move(self, layout):
            if (self.direction is 'LEFT'):
                self.collision(math.floor(self.coordinates[0]/16)*16-16, math.floor(self.coordinates[1]/16)*16, layout) #podloga, bo wspolrzedne ghost moga nie byc co 16 kiedy sie porusza, a plytek sa
            elif (self.direction is 'RIGHT'):
                self.collision(math.floor(self.coordinates[0]/16)*16+16, math.floor(self.coordinates[1]/16)*16, layout)
            elif (self.direction is 'UP'):
                self.collision(math.floor(self.coordinates[0]/16)*16, math.floor(self.coordinates[1]/16)*16-16, layout)
            elif (self.direction is 'DOWN'):
                self.collision(math.floor(self.coordinates[0]/16)*16, math.floor(self.coordinates[1]/16)*16+16, layout)

这是它使用的碰撞函数:

def collide(p1, length1, p2, length2): #coordinate, length, coordinate, length
    collided = False
    if p1 < p2:
        if p2+length2-p1 < length1+length2:
            collided = True
    elif p1 > p2:
        if p1+length1-p2 < length1+length2:
            collided = True
    elif p1 == p2:
        collided = True
    return collided

现在,我知道这不是最好的方法,我考虑使整个地图成为一个图形,但我仍然需要这种形式的花瓣尚未实现。代码还有一些其他问题,但是现在我只想知道为什么碰撞检测不起作用......

0 个答案:

没有答案