In this article we are going to create a start scene for our pygame project. The start scene itself looks really simple but the process to render the start scene will involve the modification of a few files.
First of all, lets create the start scene class which will render a background and a play button on the game scene. When we click on that play button the game will start.
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/bg.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_button = GameSprite(self.button_image, self.button_rect) self.surface = self.sprite.getImage() # get the background 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): self.scene.blit(self.surface, self.draw_pos) # draw a background sprite self.scene.blit(self.button_surface, self.draw_button_pos) # draw a button sprite pygame.display.flip()
Next we will create an instance of the above class in our Game Manager class. Besides that we will do the following.
1) create a new loop method for the Game Manager class which will be called in the main game file to update and draw the objects on the game scene.
2) create the loading state and the game state constant variable together with the state variable which is used to determine whether the game manager should load the start scene or load the game scene.
3) create the isAreaClick method to determine whether the player has clicked on the start scene play button or not, if yes then the game state will change from loading state to gaming state and the game will start.
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 * 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) self.start_scene = StartScene(scene) self.load_music() self.play_music() #game state self.LOAD = 0 self.GAME = 1 self.state = self.LOAD def loop(self): if(self.state == self.LOAD): self.start_scene.draw() elif(self.state == self.GAME): self.update() self.draw() def isAreaClick(self, pos): if (self.state == self.LOAD): # if it is loading state then response to the click event 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): pygame.mixer_music.load('Music/winternight.ogg') def play_music(self): pygame.mixer_music.play(-1) #play the music infinite time def set_player_x(self, _x): if (self.state == self.GAME): self.player.setX(_x) def set_player_y(self, _y): if (self.state == self.GAME): self.player.setY(_y) def set_missile_strike(self, strike): if (self.state == self.GAME): self.player.setStrike(strike) def update(self): self.player.update() self.enemy_manager.update() self.isOverlap() self.explosion_manager.explosion_update() # 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): self.background.draw() self.player.draw() self.enemy_manager.draw() self.explosion_manager.draw() self.score_manager.draw() pygame.display.flip()
The last file we need to edit is the main file where we will add in the click event so we can find out whether the player has clicked on the play button or not. We will also remove the update and the draw method from the Game Manager class and replace them with a single loop method which we have talked about previously.
import sys, pygame from pygame.locals import * from GameManager import GameManager pygame.init() size = width, height = 660, 660 pygame.display.set_caption("Air Strike") # set the title of the window screen = pygame.display.set_mode(size) game_manager = GameManager(screen) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mixer_music.stop() sys.exit() # detect key press event if event.type == KEYDOWN: if event.key == K_LEFT: game_manager.set_player_x(-0.1) elif event.key == K_RIGHT: game_manager.set_player_x(0.1) if event.key == K_UP: game_manager.set_player_y(-0.1) elif event.key == K_DOWN: game_manager.set_player_y(0.1) if event.key == K_SPACE: game_manager.set_missile_strike(True) elif event.type == KEYUP: if event.key == K_LEFT: game_manager.set_player_x(0) elif event.key == K_RIGHT: game_manager.set_player_x(0) if event.key == K_UP: game_manager.set_player_y(0) elif event.key == K_DOWN: game_manager.set_player_y(0) if event.key == K_SPACE: game_manager.set_missile_strike(False) elif event.type == pygame.MOUSEBUTTONDOWN: # 1 is the left mouse button if event.button == 1: game_manager.isAreaClick(event.pos) game_manager.loop()
We will continue to modify the Game Manager class and create a game over scene class in the next chapter. If you want to know when will this website posts a new post then don’t forget to click on the bell notification button below this website.