-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscene.py
47 lines (36 loc) · 1.31 KB
/
scene.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Import the needed libraries
import pygame
# Create a custom event to handle changing of scenes
SCENE_TRANSITION = pygame.USEREVENT
# Base class for scenes
class Scene:
# Maintain a stack of scenes
scene_stack = []
# Flag to check if Transition event needs to be fired
fire_event = True
# First time loading the scene
already_loaded = False
# Called once on transition
def start(self, screen):
return
# Called for every event
def update(self, event):
return
# Called every frame
def draw(self, screen):
return
# I Should really have created a Finite State Machine, but this is fine for a small game like this :-P
# Used to fire the SCENE_TRANSITION event to push a scene onto the scene stack, that is, load the next scene
@staticmethod
def push_scene(next_scene_id):
Scene.scene_stack.append(next_scene_id)
if Scene.fire_event:
event = pygame.event.Event(SCENE_TRANSITION)
pygame.event.post(event)
# Used to fire the SCENE_POP event to pop a scene from the scene stack, that is, return to the previous scene
@staticmethod
def pop_scene():
Scene.scene_stack.pop()
if Scene.fire_event:
event = pygame.event.Event(SCENE_TRANSITION)
pygame.event.post(event)