Further modifying the enemy class

In the previous article, we have successfully created the animation effect for the enemy object, in this article, we will add in the switch direction method which will switch the direction of the enemy image from facing left to facing right after a certain time period and then continue to switch back and forth. In order to achieve the previous mentioned effect we only need to further edit the enemy class. We will move all the enemy object surface creation processes to the enemy class and only leave a small amount of image rendering codes on the enemy sprite class.

from pygame.locals import *
from GameSprite import GameSprite

class Enemy(object):

    def __init__(self):

        self.count = 0
        self.forward = False
        self.timer = 0
        self.right = True
        self.switch_timer = 900
        self.switch_timer_left = 900
        self.sprite_frame_list_right = []
        self.sprite_frame_list_left = []

        self.enemy_image_sprite_right = 'Asset/right.png'
        self.enemy_image_sprite_left = 'Asset/left.png'

        self.sprite_frame_list = self.create_surface(self.enemy_image_sprite_right)

    def create_surface(self, enemy_image_sprite):

        if(self.right == True and len(self.sprite_frame_list_right) > 0):
            return self.sprite_frame_list_right
        elif (self.right == True and len(self.sprite_frame_list_right) <= 0):
            self.sprite_frame_list_right = self.create_sub_surface(enemy_image_sprite, self.sprite_frame_list_right)
            return self.sprite_frame_list_right
        elif(self.right == False and len(self.sprite_frame_list_left) > 0):
            return self.sprite_frame_list_left
        elif (self.right == False and len(self.sprite_frame_list_left) <= 0):
            self.sprite_frame_list_left = self.create_sub_surface(enemy_image_sprite, self.sprite_frame_list_left)
            return self.sprite_frame_list_left

    def create_sub_surface(self, enemy_image_sprite, sprite_frame_list):
        # the first frame
        self.frame_1_rect = Rect(0, 0, 64, 64)
        self.sprite_frame_1 = GameSprite(enemy_image_sprite, self.frame_1_rect)
        self.sprite_frame_1_surface = self.sprite_frame_1.getImage()
        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(enemy_image_sprite, self.frame_2_rect)
        self.sprite_frame_2_surface = self.sprite_frame_2.getImage()
        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(enemy_image_sprite, self.frame_3_rect)
        self.sprite_frame_3_surface = self.sprite_frame_3.getImage()
        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(enemy_image_sprite, self.frame_4_rect)
        self.sprite_frame_4_surface = self.sprite_frame_4.getImage()
        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(enemy_image_sprite, self.frame_5_rect)
        self.sprite_frame_5_surface = self.sprite_frame_5.getImage()
        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(enemy_image_sprite, self.frame_6_rect)
        self.sprite_frame_6_surface = self.sprite_frame_6.getImage()
        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(enemy_image_sprite, self.frame_7_rect)
        self.sprite_frame_7_surface = self.sprite_frame_7.getImage()
        sprite_frame_list.append(self.sprite_frame_7_surface)

        return sprite_frame_list

    def update(self):

        self.switch_position()
        self.change_graphic()

    def change_graphic(self):
        if (self.right == True):
            if (self.timer > 30):
                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 < 1 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.timer = 0
            else:
                self.timer += 1
        else:
            if (self.timer > 30):
                if (self.count > 5 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.timer = 0
            else:
                self.timer += 1

    def switch_position(self):

        if(self.switch_timer <= 0):
            self.right = False
            self.timer = 0
            self.count = 0
            self.switch_timer = 900
            self.sprite_frame_list=self.create_surface(self.enemy_image_sprite_left)
        elif (self.right == True and self.switch_timer > 0):
            self.switch_timer -= 1
        elif(self.right == False and self.switch_timer_left > 0):
            self.switch_timer_left -= 1
        elif(self.switch_timer_left <= 0):
            self.right = True
            self.timer = 0
            self.count = 0
            self.switch_timer_left = 900
            self.sprite_frame_list=self.create_surface(self.enemy_image_sprite_right)

    def get_enemy_surface(self):

        return self.sprite_frame_list[self.get_count()]

    def get_count(self):

        if(self.count < 1 and self.forward == False):
            self.count += 1
            self.forward = True
        elif(self.count > 6 and self.forward == True):
            self.count -= 1
            self.forward = False

        return self.count

As you can see we have included another sprite sheet for the right facing images. Now, all we need is to modify the enemy sprite class a little bit.

from EnemyObject import EnemyObject
from pygame import math as mt
from Enemy import Enemy

class EnemySprite(object):

    def __init__(self, level, scene):

        self.row = 10
        self.column = 10
        self.scene = scene # game scene instance
        self.space = 64 # image width and height

        self.create_enemy(level)

        self.level = level # current game level

        self.enemy_object = EnemyObject()

    def create_enemy(self, level):
        self.total_enemy_list = []
        i = 0
        if(level == 1):
            while(i < 5):
                self.total_enemy_list.append(Enemy())
                i += 1

    def update(self):

        for enemy in self.total_enemy_list:
            enemy.update()

    def draw(self):
        self.counter = 0
        for row in range(self.row):

            for column in range(self.column):

                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.enemy = self.total_enemy_list[self.counter]

                    self.scene.blit(self.enemy.get_enemy_surface(), self.draw_pos)  # draw enemy frame

                    self.counter += 1


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

Basically, we will get the below outcome after we have changed the above classes.

Leave a Reply

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