Game progress — creating game state engine

Game state engine will be the one which uses by this game to control the world object which will control the game entities such as rock and ship. Another usage of the game state engine is uses to control the game screen (loading screen, winning screen, game over screen and etc). These few days I will need to figure it out how to connect the game state engine with various states that ask the world object as well as the screen object to perform various jobs.

So far here is how the game state engine looks like.

from state_engine import StateEngine 
from state_loading import GameStateLoading
from state_playing import GameStatePlaying
from state_winning import GameStateWinning
from state_losing import GameStateLosing
from state_over import GameStateOver

class GameStateEngine(StateEngine):
    
    def __init__(self, surface):
        
        StateEngine.__init__(self)
        
        self.surface = surface #the game surface
        
        loading_state = GameStateLoading('load', self.surface) 
        self.add_state(loading_state)
        
        playing_state = GameStatePlaying('play', self.surface)
        self.add_state(playing_state)
        
        winning_state = GameStateWinning('win', self.surface)
        self.add_state(winning_state)
        
        losing_state = GameStateLosing('lose', self.surface)
        self.add_state(losing_state)
        
        game_over_state = GameStateOver('over', self.surface)
        self.add_state(game_over_state)
        
        self.active_state = loading_state #set the active state as loading state when the game first started

The GameStateEngine above is derived from the StateEngine main class.

Here is one of the state which will load the welcome screen.

from state import State
from screen_load import ScreenLoad 

class GameStateLoading(State):
    
    def __init__(self, name, surface):
        State.__init__(self, name)
        self.surface = surface #the surface of the screen
        self.screen = ScreenLoad(surface) #initialize the ScreenLoad module
        
    def do_actions(self):
        screen.render() #render the welcome screen
        
    def check_conditions(self, pos):
        if(screen.isButtonClick(pos)): #if the play button has been clicked then return 'play' as a new state
            return 'play'

GameStateLoading which derives from the State class will responsible for the loading of the welcome screen.

Here is how the ScreenLoad object looks like.

from screen import Screen

class ScreenLoad(Screen):
    
    def __init__(self, surface):
        Screen.__init__(self)
        self.surface = surface #surface of the main screen
        self.main = 'title.png'
        self.play = 'play_on.png'
        self.screen = pygame.image.load(self.main).convert_alpha()
        self.play_button = pygame.image.load(self.play).convert_alpha()
        
    def render(self):
        self.surface.blit(self.screen, (0,0))
        self.button = self.surface.blit(self.play, (w/2, h/2))
        
    def isButtonClick(self, pos):
        return self.button.collidepoint(pos) #if the click area is within the button boundary then return true

Besides loading the title screen the ScreenLoad module will also responsible for the loading of the winning screen and etc therefore there will be further changes for this module.

As you can see the GameStateEngine class needs a lot of modules to function properly therefore it will take a few days for me to figure it out the entire game plan so stay tuned for the further progress of this game!