不总是检测到与障碍物的碰撞

时间:2015-11-11 10:02:23

标签: python pygame collision-detection mask

我没有记下可运行的代码(~1.7K行),只是与碰撞相关的部分。我将许多函数保存在名为os.fsync(sys.stdout.fileno())的文件中。代码在auxfuncs.py中运行。这里有许多变量和物体,但主要的想法是,如果障碍物和移动船之间存在蒙面碰撞,船舶来自的方向和速度将用于将其移回相同距离和相反方向

问题是,此代码由于某种原因仅在大约75%的时间内起作用。

main.py

玩家的动作是

import auxfuncs
...
#check masked collision
for ws in wall_sprite_group:
                offset = (hero.rect.x - ws.rect.x, hero.rect.y - ws.rect.y)
                diff = ws.wall_mask.overlap(hero.hero_mask, offset)
                if diff:
                    auxfuncs.push_back(hero, sec)

最后,# fighter movement if hero.direct == "left" and hero.rect.x > 0 and not hero.collide_list[2]: hero.rect.x -= hero.set_speed * sec elif hero.direct == "right" and hero.rect.right < SCREEN_SIZE[0] and not hero.collide_list[3]: hero.rect.x += hero.set_speed * sec elif hero.direct == "up" and hero.rect.y > 0 and not hero.collide_list[0]: hero.rect.y -= hero.set_speed * sec elif hero.direct == "down" and hero.rect.bottom < SCREEN_SIZE[1] and not hero.collide_list[1]: hero.rect.y += hero.set_speed * sec elif hero.direct == "northwest" and hero.rect.top > 0 and hero.rect.left > 0 and not hero.collide_list[ 0] and not hero.collide_list[2]: hero.rect.x -= hero.set_speed * sec hero.rect.y -= hero.set_speed * sec elif hero.direct == "northeast" and hero.rect.top > 0 and hero.rect.right < SCREEN_SIZE[0] and not \ hero.collide_list[0] and not hero.collide_list[3]: hero.rect.x += hero.set_speed * sec hero.rect.y -= hero.set_speed * sec elif hero.direct == "southwest" and hero.rect.bottom < SCREEN_SIZE[1] and hero.rect.left > 0 and not \ hero.collide_list[1] and not hero.collide_list[2]: hero.rect.x -= hero.set_speed * sec hero.rect.y += hero.set_speed * sec elif hero.direct == "southeast" and hero.rect.bottom < SCREEN_SIZE[1] and hero.rect.right < SCREEN_SIZE[ 0] and not hero.collide_list[1] and not hero.collide_list[3]: hero.rect.x += hero.set_speed * sec hero.rect.y += hero.set_speed * sec 函数将船舶向后移动的方向是

auxfuncs.push_back

编辑:在英雄类中有变量指向发生碰撞的方向:

def push_back(charact, sec):
    if charact.direct == "left":
        charact.rect.x += charact.set_speed * sec
        charact.collide_list[2] = True
    elif charact.direct == "right":
        charact.rect.x -= charact.set_speed * sec
        charact.collide_list[3] = True
    elif charact.direct == "down":
        charact.rect.y -= charact.set_speed * sec
        charact.collide_list[1] = True
    elif charact.direct == "up":
        charact.rect.y += charact.set_speed * sec
        charact.collide_list[0] = True
    elif charact.direct == "northwest":
        charact.rect.x += charact.set_speed * sec
        charact.rect.y += charact.set_speed * sec
        charact.collide_list[0], charact.collide_list[2] = True, True
    elif charact.direct == "northeast":
        charact.rect.x -= charact.set_speed * sec
        charact.rect.y += charact.set_speed * sec
        charact.collide_list[0], charact.collide_list[3] = True, True
    elif charact.direct == "southwest":
        charact.rect.x += charact.set_speed * sec
        charact.rect.y -= charact.set_speed * sec
        charact.collide_list[1], charact.collide_list[2] = True, True
    elif charact.direct == "southeast":
        charact.rect.x -= charact.set_speed * sec
        charact.rect.y -= charact.set_speed * sec
        charact.collide_list[1], charact.collide_list[3] = True, True

0 个答案:

没有答案