forked from seanmcortes/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
108 lines (93 loc) · 3.78 KB
/
main.py
File metadata and controls
108 lines (93 loc) · 3.78 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sys
from os import path
import pygame
import pygame.freetype
from settings import *
from sprites import *
from scenes import TutorialMovement, TutorialBlock, TutorialEnemy, TutorialIce, \
BlockUnitTest, Level5, Level6, Level7, Level8, Level9, Level10, DevRoom
from menu import *
"""
Manages games scenes
"""
class Game:
def __init__(self):
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
pygame.key.set_repeat(500, 100)
self.playing = True
self.dt = None # sub-initialization in run()
self.scene = None # sub-initialization in go_to()
self.debug = False
self.scene_dictionary = {
1: TutorialMovement,
2: TutorialBlock,
3: TutorialEnemy,
4: TutorialIce,
5: Level5,
6: Level6,
7: Level7,
8: Level8,
9: Level9,
10: Level10,
}
if "-debug" in sys.argv:
self.debug = True
# Naive debug menu. e.g. 'python main.py -debug Level1Scene'
if self.debug:
function_index = sys.argv.index("-debug") + 1
self.go_to(eval(sys.argv[function_index])(self))
else:
self.go_to(MainMenuScene(self))
def go_to(self, scene):
self.scene = scene
def run(self):
#https://www.pygame.org/docs/ref/mixer.html
pygame.mixer.music.load(path.join(MUSIC_FOLDER,'music_loop.wav'))
pygame.mixer.music.play(-1)
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.scene.handle_events(pygame.event.get())
self.scene.update()
self.scene.render()
pygame.display.flip()
################################################################################
#Description: Loads a scene
#Args:
# self: a copy of the Game object
# level: the scene you want to load
###############################################################################
def select_scene(self, level):
# Get the function from switcher dictionary
#Source: https://jaxenter.com/implement-switch-case-statement-python-138315.html
func = self.scene_dictionary.get(level)
# Execute the function
self.go_to(func(self))
################################################################################
# Description: Returns a scene number based on a scene name
# Args:
# self: a copy of the Game object
# scene_name: the name of the scene you want to load (string)
# Returns: the scene number
###############################################################################
def get_scene_number(self, scene_name):
#create a reverse dictionary to look up scene number by scene name
inverse_scene_dictionary= {v: k for k, v in self.scene_dictionary.items()} #Python 3 version
#Source: https://stackoverflow.com/questions/483666/python-reverse-invert-a-mapping
# Get the function from switcher dictionary
#Source: https://jaxenter.com/implement-switch-case-statement-python-138315.html
scene_number = inverse_scene_dictionary.get(scene_name, lambda: 0)
return scene_number
def main():
delve = Game()
while True:
delve.run()
if __name__ == "__main__":
main()
# Sources:
# https://github.com/kidscancode/pygame_tutorials/blob/master/tilemap/part%2007/main.py
# https://stackoverflow.com/questions/14700889/pygame-level-menu-states
# https://stackoverflow.com/questions/21937695/python-cx-freeze-name-file-is-not-defined