-
Notifications
You must be signed in to change notification settings - Fork 0
/
Platformer.py
530 lines (491 loc) · 20.6 KB
/
Platformer.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
"""
STUFF TO REMEMBER:
1. the background for the animation image should be green
2. ON LINE 79 and 80 for every frame you must add a 7 except for img_0, eg'img_0,img_1'should be : [7]
TODO:
Fix pixel art
newest update:
removed unused files
"""
# ---------------------------- IMPORTING_MODULES ---------------------------- #
import pygame
import sys
from pygame.image import load
from functions import func
from functions import button
from PIL import Image
import random
from time import sleep
# ---------------------------- VARIABLES ---------------------------- #
moving_right = False
moving_left = False
vertical_momentum = 0
air_timer = 0
true_scroll = [0, 0]
player_action = 'idle'
player_frame = 0
FPS = 10
player_flip = False
grass_sound_timer = 0
player_rect = pygame.Rect(150, 200, 25, 35)
global game_map
global game_level
wb = []
right_timer = 0
game_map = func.load_map('maps/map.csv')
zoom = 2
true_scroll[0] += (player_rect.x - true_scroll[0] - 152) / 20
true_scroll[1] += (player_rect.y - true_scroll[1] - 106) / 20
scroll = true_scroll.copy()
# ---------------------------- INITIATION ---------------------------- #
clock = pygame.time.Clock()
pygame.init() # initiates pygame
pygame.mixer.init()
pygame.mixer.set_num_channels(64)
pygame.display.set_caption('Jumpster')
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) # initiate the window
displayInfo = pygame.display.Info()
width = displayInfo.current_w
height = displayInfo.current_h
bg = Image.open('images/background.png')
bg = bg.resize((width, height))
bg.save('images/background.png')
WINDOW_SIZE = (width, height)
display = pygame.Surface((int(WINDOW_SIZE[0] / zoom), int(WINDOW_SIZE[1] / zoom)))
half = width / 2
quarter = half / 2
right = half + quarter
font = pygame.font.SysFont("Arial", 25)
display_size = (pygame.display.Info().current_w , pygame.display.Info().current_h)
image = pygame.image.load(r'SGK.png')
image_size = image.get_rect().size
centered_image = [(display_size[0] - image_size[0])/2, (display_size[1] - image_size[1])/2]
for i in range (155):
screen.fill((255,255,255))
image.set_alpha(i)
screen.blit(image, centered_image)
pygame.display.update()
sleep(0.001)
sleep(2)
# ---------------------------- FUNCTIONS ---------------------------- #
def load_animation(path, frame_durations):
global animation_frames
animation_name = path.split('/')[-1]
animation_frame_data = []
n = 0
for frame in frame_durations:
animation_frame_id = animation_name + '_' + str(n)
img_loc = path + '/' + animation_frame_id + '.png'
animation_image = pygame.image.load(img_loc).convert()
animation_image.set_colorkey((0, 209, 0))
animation_frames[animation_frame_id] = animation_image.copy()
for i in range(frame):
animation_frame_data.append(animation_frame_id)
n += 1
return animation_frame_data
# ---------------------------- ANIMATION_DATABASE ---------------------------- #
global animation_frames
animation_frames = {}
animation_database = {}
animation_database['run'] = load_animation('player_animations/run', [7])
animation_database['idle'] = load_animation('player_animations/idle', [7, 7, 7, 7, 7, 7, 60])
# ---------------------------- DEFINING_ALL_TEXTURES ---------------------------- #
id0 = load('tiles/0.png').convert()
id1 = load('tiles/1.png').convert()
id2 = load('tiles/2.png').convert()
id3 = load('tiles/3.png').convert()
id4 = load('tiles/4.png').convert()
id5 = load('tiles/5.png').convert()
id6 = load('tiles/6.png').convert()
id7 = load('tiles/7.png').convert()
id8 = load('tiles/8.png').convert()
id9 = load('tiles/9.png').convert()
id10 = load('tiles/10.png').convert()
id11 = load('tiles/11.png').convert()
id12 = load('tiles/12.png').convert()
id13 = load('tiles/13.png').convert()
id14 = load('tiles/14.png').convert()
id15 = load('tiles/15.png').convert()
id16 = load('tiles/16.png').convert()
id17 = load('tiles/17.png').convert()
id18 = load('tiles/18.png').convert()
id19 = load('tiles/19.png').convert()
id20 = load('tiles/20.png').convert()
id21 = load('tiles/21.png').convert()
id22 = load('tiles/22.png').convert()
# ---------------------------- TEXTURE_TRANSPARENCY ---------------------------- #
id0.set_colorkey((255, 255, 255))
id1.set_colorkey((255, 255, 255))
id2.set_colorkey((255, 255, 255))
id3.set_colorkey((255, 255, 255))
id4.set_colorkey((255, 255, 255))
id5.set_colorkey((255, 255, 255))
id6.set_colorkey((255, 255, 255))
id7.set_colorkey((255, 255, 255))
id8.set_colorkey((255, 255, 255))
id9.set_colorkey((255, 255, 255))
id10.set_colorkey((255, 255, 255))
id11.set_colorkey((255, 255, 255))
id12.set_colorkey((255, 255, 255))
id13.set_colorkey((255, 255, 255))
id14.set_colorkey((255, 255, 255))
id15.set_colorkey((255, 255, 255))
id16.set_colorkey((255, 255, 255))
id17.set_colorkey((255, 255, 255))
id18.set_colorkey((255, 255, 255))
id19.set_colorkey((255, 255, 255))
id20.set_colorkey((255, 255, 255))
id21.set_colorkey((255, 255, 255))
id22.set_colorkey((255, 255, 255))
def Win_Screen():
start_img = pygame.image.load('buttons/start.png').convert_alpha()
start_img.set_colorkey((255,255,255))
exit_img = pygame.image.load('buttons/quit.png').convert_alpha()
exit_img.set_colorkey((255,255,255))
jumpster_img = pygame.image.load('buttons/Jumpster.png').convert_alpha()
start_button = button.Button(width/4, height/2+300, start_img, 0.8)
jumpster_button = button.Button(width/2, height/2-200, jumpster_img, 0.8)
exit_button = button.Button(width/4*3,height/2+300, exit_img,0.8)
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button = button.VideoButton(0, 0, pygame_frame,0.8)
video_button.draw(screen)
pygame.mixer.music.load('audio/music.wav')
pygame.mixer.music.play()
animation = ['images/you_won/you_won_0.png', 'images/you_won/you_won_0.png', 'images/you_won/you_won_1.png', 'images/you_won/you_won_1.png', 'images/you_won/you_won_2.png', 'images/you_won/you_won_2.png', 'images/you_won/you_won_3.png', 'images/you_won/you_won_3.png', 'images/you_won/you_won_4.png', 'images/you_won/you_won_4.png', 'images/you_won/you_won_5.png', 'images/you_won/you_won_5.png']
run = True
x = 0
while run:
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button.image = pygame_frame
video_button.draw(screen)
x = x + 1
if len(animation) == x:
x = 0
img = pygame.image.load(animation[x]).convert_alpha()
win_button = button.Button(width/2, height/2-50, img, 0.8)
jumpster_button.draw(screen)
win_button.draw(screen)
if start_button.draw(screen):
pygame.mixer.music.fadeout(1000)
run = False
if exit_button.draw(screen):
pygame.mixer.music.fadeout(1000)
pygame.quit()
sys.exit()
# event handler
for event in pygame.event.get():
# quit game
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
game_loop()
def Lose_Screen():
start_img = pygame.image.load('buttons/start.png').convert_alpha()
start_img.set_colorkey((255,255,255))
exit_img = pygame.image.load('buttons/quit.png').convert_alpha()
exit_img.set_colorkey((255,255,255))
jumpster_img = pygame.image.load('buttons/Jumpster.png').convert_alpha()
lost_img = pygame.image.load('images/you_lost.png')
start_button = button.Button(width/4, height/2+300, start_img, 0.8)
jumpster_button = button.Button(width/2, height/2-200, jumpster_img, 0.8)
lost_button = button.Button(width/2, height/2-50, lost_img, 0.8)
exit_button = button.Button(width/4*3,height/2+300, exit_img,0.8)
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button = button.VideoButton(0, 0, pygame_frame,0.8)
video_button.draw(screen)
pygame.mixer.music.load('audio/music.wav')
pygame.mixer.music.play()
run = True
while run:
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button.image = pygame_frame
video_button.draw(screen)
jumpster_button.draw(screen)
lost_button.draw(screen)
if start_button.draw(screen):
pygame.mixer.music.fadeout(1000)
run = False
if exit_button.draw(screen):
pygame.mixer.music.fadeout(1000)
pygame.quit()
sys.exit()
# event handler
for event in pygame.event.get():
# quit game
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
game_loop()
# ---------------------------- START_SCREEN ---------------------------- #
def Start_Screen():
start_img = pygame.image.load('buttons/start.png').convert_alpha()
start_img.set_colorkey((255,255,255))
exit_img = pygame.image.load('buttons/quit.png').convert_alpha()
exit_img.set_colorkey((255,255,255))
options_img = pygame.image.load('buttons/options.png').convert_alpha()
jumpster_img = pygame.image.load('buttons/Jumpster.png').convert_alpha()
start_button = button.Button(width/4, height/2+300, start_img, 0.8)
options_button = button.Button(width/2,height/2+300, options_img,0.8)
jumpster_button = button.Button(width/2, height/2-200, jumpster_img, 0.8)
exit_button = button.Button(width/4*3,height/2+300, exit_img,0.8)
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button = button.VideoButton(0, 0, pygame_frame,0.8)
video_button.draw(screen)
pygame.mixer.music.load('audio/music.wav')
pygame.mixer.music.play()
run = True
while run:
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button.image = pygame_frame
video_button.draw(screen)
jumpster_button.draw(screen)
if start_button.draw(screen):
pygame.mixer.music.fadeout(1000)
run = False
if exit_button.draw(screen):
pygame.mixer.music.fadeout(1000)
pygame.quit()
sys.exit()
if options_button.draw(screen):
options()
# event handler
for event in pygame.event.get():
# quit game
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
def options():
pygame_frame = pygame.image.load('images/background.png').convert_alpha()
video_button = button.VideoButton(0, 0, pygame_frame,0.8)
back_img = pygame.image.load('buttons/back.png').convert_alpha()
back_button = button.Button(width/4, height/2+300, back_img, 0.8)
video_button.draw(screen)
run = True
while run:
video_button.image = pygame_frame
video_button.draw(screen)
if back_button.draw(screen):
run = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
pygame.display.update()
def pause_Screen():
start_img = pygame.image.load('buttons/return.png').convert_alpha()
exit_img = pygame.image.load('buttons/quit.png').convert_alpha()
jumpster_img = pygame.image.load('buttons/Jumpster.png').convert_alpha()
start_button = button.Button(width/4, height/2+300, start_img, 0.8)
jumpster_button = button.Button(width/2, height/2-200, jumpster_img, 0.8)
exit_button = button.Button(width/4*3,height/2+300, exit_img,0.8)
pygame.mixer.music.load('audio/music.wav')
pygame.mixer.music.play(-1)
run = True
while run:
screen.fill((202, 228, 241))
jumpster_button.draw(screen)
if start_button.draw(screen):
pygame.mixer.music.stop()
run = False
if exit_button.draw(screen):
pygame.quit()
sys.exit()
# event handler
for event in pygame.event.get():
# quit game
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
# ---------------------------- FPS_COUNTER ---------------------------- #
def update_fps():
fps = str(int(clock.get_fps()))
fps_text = font.render(fps, 1, pygame.Color(0,0,0))
global FPS
if FPS == 10:
fps = 'bruh'
fps_text = font.render(fps, 1, pygame.Color(255, 0, 0))
return fps_text
# ---------------------------- GAME_LOOP ---------------------------- #
def game_loop():
moving_right = False
moving_left = False
vertical_momentum = 0
air_timer = 0
true_scroll = [0, 0]
player_action = 'idle'
player_frame = 0
global FPS
player_flip = False
grass_sound_timer = 0
player_rect = pygame.Rect(150, 300, 25, 35)
global game_map
global game_level
right_timer = 0
game_map = func.load_map('maps/map.csv')
zoom = 2
true_scroll[0] += (player_rect.x - true_scroll[0] - 152) / 20
true_scroll[1] += (player_rect.y - true_scroll[1] - 106) / 20
scroll = true_scroll.copy()
jump_sound = pygame.mixer.Sound('audio/jump.wav')
grass_sounds = [pygame.mixer.Sound('audio/grass_0.wav'), pygame.mixer.Sound('audio/grass_1.wav')]
grass_sounds[0].set_volume(0.2)
grass_sounds[1].set_volume(0.2)
while True:
display.fill((146, 244,255))
display.blit(update_fps(), (10, 0))
FPS = 70
if grass_sound_timer > 0:
grass_sound_timer -= 1
scroll[0] = player_rect.x - int(WINDOW_SIZE[0] / (zoom * 2)) + 2 # sets the 'zoom value'
scroll[1] = player_rect.y - int(WINDOW_SIZE[1] / (zoom * 2)) + 5 # sets the 'zoom value'
tile_rects = []
y = 0
# ---------------------------- MAP_BUILDER ---------------------------- #
for layer in game_map:
x = 0
for tile in layer:
if tile == '0':
display.blit(id0, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '1':
display.blit(id1, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '2':
display.blit(id2, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '3':
display.blit(id3, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '4':
display.blit(id4, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '5':
display.blit(id5, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '6':
display.blit(id6, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '6':
display.blit(id6, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '7':
display.blit(id7, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '8':
display.blit(id8, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '9':
display.blit(id9, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile =='10':
display.blit(id10, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile =='11':
display.blit(id11, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile =='12':
display.blit(id2, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '13':
display.blit(id13, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '14':
display.blit(id14, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '15':
display.blit(id15, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '12':
display.blit(id16, (x * 32 - scroll[0], y * 32 - scroll[1]))
if tile == '27':
wb.append([game_map[y][x], x, y])
display.blit(id21, (x * 32 - scroll[0], y * 32 - scroll[1]))
del game_map[y][x]
if tile == '41':
wb.append([game_map[y][x], x, y])
display.blit(id22, (x * 32 - scroll[0], y * 32 - scroll[1]))
del game_map[y][x]
if tile != '-1':
tile_rects.append(pygame.Rect(x * 32, y * 32, 32, 32))
x += 1
y += 1
for z in wb:
x = z[1]
y = z[2]
z = z[0]
if z == '27':
display.blit(id21, (x * 32 - scroll[0], y * 32 - scroll[1]))
if z == '41':
win_x = x * 32
win_y = y * 32
win_y_upper = y * 32 + 32
display.blit(id22, (x * 32 - scroll[0], y * 32 - scroll[1]))
player_movement = [0, 0]
if moving_right == True:
player_movement[0] += 2
right_timer += 1
if moving_left == True:
player_movement[0] -= 2
right_timer -= 1
player_movement[1] += vertical_momentum
vertical_momentum += 0.2
if vertical_momentum > 3:
vertical_momentum = 3
if player_movement[0] == 0:
player_action, player_frame = func.change_action(player_action, player_frame, 'idle')
if player_movement[0] > 0:
player_flip = False
player_action, player_frame = func.change_action(player_action, player_frame, 'run')
if player_movement[0] < 0:
player_flip = True
player_action, player_frame = func.change_action(player_action, player_frame, 'run')
player_rect, collisions = func.move(player_rect, player_movement, tile_rects)
if win_x <= player_rect.x and (win_y <= player_rect.y or win_y_upper <= player_rect.y):
Win_Screen()
if collisions['bottom'] == True:
air_timer = 0
vertical_momentum = 0
if player_movement[0] != 0:
if grass_sound_timer == 0:
grass_sound_timer = 30
random.choice(grass_sounds).play()
else:
air_timer += 1
player_frame += 1
if player_frame >= len(animation_database[player_action]):
player_frame = 0
player_img_id = animation_database[player_action][player_frame]
player_img = animation_frames[player_img_id]
display.blit(pygame.transform.flip(player_img, player_flip, False),
(player_rect.x - scroll[0], player_rect.y - scroll[1]))
x = 1
if air_timer > 100:
FPS = 10
if air_timer > 120:
Lose_Screen()
break
# ---------------------------- EVENT_LOOP ---------------------------- #
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
pygame.mixer.music.fadeout(1000)
if event.key == pygame.K_RIGHT:
moving_right = True
if event.key == pygame.K_LEFT:
moving_left = True
if event.key == pygame.K_UP:
if air_timer < 6:
jump_sound.play()
vertical_momentum = -5
if event.key == pygame.K_ESCAPE:
pause_Screen()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_right = False
if event.key == pygame.K_LEFT:
moving_left = False
screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
pygame.display.update()
clock.tick(FPS)
if __name__ == '__main__':
for i in range (155, 0, -1):
screen.fill((255,255,255))
image.set_alpha(i)
screen.blit(image, centered_image)
pygame.display.update()
sleep(0.001)
Start_Screen()
game_loop()