-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathological.py
executable file
·2388 lines (2028 loc) · 68.8 KB
/
pathological.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
(C) 2004 Joe Wreschnig
(C) 2016 Nina Ripoll (Editor/Levelsets)
(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, hashlib
from pygame.locals import *
# Parse the command line
highscores_file = os.path.join(os.environ["HOME"], ".pathological_scores")
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] [highscores-file]\n")
sys.exit(1)
else:
highscores_file = arg
if colorblind:
cbext = '-cb.png'
else:
cbext = '.png'
# The location of the setgid script for writing highscores
# This script is only used if the highscores file is not writable directly
write_highscores = "/usr/lib/games/pathological/bin/write-highscores"
# Game constants
wheel_steps = 9
frames_per_sec = 100
timer_width = 36
timer_margin = 4
info_height = 20
initial_lives = 3
extra_life_frequency = 5000 # Extra life awarded every this many points
max_spare_lives = 10
# Volume levels
music_volume = 0.5
intro_music_volume = 0.5
ingame_music_volume = 0.5
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
default_launch_timer = 6 # 6 passes
default_board_timer = 30 # 30 seconds per wheel
marble_speed = 2 # Marble speed in pixels/frame (must be 1, 2 or 4)
trigger_time = 30 # 30 seconds
replicator_delay = 35 # 35 frames
# 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 = 800
screen_height = 600
marble_size = 28
tile_size = 92
wheel_margin = 4
stoplight_marble_size = 28
life_marble_size = 16
# 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)))
# Direction references
dirs = ((0,-1),(1,0),(0,1),(-1,0))
# 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'
levelNumber = {}
customsSetsFiles = ['all-boards']
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 resources
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, (int(size[0]),int(size[1])))
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 = 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 == 'all-boards': levelsetFolder = 'circuits'
else: levelsetFolder = 'user_circuits'
IntroScreen.start_level = 1
def countLevels(levelsetToCheck=None):
if not levelsetToCheck:
fullname = os.path.join(levelsetFolder, levelset)
else:
if levelsetToCheck == 'all-boards': folder='circuits'
else: folder='user_circuits'
fullname = os.path.join(folder,levelsetToCheck)
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, -1,
(marble_size, marble_size)))
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
Wheel.images = (
load_image('wheel.png',-1,(tile_size,tile_size)),
load_image('wheel-dark.png',-1,(tile_size, tile_size)),
)
Wheel.blank_images = (
load_image('blank-wheel.png',-1,(tile_size,tile_size)),
load_image('blank-wheel-dark.png',-1,(tile_size, tile_size)),
)
Wheel.moving_holes = (
load_image('moving-hole.png',-1,(marble_size,marble_size)),
load_image('moving-hole-dark.png',-1,(marble_size, marble_size)),
)
Buffer.bottom = load_image('buffer.png',-1,(tile_size,tile_size))
Buffer.top = load_image('buffer-top.png',-1,(tile_size,tile_size))
Painter.images = []
for i in range(8):
Painter.images.append( load_image('painter-'+repr(i)+cbext, -1,
(tile_size,tile_size)))
Filter.images = []
for i in range(8):
Filter.images.append( load_image('filter-'+repr(i)+cbext, -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)))
Replicator.image = load_image('replicator.png',-1,(tile_size,tile_size))
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.life_marble = load_image('life-marble.png', -1,
(life_marble_size, life_marble_size))
Board.launcher_background = load_image('launcher.png', None,
(horiz_tiles * tile_size,marble_size))
Board.launcher_v = load_image('launcher-v.png', None,
(marble_size, vert_tiles * tile_size + marble_size))
Board.launcher_corner = load_image('launcher-corner.png', (255,0,0),
((tile_size-marble_size)//2+marble_size,marble_size))
Board.launcher_entrance = load_image('entrance.png', -1,
(tile_size,marble_size))
IntroScreen.background = load_image('intro.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)
IntroScreen.hs_font = pygame.font.Font(
None, IntroScreen.hs_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):
self.color = color
self.rect = pygame.Rect((0,0,marble_size,marble_size))
self.rect.center = center
self.direction = direction
def update(self, board):
self.rect.move_ip(
marble_speed * dirs[self.direction][0],
marble_speed * dirs[self.direction][1])
board.affect_marble( self)
def undraw(self, screen, background):
screen.set_clip( self.rect)
screen.blit( background, (0,0))
screen.set_clip()
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
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.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, dir):
pass
def affect_marble(self, board, marble, rpos):
if rpos == (tile_size//2,tile_size//2):
if self.paths & (1 << marble.direction): return
# Figure out the new direction
t = self.paths - (1 << (marble.direction^2))
if t == 1: marble.direction = 0
elif t == 2: marble.direction = 1
elif t == 4: marble.direction = 2
elif t == 8: marble.direction = 3
else: marble.direction = marble.direction ^ 2
class Wheel(Tile):
def __init__(self, paths, center=None):
Tile.__init__(self, paths, center) # Call base class intializer
self.spinpos = 0
self.spindir = 0
self.completed = 0
self.marbles = [ -3, -3, -3, -3 ]
def draw_back(self, surface):
if self.drawn: return 0
Tile.draw_back(self, surface)
if self.spinpos:
surface.blit( self.blank_images[self.completed], self.rect.topleft)
if self.spindir == 1:
for i in range(4):
holecenter = holecenters[self.spinpos][i]
surface.blit( self.moving_holes[self.completed],
(holecenter[0]-marble_size//2+self.rect.left,
holecenter[1]-marble_size//2+self.rect.top))
else:
for i in range(3,-1,-1):
holecenter = holecenters[self.spinpos][i]
surface.blit( self.moving_holes[self.completed],
(holecenter[1]-marble_size//2+self.rect.left,
holecenter[0]-marble_size//2+self.rect.top))
else:
surface.blit( self.images[self.completed], self.rect.topleft)
self.spindir = 0
if self.spindir == -1:
for i in range(3,-1,-1):
color = self.marbles[i]
if color >= 0:
holecenter = holecenters[self.spinpos][i]
surface.blit( Marble.images[color],
(holecenter[1]-marble_size//2+self.rect.left,
holecenter[0]-marble_size//2+self.rect.top))
else:
for i in range(4):
color = self.marbles[i]
if color >= 0:
holecenter = holecenters[self.spinpos][i]
surface.blit( Marble.images[color],
(holecenter[0]-marble_size//2+self.rect.left,
holecenter[1]-marble_size//2+self.rect.top))
return 1
def update(self, board):
if self.spinpos > 0:
self.spinpos -= 1
self.drawn = 0
def click(self, board, posx, posy, tile_x, tile_y, dir):
# Ignore all clicks while rotating
if self.spinpos: return
b1, b2, b3 = pygame.mouse.get_pressed()
if b3 or dir != None:
# First, make sure that no marbles are currently entering
for i in self.marbles:
if i == -1 or i == -2: return
# Start the wheel spinning
self.spinpos = wheel_steps - 1
self.spindir = dir
play_sound( wheel_turn)
# Reposition the marbles
if dir == None or dir == 1:
t = self.marbles[0]
self.marbles[0] = self.marbles[1]
self.marbles[1] = self.marbles[2]
self.marbles[2] = self.marbles[3]
self.marbles[3] = t
else:
t = self.marbles[3]
self.marbles[3] = self.marbles[2]
self.marbles[2] = self.marbles[1]
self.marbles[1] = self.marbles[0]
self.marbles[0] = t
self.drawn = 0
elif b1:
# Determine which hole is being clicked
for i in range(4):
# If there is no marble here, skip it
if self.marbles[i] < 0: continue
holecenter = holecenters[0][i]
rect = pygame.Rect( 0, 0, marble_size, marble_size)
rect.center = holecenter
if rect.collidepoint( posx, posy):
# Determine the neighboring tile
neighbor = board.tiles[ (tile_y + dirs[i][1]) %
vert_tiles][ (tile_x + dirs[i][0]) % horiz_tiles]
if (
# Disallow marbles to go off the top of the board
(tile_y == 0 and i==0) or
# If there is no way out here, skip it
((self.paths & (1 << i)) == 0) or
# If the neighbor is a wheel that is either turning
# or has a marble already in the hole, disallow
# the ejection
(isinstance(neighbor, Wheel) and
(neighbor.spinpos or
neighbor.marbles[i^2] != -3))
):
play_sound( incorrect)
else:
# If the neighbor is a wheel, apply a special lock
if isinstance(neighbor, Wheel):
neighbor.marbles[i^2] = -2
elif len(board.marbles) >= board.live_marbles_limit:
# Impose the live marbles limit
play_sound( incorrect)
break
# Eject the marble
board.marbles.append(
Marble( self.marbles[i],
(holecenter[0]+self.rect.left,
holecenter[1]+self.rect.top),
i))
self.marbles[i] = -3
play_sound( marble_release)
self.drawn = 0
break
def affect_marble(self, board, marble, rpos):
# Watch for marbles entering
if rpos[0]+marble_size//2 == wheel_margin or \
rpos[0]-marble_size/2 == tile_size - wheel_margin or \
rpos[1]+marble_size/2 == wheel_margin or \
rpos[1]-marble_size/2 == tile_size - wheel_margin:
if self.spinpos or self.marbles[marble.direction^2] >= -1:
# Reject the marble
marble.direction = marble.direction ^ 2
play_sound( ping)
else:
self.marbles[marble.direction^2] = -1
for holecenter in holecenters[0]:
if rpos == holecenter:
# Accept the marble
board.marbles.remove( marble)
self.marbles[marble.direction^2] = marble.color
self.drawn = 0
break
def complete(self, board):
# Complete the wheel
for i in range(4): self.marbles[i] = -3
if self.completed: board.game.increase_score( 10)
else: board.game.increase_score( 50)
self.completed = 1
play_sound( wheel_completed)
self.drawn = 0
def maybe_complete(self, board):
if self.spinpos > 0: return 0
# Is there a trigger?
if (board.trigger is not None) and \
(board.trigger.marbles is not None):
# Compare against the trigger
for i in range(4):
if self.marbles[i] != board.trigger.marbles[i] and \
self.marbles[i] != 8: return 0
self.complete( board)
board.trigger.complete( board)
return 1
# Do we have four the same color?
color = 8
for c in self.marbles:
if c < 0: return 0
if color==8: color=c
elif c==8: c=color
elif c != color: return 0
# Is there a stoplight?
if (board.stoplight is not None) and \
(board.stoplight.current < 3):
# Compare against the stoplight
if color != 8 and \
color != board.stoplight.marbles[board.stoplight.current]:
return 0
else:
board.stoplight.complete( board)
self.complete( board)
return 1
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
def affect_marble(self, board, marble, rpos):
# Watch for marbles entering
if (rpos[0]+marble_size == tile_size//2 and marble.direction == 1) or \
(rpos[0]-marble_size == tile_size//2 and marble.direction == 3) or \
(rpos[1]+marble_size == tile_size//2 and marble.direction == 2) or \
(rpos[1]-marble_size == tile_size//2 and marble.direction == 0):
if self.entering is not None:
# Bump the marble that is currently entering
newmarble = self.entering
newmarble.rect.center = self.rect.center
newmarble.direction = marble.direction
play_sound( ping)
# Let the base class affect the marble
Tile.affect_marble(self, board, newmarble,
(tile_size//2,tile_size//2))
elif self.marble >= 0:
# Bump the marble that is currently caught
newmarble = Marble( self.marble, self.rect.center, marble.direction)
board.marbles.append( newmarble)
play_sound( ping)
# Let the base class affect the marble
Tile.affect_marble(self, board, newmarble,
(tile_size//2,tile_size//2))
self.marble = -1
self.drawn = 0
# Remember which marble is on its way in
self.entering = marble
elif rpos == (tile_size//2, tile_size//2):
# Catch this marble
self.marble = marble.color
board.marbles.remove( marble)
self.entering = None
self.drawn = 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
def affect_marble(self, board, marble, rpos):
Tile.affect_marble( self, board, marble, rpos)
if rpos == (tile_size//2, tile_size//2):
if marble.color != self.color:
# Change the color
marble.color = self.color
play_sound( change_color)
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
def affect_marble(self, board, marble, rpos):
if rpos == (tile_size//2, tile_size//2):
# If the color is wrong, bounce the marble
if marble.color != self.color and marble.color != 8:
marble.direction = marble.direction ^ 2
play_sound( ping)
else:
Tile.affect_marble( self, board, marble, rpos)
play_sound( filter_admit)
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
def affect_marble(self, board, marble, rpos):
if rpos == (tile_size//2, tile_size//2):
marble.direction = self.direction
play_sound( direct_marble)
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
def affect_marble(self, board, marble, rpos):
if rpos == (tile_size//2, tile_size//2):
board.marbles.remove( marble)
play_sound( shredder)
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
self.switched = 0
def switch(self):
t = self.curdir
self.curdir = self.otherdir
self.otherdir = t
self.switched = 1
play_sound( switch)
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)
rc = self.switched
self.switched = 0
return rc
def affect_marble(self, board, marble, rpos):
if rpos == (tile_size//2, tile_size//2):
marble.direction = self.curdir
self.switch()
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)
return 0
def update(self, board):
for i in self.pending[:]:
i[3] -= 1
if i[3] == 0:
i[3] = replicator_delay
# Make sure that the active marble limit isn't exceeded
if len(board.marbles) >= board.live_marbles_limit:
# Clear the pending list
self.pending = []
return
# Add the new marble
board.marbles.append(Marble(i[0],self.rect.center,i[1]))
play_sound( replicator)
i[2] -= 1
if i[2] <= 0: self.pending.remove( i)
def affect_marble(self, board, marble, rpos):
Tile.affect_marble( self, board, marble, rpos)
if rpos == (tile_size//2, tile_size//2):
# Add the marble to the pending list
self.pending.append( [marble.color,marble.direction,
self.count - 1, replicator_delay]);
play_sound( replicator)
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
if other is not None: self.connect( other)
def draw_fore(self, surface):
surface.blit( self.tunnels[self.paths], self.rect.topleft)
surface.blit( self.image, self.rect.topleft)
return 0
def connect(self, other):
self.other = other
other.other = self
def affect_marble(self, board, marble, rpos):
if rpos == (tile_size//2, tile_size//2):
marble.rect.center = self.other.rect.center
play_sound( teleport)
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):
if self.countdown > 0:
self.countdown -= 1
if self.countdown == 0:
self._setup( board.colors)
play_sound( trigger_setup)
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
def complete(self, board):
self.marbles = None
self.countdown = trigger_time * frames_per_sec
self.drawn = 0
board.game.increase_score( 50)
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
def complete(self, board):
for i in range(3):
if self.marbles[i] >= 0:
self.marbles[i] = -1
break
self.current += 1
self.drawn = 0
board.game.increase_score( 20)
class Board:
def __init__(self, game, pos):
self.game = game
self.pos = pos
self.marbles = []
self.screen = game.screen
self.trigger = None
self.stoplight = None
self.launch_queue = []
self.board_complete = 0
self.paused = 0
self.name = "Unnamed"
self.live_marbles_limit = 10
self.launch_timeout = -1
self.board_timeout = -1
self.colors = default_colors
self.launched = 1
self.set_launch_timer( default_launch_timer)
self.set_board_timer( default_board_timer)
# Create the board array
self.tiles = []
for j in range( vert_tiles):
row = list(range( horiz_tiles))
self.tiles.append( row)
# Load the level
# For levels above game.level, use a pseudo-random
# level selection method.
if( game.level < game.numlevels):
self._load( game.circuit, game.level)
else:
# Compute a hash of the current level, involving
# a static timestamp. This provides a consistent,
# backtrackable pseudo-random function.
hashfloater = str(float(game.gamestart) / float(game.level))
hash = hashlib.md5(hashfloater.encode('utf-8')).hexdigest()
hashval = (ord(hash[0]) + (ord(hash[1]) << 8) + \
(ord(hash[2]) << 16) + (ord(hash[3]) << 24)) & 32767;
self._load( game.circuit, hashval % game.numlevels);