Create a score manager class for pygame project

In this article we will create a score manager class to render the player score on the scene for our pygame project. At the moment we will only increase the score of the player each time the player’s missile hits the enemy ship and deduct the player score each time the player gets hit by the enemy missile, in the future we will introduce more features into the score manager class but for now lets just create a simple score manager class so we can edit it later on. Here is the score manager class.

import pygame.font as txt
from pygame.locals import *

class Score(object):

    def __init__(self, scene):

        self.score = 0
        self.scene = scene
        self.rect = Rect(20,20, 100, 30)
        self.font = txt.Font(None, 30)
        self.score_text = "Score : " + str(self.score)

    def set_score(self, score):

        self.score += score
        self.score_text = "Score : " + str(self.score)

    def draw(self):

        self.text = self.font.render(self.score_text, 1, (255, 255, 255))
        self.scene.blit(self.text, self.rect)

The set score method will be called in the overlap object’s iseOverlap method each time the player gets hit by the enemy missile or the enemy ship gets hit by the player missile. We will pass the score manager object into the iseOverlap method of the Overlap object in each game loop from the Game Manager class.

Lets edit the Game Manager class by creating a new score manager object and then pass the score manager object into the iseOverlap method of the Overlap object in each game loop. We will then call the score manager to draw the new score on the scene in the draw method of the Game Manager.

from Player import Player
from Background import Background
from EnemyManager import EnemyManager
from Overlap import Overlap
from ExplosionManager import ExplosionManager
from Score import Score
import pygame

class GameManager(object):

    def __init__(self, scene):

        self.scene = scene
        self.player = Player(self.scene)
        self.background = Background(self.scene)
        self.enemy_manager = EnemyManager(self.scene, self.player)
        self.overlap_manager = Overlap()
        self.explosion_manager = ExplosionManager(self.scene)
        self.score_manager = Score(self.scene)

    def load_music(self):

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

    def set_player_x(self, _x):

    def set_player_y(self, _y):

    def set_missile_strike(self, strike):

    def update(self):

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

    def draw(self):

The Overlap method just needs slightly modification.

from pygame.locals import *

class Overlap(object):

    def __init__(self):
        pass # nothing here

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

        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

        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)

        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

Here is the result from the above program.

score manager in action
score manager in action

We will revisit the score manager again when we need it but for now it is time to move on to the next topic.

Leave a Reply

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