Create the animation character on the maze

Today we are going to create the animated enemy for our new pygame project. We are going to edit the enemy manager class and the game manager class then we are going to create an enemy sprite class and an enemy object class which will serve as the original sprite container just like the previous background object class does.

Alright, let us get the thing going. First of all we are going to create the enemy object class which will hold the list which contains the initial enemy object’s position on the maze board for each level.

class EnemyObject(object):

    def __init__(self):

        self.list_1 = [

            [0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 1, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        ]

    def get_object_list(self, level):

        if(level == 1):
            return self.list_1

The above class is just like the background object class which you have seen previously.

Next we will create the enemy sprite class which will perform the same job as the background sprite class which is to render the enemy object on the game scene.

from EnemyObject import EnemyObject
from pygame.locals import *
from pygame import math as mt
from GameSprite import GameSprite

class EnemySprite(object):

    def __init__(self, level, scene):

        self.row = 10
        self.column = 10
        self.enemy_image_sprite = 'Asset/right.png'
        self.scene = scene # game scene instance
        self.space = 64 # image width and height

        self.sprite_frame_list = []
        self.count = -1 # frame controller
        self.forward = False # switch to move counter forward or backward

        # the first frame
        self.frame_1_rect = Rect(0, 0, 64, 64)
        self.sprite_frame_1 = GameSprite(self.enemy_image_sprite, self.frame_1_rect)
        self.sprite_frame_1_surface = self.sprite_frame_1.getImage()
        self.sprite_frame_list.append(self.sprite_frame_1_surface)

        # the second frame
        self.frame_2_rect = Rect(64, 0, 64, 64)
        self.sprite_frame_2 = GameSprite(self.enemy_image_sprite, self.frame_2_rect)
        self.sprite_frame_2_surface = self.sprite_frame_2.getImage()
        self.sprite_frame_list.append(self.sprite_frame_2_surface)

        # the third frame
        self.frame_3_rect = Rect(64 * 2, 0, 64, 64)
        self.sprite_frame_3 = GameSprite(self.enemy_image_sprite, self.frame_3_rect)
        self.sprite_frame_3_surface = self.sprite_frame_3.getImage()
        self.sprite_frame_list.append(self.sprite_frame_3_surface)

        # the fourth frame
        self.frame_4_rect = Rect(64 * 3, 0, 64, 64)
        self.sprite_frame_4 = GameSprite(self.enemy_image_sprite, self.frame_4_rect)
        self.sprite_frame_4_surface = self.sprite_frame_4.getImage()
        self.sprite_frame_list.append(self.sprite_frame_4_surface)

        # the fifth frame
        self.frame_5_rect = Rect(64 * 4, 0, 64, 64)
        self.sprite_frame_5 = GameSprite(self.enemy_image_sprite, self.frame_5_rect)
        self.sprite_frame_5_surface = self.sprite_frame_5.getImage()
        self.sprite_frame_list.append(self.sprite_frame_5_surface)

        # the sixth frame
        self.frame_6_rect = Rect(64 * 5, 0, 64, 64)
        self.sprite_frame_6 = GameSprite(self.enemy_image_sprite, self.frame_6_rect)
        self.sprite_frame_6_surface = self.sprite_frame_6.getImage()
        self.sprite_frame_list.append(self.sprite_frame_6_surface)

        # the seventh frame
        self.frame_7_rect = Rect(64 * 6, 0, 64, 64)
        self.sprite_frame_7 = GameSprite(self.enemy_image_sprite, self.frame_7_rect)
        self.sprite_frame_7_surface = self.sprite_frame_7.getImage()
        self.sprite_frame_list.append(self.sprite_frame_7_surface)

        self.level = level # current game level

        self.enemy_object = EnemyObject()

    def draw(self):

        for row in range(self.row):

            for column in range(self.column):

                if (self.count > 6 and self.forward == True):  # use this logic to control the counter and the image
                    self.count -= 1
                    self.forward = False
                elif (self.count < 0 and self.forward == False):
                    self.count += 1
                    self.forward = True
                elif (self.forward == False):
                    self.count -= 1
                elif (self.forward == True):
                    self.count += 1

                self.draw_pos = mt.Vector2(self.space * column, self.space * row) # the position of the image on game scene

                if(self.enemy_list[row][column] == 1):
                    self.scene.blit(self.sprite_frame_list[self.count], self.draw_pos)  # draw enemy frame

    def get_level(self, level):
        self.enemy_list = self.enemy_object.get_object_list(level)

The above class is more complicated than the background sprite class but the idea is the same, which is to render the game’s object on the scene. We will come back to further modify this class in the next chapter but for now this class will just initiate the enemy object on the game scene and then continuously animates the enemy object on each update.

Our next target is to modify the enemy manager class accordingly.

from EnemySprite import EnemySprite

class EnemyManager(object):

    def __init__(self, scene, game_level):

        self.scene = scene
        self.enemy_list = []
        self.level = game_level
        self.enemy = EnemySprite(self.level, self.scene)
        self.create_enemy()

    def create_enemy(self):

        self.enemy.get_level(self.level)

    def update(self):

        pass

    # check the boundary of the enemy with the game scene area
    def check_boundary(self):

        pass

    def draw(self):

        self.enemy.draw()

Then on the game manager class, we will call the enemy manager class to prepare and to draw the enemy object during each game loop.

from Player import Player
from EnemyManager import EnemyManager
from Overlap import Overlap
from ExplosionManager import ExplosionManager
from Score import Score
from StartScene import StartScene
from pygame.locals import *
from LevelManager import LevelManager
import pygame
import webbrowser
from Scene import Scene
from BackGroundSprite import BackGroundSprite

class GameManager(object):

    def __init__(self, scene):

        self.scene = scene

        self.load_music()
        self.play_music()
        self.overlap_manager = Overlap()
        self.level_manager = LevelManager(self)
        self.level_manager.set_level()
        self.setup(self.level_manager.get_level())
        self.start_scene = StartScene(scene, self.level_manager)

        self.pause = False # flag to pause the game

        self.game_scene = Scene(self.scene)

        #game state
        self.LOAD = 0
        self.GAME = 1
        self.OVER = 2
        self.NEXT = 3
        self.WIN = 4
        self.ABOUT = 5
        self.MANUAL = 6
        self.PAUSE = 7
        self.SCORE = 8
        self.SCENE = 9

        self.state = self.LOAD

    def setup(self, game_level):

        self.game_level = game_level
        self.score_manager = Score(self.scene, self.level_manager)
        self.background = BackGroundSprite(self.game_level, self.scene)
        self.player = Player(self.scene)
        self.enemy_manager = EnemyManager(self.scene, self.game_level)
        self.explosion_manager = ExplosionManager(self.scene)

    def loop(self):

        if(self.state == self.LOAD):
            self.start_scene.draw(self.state)
        elif(self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN or self.state == self.ABOUT or self.state == self.MANUAL or self.state == self.SCORE):
            self.start_scene.draw(self.state)
        elif(self.state == self.SCENE):
            self.game_scene.draw()
        elif(self.state == self.GAME):

            self.update()
            self.draw()

        elif(self.state == self.PAUSE):

            self.start_scene.draw(self.state)

    def isAreaClick(self, x, y):
        if (self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN or self.state == self.ABOUT or self.state == self.SCORE or self.state == self.SCENE or self.state == self.MANUAL or self.state == self.PAUSE):
            self.rect = Rect(177, 274, 306, 112) # the position of the play button on the scene
            self.rect_play = Rect(229, 200, 200, 53)  # the position of the play button on the home scene
            self.rect_about = Rect(229, 263, 200, 53)  # the position of the about button on the home scene
            #self.rect_exit = Rect(229, 456, 200, 53)  # the position of the exit button on the home scene
            self.rect_pause_home = Rect(229, 263, 200, 53)  # the position of the home button on pause scene
            self.rect_score = Rect(229, 328, 200, 53)  # the position of the score button on the home scene
            self.rect_manual = Rect(229, 393, 200, 53)  # the position of the manual button on the home scene
            self.rect_scene = Rect(229, 519, 200, 53)  # the position of the manual button on the home scene
            self.rect_back = Rect(10, 620, 40, 30)  # the position of the back button on the home scene
            self.rect_sound = Rect(10, 600, 30, 30) # the position of the sound button on the home scene
            self.rect_scene_next = Rect(610, 330, 30, 30)  # the position of the next scene button on scene
            self.rect_scene_previous = Rect(50, 330, 30, 30)  # the position of the previous scene button on scene

            if(self.rect.collidepoint(x, y) and (self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN)):
                self.state = self.GAME
            elif(self.rect_play.collidepoint(x,y) and self.state == self.LOAD):
                self.state = self.GAME
            elif (self.rect_play.collidepoint(x, y) and self.state == self.PAUSE):
                self.state = self.GAME
            elif (self.rect_about.collidepoint(x, y) and self.state == self.LOAD):
                self.state = self.ABOUT
            elif (self.rect_score.collidepoint(x, y) and self.state == self.LOAD):
                self.state = self.SCORE
            elif (self.rect_scene.collidepoint(x, y) and self.state == self.LOAD):
                self.state = self.SCENE
            elif (self.rect_back.collidepoint(x, y) and self.state == self.SCENE):
                self.state = self.LOAD
            elif (self.rect_scene_next.collidepoint(x, y) and self.state == self.SCENE):
                self.game_scene.set_next_image()
            elif (self.rect_scene_previous.collidepoint(x, y) and self.state == self.SCENE):
                self.game_scene.set_previous_image()
            elif (self.rect_pause_home.collidepoint(x, y) and self.state == self.PAUSE):

                self.state = self.LOAD
                self.setup(self.level_manager.get_level())
                self.save_level()

            elif (self.rect_manual.collidepoint(x, y) and self.state == self.LOAD):
                webbrowser.open_new('http://gamingdirectional.com/blog/2018/12/25/air-strike//')
            elif (self.rect_back.collidepoint(x, y) and (self.state == self.ABOUT or self.state == self.MANUAL or self.state == self.SCORE)):
                self.state = self.LOAD
            elif (self.rect_sound.collidepoint(x, y) and self.state == self.LOAD):

                if(self.start_scene.soundon == True):
                    self.start_scene.soundon = False
                    pygame.mixer_music.pause()
                else:
                    self.start_scene.soundon = True
                    pygame.mixer_music.unpause()

    def save_level(self):
       self.level_manager.save_level()

    def set_pause(self, pause):

        self.pause = pause

        if(self.pause == True):

            self.state = self.PAUSE

    def load_music(self):
        pygame.mixer_music.load('Music/winternight.ogg')

    def play_music(self):
        pygame.mixer_music.play(-1) #play the music infinite time

    def set_player_x(self, _x):
        if (self.state == self.GAME):
            self.player.setX(_x)

    def save_scene(self):
        self.game_scene.take_screen()

    def set_player_y(self, _y):
        if (self.state == self.GAME):
            self.player.setY(_y)

    def set_missile_strike(self, strike):
        if (self.state == self.GAME):
            self.player.setStrike(strike)

    def update(self):
        #self.player.update()
        #self.enemy_manager.update()
        #self.isOverlap()
        #self.explosion_manager.explosion_update()
        pass

    # check for player, enemy, missiles overlap
    def isOverlap(self):
        self.overlap_manager.isOverlap(self.player, self.enemy_manager, self.explosion_manager, self.score_manager, self)

    def draw(self):
        if(self.state == self.GAME):
            self.background.draw()
            #self.player.draw()
            self.enemy_manager.draw()
            #self.explosion_manager.draw()
            #self.score_manager.draw()
            pygame.display.flip()

That is it, as we slowly develop our new pygame project we will further edit the above game manager class.

http://islandstropicalman.tumblr.com/post/182491960069/pygame-project

If you like this tutorial and you have excessive bitcoins or other cryptocurrencies then kindly do consider to support this site by donating any amount of them through the below donation box. I have created many sites like this one and I do need cryptocurrencies to pay for the web hosting package so those sites include this one will be able to continue producing free source code for those who need it. Every cent you pay will be used to fund this free source code’s website and the other free source code sites that I have created so please help me out if it is possible. A little amount such as a few dollars will do, thank you very much indeed.

  • Bitcoin
  • Ethereum
  • Bitcoin cash
  • Litecoin
  • Stellar
Scan to Donate Bitcoin to 38han7NG6KHtxZsS4ppTwAtSw7UfLSY5Vd

Donate Bitcoin to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin

Scan to Donate Ethereum to 0x9Dfd0786c5fE998deebf6C86326bD036Ee2518c1

Donate Ethereum to this address

Scan the QR code or copy the address below into your wallet to send some Ethereum

Scan to Donate Bitcoin cash to qqegrlxc93q5ah0s2s6w9zxwf685vr26nu0kxlkvpc

Donate Bitcoin cash to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin cash

Scan to Donate Litecoin to MJJ2T1XyD4NtSjTA3NpcqTuaTaCKfT3fdA

Donate Litecoin to this address

Scan the QR code or copy the address below into your wallet to send some Litecoin

Scan to Donate Stellar to GAJRUOEMPNSHHR2ZB4KOOQ3MIR2BN54BBXGHTYA6QWMGST7AGI46WIHU

Donate Stellar to this address

Scan the QR code or copy the address below into your wallet to send some Stellar

Leave a Reply

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