【发布时间】:2020-05-31 16:31:14
【问题描述】:
我目前正在用 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()
【问题讨论】:
-
Rabbid76,循环代码我加了,你能再看一遍吗?
标签: python python-3.x vector pygame game-physics