I've created a turret that continously shoots. However, i want to make it turn towards zombies that continously spawn. I manage to make the turrets aim towards the player since i have initialized it:
if tile_object.name == 'player':
self.player = Player(self, obj_center.x, obj_center.y)
then the target is: 'self.player'. Although, for the zombies i initialize like this:
if tile_object.name == 'zombie':
Mob(self, obj_center.x, obj_center.y, 'normal', 420)
Therafter i try to give it the target of game.mobs (instead of game.player), and it prompts that the group mobs do not posess the attribute 'pos', which i am very sure it does.
I feel like i am completely lost to why it cannot aim towards mobs in general. I tried reading up on groups and sprites, but it didn't make anything clearer.
This is the code for my Turret:
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)
Any help is greatly appreciated, because i feel completely lost when trying to make the Turret target the mobs...
update
method looks promising. What exactly is the problem? - Rabbid76Mob
object has the attributepos
, but the Group which contains the mobs has no attributepos
- Rabbid76