Python pygame中的Pacman-碰撞

Python pygame中的Pacman-碰撞,python,pygame,Python,Pygame,我正在尝试使用pygame编写python中的pacman克隆,尽管我仍在学习这两者 我对鬼魂的碰撞检测有困难。目前只有一个幽灵-它将地图视为数字矩阵,其中0表示墙。它应该在每次发生碰撞时随机选择方向(它的起始方向是向上的),但现在它只是向上,忽略所有的墙 这是主要的游戏循环: while True: for event in pygame.event.get(): if event.type == QUIT: exit() pacman.

我正在尝试使用pygame编写python中的pacman克隆,尽管我仍在学习这两者

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

这是主要的游戏循环:

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()
这是ghost类(请忽略波兰语注释):

这是它使用的碰撞函数:

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
def碰撞(p1,长度1,p2,长度2):#坐标,长度,坐标,长度
碰撞=错误
如果p1p2:
如果p1+长度1-p2<长度1+长度2:
碰撞=真
elif p1==p2:
碰撞=真
返回碰撞

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

BTW:shorter:
returnrandom.choice(['UP','DOWN','LEFT','RIGHT'])
BTW:Facebook上有波兰组:Python Poland,Python:pierwsze kroki.use
print()
要查看变量中的值以及执行的代码部分,tt有助于发现问题。也许
choose\u滴液
从未使用过。PyGame有
PyGame.Rect()
PyGame.sprite.sprite()
,它们都有检查冲突的方法。非常感谢,我要去看看。但从技术上讲,这个版本也应该可以工作……这个代码太长,无法手动检查,并且无法使用许多
print()
运行它,并查看变量中发生了什么以及执行了哪些部分。
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