PYGAME - 使一个对象朝向自动生成的对象旋转
PYGAME - Making one object rotate towards auto generated objects
我创造了一个可以连续射击的炮塔。但是,我想让它转向不断产卵的僵尸。我设法让炮塔瞄准玩家,因为我已经初始化了它:
if tile_object.name == 'player':
self.player = Player(self, obj_center.x, obj_center.y)
那么目标是:'self.player'。虽然,对于僵尸我这样初始化:
if tile_object.name == 'zombie':
Mob(self, obj_center.x, obj_center.y, 'normal', 420)
之后我尝试给它game.mobs的目标(而不是game.player),它提示群怪没有属性'pos',我是非常确定。
我觉得我完全不知道为什么它不能针对一般的暴民。我尝试阅读有关组和精灵的内容,但并没有让任何事情变得更清楚。
这是我的炮塔的代码:
class Turret(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.last_shot = 0
self.maxforce = 0.1
self.damage = 0
self.detect_radi = 360
self._layer = MOB_LAYER
self.groups = game.all_sprites, game.turrets
self.image = game.turret_img.copy()
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.hit_rect = MOB_HIT_RECT.copy()
self.hit_rect.center = self.rect.center
self.weapon = 'pistol'
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.pos = vec(x, y)
self.rect.center = self.pos
self.rot = 0
############## HERE IS WHERE THE ISSUE IS ##############
self.target = game.mobs
def update(self):
target_dist = self.target.pos - self.pos
if target_dist.length_squared() < self.detect_radi**2:
self.rot = target_dist.angle_to(vec(1, 0))
self.image = pg.transform.rotate(self.game.turret_img, self.rot)
self.rect.center = self.pos
self.acc = vec(1, 0).rotate(-self.rot)
self.shoot(self.weapon)
非常感谢任何帮助,因为当我试图让炮塔瞄准暴民时我感到完全迷失了...
it prompts that the group mobs do not posess the attribute 'pos', which i am very sure it does."*
我非常确定生物对象具有 pos
属性,但包含生物的组没有 pos
属性。
找到距离最近的 diestane 的生物对象:
target_dist = self.target.pos - self.pos
target_dist = None
for mob in self.target:
dist = mob.pos - self.pos
if target_dist == None or dist.length_squared() < target_dist.length_squared()
target_dist = dist
注意:如果self.target
不包含任何敌人,您需要实施一个额外的案例。
我创造了一个可以连续射击的炮塔。但是,我想让它转向不断产卵的僵尸。我设法让炮塔瞄准玩家,因为我已经初始化了它:
if tile_object.name == 'player':
self.player = Player(self, obj_center.x, obj_center.y)
那么目标是:'self.player'。虽然,对于僵尸我这样初始化:
if tile_object.name == 'zombie':
Mob(self, obj_center.x, obj_center.y, 'normal', 420)
之后我尝试给它game.mobs的目标(而不是game.player),它提示群怪没有属性'pos',我是非常确定。
我觉得我完全不知道为什么它不能针对一般的暴民。我尝试阅读有关组和精灵的内容,但并没有让任何事情变得更清楚。
这是我的炮塔的代码:
class Turret(pg.sprite.Sprite):
def __init__(self, game, x, y):
self.last_shot = 0
self.maxforce = 0.1
self.damage = 0
self.detect_radi = 360
self._layer = MOB_LAYER
self.groups = game.all_sprites, game.turrets
self.image = game.turret_img.copy()
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.hit_rect = MOB_HIT_RECT.copy()
self.hit_rect.center = self.rect.center
self.weapon = 'pistol'
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.pos = vec(x, y)
self.rect.center = self.pos
self.rot = 0
############## HERE IS WHERE THE ISSUE IS ##############
self.target = game.mobs
def update(self):
target_dist = self.target.pos - self.pos
if target_dist.length_squared() < self.detect_radi**2:
self.rot = target_dist.angle_to(vec(1, 0))
self.image = pg.transform.rotate(self.game.turret_img, self.rot)
self.rect.center = self.pos
self.acc = vec(1, 0).rotate(-self.rot)
self.shoot(self.weapon)
非常感谢任何帮助,因为当我试图让炮塔瞄准暴民时我感到完全迷失了...
it prompts that the group mobs do not posess the attribute 'pos', which i am very sure it does."*
我非常确定生物对象具有 pos
属性,但包含生物的组没有 pos
属性。
找到距离最近的 diestane 的生物对象:
target_dist = self.target.pos - self.pos
target_dist = None
for mob in self.target:
dist = mob.pos - self.pos
if target_dist == None or dist.length_squared() < target_dist.length_squared()
target_dist = dist
注意:如果self.target
不包含任何敌人,您需要实施一个额外的案例。