Create a win scene and the level manager class for pygame project

In this article we will create a win scene which will be shown after the player has won the final level of the game, the win scene will then ask the player whether he wants to start that game from the beginning or not? We will also create a framework for the level manager class which we will further add in more features in the next chapter.

First of all, we will modify the start scene class again to include a win scene graphic.

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

class StartScene(object):

    def __init__(self, scene):
        self.scene = scene
        self.button_image = 'Asset/button_play.png'
        self.bg_image = 'Asset/start.png'
        self.over_image = 'Asset/ove.png'
        self.next_image = 'Asset/next.png'
        self.win_image = 'Asset/winn.png'
        self.bg_rect = Rect(0, 0, 660, 660)
        self.button_rect = Rect(0, 0,  306, 112)
        self.sprite = BgSprite(self.bg_image, self.bg_rect)
        self.sprite_win = BgSprite(self.win_image, self.bg_rect)
        self.sprite_over = BgSprite(self.over_image, self.bg_rect)
        self.sprite_next = BgSprite(self.next_image, self.bg_rect)
        self.sprite_button = GameSprite(self.button_image, self.button_rect)
        self.win_surface = self.sprite_win.getImage()  # get the win sprite surface
        self.next_surface = self.sprite_next.getImage()  # get the next level sprite surface
        self.over_surface = self.sprite_over.getImage()  # get the game over sprite surface
        self.surface = self.sprite.getImage()  # get the start scene sprite surface
        self.button_surface = self.sprite_button.getImage() # get the button sprite surface
        self.draw_pos = mt.Vector2(0, 0)
        self.draw_button_pos = mt.Vector2(177, 274)

    def draw(self, state):

        if(state == 0):
            self.scene.blit(self.surface, self.draw_pos) # draw a start scene sprite
        elif(state == 2):
            self.scene.blit(self.over_surface, self.draw_pos)  # draw a game over sprite
        elif (state == 3):
            self.scene.blit(self.next_surface, self.draw_pos)  # draw a next level sprite
        elif(state == 4):
            self.scene.blit(self.win_surface, self.draw_pos)  # draw a win sprite

        self.scene.blit(self.button_surface, self.draw_button_pos)  # draw a button sprite

Next we will modify the overlap class where we will call the level manager to update the game status instead of directly call the game manager to do the same thing whenever the player has reached a new level.

from pygame.locals import *

class Overlap(object):

    def __init__(self):
        pass # nothing here

    # is player and enemy, player missile, enemy missile overlap
    def isOverlap(self, player, em, ex, score, gm):

        self.player_rect = Rect(player.pos.x, player.pos.y, player.width, player.height)

        for i in range(len(em.enemy_list)): # is player collides with enemy

            self.em_rect = Rect(em.enemy_list[i].x, em.enemy_list[i].y, em.width, em.height)
            if (self.player_rect.colliderect(self.em_rect)):
                em.enemy_list[i].on = False
                if(em.enemy_list[i].hit == False):
                    ex.create_explosion(player.pos.x + 2, player.pos.y + 2)
                    em.enemy_list[i].hit = True
                    gm.state = gm.OVER

        for i in range(len(em.enemy_missile_manager.missile_list)): # is enemy missile hits player

            self.em_rect = Rect(em.enemy_missile_manager.missile_list[i].x, em.enemy_missile_manager.missile_list[i].y, em.enemy_missile_manager.width, em.enemy_missile_manager.height)
            if (self.player_rect.colliderect(self.em_rect)):
                em.enemy_missile_manager.missile_list[i].on = False
                ex.create_explosion(player.pos.x + 2, player.pos.y + 2)
                if(score.score < 0):
                        gm.state = gm.OVER

        for i in range(len(em.enemy_list)): # is player missile hits enemy

            self.em_rect = Rect(em.enemy_list[i].x, em.enemy_list[i].y, em.width, em.height)

            for j in range(len(player.getMissileManager().missile_list)):

                self.mm_rect = Rect(player.getMissileManager().missile_list[j].x, player.getMissileManager().missile_list[j].y, player.getMissileManager().width, player.getMissileManager().height)

                if (self.em_rect.colliderect(self.mm_rect)):
                    em.enemy_list[i].on = False
                    player.getMissileManager().missile_list[j].on = False
                    if (em.enemy_list[i].hit == False):
                        ex.create_explosion(em.enemy_list[i].x, em.enemy_list[i].y + 2)
                        em.enemy_list[i].hit = True
                        if(score.score >= 30):

Next We will need to create an instance of the level manager class within the game manager class and check the game level in game manager’s setup method. Besides that we will also create a win state game variable to represent the winning state of the game.

from Player import Player
from Background import Background
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

class GameManager(object):

    def __init__(self, scene):

        self.scene = scene
        self.start_scene = StartScene(scene)
        self.overlap_manager = Overlap()
        self.level_manager = LevelManager(self)


        #game state
        self.LOAD = 0
        self.GAME = 1
        self.OVER = 2
        self.NEXT = 3
        self.WIN = 4

        self.state = self.LOAD

    def setup(self, game_level):

        self.game_level = game_level
        self.score_manager = Score(self.scene)
        self.background = Background(self.scene)

        if(self.game_level == 0):
            self.player = Player(self.scene)
            self.enemy_manager = EnemyManager(self.scene, self.player)
            self.explosion_manager = ExplosionManager(self.scene)

    def loop(self):

        if(self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN):


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


    def isAreaClick(self, pos):
        if (self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN):
            self.rect = Rect(177, 274, 306, 112) # the position of the play button on the scene
            x, y = pos
            if(self.rect.collidepoint(x, y)):
                self.state = self.GAME

    def load_music(self):

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

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

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

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

    def update(self):

    # 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):

As you can see the setup method will check which game level is that and do the game objects setup according to that level. There is only one level at the moment so we will need to further modify the program in the next chapter, for example we will need to modify the enemy manager class because we will need to create different enemies at different level of the game!

Now here is the level manager class.

class LevelManager(object):

    def __init__(self, gm):

        self.game_manager = gm
        self.level = 0

    def increase_level(self):

        self.level += 1
        if(self.level > 1):
            self.game_manager.state = self.game_manager.WIN
            self.level = 0
            self.game_manager.state = self.game_manager.NEXT

    def get_level(self):

        return self.level

The class is very simple at the moment, the increase level method either increase the game level or reset the level to 0 if the player has won the game! We can now concentrate on the level manager class in the next chapter and come back again to create a few more scenes like the about scene which contains game instructions as well as the credit scene which gives credit to the game creator. If you like this post don’t forget to click on the red bell button below this post to subscribe to the post’s notification or you also can subscribe to the individual rss feed from the sidebar of this website. There are more articles related to the game creation topic coming out almost everyday so make sure to subscribe to this website to get the latest daily game development article!