After one and the half day of working on and off on the game state engine, this mega structure has finally ready for the real production! Basically this game engine will become the brain which will control the state object which will then control the screen and the world object. Now our main game module has became so simple because of the introduction of the game state engine above.
#!/usr/bin/env python import pygame from pygame.locals import * from sys import exit from vector2d import Vector2D from game_state_engine import GameStateEngine SCREEN_SIZE = (550, 550) #game screen size pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32) screen.fill((0, 0, 0)) pygame.display.set_caption("Rock Sweeper") state_engine = GameStateEngine(screen) while True: for event in pygame.event.get(): if event.type == QUIT: exit() elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: pos = pygame.mouse.get_pos() if(state_engine.get_state_name() == 'load'): state_engine.pos = pos pressed_keys = pygame.key.get_pressed() # get all the keys from key-press events if pressed_keys[K_LEFT]: if(state_engine.get_state_name() == 'play'): state_engine.v = Vector2D(-1., 0.) # move the top ship one unit to the left state_engine.v1 = Vector2D(1., 0.) # move the bottom ship one unit to the right elif pressed_keys[K_RIGHT]: if(state_engine.get_state_name() == 'play'): state_engine.v = Vector2D(1., 0.) # move the top ship one unit to the right state_engine.v1 = Vector2D(-1., 0.) # move the bottom ship one unit to the left else: #reset the speed to zero for both sjips state_engine.v = Vector2D(0.0, 0.0) state_engine.v1 = Vector2D(0.0, 0.0) state_engine.do_action() #do action state_engine.check_conditions() #what is the state now? pygame.display.flip()
Now with the building of this game state engine thing will become easier and simple because all we need to do now is to change the state within the game state engine!