-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
executable file
·1955 lines (1659 loc) · 58.5 KB
/
editor.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
#! /usr/bin/python
# -*- coding: iso-8859-1 -*-
"""
Copyright (C) 2003 John-Paul Gignac (Game)
(C) 2004 Joe Wreschnig (Game)
(C) 2016 Nina Ripoll (Editor)
(C) 2021 flowerbug
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
# Import Modules
import os, pygame, random, time, math, re, sys, getpass, hashlib
from shutil import copyfile
from pygame.locals import *
# Parse the command line
screenshot = 0
fullscreen = 0
colorblind = 0
sound_on = 1
music_on = 1
music_pending_song = 0
for arg in sys.argv[1:]:
if arg == '-s':
screenshot = 1
elif arg == '-f':
fullscreen = 1
elif arg == '-cb':
colorblind = 1
elif arg == '-q':
sound_on = 0
music_on = 0
elif arg[0] == '-':
print("Usage: "+sys.argv[0]+" [-cb] [-f] [-s]\n")
sys.exit(1)
else:
print("Usage: "+sys.argv[0]+" [-cb] [-f] [-s]\n")
sys.exit(1)
if colorblind:
cbext = '-cb'
else:
cbext = ''
# Game constants
wheel_steps = 9
frames_per_sec = 100
timer_width = 36
timer_margin = 4
info_height = 20
# Volume levels
intro_music_volume = 0.1
ingame_music_volume = 0.1
sound_effects_volume = 0.5
# Changing these may affect the playability of levels
default_colors = (2,3,4,6) # Blue, Green, Yellow, Red
default_stoplight = (6,4,3) # Red, Yellow, Green
# Don't change these constants unless you
# redo all of the levels
horiz_tiles = 8
vert_tiles = 6
# Don't change these constants unless you
# update the graphics files correspondingly.
screen_width = 1000
screen_height = 800
marble_size = 28
tile_size = 92
wheel_margin = 4
stoplight_marble_size = 28
# The positions of the holes in the wheels in
# each of the three rotational positions
holecenter_radius = (tile_size - marble_size) / 2 - wheel_margin
holecenters = []
for i in range(wheel_steps):
theta = math.pi * i / (2 * wheel_steps)
c = math.floor( 0.5 + math.cos(theta)*holecenter_radius)
s = math.floor( 0.5 + math.sin(theta)*holecenter_radius)
holecenters.append((
(tile_size//2 + s, tile_size//2 - c),
(tile_size//2 + c, tile_size//2 + s),
(tile_size//2 - s, tile_size//2 + c),
(tile_size//2 - c, tile_size//2 - s)))
# Directions/tiles references
tilesSymbols = {'Tile':'','Wheel':'O','Painter':'&','Filter':'#','Buffer':'@',\
'Replicator':'*','Shredder':'X', 'Teleporter':'=','Stoplight':'!','Trigger':'%'}
directionsSymbols = ['^','>','v','<']
# More global variables
board_width = horiz_tiles * tile_size
board_height = vert_tiles * tile_size
launch_timer_pos = (0,info_height)
board_pos = (timer_width, info_height + marble_size)
timer_height = board_height + marble_size
music_loaded = 0
# Levelset variables
levelset = 'all-boards'
levelsetFolder = 'circuits'
customsSetsFiles = ['Default']
customsSetsFiles += [f for f in os.listdir('user_circuits') \
if os.path.isfile(os.path.join('user_circuits', f)) and '~' not in f]
# Functions to create our resourcesxws
def load_image(name, colorkey=-1, size=None):
fullname = os.path.join('graphics', name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Cannot load image:', fullname)
raise SystemExit(message)
if size is not None:
image = pygame.transform.scale( image, size)
image = image.convert()
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image
def load_sound(name, volume=1.0):
class NoneSound:
def play(self): pass
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join('sounds', name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error as message:
print('Cannot load sound:', fullname)
return NoneSound()
sound.set_volume( volume * sound_effects_volume)
return sound
def play_sound(sound):
if sound_on: sound.play()
def start_music(name, volume=-1):
global music_pending_song, music_loaded, music_volume
music_volume = volume
if not music_on:
music_pending_song = name
return
if not pygame.mixer or not pygame.mixer.music:
print("Background music not available.")
return
pygame.mixer.music.stop()
fullname = os.path.join('music', name)
try:
pygame.mixer.music.load(fullname)
except pygame.error as message:
print('Cannot load music:', fullname)
return
music_loaded = 1
pygame.mixer.music.play(-1)
if music_volume >= 0:
pygame.mixer.music.set_volume( music_volume)
music_pending_song = 0
def toggle_fullscreen():
global fullscreen
if pygame.display.toggle_fullscreen():
fullscreen = fullscreen ^ 1
return 1
else:
return 0
def toggle_sound():
global sound_on
sound_on = sound_on ^ 1
def toggle_music():
global music_pending_song, music_on
music_on = music_on ^ 1
if music_on:
if music_pending_song:
start_music( music_pending_song)
elif music_loaded:
pygame.mixer.music.unpause()
elif music_loaded:
if not music_pending_song:
pygame.mixer.music.pause()
def setLevelset():
global levelset, levelsetFolder
levelset = customsSetsFiles[IntroScreen.start_levelset]
if levelset == 'Default':
levelset = 'all-boards'
levelsetFolder = 'circuits'
else: levelsetFolder = 'user_circuits'
IntroScreen.start_level = 0
def countLevels(customSet=False):
if customSet==True: circuit = ('user_circuits','Custom')
else: circuit = (levelsetFolder,levelset)
fullname = os.path.join(circuit[0], circuit[1])
f = open( fullname)
j=0
while 1:
line = f.readline()
if line == '': break
if line[0] == '|': j += 1
f.close()
numlevels = j / vert_tiles
return numlevels
# A better tick function
next_frame = pygame.time.get_ticks()
def my_tick( frames_per_sec):
global next_frame
# Wait for the next frame
next_frame += 1000.0 / frames_per_sec
now = pygame.time.get_ticks()
if next_frame < now:
# No time to wait - just hide our mistake
# and keep going as fast as we can.
next_frame = now
else:
pygame.time.wait( int(next_frame) - now)
# Load the sounds
def load_sounds():
global filter_admit,wheel_turn,wheel_completed,change_color
global direct_marble,ping,trigger_setup,teleport,marble_release
global levelfinish,die,incorrect,switch,shredder,replicator
global extra_life,menu_scroll,menu_select
filter_admit = load_sound('filter_admit.wav', 0.8)
wheel_turn = load_sound('wheel_turn.wav', 0.8)
wheel_completed = load_sound('wheel_completed.wav', 0.7)
change_color = load_sound('change_color.wav', 0.8)
direct_marble = load_sound('direct_marble.wav', 0.6)
ping = load_sound('ping.wav', 0.8)
trigger_setup = load_sound('trigger_setup.wav')
teleport = load_sound('teleport.wav', 0.6)
marble_release = load_sound('marble_release.wav', 0.5)
levelfinish = load_sound('levelfinish.wav', 0.6)
die = load_sound('die.wav')
incorrect = load_sound('incorrect.wav', 0.15)
switch = load_sound('switch.wav')
shredder = load_sound('shredder.wav')
replicator = load_sound('replicator.wav')
extra_life = load_sound('extra_life.wav')
menu_scroll = load_sound('menu_scroll.wav', 0.8)
menu_select = load_sound('switch.wav')
# Load the fonts for various parts of the game
def load_fonts():
global launch_timer_font,active_marbles_font,popup_font,info_font
launch_timer_font = pygame.font.Font(None, timer_width - 2*timer_margin)
active_marbles_font = pygame.font.Font(None, marble_size)
popup_font = pygame.font.Font(None, 24)
info_font = pygame.font.Font(None, info_height)
# Load all of the images for the various game classes.
# The images are stored as class variables in the corresponding classes.
def load_images():
Marble.images = []
for i in range(9):
Marble.images.append( load_image('marble-'+repr(i)+cbext+'.png', -1,
(marble_size, marble_size)))
Marble.crossImg = load_image('cross.png', 0, (marble_size,marble_size))
Marble.onPathImg = load_image('marblePath.png', -1, (tile_size-6,tile_size-6))
Tile.plain_tiles = []
Tile.tunnels = []
for i in range(16):
tile = load_image('tile.png', (206,53,53), (tile_size,tile_size))
path = load_image('path-'+repr(i)+'.png', -1, (tile_size,tile_size))
tile.blit( path, (0,0))
Tile.plain_tiles.append( tile)
Tile.tunnels.append(load_image('tunnel-'+repr(i)+'.png',
-1,(tile_size,tile_size)))
Tile.paths = 0
Tile.image = load_image('tile.png', (206,53,53), (tile_size,tile_size))
Tile.image.blit(load_image('blank-bg-tile.png',-1,(tile_size,tile_size)), (0,0))
Tile.imageSmall = load_image('blank-bg-tile.png',-1,(tile_size-6,tile_size-6))
Wheel.image = load_image('wheel.png',-1,(tile_size,tile_size))
Buffer.bottom = load_image('buffer.png',-1,(tile_size,tile_size))
Buffer.top = load_image('buffer-top.png',-1,(tile_size,tile_size))
Buffer.selectedImg = [load_image('buffer-empty.jpg',-1,(38,38)), \
load_image('buffer-full.jpg',-1,(38,38))]
Painter.images = []
for i in range(8):
Painter.images.append( load_image('painter-'+repr(i)+cbext+'.png', -1,
(tile_size,tile_size)))
Filter.images = []
for i in range(8):
Filter.images.append( load_image('filter-'+repr(i)+cbext+'.png', -1,
(tile_size,tile_size)))
Director.images = (
load_image('director-0.png',-1,(tile_size,tile_size)),
load_image('director-1.png',-1,(tile_size,tile_size)),
load_image('director-2.png',-1,(tile_size,tile_size)),
load_image('director-3.png',-1,(tile_size,tile_size)),
)
Shredder.image = load_image('shredder.png',-1,(tile_size,tile_size))
Switch.images = []
for i in range(4):
Switch.images.append( [])
for j in range(4):
if i == j: Switch.images[i].append( None)
else: Switch.images[i].append( load_image(
'switch-'+repr(i)+repr(j)+'.png',-1,(tile_size,tile_size)))
Switch.directionsImages = [Switch.images[0][1],Switch.images[1][0], \
Switch.images[2][1],Switch.images[3][2]]
Replicator.image = load_image('replicator.png',-1,(tile_size,tile_size))
Replicator.selectedImg = {2:load_image('replicatorX2.jpg',-1,(38,38)), \
4:load_image('replicatorX4.jpg',-1,(38,38))}
Teleporter.image_h = load_image('teleporter-h.png',-1,(tile_size,tile_size))
Teleporter.image_v = load_image('teleporter-v.png',-1,(tile_size,tile_size))
Trigger.image = load_image('trigger.png',-1,(tile_size,tile_size))
Stoplight.image = load_image('stoplight.png',-1,(tile_size,tile_size))
Stoplight.smallmarbles = []
for im in Marble.images:
Stoplight.smallmarbles.append( pygame.transform.scale(im,
(stoplight_marble_size,stoplight_marble_size)))
Board.toolOptionsImg = load_image('options'+cbext+'.jpg', None, (736,200))
Board.saveIcon = load_image('save-icon.jpg', -1, (34,34))
Board.exitIcon = load_image('exit.png', -1, (40,40))
IntroScreen.background = load_image('introEditor.png', None,
(screen_width, screen_height))
IntroScreen.menu_font = pygame.font.Font(
None, IntroScreen.menu_font_height)
IntroScreen.scroller_font = pygame.font.Font(
None, IntroScreen.scroller_font_height)
# Function to set the video mode
def set_video_mode():
global screen
icon = pygame.image.load(os.path.join('graphics','icon.png'))
icon.set_colorkey(icon.get_at((0,0)), RLEACCEL)
pygame.display.set_icon(icon) # Needed both before and after set_mode
screen = pygame.display.set_mode( (screen_width, screen_height),
fullscreen * FULLSCREEN)
pygame.display.set_icon(icon) # Needed both before and after set_mode
pygame.display.set_caption('Pathological')
# Classes for our game objects
class Marble:
def __init__(self, color, center, direction, tilePos):
self.color = color
self.rect = pygame.Rect((0,0,marble_size,marble_size))
self.rect.center = center
self.direction = direction
self.tilePos = tilePos
def update(self, board): pass
def draw(self, screen):
screen.blit( self.images[self.color], self.rect.topleft)
class Tile:
def __init__(self, paths=0, center=None):
self.paths = paths
self.empty = 0
if center is None:
center = (0,0)
self.center = center
self.rect = pygame.Rect((0,0,tile_size,tile_size))
self.rect.center = center
self.drawn = 0
def draw_back(self, surface):
if self.empty==1:
surface.blit( self.image, self.rect.topleft)
self.empty = 0
if self.drawn: return 0
surface.blit( self.plain_tiles[self.paths], self.rect.topleft)
self.drawn = 1
return 1
def update(self, board): pass
def draw_fore(self, surface): return 0
def click(self, board, posx, posy, tile_x, tile_y): pass
class Wheel(Tile):
def __init__(self, paths, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
def draw_back(self, surface):
if self.drawn: return 0
Tile.draw_back(self, surface)
surface.blit( self.image, self.rect.topleft)
return 1
def update(self, board): pass
def draw_fore(self, surface): return 0
def click(self, board, posx, posy, tile_x, tile_y): pass
class Buffer(Tile):
def __init__(self, paths, color=-1):
Tile.__init__(self, paths) # Call base class intializer
self.marble = color
self.entering = None
def draw_back(self, surface):
if self.drawn: return 0
Tile.draw_back(self, surface)
color = self.marble
if color >= 0:
holecenter = self.rect.center
surface.blit( Marble.images[color],
(holecenter[0]-marble_size//2,
holecenter[1]-marble_size//2))
else:
surface.blit( self.bottom, self.rect.topleft)
return 1
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.top, self.rect.topleft)
return 0
class Painter(Tile):
def __init__(self, paths, color, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
self.color = color
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.images[self.color], self.rect.topleft)
return 0
class Filter(Tile):
def __init__(self, paths, color, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
self.color = color
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.images[self.color], self.rect.topleft)
return 0
class Director(Tile):
def __init__(self, paths, direction, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
self.direction = direction
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.images[self.direction], self.rect.topleft)
return 0
class Shredder(Tile):
def __init__(self, paths, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.image, self.rect.topleft)
return 0
class Switch(Tile):
def __init__(self, paths, dir1, dir2, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
self.curdir = dir1
self.otherdir = dir2
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.images[self.curdir][self.otherdir],
self.rect.topleft)
return 0
class Replicator(Tile):
def __init__(self, paths, count, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
self.count = count
self.pending = []
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.image, self.rect.topleft)
textLabel= info_font.render( 'x'+str(self.count), 1, (0,0,0))
surface.blit( textLabel, (self.rect.left+40,self.rect.top+52))
return 0
def update(self, board): pass
class Teleporter(Tile):
def __init__(self, paths, other=None, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
if paths & 5: self.image = self.image_v
else: self.image = self.image_h
self.labelDrawn = False
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.image, self.rect.topleft)
if not self.labelDrawn:
textLabel= info_font.render( str(self.label), 1, (0,0,0))
screen.blit( textLabel, (self.rect.left+4,self.rect.top+2))
self.labelDrawn = True
return 0
class Trigger(Tile):
def __init__(self, colors, center=None):
Tile.__init__(self, 0, center) # Call base class intializer
self.marbles = None
self._setup( colors)
def _setup(self, colors):
self.countdown = 0
self.marbles = [
random.choice(colors),
random.choice(colors),
random.choice(colors),
random.choice(colors),
]
self.drawn = 0
def update(self, board): pass
def draw_back(self, surface):
if self.drawn: return 0
Tile.draw_back(self, surface)
surface.blit( self.image, self.rect.topleft)
if self.marbles is not None:
for i in range(4):
surface.blit( Marble.images[self.marbles[i]],
(holecenters[0][i][0]+self.rect.left-marble_size//2,
holecenters[0][i][1]+self.rect.top-marble_size//2))
return 1
class Stoplight(Tile):
def __init__(self, colors, center=None):
Tile.__init__(self, 0, center) # Call base class intializer
self.marbles = list(colors)
self.current = 0
def draw_back(self, surface):
if self.drawn: return 0
Tile.draw_back(self, surface)
surface.blit( self.image, self.rect.topleft)
for i in range(self.current,3):
surface.blit( self.smallmarbles[self.marbles[i]],
(self.rect.centerx-14,
self.rect.top+3+(29*i)))
return 1
class Board:
def __init__(self, game, pos):
self.game = game
self.level = game.level
self.savedFromDefaultSet = False
self.pos = pos
self.marbles = []
self.screen = game.screen
self.trigger = None
self.stoplight = None
self.quitPopup = 0
self.warningPopup = []
self.name = "Unnamed"
self.boardTimer = 600
self.launchTimer = 6
self.author = getpass.getuser().title()
self.live_marbles_limit = 10
self.colors = default_colors
self.StoplightColors = default_stoplight
self.levelConfig_drawn = 0
self.tools_drawn = 0
self.selectedOptions_drawn = 0
# Tools / toolTiles positions
self.toolsList = {(8,0):'Wheel',(9,0):'Tile',(8,1):'Painter',(9,1):'Filter', \
(8,2):'Buffer',(9,2):'Teleporter', (8,3):'Switch',(9,3):'Replicator', \
(8,4):'Director',(9,4):'Shredder',(8,5):'Trigger',(9,5):'Stoplight', \
(8,6):'Tile', (9,6):'Marble'}
# Tools images
self.toolsImages = {'Wheel':((0,0),Wheel.image), 'Painter':((0,1),Painter.images[0]), \
'Filter':((1,1),Filter.images[0]), 'Buffer':((0,2),Buffer.top), \
'Teleporter':((1,2),Teleporter.image_v), 'Switch':((0,3),Switch.images[0][2]), \
'Replicator':((1,3),Replicator.image), 'Director':((0,4),Director.images[0]), \
'Shredder':((1,4),Shredder.image), 'Stoplight':((1,5),Stoplight.image),\
'Trigger':((0,5),Trigger.image), 'Path':((1,0),Tile.plain_tiles[5]), \
'Tile':((0,6),Tile.imageSmall), 'Marble':((1,6),Marble.onPathImg)}
# Option Tiles positions
self.colorOptionsPos = {(0,0):6,(1,0):4,(0,1):3,(1,1):2,(0,2):5,(1,2):7,(0,3):1,(1,3):0}
self.pathOptionsPos = {(2,0):1,(2,1):4,(2,2):8,(2,3):2,(3,0):5,(3,1):10,(3,2):3,(3,3):9, \
(4,0):6,(4,1):12,(4,2):15,(4,3):11,(5,0):14,(5,1):7,(5,2):13,(5,3):0}
self.tool = 'Wheel'
self.toolPath = 5
self.toolColor = 0
self.toolBuffer = 0
self.toolSwitchDirector = 0
self.toolSwitchDirection = 3
self.toolReplicatorFactor = 2
self.toolTeleporters = {}
self.toolTeleporterLabel = 0
# Create the board array
self.tiles = []
for j in range( vert_tiles):
row = list(range( horiz_tiles))
self.tiles.append( row)
# Load the level
self._load( game.circuit, self.level)
# Create The Background
self.background = pygame.Surface(screen.get_size()).convert()
self.background.fill((200, 200, 200)) # Color of Info Bar
# Draw the Backdrop
backdrop = load_image('backdrop.jpg', None,
(horiz_tiles * tile_size, vert_tiles * tile_size))
self.background.blit( backdrop, board_pos);
# Draw options box + save / exit icons
self.background.blit( self.toolOptionsImg, \
(board_pos[0], tile_size*7-info_height*2))
self.background.blit( self.saveIcon, \
(board_pos[0]+tile_size*9+45, tile_size*8-info_height*2+55))
self.background.blit( self.exitIcon, \
(board_pos[0]+tile_size*9+85, tile_size*8-info_height*2+55))
# Initialize the screen
screen.blit(self.background, (0, 0))
if self.StoplightColors != default_stoplight:
i=0
for color in self.StoplightColors:
self.toolColor = color
self.setStoplightColor(i)
i+=1
self.toolColor = 0
def draw_tools(self, dirty_rects):
if self.tools_drawn == 1: return
# Prepare
toolsSurface = pygame.Surface((190,650)).convert()
toolsSurface.fill((200, 200, 200))
screen.blit(toolsSurface, (772, 45))
selectedSurface = pygame.Surface((tile_size,tile_size)).convert()
selectedSurface.fill((100, 200, 200))
for tool,coord_img in list(self.toolsImages.items()):
coordX,coordY=coord_img[0]
posImgX = board_pos[0]+(horiz_tiles+coordX)*tile_size
posImgY = board_pos[1]+tile_size*coordY
coordSelect = (posImgX,posImgY)
if tool == 'Tile' or tool == 'Marble':
posImgX+=3
posImgY+=3
img=coord_img[1]
# Selector
if (self.tool=='Tile'==tool and self.toolPath==0) or \
(self.tool=='Tile' and tool=='Path' and self.toolPath!=0) or \
(tool == self.tool and self.tool!='Tile'):
screen.blit(selectedSurface,coordSelect)
# Draw tool
screen.blit(img,(posImgX,posImgY))
if tool == 'Buffer': screen.blit(Buffer.bottom,(posImgX,posImgY))
updateZone = Rect(770, 45, 190, 650)
dirty_rects.append(updateZone)
self.tools_drawn = 1
def draw_selectedOptions(self, dirty_rects):
if self.selectedOptions_drawn == 1: return
bg = pygame.Surface((150,170))
bg.fill((200, 200, 200))
screen.blit(bg, (600,625))
screen.blit( Marble.images[self.toolColor], (607,630))
screen.blit( Tile.plain_tiles[self.toolPath], (653,626))
screen.blit( Director.images[self.toolSwitchDirector], (630,702))
screen.blit( Switch.directionsImages[self.toolSwitchDirection], (680,702))
screen.blit( Replicator.selectedImg[self.toolReplicatorFactor], (603,665))
screen.blit( Buffer.selectedImg[self.toolBuffer], (603,715))
updateZone = Rect(600, 625, 150, 170)
dirty_rects.append(updateZone)
self.selectedOptions_drawn = 1
def draw_levelConfig(self, dirty_rects):
if self.levelConfig_drawn == 1: return
bg = pygame.Surface((screen_width,45))
bg.fill((200, 200, 200))
if self.level == -1: board_name = "New level - " + self.name
else: board_name = repr(self.level+1) + " - " + self.name
if self.level != game.level and levelset != 'Custom':
levelsetDisplay = levelset
if levelset == 'all-boards': levelsetDisplay = 'Default'
board_name += " (from "+levelsetDisplay+" / level "+repr(game.level+1)+")"
textName = info_font.render( board_name, 1, (0,0,0))
rect = textName.get_rect()
rect.top = 10
rect.left = timer_width
bg.blit( textName, rect)
board_timer = "Board timer: "+str(self.boardTimer)+ "s"
textBoardTimer = info_font.render( board_timer, 1, (0,0,0))
rect = textBoardTimer.get_rect()
rect.top = 30
rect.left = timer_width
bg.blit( textBoardTimer, rect)
launch_timer = "Launch timer (passes): "+str(self.launchTimer)
textLaunchTimer = info_font.render( launch_timer, 1, (0,0,0))
rect = textLaunchTimer.get_rect()
rect.top = 30
rect.left = timer_width + 140
bg.blit( textLaunchTimer, rect)
maxMarbles = "Max marbles: "+str(self.live_marbles_limit)
textMaxMarbles = info_font.render( maxMarbles, 1, (0,0,0))
rect = textMaxMarbles.get_rect()
rect.top = 30
rect.left = timer_width + 320
bg.blit( textMaxMarbles, rect)
levelColors = "Level colors"
textlevelColors = info_font.render( levelColors, 1, (0,0,0))
rect = textlevelColors.get_rect()
rect.top = 2
rect.left = timer_width + 545
bg.blit( textlevelColors, rect)
i=timer_width + 450
for color in range(9):
if color in self.colors:
bg.blit(Marble.images[color],(i,17))
else:
bg.blit(Marble.images[color],(i,17))
bg.blit(Marble.crossImg,(i,17))
i+=30
author = "Author: "+self.author
textAuthor = info_font.render( author, 1, (0,0,0))
rect = textAuthor.get_rect()
rect.top = 10
rect.right = screen_width - timer_width
bg.blit( textAuthor, rect)
self.screen.set_clip()
self.screen.blit( bg, (0,0))
updateZone = Rect(0, 0, 1000, 45)
dirty_rects.append(updateZone)
self.levelConfig_drawn = 1
def draw_back(self, dirty_rects):
for row in self.tiles:
for tile in row:
if tile.draw_back( self.background):
self.screen.set_clip( tile.rect)
self.screen.blit( self.background, (0,0))
self.screen.set_clip()
dirty_rects.append( tile.rect)
def draw_fore(self, dirty_rects):
for row in self.tiles:
for tile in row:
if tile.draw_fore(self.screen):
dirty_rects.append( tile.rect)
def update(self):
# Create the list of dirty rectangles
dirty_rects = []
# Draw the tools
self.draw_tools( dirty_rects)
# Draw selected tools options
self.draw_selectedOptions( dirty_rects)
# Draw level configuration
self.draw_levelConfig( dirty_rects)
# Draw the background
self.draw_back( dirty_rects)
# Draw all of the marbles
for marble in self.marbles:
marble.draw( self.screen)
dirty_rects.append( marble.rect)
textColor = (0,0,0)
if marble.color == 0 or marble.color == 2: textColor = (255,255,255)
dirSymbol = directionsSymbols[marble.direction]
textDirection= info_font.render( dirSymbol, 1, textColor)
txtX = marble.rect.center[0] - 3
txtY = marble.rect.center[1] - 8
if colorblind: txtY += 9
self.screen.blit( textDirection, (txtX,txtY))
# Draw the foreground
self.draw_fore( dirty_rects)
# Flip the display
pygame.display.update( dirty_rects)
def set_tile(self, x, y, tile, emptyTile=False):
self.background = pygame.Surface(screen.get_size()).convert()
self.background.blit( Tile.image, \
(timer_width+tile_size*x,info_height + marble_size + tile_size*y))
# set tile
self.tiles[y][x] = tile
tile.rect.left = self.pos[0] + tile_size * x
tile.rect.top = self.pos[1] + tile_size * y
tile.x = x
tile.y = y
if emptyTile == True: tile.empty=1
# If it's a trigger, keep track of it
if isinstance( tile, Trigger):
self.trigger = tile
# If it's a stoplight, keep track of it
if isinstance( tile, Stoplight):
self.stoplight = tile
def setLevelConfig(self,title,attr):
chooseCfg = popup(title+":\n\n\nPress enter", (300, 180))
cfg = get_name(self.screen, popup_font,((screen_width-250)/2,310,250, \
popup_font.get_height()), (255,255,255), (0,0,0))
# Check if int for board timer, launch timer and max marbles
if(attr == 'boardTimer' or attr == 'launchTimer' \
or attr == 'live_marbles_limit') and cfg.isdigit() == False:
popdown(chooseCfg)
play_sound(filter_admit)
self.warning("WARNING\nMust be a number")
return
if cfg: setattr(self,attr,cfg)
popdown(chooseCfg)
self.levelConfig_drawn = 0
def setStoplightColor(self,pos):
temp = list(self.StoplightColors)
temp[pos] = self.toolColor
self.StoplightColors = tuple(temp)
if pos == 0: coord = (484,629)
elif pos == 1: coord = (484,670)
elif pos == 2: coord = (484,710)
screen.blit( Marble.images[self.toolColor], coord)
updateZone = Rect(485, 630, 40, 110)
pygame.display.update(updateZone)
def click(self, pos):
play_sound(menu_select)
# Determine where the pointer is
tile_x = (pos[0] - self.pos[0]) // tile_size
tile_y = (pos[1] - self.pos[1]) // tile_size
tile_xr = pos[0] - self.pos[0] - tile_x * tile_size
tile_yr = pos[1] - self.pos[1] - tile_y * tile_size
# Click on a tile
if tile_x >= 0 and tile_x < horiz_tiles and \
tile_y >= 0 and tile_y < vert_tiles:
tile = self.tiles[tile_y][tile_x]
# Trigger / stoplights special cases
if isinstance( tile, Stoplight): self.stoplight = None
if isinstance( tile, Trigger): self.trigger = None
if self.tool == 'Stoplight':
if (self.stoplight is not None):
play_sound(filter_admit)
self.warning("WARNING\nOnly one stoplight per level")
return
elif self.tool == 'Trigger':
if (self.trigger is not None):
play_sound(filter_admit)
self.warning("WARNING\nOnly one trigger per level")
return
# Remove potential marble
if self.tool != 'Marble':
for marble in self.marbles:
if marble.tilePos == (tile_x,tile_y): self.marbles.remove(marble)
# Teleporter special cases
if isinstance( tile, Teleporter):
# Don't put a teleporter on another one
if self.tool == 'Teleporter':
play_sound(filter_admit)
self.warning("WARNING\nCan't add teleporter on another one\nDelete it first")
return
# Remove teleporter(s) in list
else:
teleportersCoords = list((k) for k, v \
in list(self.toolTeleporters.items()) if v == tile.label)
for coord in teleportersCoords:
self.toolTeleporters.pop(coord, None)
# Remove other teleporter tile
if coord != (tile_x,tile_y):
otherTeleporter = Tile()
self.set_tile(coord[0],coord[1],otherTeleporter, True)
# Create new tile
constructor = globals()[self.tool]
if self.tool == 'Painter' or self.tool == 'Filter':
tile = constructor(self.toolPath,self.toolColor)
elif self.tool == 'Replicator':
tile = constructor(self.toolPath,self.toolReplicatorFactor)
elif self.tool == 'Director':
tile = constructor(self.toolPath,self.toolSwitchDirector)
elif self.tool == 'Switch':
if self.toolSwitchDirector == self.toolSwitchDirection:
play_sound(filter_admit)
self.warning("WARNING\nSwitcher's 1st and 2nd direction can't be identical")
return
tile = constructor(self.toolPath,self.toolSwitchDirector,self.toolSwitchDirection)
elif self.tool == 'Trigger': tile = constructor(self.colors)
elif self.tool == 'Stoplight': tile = constructor(self.StoplightColors)
elif self.tool == 'Buffer' and self.toolBuffer == 1:
tile = constructor(self.toolPath,self.toolColor)
# Teleporter special case
elif self.tool == 'Teleporter':
tile = constructor(self.toolPath)
tile.label = self.toolTeleporterLabel
teleportersNum = len(self.toolTeleporters)
# Create new teleporter
if (teleportersNum%2 == 0):
self.toolTeleporters[(tile_x,tile_y)] = self.toolTeleporterLabel
# Create 2nd teleporter
else:
self.toolTeleporters[(tile_x,tile_y)] = self.toolTeleporterLabel
self.toolTeleporterLabel += 1
elif self.tool == 'Marble':
if self.toolPath == 0:
play_sound(filter_admit)
self.warning("WARNING\nChoose a path first")
return
tile = Tile(self.toolPath)
marbleCoord = tile_x*tile_size+tile_size-10,tile_y*tile_size+tile_size+2
self.marbles.append(Marble(self.toolColor,marbleCoord,self.toolSwitchDirection,(tile_x,tile_y)))
else: tile = constructor(self.toolPath)
self.set_tile(tile_x,tile_y,tile)
# click on a tool