Python:pygame - 动画bug

时间:2017-02-08 19:00:58

标签: python animation pygame sprite

我正在制作某种RPG风格的游戏,我正在尝试实施Zombie的方向。我有一个Zombie类,Zombies应该跟随玩家。我通过计算(玩家位置 - 僵尸位置)到矢量的程度来确定僵尸方向(他去哪里),该矢量平行于x轴并且通过在一些if-elif块中使用这个度我确定僵尸移动。 (例如" self.vel = vec(zombieSpeed,0)")

代码工作正常,但是当Zombie超过22.5 + 45 *度之一时,zombiedirection正在改变游戏更新的频率,因为在下一个followObject(...)中 - 调用速度僵尸改变了另一个方向。

首先:我在Zombie类的顶部创建了一个变量vec:

vec = pg.math.Vector2

现在Zombie跟随对象,其中设置了移动速度和方向:

def followObject(self, entity):
    degree = (self.game.player.newPos - self.newPos).angle_to(vec(1,0))

    if (degree > -22.5 and degree <= 22.5):
        self.vel = vec(zombieSpeed,0)
    elif (degree > 22.5 and degree <= 67.5):
        self.vel = vec(zombieSpeed,-zombieSpeed)
    elif (degree > 67.5 and degree <= 112.5):
        self.vel = vec(0,-zombieSpeed)
    elif (degree > 112.5 and degree <= 157.5):
        self.vel = vec(-zombieSpeed,-zombieSpeed)
    elif (degree > 157.5 and degree <= 180) or (degree <= -180 and degree >= -157.5):
        self.vel = vec(-zombieSpeed,0)
    elif (degree > -157.5 and degree <= -112.5):
        self.vel = vec(-zombieSpeed,zombieSpeed)
    elif (degree > -112.5 and degree <= -67.5):
        self.vel = vec(0,zombieSpeed)
    elif (degree > -67.5 and degree <= -22.5):
        self.vel = vec(zombieSpeed,zombieSpeed)
    if self.vel.x != 0 and self.vel.y != 0:
        self.vel *= math.sqrt(2)/2

我创建了这个方法来确定字符必须在哪里看:

def getDirectionLooking(self, oldPos, newPos):

    vecPosition = newPos - oldPos
    degree = vecPosition.angle_to(vec(1,0))
    if self.isMoving == True:
        if (degree >= -45 and degree <= 45):
            return 'r' #character right sprites
        elif (degree > 45 and degree < 135):
            return 'b' #character back sprites
        elif (degree >= 135 and degree <= 180) or (degree >= -180 and degree <= -135):
            return 'l' #character left sprites
        elif (degree < -45 and degree > -135):
            return 'f' #character front sprites
    else:
        return self.oldDir

这是Zombie的更新功能,每次比赛都会重复:

def update(self):
    print(self.newDir,end=' ')
    self.followObject(self.followEntity) #followEntity is the playerinstance
    self.oldPos = vec(self.newPos)
    self.newPos += self.vel * self.game.dt
    self.newDir = self.getDirectionLooking(self.oldPos, self.newPos)

    self.animationUpdate(self.newDir)

    self.rect.x = self.newPos.x
    self.collideWithWalls('x')
    self.rect.y = self.newPos.y
    self.collideWithWalls('y')

所以我需要一种方法,在这种方法中,我可以让僵尸在没有这个错误的情况下跟随角色#39;但我仍然希望僵尸只能移动到8个方向。

Zombie方向的输出:

 l f f f f f f f f f f l l f f f l f f l l f l f 

这是项目:

Link to the project

0 个答案:

没有答案