2
votes

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...

1
Your code doesn't look that bad? Your update method looks promising. What exactly is the problem? - Rabbid76
Thanks, to be more precise, the game starts with no 'zombies' spawned, similar to a survival game where there are waves of zombies coming. However, i would like for the turret to simply target zombies when they come. Although, when i try to reference the turret to aim for zombies/mobs, it prompts that the group of zombies do not possess any position --> AttributeError: 'Group' object has no attribute 'pos'. All mobs have 'self.pos = vec(x,y)'. - hanmic-6
" it prompts that the group mobs do not posess the attribute 'pos', which i am very sure it does." - No it does not. I'm very sure that a Mob object has the attribute pos, but the Group which contains the mobs has no attribute pos - Rabbid76
OK, do you know how to better formulate the self.target choice in that case? Im mistaken then because i thought the group.mobs is basically alll mob/zombie instances. - hanmic-6

1 Answers

2
votes

it prompts that the group mobs do not posess the attribute 'pos', which i am very sure it does."*

I'm very sure that a mob object has the pos attribute, but the group that contains the mobs does not have a pos attribute.

Find the mob object with the closest 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  

Note: If self.target doesn't contain any enemies, you need to implement an additional case.