Create a background object class in Pygame

In this article we will create the next pygame object class, which is the background object class that will render the background graphic on the game scene, just like the player class in the previous article, background class will take in a scene reference object which will be used by this background class to blit the background graphic on the game scene. It has a very simple draw method which will be called in each game loop to redraw the background game graphic. Here is the entire class.

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

class Background(object):

    def __init__(self, scene):
        self.scene = scene
        self.image = 'Asset/bg.png'
        self.rect = Rect(0, 0, 660, 660)
        self.sprite = BgSprite(self.image, self.rect)
        self.surface = self.sprite.getImage()  # get the background sprite surface
        self.draw_pos = mt.Vector2(0, 0)

    def draw(self):
        self.scene.blit( self.surface, self.draw_pos) # draw a background sprite

That is it, as simple as it is! Now before we can create our Game Manager class we will need to modify the Missile Manager class we have created previously in the next chapter so it can be called directly by our new Game Manager class later on.

Leave a Reply

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