我遇到的问题似乎在random_movement函数中。它会继续在170左右吐出相同的数字,这会使子画面在屏幕的左上角聚集。任何想法为何以及我可能会做些什么?
我想要精灵执行的操作是移动一小段距离然后改变方向并再移动一点。
def avoid_mobs(self):
for mob in self.game.mobs:
if mob != self:
dist = self.pos - mob.pos
if 0 < dist.length() < AVOID_RADIUS:
self.acc += dist.normalize()
def random_movement(self):
new_angle = uniform(-1, 1)
new_x = math.sin(new_angle * math.pi/2)
new_y = math.cos(new_angle * math.pi/2)
new_vector = pg.math.Vector2(new_x,new_y)
new_vector.normalize()
new_vector *= randrange(360)
print ('new_vec: ' + str(new_vector))
return new_vector
def chase(self, target_dist):
self.rot = target_dist.angle_to(vec(1, 0))
print ('rot: ' + str(self.rot))
print ('vel: ' + str(self.vel))
self.image = pg.transform.rotate(self.game.mob_img, self.rot)
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.acc = vec(1, 0).rotate(-self.rot)
self.avoid_mobs()
self.acc.scale_to_length(self.speed)
self.acc += self.vel * -1
self.vel += self.acc * self.game.dt
self.pos += self.vel * self.game.dt + 0.5 * self.acc * self.game.dt ** 2
self.hit_rect.centerx = self.pos.x
collide_with_walls(self, self.game.walls, 'x')
self.hit_rect.centery = self.pos.y
collide_with_walls(self, self.game.walls, 'y')
self.rect.center = self.hit_rect.center
def update(self):
#target_dist = self.target.pos - self.pos
#if target_dist.length_squared() < DETECT_RADIUS ** 2:
# # Play zombie moans once in a while
# if random() < 0.002:
# choice(self.game.zombie_moan_sounds).play()
# self.wander = target_dist
## self.chase(target_dist)
#else:
if random() < 0.05:
self.wander = self.random_movement() - self.pos
print ('target' + str(self.wander))
self.chase(self.wander)
if self.health <= 0:
snd = self.game.zombie_death_sound[0]
if snd.get_num_channels() > 2:
snd.stop()
snd.play()
self.kill()
self.game.map_img.blit(self.game.splat, self.pos - vec(32, 32))
def draw_health(self):
if self.health > 60:
col = GREEN
elif self.health > 30:
col = YELLOW
else:
col = RED
width = int(self.rect.width * self.health / 100)
self.health_bar = pg.Rect(0, 0, width, 7)
if self.health < 100:
pg.draw.rect(self.image, col, self.health_bar)