-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteves_pixels.py
1005 lines (742 loc) · 28.4 KB
/
steves_pixels.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
# hide pygame message
import pygame
import copy
import random
import sys
from win32gui import SetWindowPos
from pygame.locals import *
from PIL import Image, ImageFilter
# Code by:
# ..\ Ishan Jindal
# Orignal Project by:
# ..\ Al Sweigart
# Based on:
# ..\ Retro Game - Sokoban
# GitHub Repo Link:
# https://github.com/IshanJ25/Steves-Pixels
# This project is BSD 2-Clause Licensed
# It is open-source under fair usage
# Kindly mention Authors' names if you copy this code
assets_folder = 'assets'
# !!! very important. all assets placed here
# leave empty string to disable if root folder
cheatcode = 'helpme'
# Don't tell anyone!
# leave empty string to disable
pygame.init()
screen_w = pygame.display.Info().current_w
screen_h = pygame.display.Info().current_h
color = {
'black': (50, 50, 50),
'blacker': (20, 20, 20),
'white': (240, 240, 240),
'gray': (150, 150, 150),
'red': (255, 50, 50),
'orange': (255, 150, 0),
'yellow': (255, 200, 0),
'green': (100, 200, 50),
'blue': (0, 220, 220),
'purple': (150, 100, 255),
}
image_dict = {'uncovered goal': pygame.image.load(f'{assets_folder}\\orange_pad.png'),
'covered goal': pygame.image.load(f'{assets_folder}\\green_pad.png'),
'pixel': pygame.image.load(f'{assets_folder}\\pixels.png'),
'wall': pygame.image.load(f'{assets_folder}\\wood.png'),
'inside floor': pygame.image.load(f'{assets_folder}\\sand.png'),
'outside floor': pygame.image.load(f'{assets_folder}\\grass.png'),
'title': pygame.image.load(f'{assets_folder}\\title.png'),
'solved': pygame.image.load(f'{assets_folder}\\solved.png'),
'p_front': pygame.image.load(f'{assets_folder}\\steve_front.png'),
'p_back': pygame.image.load(f'{assets_folder}\\steve_back.png'),
'p_left': pygame.image.load(f'{assets_folder}\\steve_left.png'),
'p_right': pygame.image.load(f'{assets_folder}\\steve_right.png'),
'rock': pygame.image.load(f'{assets_folder}\\rock.png'),
'tall tree': pygame.image.load(f'{assets_folder}\\bush.png'),
'cursor': pygame.image.load(f'{assets_folder}\\cursor.png'),
'cut_in': pygame.image.load(f'{assets_folder}\\start.png'),
'cut_out': pygame.image.load(f'{assets_folder}\\over.png'),
'creds': pygame.image.load(f'{assets_folder}\\credits.png')}
sound_dict = {'pause': f'{assets_folder}\\pause.mp3',
'resume': f'{assets_folder}\\resume.mp3',
'level_complete': f'{assets_folder}\\level.mp3',
'theme': f'{assets_folder}\\theme.mp3'}
##################################################
##################################################
############# EDITABLE PARAMETERS ###############
##################################################
##################################################
fullscreen = True
win_w = 1500
win_h = 800
# ignored if fullscreen
window_y_offset = 0.6
# offset y position of window by -60%
music_during_levels = True
tile_w = 50
tile_h = 85
tile_floor_height = 40
scale_map_int: int = 2
# choose among 1, 2, 3; 2 is optimal in fullscreen; Ignored if auto_scale is True
scale_map: int
auto_scale = True
title_transform = [[60, 75], 3]
# min and max percentage of title image wrt window size
# slowness wrt 60 fps
pause_blur = 25
pause_opacity = 150
# out of 255
lvl_complete_blur = 5
grass_decoration_percentage = 40
bg_color = color['black']
txt_color = color['white']
font_size = round(win_h / 25 * 0.75)
music_volume = [0.4, 0.08, 0.16]
# max, min, levels
if not music_during_levels:
music_volume[2] = 0
##################################################
##################################################
############# STANDARD DEFINITIONS ##############
##################################################
##################################################
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
fps_clock = pygame.time.Clock()
if not fullscreen:
screen = pygame.display.set_mode((win_w, win_h))
SetWindowPos(pygame.display.get_wm_info()['window'], -1,
round(screen_w / 2 - win_w / 2), round((screen_h / 2 - win_h / 2) * window_y_offset), 0, 0, 1)
else:
win_w = screen_w
win_h = screen_h
screen = pygame.display.set_mode((win_w, win_h), pygame.FULLSCREEN)
if not auto_scale:
scale_map = scale_map_int
else:
map_size_chunk = tile_w * tile_w * 100
window_area = win_w * win_h
tile_screen_percent = round(map_size_chunk / window_area * 100)
if tile_screen_percent <= 10:
scale_map = 3
elif tile_screen_percent <= 30:
scale_map = 2
else:
scale_map = 1
CAM_MOVE_SPEED = scale_map / 3
win_w_half = int(win_w / 2)
win_h_half = int(win_h / 2)
cursor_img = pygame.transform.scale(image_dict['cursor'], (32, 32))
pygame.mouse.set_visible(False)
pause_sound = pygame.mixer.Sound(sound_dict['pause'])
resume_sound = pygame.mixer.Sound(sound_dict['resume'])
level_sound = pygame.mixer.Sound(sound_dict['level_complete'])
music = pygame.mixer.music.load(sound_dict['theme'])
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(music_volume[0])
##################################################
##################################################
##################################################
def main():
global tile_mapping, grass_deco_mapping, basic_font, player_images, currentImage
global scale_map, tile_w, tile_h, tile_floor_height
tile_w *= scale_map
tile_h *= scale_map
tile_floor_height *= scale_map
pygame.display.set_caption('Steve\'s Pixels')
pygame.display.set_icon(image_dict['cursor'])
basic_font = pygame.font.Font(f'{assets_folder}\\AccordAlternate-Bold.ttf', font_size)
for i in image_dict:
if i not in ('title', 'solved', 'cursor', 'cut_in', 'cut_out', 'creds'):
image_dict[i] = pygame.transform.scale(image_dict[i], (tile_w, tile_h))
tile_mapping = {'#': image_dict['wall'],
'o': image_dict['inside floor'],
' ': image_dict['outside floor']}
grass_deco_mapping = {'0': image_dict['rock'],
'1': image_dict['tall tree']}
currentImage = 0
player_images = [image_dict['p_front'],
image_dict['p_back'],
image_dict['p_left'],
image_dict['p_right']]
startScreen()
cutscene('start')
levels = readLevelsFile()
currentLevelIndex = 0
while True:
result = runLevel(levels, currentLevelIndex)
if result in ('solved', 'next'):
currentLevelIndex += 1
if currentLevelIndex >= len(levels):
break
pygame.mixer.music.set_volume(music_volume[0])
cutscene('over')
creds()
def runLevel(levels, levelNum):
pygame.mixer.music.set_volume(music_volume[2])
global currentImage
levelObj = levels[levelNum]
mapObj = decorateMap(levelObj['mapObj'], levelObj['startState']['player'])
gameStateObj = copy.deepcopy(levelObj['startState'])
mapNeedsRedraw = True
levelSurf = basic_font.render(f'Level {levelNum + 1} of {len(levels)}', 1, txt_color)
levelRect = levelSurf.get_rect()
levelRect.bottomleft = (20, round(win_h - 2 * font_size * 4 / 3))
mapWidth = len(mapObj) * tile_w
mapHeight = (len(mapObj[0]) - 1) * tile_floor_height + tile_h
MAX_CAM_X_PAN = abs(win_h_half - int(mapHeight / 2)) + tile_w
MAX_CAM_Y_PAN = abs(win_w_half - int(mapWidth / 2)) + tile_h
levelIsComplete = False
cameraOffsetX = 0
cameraOffsetY = 0
cameraUp = False
cameraDown = False
cameraLeft = False
cameraRight = False
solved_shown = False
cheat_entered = ''
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z']
while True:
playerMoveTo = None
for event in pygame.event.get():
if event.type == QUIT:
pause_screen()
elif event.type == KEYDOWN:
current_key = pygame.key.name(event.key)
if current_key in alphabets and len(cheat_entered) < len(cheatcode):
if current_key == cheatcode[len(cheat_entered)]:
cheat_entered += current_key
else:
cheat_entered = ''
if solved_shown:
currentImage = 0
return 'solved'
if event.key == K_j:
cameraLeft = True
elif event.key == K_l:
cameraRight = True
elif event.key == K_i:
cameraUp = True
elif event.key == K_k:
cameraDown = True
elif event.key == K_a or event.key == K_LEFT:
currentImage = 2
playerMoveTo = LEFT
mapNeedsRedraw = True
elif event.key == K_d or event.key == K_RIGHT:
currentImage = 3
playerMoveTo = RIGHT
mapNeedsRedraw = True
elif event.key == K_w or event.key == K_UP:
currentImage = 1
playerMoveTo = UP
mapNeedsRedraw = True
elif event.key == K_s or event.key == K_DOWN:
currentImage = 0
playerMoveTo = DOWN
mapNeedsRedraw = True
elif event.key == K_ESCAPE:
pause_screen()
elif event.key == K_BACKSPACE:
currentImage = 0
mapNeedsRedraw = True
return 'reset'
elif event.type == KEYUP:
if event.key == K_j:
cameraLeft = False
elif event.key == K_l:
cameraRight = False
elif event.key == K_i:
cameraUp = False
elif event.key == K_k:
cameraDown = False
if playerMoveTo is not None and not levelIsComplete:
moved = makeMove(mapObj, gameStateObj, playerMoveTo)
if moved:
gameStateObj['stepCounter'] += 1
mapNeedsRedraw = True
if isLevelFinished(levelObj, gameStateObj):
levelIsComplete = True
screen.fill(bg_color)
if mapNeedsRedraw:
mapSurf = drawMap(mapObj, gameStateObj, levelObj['goals'])
mapNeedsRedraw = False
if cameraUp and cameraOffsetY < MAX_CAM_X_PAN:
cameraOffsetY += CAM_MOVE_SPEED
elif cameraDown and cameraOffsetY > -MAX_CAM_X_PAN:
cameraOffsetY -= CAM_MOVE_SPEED
if cameraLeft and cameraOffsetX < MAX_CAM_Y_PAN:
cameraOffsetX += CAM_MOVE_SPEED
elif cameraRight and cameraOffsetX > -MAX_CAM_Y_PAN:
cameraOffsetX -= CAM_MOVE_SPEED
mapSurfRect = mapSurf.get_rect()
mapSurfRect.center = (win_w_half + cameraOffsetX, win_h_half + cameraOffsetY)
screen.blit(mapSurf, mapSurfRect)
screen.blit(levelSurf, levelRect)
stepSurf = basic_font.render('Steps: %s' % (gameStateObj['stepCounter']), 1, txt_color)
stepRect = stepSurf.get_rect()
stepRect.bottomleft = (20, round(win_h - font_size * 4 / 3))
screen.blit(stepSurf, stepRect)
if cheat_entered == cheatcode != '':
levelIsComplete = True
if levelIsComplete:
old_w = image_dict['solved'].get_width()
old_h = image_dict['solved'].get_height()
new_w = round(win_w / 2)
new_h = round(old_h * new_w / old_w)
solved_img = pygame.transform.scale(image_dict['solved'], (new_w, new_h))
solvedRect = solved_img.get_rect()
solvedRect.x = round(win_w / 12)
solvedRect.y = round(win_h / 10)
solved_overlay = lvl_complete_blurry(solved_img, solvedRect)
if not solved_shown:
level_sound.play()
for i in range(0, 256, 4):
for event in pygame.event.get():
if event.type == QUIT:
pause_screen()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pause_screen()
currentImage = 0
return 'solved'
solved_overlay.set_alpha(i)
screen.blit(solved_overlay, (0, 0))
pygame.display.update()
fps_clock.tick(64)
solved_shown = True
else:
screen.blit(solved_overlay, (0, 0))
pygame.display.update()
fps_clock.tick()
def isWall(mapObj, x, y):
if x < 0 or x >= len(mapObj) or y < 0 or y >= len(mapObj[x]):
return False
elif mapObj[x][y] in ('#', 'x'):
return True
return False
def decorateMap(mapObj, start_xy):
start_x, start_y = start_xy
mapObjCopy = copy.deepcopy(mapObj)
for x in range(len(mapObjCopy)):
for y in range(len(mapObjCopy[0])):
if mapObjCopy[x][y] in ('$', '.', '@', '+', '*'):
mapObjCopy[x][y] = ' '
floodFill(mapObjCopy, start_x, start_y, ' ', 'o')
for x in range(len(mapObjCopy)):
for y in range(len(mapObjCopy[0])):
if mapObjCopy[x][y] == ' ' and random.randint(0, 99) < grass_decoration_percentage:
mapObjCopy[x][y] = random.choice(list(grass_deco_mapping.keys()))
return mapObjCopy
def isBlocked(mapObj, gameStateObj, x, y):
if isWall(mapObj, x, y):
return True
elif x < 0 or x >= len(mapObj) or y < 0 or y >= len(mapObj[x]):
return True
elif (x, y) in gameStateObj['pixels']:
return True
return False
def makeMove(mapObj, gameStateObj, playerMoveTo):
player_x, player_y = gameStateObj['player']
pixels = gameStateObj['pixels']
if playerMoveTo == UP:
xOffset = 0
yOffset = -1
elif playerMoveTo == RIGHT:
xOffset = 1
yOffset = 0
elif playerMoveTo == DOWN:
xOffset = 0
yOffset = 1
elif playerMoveTo == LEFT:
xOffset = -1
yOffset = 0
if isWall(mapObj, player_x + xOffset, player_y + yOffset):
return False
else:
if (player_x + xOffset, player_y + yOffset) in pixels:
if not isBlocked(mapObj, gameStateObj, player_x + (xOffset * 2), player_y + (yOffset * 2)):
ind = pixels.index((player_x + xOffset, player_y + yOffset))
pixels[ind] = (pixels[ind][0] + xOffset, pixels[ind][1] + yOffset)
else:
return False
gameStateObj['player'] = (player_x + xOffset, player_y + yOffset)
return True
def startScreen():
title_img = image_dict['title']
title_w = title_img.get_width()
title_h = title_img.get_height()
upper_limit = title_transform[0][1]
lower_limit = title_transform[0][0]
fps = 60
counter = title_transform[1]
counter_limit = 100
check_divisibilty_of = counter
title_image_size_factor = lower_limit
size_change_factor = +1
new_title_w = round(win_w * lower_limit / 100)
new_title_h = round(title_h * new_title_w / title_w)
title_x = round((win_w - new_title_w) / 2)
title_yy = round(win_h * 2 / 7)
instructionText = ['Push all the Pixel-Crystals in the portals!',
'',
'Movement: WASD/Arrow keys',
'Camera control: I-J-K-L',
'Exit game: Escape',
'Reset level: Backspace',
'Continue: Enter']
while True:
if counter % check_divisibilty_of == 0:
if title_image_size_factor <= lower_limit:
size_change_factor = +1
if title_image_size_factor >= upper_limit:
size_change_factor = -1
title_image_size_factor += size_change_factor
new_title_w = win_w * title_image_size_factor / 100
new_title_h = title_h * new_title_w / title_w
title_x = round((win_w - new_title_w) / 2)
title_y = title_yy - round(new_title_h / 2)
screen.fill(bg_color)
screen.blit(pygame.transform.scale(title_img, (new_title_w, new_title_h)),
(title_x, title_y - round(new_title_h / 2)))
topCoord = round(win_h * 4 / 9)
for i in range(len(instructionText)):
instSurf = basic_font.render(instructionText[i], 1, txt_color)
instRect = instSurf.get_rect()
topCoord += 10
instRect.top = topCoord
instRect.centerx = win_w_half
topCoord += instRect.height
screen.blit(instSurf, instRect)
cur_func()
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
elif event.key == K_RETURN:
return
fps_clock.tick(fps)
counter += 1
if counter > counter_limit:
counter = 2
def cutscene(state: str):
if state == 'start':
img = image_dict['cut_in']
elif state == 'over':
img = image_dict['cut_out']
else:
return
img_w_old = img.get_width()
img_h_old = img.get_height()
img_rect = img.get_rect()
img_rect.w = win_w
img_rect.h = round(img_h_old * img_rect.w / img_w_old, 2)
img_rect.x = 0
img_rect.y = round(win_h / 2 - img_rect.h / 2)
if win_w / win_h <= img_w_old / img_h_old:
img_rect.w = win_w
img_rect.h = round(img_h_old * img_rect.w / img_w_old, 2)
img_rect.x = 0
img_rect.y = round(win_h / 2 - img_rect.h / 2)
elif win_w / win_h > img_w_old / img_h_old:
img_rect.h = win_h
img_rect.w = round(img_w_old * img_rect.h / img_h_old)
img_rect.x = round(win_w / 2 - img_rect.w / 2)
img_rect.y = 0
alpha = 0
img = pygame.transform.scale(img, (img_rect.w, img_rect.h))
for alpha in range(0, 256, 8):
for event in pygame.event.get():
if event.type == QUIT:
pause_screen()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pause_screen()
else:
done = True
img.set_alpha(alpha)
pygame.draw.rect(screen, color['blacker'], pygame.Rect(0, 0, win_w, win_h))
screen.blit(img, img_rect)
pygame.display.update()
fps_clock.tick(32)
img.set_alpha(255)
done = False
while not done:
for event in pygame.event.get():
if event.type == QUIT:
pause_screen()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pause_screen()
else:
done = True
pygame.draw.rect(screen, color['blacker'], pygame.Rect(0, 0, win_w, win_h))
screen.blit(img, img_rect)
pygame.display.update()
fps_clock.tick(60)
return
def creds():
img = image_dict['creds']
img_w_old = img.get_width()
img_h_old = img.get_height()
img_rect = img.get_rect()
img_rect.w = win_w
img_rect.h = round(img_h_old * img_rect.w / img_w_old, 2)
img_rect.x = 0
img_rect.y = round(win_h / 2 - img_rect.h / 2)
if win_w / win_h <= img_w_old / img_h_old:
img_rect.w = win_w
img_rect.h = round(img_h_old * img_rect.w / img_w_old, 2)
img_rect.x = 0
img_rect.y = round(win_h / 2 - img_rect.h / 2)
elif win_w / win_h > img_w_old / img_h_old:
img_rect.h = win_h
img_rect.w = round(img_w_old * img_rect.h / img_h_old)
img_rect.x = round(win_w / 2 - img_rect.w / 2)
img_rect.y = 0
alpha = 0
img = pygame.transform.scale(img, (img_rect.w, img_rect.h))
for alpha in range(0, 256, 8):
for event in pygame.event.get():
if event.type == QUIT:
pass
img.set_alpha(alpha)
pygame.draw.rect(screen, color['blacker'], pygame.Rect(0, 0, win_w, win_h))
screen.blit(img, img_rect)
pygame.display.update()
fps_clock.tick(32)
img.set_alpha(255)
done = False
while not done:
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == KEYDOWN:
done = True
pygame.draw.rect(screen, color['blacker'], pygame.Rect(0, 0, win_w, win_h))
screen.blit(img, img_rect)
pygame.display.update()
fps_clock.tick(60)
return
def cur_func():
cur_x, cur_y = pygame.mouse.get_pos()
cur_rect = cursor_img.get_rect()
cur_rect.x = round(cur_x - cur_rect.w / 2)
cur_rect.y = round(cur_y - cur_rect.h / 2)
screen.blit(cursor_img, cur_rect)
def readLevelsFile():
# FORMAT:',
# @ - Steve
# $ - Pixel
# . - Goal
# # - Wall
# <space> - space
content = [
' ',
' ######',
' # $.#',
' #@ #',
' ######',
' ',
'',
' ',
' #########',
' # #',
' # . $ #',
' # @ #',
' # $ . #',
' # #',
' #########',
' ',
'',
'#########',
'# #',
'# ##### #',
'# # #',
'##### # #',
'# # #',
'# #### #',
' # @#.$ #',
' ### #',
' ####',
'',
' ',
' ####### ',
' #@# #',
' # # $ #',
' # ## ## ',
' ## .# ',
' ### # ',
' ## ',
' ',
'',
' ',
' ###########',
' # #',
' # . . . #',
' ## ##### ##',
' # $ $ $ #',
' # @ #',
' #########',
' ',
''
]
levels = []
levelNum = 0
mapTextLines = []
mapObj = []
for lineNum in range(len(content)):
line = content[lineNum].rstrip('\r\n')
if line != '':
mapTextLines.append(line)
elif line == '' and len(mapTextLines) > 0:
maxWidth = -1
for i in range(len(mapTextLines)):
if len(mapTextLines[i]) > maxWidth:
maxWidth = len(mapTextLines[i])
for i in range(len(mapTextLines)):
mapTextLines[i] += ' ' * (maxWidth - len(mapTextLines[i]))
for x in range(len(mapTextLines[0])):
mapObj.append([])
for y in range(len(mapTextLines)):
for x in range(maxWidth):
mapObj[x].append(mapTextLines[y][x])
start_x = None
start_y = None
goals = []
pixels = []
for x in range(maxWidth):
for y in range(len(mapObj[x])):
if mapObj[x][y] == '@':
start_x = x
start_y = y
if mapObj[x][y] == '.':
goals.append((x, y))
if mapObj[x][y] == '$':
pixels.append((x, y))
gameStateObj = {'player': (start_x, start_y),
'stepCounter': 0,
'pixels': pixels}
levelObj = {'width': maxWidth,
'height': len(mapObj),
'mapObj': mapObj,
'goals': goals,
'startState': gameStateObj}
levels.append(levelObj)
mapTextLines = []
mapObj = []
gameStateObj = {}
levelNum += 1
return levels
def floodFill(mapObj, x, y, oldCharacter, newCharacter):
if mapObj[x][y] == oldCharacter:
mapObj[x][y] = newCharacter
if x < len(mapObj) - 1 and mapObj[x + 1][y] == oldCharacter:
floodFill(mapObj, x + 1, y, oldCharacter, newCharacter)
if x > 0 and mapObj[x - 1][y] == oldCharacter:
floodFill(mapObj, x - 1, y, oldCharacter, newCharacter)
if y < len(mapObj[x]) - 1 and mapObj[x][y + 1] == oldCharacter:
floodFill(mapObj, x, y + 1, oldCharacter, newCharacter)
if y > 0 and mapObj[x][y - 1] == oldCharacter:
floodFill(mapObj, x, y - 1, oldCharacter, newCharacter)
def drawMap(mapObj, gameStateObj, goals):
mapSurfWidth = len(mapObj) * tile_w
mapSurfHeight = (len(mapObj[0]) - 1) * tile_floor_height + tile_h
mapSurf = pygame.Surface((mapSurfWidth, mapSurfHeight))
mapSurf.fill(bg_color)
for x in range(len(mapObj)):
for y in range(len(mapObj[x])):
spaceRect = pygame.Rect((x * tile_w, y * tile_floor_height, tile_w, tile_h))
if mapObj[x][y] in tile_mapping:
baseTile = tile_mapping[mapObj[x][y]]
elif mapObj[x][y] in grass_deco_mapping:
baseTile = tile_mapping[' ']
mapSurf.blit(baseTile, spaceRect)
if mapObj[x][y] in grass_deco_mapping:
mapSurf.blit(grass_deco_mapping[mapObj[x][y]], spaceRect)
elif (x, y) in gameStateObj['pixels']:
if (x, y) in goals:
mapSurf.blit(image_dict['covered goal'], spaceRect)
mapSurf.blit(image_dict['pixel'], spaceRect)
elif (x, y) in goals:
mapSurf.blit(image_dict['uncovered goal'], spaceRect)
if (x, y) == gameStateObj['player']:
mapSurf.blit(player_images[currentImage], spaceRect)
return mapSurf
def isLevelFinished(levelObj, gameStateObj):
for goal in levelObj['goals']:
if goal not in gameStateObj['pixels']:
return False
return True
def pause_screen():
exit_lines = [
'PAUSE MENU',
'',
'ESCAPE to resume game',
'ENTER to exit the game'
]
blurry = generate_blurry()
done = False
pygame.mixer.music.set_volume(music_volume[1])
pause_sound.play()
while not done:
screen.blit(blurry, (0, 0))
topCoord = round(win_h * 0.25)
for i in range(len(exit_lines)):
instSurf = basic_font.render(exit_lines[i], 1, txt_color)
instRect = instSurf.get_rect()
topCoord += round(font_size * 2 / 3)
instRect.top = topCoord
instRect.centerx = win_w_half
topCoord += instRect.height
screen.blit(instSurf, instRect)
cur_func()
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
done = True
elif event.key == K_RETURN:
terminate()
fps_clock.tick(60)
resume_sound.play()
pygame.mixer.music.set_volume(music_volume[0])
return
def generate_blurry():
rect = pygame.Rect(0, 0, win_w, win_h)
sub = screen.subsurface(rect)
screenshot = pygame.Surface((win_w, win_h))
screenshot.blit(sub, (0, 0))
pil_string_image = pygame.image.tostring(screenshot, "RGBA", False)
pil_image = Image.frombytes("RGBA", (win_w, win_h), pil_string_image)
pil_image = pil_image.filter(ImageFilter.GaussianBlur(radius=pause_blur))
overlay = Image.new('RGBA', (win_w, win_h), color['black'])
overlay.putalpha(pause_opacity)
pil_image = Image.alpha_composite(pil_image, overlay)
return pygame.image.fromstring(pil_image.tobytes(), pil_image.size, 'RGBA')
def lvl_complete_blurry(img, img_rect):
rect = pygame.Rect(0, 0, win_w, win_h)
sub = screen.subsurface(rect)
screenshot = pygame.Surface((win_w, win_h))
screenshot.blit(sub, (0, 0))
pil_string_image = pygame.image.tostring(screenshot, "RGBA", False)
pil_image = Image.frombytes("RGBA", (win_w, win_h), pil_string_image)
pil_image = pil_image.filter(ImageFilter.GaussianBlur(radius=lvl_complete_blur))
glass = pygame.image.fromstring(pil_image.tobytes(), pil_image.size, 'RGBA')
glass.blit(img, img_rect)
return glass
def terminate():