Create Enemy Manager Class and Enemy Class in a Pygame Project

Hello again, today chapter will be very short but rich in content. As I have mentioned earlier, we have finished the stage one of the game creation process yesterday and today we have entered the stage two of our game creation process, which is to create the enemy, enemy missile, level and the exploration classes, we certainly are getting very close now to finish up this latest pygame project.

In this short article you will read all the code for the Enemy Manager and Enemy Class plus I do have a special surprise for you at the end of this article so make sure you read it till the end.

This is the part one of the three part enemy’s class related article series and in this part we will create enemy spaceships that will come down from a certain distance above the game scene, the enemy object which get passed the bottom of the game scene will get removed by the Enemy Manager which will then create a new enemy object after it has removed the old one. We will generate the enemy object from a random x position within this game scene with it’s y position start off from a fix distance above the top of the game scene. Besides that, we have also included the time control variable in the Enemy Manager class so this class will generate the enemy at an acceptable speed. The contact detection between enemy and player object, recycle of the enemy object, enemy missile object and many more methods and classes will be created in the upcoming articles.

Below is the enemy class with an update method which will move the enemy to a new y position in every game loop.

from pygame import math as mt

class Enemy(object):

    def __init__(self, enemy_surface, x, y):
        self.on = True
        self.enemy_surface = enemy_surface
        self.x = x
        self.y = y
        self.enemy_pos = mt.Vector2(self.x, self.y)

    def update(self):
        self.y += 0.1
        self.enemy_pos = mt.Vector2(self.x, self.y)

Here is the enemy manager class which is used to control the enemy with the features I have mentioned above.

from Enemy import Enemy
from GameSprite import GameSprite
from pygame.locals import *
import random

class EnemyManager(object):

    def __init__(self, scene, player):

        self.scene = scene
        self.player = player
        self.enemy_count = 10
        self.enemy_list = []
        self.image = 'Asset/enemy0.png'
        self.width = 30
        self.height = 30
        self.rect = Rect(0, 0, self.width, self.height)
        self.more_enemy = 0
        self.y = -50
        self.boundary_width = 660
        self.boundary_height = 660

        # initialize game sprite object
        self.sprite = GameSprite(self.image, self.rect)

    def create_enemy(self, x, y):
        if(self.enemy_count > 0):
            self.enemy_surface = self.sprite.getImage()
            self.enemy_list.append(Enemy(self.enemy_surface, x, y))
            self.enemy_count -= 1

    def update(self):
        if (self.more_enemy > 600):
            self.more_enemy = 0
            x = random.randint(30, self.boundary_width - 50)
            self.create_enemy(x , self.y)  # create more enemy
            self.more_enemy += 1 # increase time


    def enemy_update(self):

        for item in list(self.enemy_list):
            if(item.on == False):
                self.enemy_count += 1

    def check_boundary(self):
        for i in range(len(self.enemy_list)):
            if (self.enemy_list[i].y > self.boundary_height):
                self.enemy_list[i].on = False

    def draw(self):

        # blit the enemy on  the scene
        for i in range(len(self.enemy_list)):
            self.scene.blit(self.enemy_list[i].enemy_surface, self.enemy_list[i].enemy_pos)

After we have created these two classes it is time to modify the Game Manager class which will call the Enemy Manager Class to create more enemy as well as updates the position of those enemies on the game scene plus draws the enemy object on the scene.

from Player import Player
from Background import Background
from MissileManager import MissileManager
from EnemyManager import EnemyManager
import pygame

class GameManager(object):

    def __init__(self, scene):

        self.scene = scene
        self.player = Player(self.scene)
        self.background = Background(self.scene)
        self.missile_manager = MissileManager(self.scene, self.player)
        self.enemy_manager = EnemyManager(self.scene, self.player)

    def load_music(self):

    def play_music(self): #play the music infinite time

    def set_player_x(self, _x):

    def set_player_y(self, _y):

    def set_missile_strike(self, strike):
       self. missile_manager.setStrike(True)

    def update(self):

    def draw(self):

Now run the main pygame program again and you will get the below outcome. As you can see the enemy ship will just get through the player ship with no contact with that player ship as well as no missile shooting which we will deal with in the next articles.


Below is the game music which I am going to use in this new pygame project, besides a game developer I am also a soundtrack creator, you can subscribe and listen to those new musics on my soundcloud channel.

Leave a Reply

Your email address will not be published. Required fields are marked *