Create the game background part 1

Hello, and welcome back, starting from today I will definitely use my time effectively which means less Youtubed and Googled around so I can concentrate most of the time on writing article, creating the game and managing my own shoe selling’s business. This year is make money online and offline year for me, wasting time on Youtube or surfing around must be stopped so those time can be used to do more meaningful stuff. Alright, so much for that let us get into the business.

As the title has suggested, in this chapter we will create the game’s background for our new pygame project. As I have mentioned before we are creating a maze game project so you can expect something new as compared to the previous project. Before we start let us look at our strategy to create the background object first.

  1. We will create a container which will hold all those lists that contain the background images for each level.
  2. We will create the background object’s sprite class which will be used to render those background images on the game background.

This chapter consists of two parts, in this first part we will create those two classes above and you will slowly understand what I am doing when we get to the next part.

The BgObject class is the container which will contain all the background images’ lists. At the moment the class only has one list which contains various images.

class BgObject(object):

    def __init__(self):

        self.list_1 = [
            [3, 3, 3, 3, 3, 3, 3, 3, 3, 2],
            [4, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [4, 3, 3, 3, 3, 3, 3, 3, 3, 3],
            [0, 0, 4, 0, 0, 0, 3, 3, 3, 3],
            [3, 3, 4, 3, 3, 3, 3, 3, 3, 3],
            [0, 0, 0, 0, 1, 4, 1, 0, 0, 1],
            [3, 3, 3, 3, 3, 4, 3, 3, 3, 3],
            [3, 3, 3, 3, 3, 1, 1, 0, 0, 4],
            [3, 3, 3, 3, 3, 3, 3, 3, 3, 4],
            [0, 1, 0, 0, 1, 1, 0, 1, 1, 0]
        ]

    def get_object_list(self, level):

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

This class above is a container which holds all the background objects. Now let us create the background sprite class which will responsible for the object’s rendering job.

import pygame
from BgSprite import BgSprite
from pygame.locals import *
from BgObject import BgObject
from pygame import math as mt

class BackGroundSprite(object):

    def __init__(self, level, scene):

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

        # the road sprite
        self.road_rect = Rect(0, 0, 64, 64)
        self.sprite_road = BgSprite(self.bg_image_sprite, self.road_rect)
        self.road_surface = self.sprite_road.getImage()

        # the grass sprite
        self.grass_rect = Rect(64 * 1, 0, 64, 64)
        self.sprite_grass = BgSprite(self.bg_image_sprite, self.grass_rect)
        self.grass_surface = self.sprite_grass.getImage()

        # the door sprite
        self.door_rect = Rect(64 * 2, 0, 64, 64)
        self.sprite_door = BgSprite(self.bg_image_sprite, self.door_rect)
        self.door_surface = self.sprite_door.getImage()

        # the air sprite
        self.air_rect = Rect(64 * 3, 0, 64, 64)
        self.sprite_air = BgSprite(self.bg_image_sprite, self.air_rect)
        self.air_surface = self.sprite_air.getImage()

        # the ladder sprite
        self.ladder_rect = Rect(64 * 4, 0, 64, 64)
        self.sprite_ladder = BgSprite(self.bg_image_sprite, self.ladder_rect)
        self.ladder_surface = self.sprite_ladder.getImage()

        self.level = level # current game level

        self.bg_object = BgObject()

        self.get_level(self.level)

    def draw(self):

        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.bg_list[row][column] == 0):
                    self.scene.blit(self.road_surface, self.draw_pos)  # draw road
                elif (self.bg_list[row][column] == 1):
                    self.scene.blit(self.grass_surface, self.draw_pos)  # draw grass
                elif (self.bg_list[row][column] == 2):
                    self.scene.blit(self.door_surface, self.draw_pos)  # draw door
                elif (self.bg_list[row][column] == 3):
                    self.scene.blit(self.air_surface, self.draw_pos)  # draw air
                elif (self.bg_list[row][column] == 4):
                    self.scene.blit(self.ladder_surface, self.draw_pos)  # draw ladder

        #pygame.display.flip()

    def get_level(self, level):
        self.bg_list = self.bg_object.get_object_list(level)

The game scene is consists of 10 rows 10 columns. If you are still confused with what the above classes do then you will see what I am up to when we go further down the road. Alright, so much for that, we will continue to develop the background rendering engine on the next part of this chapter, stay tuned!