当我的角色站着不动时,我的敌人不动

My enemy isn't moving when my character is standing still

我目前正在 python 上编写游戏代码,我需要一种方法让敌人 运行 攻击玩家。它必须具有一个周期 5 个像素的恒定速度,这是我当前的代码:

def move(self,character):
    characterposition = Vector(character.posx,character.posy)
    self.position = Vector(self.posx,self.posy)
    dist = characterposition.subtract(self.position)
    dist.normal(dist.length())
    length = dist.length()
    ratio = self.speed/math.sqrt(length)
    self.posx += dist.x * ratio
    self.posy += dist.y * ratio

这似乎一直有效,直到我的角色停止移动。当角色停止移动时,敌人也会停止。这是一个问题,如果有任何建议,我将不胜感激。

编辑:这是我的循环。


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             pygame.quit()
             run = False
             continue
        updatecharacterposition()
        collided = checkforbarriercollision()
        badguy.move(character)
        if collided:
            print("YOU LOST")
            pygame.quit()
            time.sleep(5)
    redrawgamewindow()

上面代码中的缩进是否正确?它在 for event in pygame.event.get(): 循环中显示 updatecharacterposition() 及其后的行。

如果确实如此,那么如果您没有收到任何事件,将调用该代码的 none。那将导致所有运动停止。

试试这个:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             pygame.quit()
             run = False
             continue
    updatecharacterposition()
    collided = checkforbarriercollision()
    badguy.move(character)
    if collided:
        print("YOU LOST")
        pygame.quit()
        time.sleep(5)
    redrawgamewindow()

顺便说一句,badguy.move(character) 是在碰撞检查之后,这对我来说似乎很奇怪。通常你会移动所有东西(玩家和'badguy')然后做碰撞检查。