-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
79 lines (65 loc) · 2.22 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import pygame as pg
from mainmenu import MainMenu, Settings, Audio, Video
#from game import Game, LevelOne
#from settings import Music_Mixer, loadCustomFont, States, screen
screen_size = screen
'''
Does not have to be a class
Could be global scope
Never more than one Control Class
Has main game loop
Has main update
Has main event loop
Switches between states
'''
class Control:
def __init__(self):
self.done = False
self.clock = pg.time.Clock()
self.screen = screen_size
def setup_states(self, state_dict, start_state):
self.state_dict = state_dict
self.state_name = start_state
self.state = self.state_dict[self.state_name]
def flip_state(self):
self.state.done = False
previous,self.state_name = self.state_name, self.state.next
self.state.cleanup()
self.state = self.state_dict[self.state_name]
self.state.startup()
self.state.previous = previous
def update(self, dt):
if self.state.quit:
self.done = True
elif self.state.done:
self.flip_state()
self.state.update(self.screen, dt)
def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
self.state.get_event(event)
def main_game_loop(self):
while not self.done:
delta_time = self.clock.tick(60)/1000
self.event_loop()
self.update(delta_time)
pg.display.update()
#Settings from above dictionary get passed into Control class
#Control creates app object.
#Then, each state (Menu & Game) object get assigned to a dicitonary.
#This allows control to be able to switch to and from any state as needed.
app = Control()
#State Dictionary. Include all state classes here.
state_dict = {'mainmenu': MainMenu(),
'game': Game() ,
'settings' : Settings(),
'levelone' : LevelOne(),
'audio' : Audio(),
'video' : Video()}
#Setup State is called and sets the initial state of the program
app.setup_states(state_dict, 'mainmenu')
#Call main game loop that runs whole program
app.main_game_loop()
pg.quit()
sys.exit()