-
Notifications
You must be signed in to change notification settings - Fork 24
/
main_oo.py
456 lines (358 loc) · 18.2 KB
/
main_oo.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import math
from classes import *
from os import listdir
from os.path import isfile, join
'''
Tile and gfxs
'''
## split a string separating numbers from letters
def splitStringIntoLettersAndNumbers(string):
split_string = []
sub_string = ""
index = 0
while index < len(string):
if string[index].isalpha():
while index < len(string) and string[index].isalpha():
sub_string += string[index]
index += 1
elif string[index].isdigit():
while index < len(string) and string[index].isdigit():
sub_string += string[index]
index += 1
else:
index += 1
split_string.append(sub_string)
sub_string = ""
return split_string
## get name and size properties from filename
def graphicPropertiesFromFilename(filename):
split_filename = splitStringIntoLettersAndNumbers(filename)
name = split_filename[0]
height = int(split_filename[3])
width = int(split_filename[1])
return (name, height, width)
## returns dictionary
def load_game_tiles():
game_tiles = [file for file in listdir("tiles/game/") if isfile(join("tiles/game/", file))] #load all the image files within the directory
ui_tiles = [file for file in listdir("tiles/ui/") if isfile(join("tiles/ui/", file))]
game_tile_dict = {} #init dictionary
ui_tile_dict = {}
#save game tiles
for savedfile in game_tiles:
image = pygame.image.load("tiles/game/" + savedfile).convert_alpha()
tile_name, tile_height, tile_width = graphicPropertiesFromFilename(savedfile)
game_tile_dict[tile_name] = (image, tile_height, tile_width)
#save ui tiles
for savedfile in ui_tiles:
image = pygame.image.load("tiles/ui/" + savedfile).convert_alpha()
tile_name, tile_height, tile_width = graphicPropertiesFromFilename(savedfile)
ui_tile_dict[tile_name] = (image, tile_height, tile_width)
return game_tile_dict, ui_tile_dict
'''
Interpic
'''
def showTitleScreen(screen, tileset, ui_tiles):
clock = pygame.time.Clock()
# init graphics
started_game = False
titlepic_level = Map(1)
dave_logo = AnimatedTile("davelogo", 0)
overlay = Scenery("blacktile", 0)
# clear screen on entering
screen.clearScreen()
# messages
creator_text = "RECREATED BY ARTHUR, CATTANI AND MURILO"
professor_text = "PROFESSOR LEANDRO K. WIVES"
instr1_text = "PRESS SPACE TO START"
instr2_text = "PRESSING ESC AT ANY MOMENT EXITS"
while not started_game:
pygame.display.update()
# print level and tiles
screen.setXPosition(14, titlepic_level.getWidth())
screen.printMap(titlepic_level, tileset)
screen.printTitlepicBorder(tileset)
screen.printTile(104, 0, dave_logo.getGraphic(ui_tiles))
screen.printTile(0, BOTTOM_OVERLAY_POS, overlay.getGraphic(ui_tiles))
# print text in center
screen.printTextAlignedInCenter(creator_text, 47)
screen.printTextAlignedInCenter(professor_text, 55)
screen.printTextAlignedInCenter(instr1_text, BOTTOM_OVERLAY_POS + 2)
screen.printTextAlignedInCenter(instr2_text, BOTTOM_OVERLAY_POS + 11)
# if player pressed escape, exit game; space, start game
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
started_game = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return True # return 0 so we know player pressed escape
pygame.display.flip()
clock.tick(200)
# clear screen on exiting
screen.clearScreen()
return False
def showInterpic(completed_levels, screen, GamePlayer, tileset, ui_tileset):
clock = pygame.time.Clock()
# init graphics
interpic_level = Map("interpic")
screen.setXPosition(0, interpic_level.getWidth())
screen.printMap(interpic_level, tileset)
screen.clearBottomUi(ui_tileset)
# init player
(player_absolute_x, player_absolute_y) = interpic_level.initPlayerPositions(0, GamePlayer)
GamePlayer.setCurrentState(STATE.WALK)
GamePlayer.setDirectionX(DIRECTION.RIGHT)
GamePlayer.setSpriteDirection(DIRECTION.RIGHT)
# init messages
intertext = "GOOD WORK! ONLY " + str(NUM_OF_LEVELS - completed_levels + 1) + " MORE TO GO!"
last_level_text = "THIS IS THE LAST LEVEL!!!"
finish_text = "YES! YOU FINISHED THE GAME!!"
# keep moving the player right, until it reaches the screen boundary
player_reached_boundary = (player_absolute_x >= screen.getUnscaledWidth())
while not player_reached_boundary:
# if player pressed escape, quit game
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return True # return so we treat exiting externally
# update player pos and animation
player_absolute_x = GamePlayer.movePlayerRight(player_absolute_x)
GamePlayer.updateAnimator()
# update screen
screen.printMap(interpic_level, tileset)
screen.printOverlays(ui_tileset)
screen.printUi(ui_tileset, GamePlayer, completed_levels-1)
screen.printPlayer(GamePlayer, player_absolute_x, player_absolute_y, tileset)
# print text accordingly to the number of completed levels
if completed_levels == NUM_OF_LEVELS + 1:
screen.printTextAlignedInCenter(finish_text, 54)
elif completed_levels == NUM_OF_LEVELS:
screen.printTextAlignedInCenter(last_level_text, 54)
else:
screen.printTextAlignedInCenter(intertext, 54)
player_reached_boundary = (player_absolute_x >= screen.getUnscaledWidth())
pygame.display.flip()
clock.tick(200)
return False
def showWarpZone(completed_levels, screen, GamePlayer, tileset, ui_tileset):
clock = pygame.time.Clock()
# init graphics
warp_level = Map("warp")
screen.setXPosition(0, warp_level.getWidth())
screen.printMap(warp_level, tileset)
screen.clearBottomUi(ui_tileset)
# init player
(player_absolute_x, player_absolute_y) = warp_level.initPlayerPositions(0, GamePlayer)
GamePlayer.resetPosAndState()
GamePlayer.setFallingState()
GamePlayer.setGfxId(0)
# keep moving the player right, until it reaches the screen boundary
player_reached_bottom = (player_absolute_y >= screen.getUnscaledHeight())
while not player_reached_bottom:
# if player pressed escape, quit game
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return True # return so we treat exiting externally
player_absolute_y += 0.5
# update screen
screen.printMap(warp_level, tileset)
screen.printPlayer(GamePlayer, player_absolute_x, player_absolute_y, tileset)
screen.printOverlays(ui_tileset)
screen.printUi(ui_tileset, GamePlayer, completed_levels-1)
player_reached_bottom = (player_absolute_y >= screen.getUnscaledHeight())
pygame.display.flip()
clock.tick(200)
return False
def getBonusMapping(current_level):
if current_level == 2: return 6
elif current_level == 5: return 2
elif current_level == 6: return 9
elif current_level == 7: return 10
elif current_level == 8: return 6
elif current_level == 9: return 7
elif current_level == 10: return 1
elif current_level == 1: return 11
else: return 1
def showScores(screen, tileset):
pass
def savePlayerScore(player_score, screen, tileset):
pass
def showCreditsScreen(screen, tileset):
pass
'''
Game processing stuff
'''
'''
Main
'''
def main():
##Init pygame
pygame.init()
game_screen = Screen(SCREEN_WIDTH, SCREEN_HEIGHT)
##Init tiles
tileset, ui_tileset = load_game_tiles()
game_open = True
while game_open:
##Show title screen
option = showTitleScreen(game_screen, tileset, ui_tileset)
#if player presses escape, close game
game_open = not option
##Init a player
GamePlayer = Player()
##Init level and spawner
current_level_number = 1
current_spawner_id = 0
##Available Keys
movement_keys = [pygame.K_UP, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_DOWN]
inv_keys = [pygame.K_LCTRL, pygame.K_RCTRL, pygame.K_LALT, pygame.K_RALT]
##Game processing
ended_game = False
while not ended_game:
# init clock and display
clock = pygame.time.Clock()
pygame.display.update()
# build the level and init screen and player positions
Level = Map(current_level_number)
(player_position_x, player_position_y) = Level.initPlayerPositions(current_spawner_id, GamePlayer)
spawner_pos_x = Level.getPlayerSpawnerPosition(current_spawner_id)[0]
game_screen.setXPosition(spawner_pos_x - 10, Level.getWidth())
# UI Inits
score_ui = 0 #initial score. Everytime it changes, we update the ui
jetpack_ui = False
# init other sprites
death_timer = -1
friendly_shot = 0
# level processing controller
ended_level = False
## Level processing
while not ended_level:
# get keys (inventory)
for event in pygame.event.get():
# stop moving
if event.type == pygame.KEYUP:
# horizontally
if event.key in [pygame.K_LEFT, pygame.K_RIGHT] and GamePlayer.getCurrentState() in [STATE.WALK, STATE.FLY, STATE.JUMP, STATE.CLIMB]:
GamePlayer.clearXMovement()
# vertically
elif event.key in [pygame.K_UP, pygame.K_DOWN] and GamePlayer.getCurrentState() in [STATE.FLY, STATE.CLIMB]:
GamePlayer.setVelocityY(0)
# hit a key
elif event.type == pygame.KEYDOWN:
# quit game
if event.key == pygame.K_ESCAPE:
game_open = False
ended_level = True
ended_game = True
# use something from the inventory
elif event.key in inv_keys:
if GamePlayer.inventoryInput(inv_keys.index(event.key)) and not friendly_shot:
friendly_shot = Level.spawnFriendlyFire(GamePlayer.getSpriteDirection())
friendly_shot_x, friendly_shot_y = player_position_x + GamePlayer.getDirectionX().value * WIDTH_OF_MAP_NODE, player_position_y
# get keys (movement)
pressed_keys = pygame.key.get_pressed()
key_map = [0,0,0,0]
for i, key in enumerate(movement_keys):
if pressed_keys[key]:
key_map[i] = 1
GamePlayer.movementInput(key_map)
# update the player position in the level and treat collisions
if GamePlayer.getCurrentState() != STATE.DESTROY:
(player_position_x, player_position_y) = GamePlayer.updatePosition(player_position_x, player_position_y, Level, game_screen.getUnscaledHeight())
# update friendly shot position, if there is one
if friendly_shot:
friendly_shot_x = friendly_shot.updatePosition(friendly_shot_x, friendly_shot_y, Level)
if (friendly_shot_x == -1):
del friendly_shot
friendly_shot = 0
# if the player ended the level, go on to the next
if GamePlayer.getCurrentState() == STATE.ENDMAP:
ended_level = True
break;
# if the player died, spawn death puff and respawn player (if he has enough lives)
elif GamePlayer.getCurrentState() == STATE.DESTROY:
''' TODO: REFACTOR '''
if death_timer == -1:
GamePlayer.takeLife()
DeathPuff = AnimatedTile("explosion", 0)
death_timer = 120
player_position_y += 0.25
death_timer -= 1
if death_timer == 0:
death_timer = -1
game_screen.setXPosition(Level.getPlayerSpawnerPosition(current_spawner_id)[0] - 10, Level.getWidth())
del DeathPuff
if (GamePlayer.resetPosAndState() != -1):
(player_position_x, player_position_y) = Level.getPlayerSpawnerPosition(current_spawner_id)
player_position_x *= WIDTH_OF_MAP_NODE
player_position_y *= HEIGHT_OF_MAP_NODE
else:
ended_level = True
ended_game = True
# if the player is close enough to one of the screen boundaries, move the screen.
player_close_to_left_boundary = (player_position_x <= game_screen.getXPositionInPixelsUnscaled() + BOUNDARY_DISTANCE_TRIGGER)
player_close_to_right_boundary = (player_position_x >= game_screen.getXPositionInPixelsUnscaled() + game_screen.getUnscaledWidth() - BOUNDARY_DISTANCE_TRIGGER)
reached_level_left_boundary = (game_screen.getXPosition() <= 0)
reached_level_right_boundary = (game_screen.getXPosition() + game_screen.getWidthInTiles() > Level.getWidth())
# move screen left
if player_close_to_left_boundary and not reached_level_left_boundary:
game_screen.moveScreenX(Level, -15, tileset, ui_tileset, GamePlayer, current_level_number)
# move screen right
elif player_close_to_right_boundary and not reached_level_right_boundary:
game_screen.moveScreenX(Level, 15, tileset, ui_tileset, GamePlayer, current_level_number)
# not moving (just update the screen)
else:
game_screen.printMap(Level, tileset)
if friendly_shot:
game_screen.printTile(friendly_shot_x - game_screen.getXPositionInPixelsUnscaled(), friendly_shot_y, friendly_shot.getGraphic(tileset))
bullet_bypassed_screen_right_boundary = (friendly_shot_x >= game_screen.getXPositionInPixelsUnscaled() + game_screen.getUnscaledWidth())
bullet_bypassed_screen_left_boundary = (friendly_shot_x <= game_screen.getXPositionInPixelsUnscaled())
if bullet_bypassed_screen_right_boundary or bullet_bypassed_screen_left_boundary:
del friendly_shot
friendly_shot = 0
if GamePlayer.getCurrentState() != STATE.DESTROY:
# print player accordingly to screen shift
game_screen.printPlayer(GamePlayer, player_position_x - game_screen.getXPositionInPixelsUnscaled(), player_position_y, tileset)
elif not ended_game:
# print death puff accordingly to screen shift
game_screen.printTile(player_position_x - game_screen.getXPositionInPixelsUnscaled(), player_position_y, DeathPuff.getGraphic(tileset))
# update UI
game_screen.printOverlays(ui_tileset)
game_screen.printUi(ui_tileset, GamePlayer, current_level_number)
if not ended_level:
if GamePlayer.inventory["gun"] == 1:
game_screen.updateUiGun(ui_tileset)
if GamePlayer.inventory["jetpack"] == 1 or jetpack_ui :
game_screen.updateUiJetpack(ui_tileset, GamePlayer.inventory["jetpack"])
jetpack_ui = True
if GamePlayer.inventory["trophy"] == 1:
game_screen.updateUiTrophy(ui_tileset)
if score_ui != GamePlayer.score:
game_screen.updateUiScore(GamePlayer.score, ui_tileset)
score_ui = GamePlayer.score
pygame.display.flip()
pygame.event.pump()
clock.tick(200)
# Onto the next level
GamePlayer.clearInventory()
if (player_position_x == -2):
current_level_number = getBonusMapping(current_level_number)
current_spawner_id = 1
elif (current_spawner_id == 1):
current_level_number = getBonusMapping(current_level_number)
current_spawner_id = 0
else:
current_level_number += 1
if current_level_number > NUM_OF_LEVELS and ended_level and not ended_game:
showCreditsScreen(game_screen, tileset)
ended_game = True
elif ended_level and current_spawner_id == 1:
option = showWarpZone(current_level_number, game_screen, GamePlayer, tileset, ui_tileset)
ended_game = option
game_open = not option
elif ended_level and not ended_game:
option = showInterpic(current_level_number, game_screen, GamePlayer, tileset, ui_tileset)
ended_game = option
game_open = not option
savePlayerScore(GamePlayer.getScore(), game_screen, tileset)
showScores(game_screen, tileset)
pygame.quit()
quit()
if __name__ == "__main__":
main()