Quarky-Up
Quarky-Up

Reputation: 45

Python - Sprite chasing another sprite

I'm developing a "Space Invaders like" game using pygame. At the moment my code is functional and I've finished the game basics. So now I'd like to make the enemies move toward my player, here is my code:

class createShip(imports.pygame.sprite.Sprite):
    def __init__(self):
        imports.pygame.sprite.Sprite.__init__(self)
        self.image = loader.joueur_image
        self.rect = loader.joueur_rect
        self.rect.x = constantes.screen_width/2 - self.rect.width/2
        self.rect.y = constantes.screen_height - self.rect.height - 20

    def update(self):
        if (imports.pygame.key.get_pressed()[imports.pygame.K_LEFT] == True):
            self.rect.x -= constantes.speed_ship
        elif (imports.pygame.key.get_pressed()[imports.pygame.K_RIGHT] == True):
            self.rect.x += constantes.speed_ship
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.x > constantes.screen_width - self.rect.width:
            self.rect.x = constantes.screen_width - self.rect.width

class createEnemy(imports.pygame.sprite.Sprite):
    def __init__(self):
        imports.pygame.sprite.Sprite.__init__(self)
        self.image = imports.pygame.Surface([50, 50])
        self.image.fill((0, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.y = 0
        self.rect.x = imports.random.randint(constantes.screen_width/2 - 350, constantes.screen_width/2 + 350)

    def update(self):
        self.rect.y += imports.random.randint(constantes.speed_enemy - 8, constantes.speed_enemy + 2)
        if self.rect.y > constantes.screen_height - 35:
            self.rect.y = 0
            self.rect.x = imports.random.randint(constantes.screen_width/2 - 350, constantes.screen_width/2 + 350)

Here is my problem. I don't want to code the moving part inside my game loop, it is simpler to call the sprite update method. So I was hoping to call my ship class inside my enemy class to get ship.rect.x and ship.rect.y, but obviously it didn't work because my ship object isn't created.

I can't find a way to get my ship coordinates inside my enemy class. I thought of doing another inheritance, but there must be an easier way to achieve that.

Upvotes: 2

Views: 408

Answers (1)

Lornioiz
Lornioiz

Reputation: 415

(I answering because I don't have the reputation to comment, otherwise i would have commented) I'm going to have the same problem very soon for a program I'm writing. My guess is to istantiate the player and then pass the variable that refer to the istance to the CreateEnemey class as a parameter, something along these lines:

class createShip(imports.pygame.sprite.Sprite):
    def __init__(self):
        imports.pygame.sprite.Sprite.__init__(self)
        pass

class createEnemy(imports.pygame.sprite.Sprite):
    def __init__(self, player):
        imports.pygame.sprite.Sprite.__init__(self)
        self.x_player = player.rect.x
        self.y_player = player.rect.y

Then, before the loop starts:

player = createShip()

I have actually did this before and it works, just not by subclassing pygame.sprite.Sprite (I had created my own Player class).

Upvotes: 1

Related Questions