-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlevels.py
174 lines (158 loc) · 5.27 KB
/
levels.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'''
levels.py
Provides the Level and LevelMenu classes
'''
import pygame,os
from run_game import Game
from objects import Object
from menus import Menu, MenuEntry
from globalvalues import Renderable,Options,Menus,GlobalObjects
pygame.init()
"""
Provides the logic for loading levels, and draws their splash screen before
giving control to the Game object
"""
class Level(Renderable):
"""
Sets up the level and loads the objects from the disk
"""
def __init__(self,worldnum,stagenum,datafile=None):
Renderable.__init__(self)
self.world=worldnum
self.stage=stagenum
self.time=-2000
self.size = self.screen.get_size()
self.playerstart = (0,500)
self.background = pygame.image.load("assets"+os.sep+"world"
+os.sep+str(worldnum)+os.sep+"background.jpg")
if datafile:
self.from_file(datafile)
else:
self.from_number(worldnum,stagenum)
"""
Returns a beautifully formatted string for easy user Level identification
The string is of the form "World X - Stage Y"
"""
def pretty_name(self):
return 'World '+ str(self.world) + ' - Stage '+str(self.stage)
"""
Loads the level off the disk, parsing it as described in levelformat
"""
def from_file(self,datafile):
print "Loading: "+datafile
self.objectdict=dict()
levelfile = open(datafile, "r")
levellines = levelfile.read().split("\n")
if levellines[0] == "ALGEBRAADVENTURELEVELFILE":
self.size = levellines[1].split(" ")
self.size = [int(i) for i in self.size]
self.playerstart = levellines[2].split(" ")
self.playerstart = [int(i) for i in self.playerstart]
i = 3
while levellines[i] != "ENDOBJECTS":
object_params = levellines[i].split(" ")
self.objectdict.setdefault((int(object_params[1]), int(object_params[2])), Object(object_params[0]))
i += 1
else:
print "Invalid level file"
"""
Loads the level based on world and stage number
"""
def from_number(self,worldnum,stagenum):
self.from_file("levels"+os.sep+"world"+str(worldnum)
+os.sep+"stage"+str(stagenum))
"""
Draws the level splash
"""
def start(self):
self.screen.fill((0,0,0))
levelintro=pygame.font.SysFont('liberationsans',32)
screencenter=(self.screen.get_width()/2,self.screen.get_height()/2)
displaysize=levelintro.size(self.pretty_name())
renderedtext=levelintro.render(self.pretty_name(),True,(255,255,255))
self.screen.blit(renderedtext,(screencenter[0]-displaysize[0]/2,
screencenter[1]-displaysize[1]/2))
self.time+=GlobalObjects.clock.get_time()
"""
Checks to see if the level should abort during the splash
"""
def processEvents(self,events):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
GlobalObjects.renderingThread.renderobj = LevelMenu()
"""
Draws the splash screen before passing control to Game
"""
def draw(self, events):
self.processEvents(events)
if self.time < 0:
self.start()
else:
GlobalObjects.renderingThread.renderobj=Game(self)
"""
Completes the level, and begins loading the next one
"""
def complete(self):
if self.stage < 4:
GlobalObjects.renderingThread.renderobj = Level(self.world,self.stage+1)
else:
with GlobalObjects.lock:
GlobalObjects.unlockedWorlds+=1
GlobalObjects.renderingThread.renderobj = Level(GlobalObjects.unlockedWorlds,1)
"""
Provides the menu from which the game is played
"""
class LevelMenu(Menu):
"""
Sends the player to World 1
"""
def world_1():
GlobalObjects.renderingThread.renderobj = Level(1,1)
"""
Sends the player to World 2
"""
def world_2():
GlobalObjects.renderingThread.renderobj = Level(2,1)
"""
Sends the player to World 3
"""
def world_3():
GlobalObjects.renderingThread.renderobj = Level(3,1)
"""
Sends the player to World 4
"""
def world_4():
GlobalObjects.renderingThread.renderobj = Level(4,1)
"""
Returns to the Main menu
"""
def return_to_main(self=None):
GlobalObjects.escInUse = False
GlobalObjects.renderingThread.renderobj=Menus.main
levelentries=(
MenuEntry("Back",return_to_main),
MenuEntry("World 1",world_1),
MenuEntry("World 2",world_2),
MenuEntry("World 3",world_3),
MenuEntry("World 4",world_4)
)
"""
Determines how many worlds are unlocked and only loads MenuEntries for those
that are
"""
def __init__(self):
Menu.__init__(self,LevelMenu.levelentries[0:GlobalObjects.unlockedWorlds+1])
"""
Checks to see if returning to Main
"""
def draw(self,events):
Menu.draw(self,events)
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.return_to_main()
"""
Run on first import, builds LevelMenu object where it's needed
"""
Menus.levels=LevelMenu()