我目前正在用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()
以上代码中的缩进是否正确?它显示updatecharacterposition()
及其在for event in pygame.event.get():
循环内的后几行。
如果确实是这种情况,那么如果没有任何事件,则不会调用该代码。这将导致所有运动停止。
尝试一下:
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)
在碰撞检查之后对我来说似乎很奇怪。通常,您会移动所有东西(玩家和“坏家伙”),然后进行碰撞检查。