-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
970 lines (856 loc) · 31.2 KB
/
modules.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
import sys
import os
from time import sleep
from pynput import keyboard
from collections import deque
from enum import Enum
import numpy as np
import re
from math import sqrt
import pickle
class Move(Enum):
LEFT = 'l'
RIGHT = 'r'
UP = 'u'
DOWN = 'd'
class State(Enum):
I_REST = 'r'
I_ATTACK = 'a'
I_END_ATTACK = 'a_e'
I_DEFEND = 'f'
I_END_DEFENSE = 'f_e'
class Block:
def __init__(self, pos_x):
self.pos_x = int(pos_x)
self.color = [Paint.bg_blue, Paint.bg_cyan, Paint.bg_yellow, Paint.bg_red, Paint.bg_green][np.random.randint(0,4)]
def __str__(self):
return self.color.format(" ")
class Player:
NUMBER_OF_PLAYERS = 0
class GuiPlayer(Player):
def __init__(self):
self.body = dict()
# Right facing bodies at index 0 and left facing body at 1
self.body[State.I_REST] = None
self.body[State.I_ATTACK] = None
self.body[State.I_DEFEND] = None
def draw(self):
return self.body[self.state]
class AIPlayer(Player):
def generate_commands(self):
other_player_pos_x = self.scene.players[1-self.who].pos_x
class GuiAIPlayer(GuiPlayer):
"""
An AI graphical player
"""
pass
class HumanPlayer(Player):
"""
A player guided by a human playing on keyboard
"""
I_REST = 0
I_ATTACK = 1
I_DEFEND = 2
I_JUMP = 3
I_DIE = 4
I_WON = 5
HEIGHT = 4
WIDTH = 9
def __init__(self, auto=False):
self.__who = Player.NUMBER_OF_PLAYERS
self.__score = 0
self.auto = auto
self.state = State.I_REST
self.__pos_x = 0
self.__pos_y = HumanPlayer.HEIGHT
self.game = None
self.facing_right = self.who == 0
self.color = Paint.fg_cyan if self.who == 0 else Paint.fg_red
self.update_body()
Player.NUMBER_OF_PLAYERS += 1
self.pending_motions = deque()
self.pending_states = deque()
def game(self, game):
self.game = game
def flip(self):
"""
If the two players have exchanged positions, their body needs an update
"""
self.facing_right = not self.facing_right
self.update_body()
def draw(self):
"""
The player returns to the Scene.print_game(...) a representation of his body
"""
pic = np.tile([Scene.AIR*self.width], (Scene.HEIGHT-self.pos_y))
pic = np.concatenate((pic, self.body), axis=0)
pic = np.concatenate((pic, np.tile([Scene.AIR*self.width], (self.pos_y-self.height))), axis=0)
pic = pic.reshape(len(pic), 1)
return pic
def update_body(self):
"""
Each time a player changes his state he needs to update his body
"""
needs_flip = False
if self.state == State.I_REST:
self.body =np.array([
"|_O ",\
" |`-)---",\
" |\\ ",\
" / | "])
needs_flip = not self.facing_right
elif self.state == State.I_ATTACK:
self.body = np.array([
" O_\\ ~",\
"---(-'\\ ~ ",\
" /| ~",\
" / \\ ~"])
needs_flip = self.facing_right
elif self.state == State.I_DEFEND:
self.body = np.array([
"| ",\
"| O_\\",\
"|-'_\\ ",\
" / |_"])
needs_flip = self.facing_right
# If the drawing we made is not facing the direction of the current player
# we flip it.
if needs_flip:
#We draw one thing facing a certain direction and find its mirror image
#We do it using a temporary placeholder which is # just like when
# you want to exchange the values of two variables a and b, you use
# a third temporary variable c and do: c = a, a = b, b = a.
self.body = [s.replace('/','#') for s in self.body]
self.body = [s.replace('\\','/') for s in self.body]
self.body = [s.replace('#','\\') for s in self.body]
self.body = [s.replace('`','#') for s in self.body]
self.body = [s.replace("'",'`') for s in self.body]
self.body = [s.replace('#',"'") for s in self.body]
self.body = [s.replace(')','#') for s in self.body]
self.body = [s.replace('(',')') for s in self.body]
self.body = [s.replace('#','(') for s in self.body]
self.body = [s[::-1] for s in self.body]
self.height = len(self.body)
self.width = len(self.body[0])
self.body = np.array([self.color.format(_) for _ in self.body])
def execute(self):
if len(self.pending_motions) > 0:
command = self.pending_motions.popleft()
if command == Move.LEFT:
self.move_left()
elif command == Move.RIGHT:
self.move_right()
elif command == Move.UP:
self.move_up()
elif command == Move.DOWN:
self.move_down()
if len(self.pending_states) > 0:
state = self.pending_states.popleft()
if state == State.I_ATTACK:
self.attack()
elif state == State.I_END_ATTACK:
self.end_attack()
elif state == State.I_DEFEND:
self.defend()
elif state == State.I_END_DEFENSE:
self.end_defense()
def receive_command(self, key):
received = False
if self.who == 0:
if key == "'q'" or key == "'Q'": #left
if len(self.pending_motions) != 0:
last_move = self.pending_motions.pop()
if last_move != Move.RIGHT:
self.pending_motions.extend([last_move, Move.LEFT])
else:
self.pending_motions.extend([Move.LEFT])
received = True
elif key == "'d'" or key == "'D'": #right
if len(self.pending_motions) > 0:
last_move = self.pending_motions.pop()
if last_move != Move.LEFT:
self.pending_motions.extend([last_move, Move.RIGHT])
else:
self.pending_motions.extend([Move.RIGHT])
received = True
elif key == "'a'" or key == "'A'": #jumping left
m_s = self.movement_speed
self.pending_motions.extend(([Move.UP]*m_s)+([Move.LEFT]*m_s)+([Move.DOWN]*m_s))
received = True
elif key == "'e'" or key == "'E'": #jumping right
m_s = self.movement_speed
self.pending_motions.extend(([Move.UP]*m_s)+([Move.RIGHT]*m_s)+([Move.DOWN]*m_s))
received = True
elif key == "'z'" or key == "'Z'": #attacking
a_s = self.attacking_speed
direction = Move.RIGHT if self.facing_right else Move.LEFT
self.pending_motions.extend([direction]*a_s)
self.pending_states.extend(([State.I_ATTACK]*(a_s-1))+[State.I_END_ATTACK])
received = True
elif key == "'s'" or key == "'S'": #blocking
b_s = self.blocking_time-1
self.pending_states.extend(([State.I_DEFEND]*b_s)+[State.I_END_DEFENSE])
received = True
elif self.who == 1:
if key == 'Key.left': #left
if len(self.pending_motions) > 0:
last_move = self.pending_motions.pop()
if last_move != Move.RIGHT:
self.pending_motions.extend([last_move, Move.LEFT])
else:
self.pending_motions.extend([Move.LEFT])
received = True
elif key == 'Key.right': #right
if len(self.pending_motions) > 0:
last_move = self.pending_motions.pop()
if last_move != Move.LEFT:
self.pending_motions.extend([last_move, Move.RIGHT])
else:
self.pending_motions.extend([Move.RIGHT])
received = True
elif key == "'l'" or key == "'L'": #jumping left
m_s = self.movement_speed
self.pending_motions.extend(([Move.UP]*m_s)+([Move.LEFT]*m_s)+([Move.DOWN]*m_s))
received = True
elif key == "'m'" or key == "'M'": #jumping right
m_s = self.movement_speed-1
self.pending_motions.extend(([Move.UP]*m_s)+([Move.RIGHT]*m_s)+([Move.DOWN]*m_s))
received = True
elif key == "'o'" or key == "'O'": #attacking
a_s = self.attacking_speed
direction = Move.RIGHT if self.facing_right else Move.LEFT
self.pending_motions.extend([direction]*a_s)
self.pending_states.extend(([State.I_ATTACK]*(a_s-1))+[State.I_END_ATTACK])
received = True
elif key == "'p'" or key == "'P'": #blocking
b_s = self.blocking_time-1
self.pending_states.extend(([State.I_DEFEND]*b_s)+[State.I_END_DEFENSE])
received = True
return received
def move_left(self, step=1):
#for b in self.game.scene.blocks:
# if b.pos_x == self.pos_x-step and self.pos_y==self.height:
# return
self.pos_x -= step
def move_right(self, step=1):
#for b in self.game.scene.blocks:
# if b.pos_x == self.pos_x+step and self.pos_y==self.height:
# return
self.pos_x += step
def move_up(self, step=1):
self.pos_y += step
def move_down(self, step=1):
self.pos_y -= step
def attack(self):
if self.state != State.I_ATTACK:
self.state = State.I_ATTACK
self.update_body()
def end_attack(self):
adversary = self.game.scene.players[1 - self.who]
his_her_state = adversary.state
dist_to_him_her = sqrt(abs(self.pos_x-adversary.pos_x)**2+abs(self.pos_y-adversary.pos_y)**2)
if his_her_state == State.I_DEFEND\
or dist_to_him_her > self.attacking_range:
Game.PLAYER_FAILED |= (1<<self.who)
else:
Game.PLAYER_SUCCEEDED |= (1<<self.who)
self.state = State.I_REST
self.update_body()
def defend(self):
if self.state != State.I_DEFEND:
self.state = State.I_DEFEND
self.update_body()
def end_defense(self):
self.state = State.I_REST
self.update_body()
@property
def pos_x(self):
return self.__pos_x
@pos_x.setter
def pos_x(self, pos_x):
if 0 < pos_x < Scene.WIDTH:
self.__pos_x = pos_x
@property
def pos_y(self):
return self.__pos_y
@pos_y.setter
def pos_y(self, pos_y):
if 0 < pos_y < Scene.HEIGHT:
self.__pos_y = pos_y
@property
def who(self):
return self.__who
@who.setter
def who(self, me):
self.__who = me
@property
def num_players(cls):
return cls.__num_players
@property
def defending_range(self):
return self.__defending_range
@defending_range.setter
def defending_range(self, param):
self.__defending_range = max(1,param)
@property
def blocking_time(self):
return self.__blocking_time
@blocking_time.setter
def blocking_time(self, param):
self.__blocking_time = max(1,param)
@property
def attacking_range(self):
return self.__attacking_range
@attacking_range.setter
def attacking_range(self, param):
self.__attacking_range = max(1,param)
@property
def attacking_speed(self):
return self.__attacking_time
@attacking_range.setter
def attacking_speed(self, param):
self.__attacking_time = max(1,param)
@property
def movement_speed(self):
return self.__movement_speed
@movement_speed.setter
def movement_speed(self, param):
self.__movement_speed = max(1,param)
@property
def score(self):
return self.__score
@score.setter
def score(self, score):
self.__score = score
class GuiHumanPlayer(HumanPlayer):
pass
class Paint:
fg_red = "\033[91m{}\033[00m"
fg_green = "\033[92m{}\033[00m"
fg_yellow = "\033[93m{}\033[00m"
fg_light_purple = "\033[94m{}\033[00m"
fg_purple = "\033[95m{}\033[00m"
fg_cyan = "\033[96m{}\033[00m"
fg_light_gray = "\033[97m{}\033[00m"
fg_black = "\033[98m{}\033[00m"
bg_red = "\x1b[41m{}\033[00m" #background red
bg_green = "\x1b[42m{}\033[00m" #background green
bg_yellow = "\x1b[43m{}\033[00m" #background yellow
bg_blue = "\x1b[44m{}\033[00m" #background blue
bg_magenta = "\x1b[45m{}\033[00m" #background magenta
bg_cyan = "\x1b[46m{}\033[00m" #background cyan
bg_white = "\x1b[47m{}\033[00m" #background white
# This is used to correct the centering of colored lines
# Inspiration from https://stackoverflow.com/questions/14889454\
# /printed-length-of-a-string-in-python#:~:text=The%20printed\
# %20length%20of%20a%20string%20depends%20on,utf-8%20is%20equal
# %20to%20the%20bytes%20in%20String.
strip_ANSI_pattern = re.compile(r"""
\x1b # the ESC character
\[ # the '[' character
[;\d]* # zero or more digits or semicolons
[A-Za-z] # a letter
""", re.VERBOSE).sub
class Scene:
HEIGHT = 10
WIDTH = 100
GOLDEN_RATIO = 233/144
AIR = " "
default_scene = "___1______x__2______"
instance = None
def __init__(self, scene_layout, fps=2):
self.players = []
self.scene_layout = scene_layout
self.fps = fps
self.pending = deque()
self.attack_show_duration = self.fps*2
self.adjust_size_to_terminal()
self.blocks = np.array([])
self.anew = True
self.attacked_player = 0
blocks = re.finditer(r'[xX]', self.scene_layout)
for b in blocks:
self.blocks = np.append(self.blocks, int(b.span()[0]
* (Scene.WIDTH-2*HumanPlayer.WIDTH+2)\
/len(self.scene_layout)))
self.blocks.astype(int)
self.blocks = np.array([Block(x) for x in self.blocks])
self.collisions = np.array([
[
" X ",\
"|_O / \\ O_\\ ",\
" |`-/ \\-'\\ ",\
" |\\ / \\ ",\
" / \\ | \\"],\
[
" \\ / ",\
"|_O X O_\\ ",\
" |`-/ \\-'\\ ",\
" |\\ / | ",\
" / | | \\"],\
[
" \\ / ",\
" |_O X O_\\ ",\
" /`-/ \\-'\\ ",\
" |\\ / | ",\
"/ \\ | \\"],\
[
" / ",\
" /_O X ",\
" /`-/ \\ O_\\ ",\
" | \\ \\-'\\ ",\
"/ / / \\ ",\
" | \\ "],
[
" / ",\
" _O X ",\
" \\/`-/ \\ O_\\ ",\
" | \\ \\-'_\\ ",\
"/ / \ |_ "]
], dtype=object)
self.swords = np.array([
[" _ |\\ ",
"[_[[[[[| |==========================>",
" |/ "]])
self.wrap_drawings()
def get_instance(scene_file="default.ffscene"):
if Scene.instance is None:# or GameManager.instance.game.scene.scene_file != scene_file:
Scene.instance = Scene(scene_file)
return Scene.instance
def loop(self):
if self.anew:
self.position_players()
self.anew = False
self.update_scoreboard()
Game.IS_PLAYING = not Game.IS_PAUSED and not Game.IS_STOPPED
while Game.IS_PLAYING:
self.clear_scene()
self.execute_commands()
if Game.PLAYER_SUCCEEDED > 0:
self.attack_succeeded_by(Game.PLAYER_SUCCEEDED)
if Game.PLAYER_FAILED > 0:
self.attack_failed_by(Game.PLAYER_FAILED)
self.print_game()
sleep(1/self.fps)
Game.IS_PLAYING = not Game.IS_PAUSED and not Game.IS_STOPPED
Game.IS_PAUSED = not Game.IS_PLAYING and not Game.IS_STOPPED
while Game.IS_PAUSED and not Game.IS_GRAPHICAL:
self.clear_scene()
self.print_pause_screen()
sleep(1/self.fps)
Game.IS_PAUSED = not Game.IS_PLAYING and not Game.IS_STOPPED
# We go back to playing
if Game.IS_PLAYING and not Game.IS_GRAPHICAL:
self.loop()
# We go graphical
if Game.IS_PLAYING and Game.IS_GRAPHICAL:
self.graphical_loop()
# If Game is not paused nor playing, then it is over!
if Game.IS_STOPPED:
self.print_goodbye_screen()
def graphical_loop(self):
self.root = None
if Game.IS_PLAYING and not Game.IS_GRAPHICAL:
# We go back to the terminal loop
self.loop()
def pause(self):
while Game.IS_PAUSED:
#self.clear_scene()
self.print_pause_screen()
sleep(1/self.fps)
def execute_commands(self):
self.players[0].execute()
self.players[1].execute()
if self.players[0].facing_right and self.players[0].pos_x > self.players[1].pos_x\
or self.players[1].facing_right and self.players[1].pos_x > self.players[0].pos_x:
self.players[0].flip()
self.players[1].flip()
def print_game(self):
"""
Print the game. We use only numpy for this.
"""
width = Scene.WIDTH+self.players[0].WIDTH+self.players[1].WIDTH
# Print the scoreboard
topboard = ('-'*width).center(Scene.WINDOW_WIDTH)
topboard += Paint.bg_green.format(('Type <space> for the MENU, <esc> to QUIT.').center(Scene.WINDOW_WIDTH))
topboard += self.scoreboard
# If no close contact fight(collision) is going on we print two players doing their own thing...
if len(self.pending) == 0:
player1 = self.players[0] if self.players[0].facing_right else self.players[1]
player2 = self.players[1] if self.players[0].facing_right else self.players[0]
# Print the players
p1 = player1.draw()
p2 = player2.draw()
playground = np.tile([Scene.AIR], (Scene.HEIGHT, player1.pos_x))
playground = np.concatenate((playground, p1), axis=1)
playground = np.concatenate((playground, np.tile([Scene.AIR], (Scene.HEIGHT, (player2.pos_x-player1.pos_x)))), axis=1)
playground = np.concatenate((playground, p2), axis=1)
playground = np.concatenate((playground, np.tile([Scene.AIR], (Scene.HEIGHT, (Scene.WIDTH-player2.pos_x)))), axis=1)
# If collision is going on...
else:
state = self.pending.popleft()
if state == 'end':
self.position_players()
self.update_scoreboard(-1)
selected_collision = int((len(self.collisions)-1)*len(self.pending)/self.attack_show_duration)
playground = np.tile([Scene.AIR], (Scene.HEIGHT, self.players[self.attacked_player].pos_x))
playground = np.concatenate((playground, self.collisions[selected_collision].reshape(len(playground),1)), axis=1)
playground = np.concatenate((playground, np.tile([Scene.AIR], (Scene.HEIGHT, \
(Scene.WIDTH+len(self.collisions[selected_collision])-self.players[self.attacked_player].pos_x-1)))), axis=1)
selected_collision = int((len(self.collisions)-1)*len(self.pending)/self.attack_show_duration)
selected_collision %= len(self.collisions)
for b in self.blocks:
if (self.players[0].pos_x-1 < b.pos_x < self.players[0].pos_x+self.players[0].width) or \
(self.players[1].pos_x-1 < b.pos_x < self.players[1].pos_x+self.players[1].width):
continue
playground[-1,b.pos_x] = str(b)
playground = [''.join(line).center(Scene.WINDOW_WIDTH) for line in playground]
# Let's add the following line to correct the misalignment at printing of colored lines.
# In fact for colored lines the str.center(length...) would center the representation.
# But the printed version of colored strings is shorter than their representation, which is why their
# centering needs the following correction...
playground = [(' '*((Scene.WINDOW_WIDTH-self.printed_length(line))//2))\
+line+\
(' '*((Scene.WINDOW_WIDTH-self.printed_length(line))//2)) for line in playground]
playground = ''.join(playground)
playground += ("*"*width).center(Scene.WINDOW_WIDTH)
playground += ("-"*width).center(Scene.WINDOW_WIDTH)
print(topboard+playground)
def print_pause_screen(self):
"""
Print the pause menu.
"""
pause_menu = Paint.bg_blue.format('<PAUSE>'.center(Scene.WINDOW_WIDTH))+'\n'+'\n'
pause_menu += " ____ __ _ _ ___ _ _ ____ ____ _ _ ___ ____ _ _ ___ ".center(Scene.WINDOW_WIDTH)
pause_menu += "( ___)/__\ ( \( )/ __)( \/ ) ( ___)( ___)( \( )/ __)(_ _)( \( )/ __)".center(Scene.WINDOW_WIDTH)
pause_menu += " )__)/(__)\ ) (( (__ \ / )__) )__) ) (( (__ _)(_ ) (( (_-.".center(Scene.WINDOW_WIDTH)
pause_menu += "(__)(__)(__)(_)\_)\___) (__) (__) (____)(_)\_)\___)(____)(_)\_)\___/".center(Scene.WINDOW_WIDTH)+'\n'+'\n'
pause_menu += 'To resume the game: press <space> again.'.center(Scene.WINDOW_WIDTH)+'\n'+'\n'
pause_menu += 'To restart a new game: press <n> again.'.center(Scene.WINDOW_WIDTH)+'\n'+'\n'
pause_menu += 'To quit the game (without saving): press <esc>.'.center(Scene.WINDOW_WIDTH)+'\n'+'\n'
pause_menu += 'To save the game at this state of execution: press <enter>.'.center(Scene.WINDOW_WIDTH)+'\n'+'\n'
pause_menu += 'To load THE LAST previously saved game(if one exists): press <shift>.'.center(Scene.WINDOW_WIDTH)+'\n'+'\n'
pause_menu += 'To launch the graphical version: press <g>.'.center(Scene.WINDOW_WIDTH)+'\n'+'\n'
print(pause_menu)
def print_goodbye_screen(self):
pass
def printed_length(self, s):
"""
The printed length of the string s
We simply strip out all the ANSI colors codes
"""
return len(Paint.strip_ANSI_pattern("", s))
def adjust_size_to_terminal(self):
size = os.get_terminal_size()
Scene.WIDTH = size.columns//2
Scene.HEIGHT = int(Scene.WIDTH/Scene.GOLDEN_RATIO/2)
Scene.WINDOW_WIDTH = size.columns
def add_player(self, player):
self.players.append(player)
def attack_succeeded_by(self, by_whom):
Game.PLAYER_SUCCEEDED = 0
by_whom -= 1
# by_whom is equal to 2 when and only when both players have updated the Game.PLAYER_SUCCEEDED attribute
# which means that they both succeeded an attack simultaneously. In this case no point is
# given to any player. Otherwise, we give one point to the one who succeeded (player with
# index by_whom).
if by_whom == 0 or by_whom == 1:
self.players[by_whom].score += 1
self.attacked_player = 0 if by_whom > 1 else 1-by_whom
self.pending.extend(['success']*self.attack_show_duration+['end'])
print("\a")
for player in self.players:
player.pending_motions.clear()
player.pending_states.clear()
self.update_scoreboard(by_whom)
self.collisions = self.success_collisions
np.random.shuffle(self.collisions)
def attack_failed_by(self, by_whom):
Game.PLAYER_FAILED = 0
self.pending.extend(['failure']*self.attack_show_duration+['end'])
self.attacked_player = -1
print("\a")
print("\a")
for player in self.players:
player.state = State.I_REST
player.pending_motions.clear()
player.pending_states.clear()
self.collisions = self.failure_collisions
np.random.shuffle(self.collisions)
def wrap_drawings(self, color=Paint.fg_green):
#We draw one thing facing a certain direction and find its mirror image
#We do it using a temporary placeholder which is # just like when
# you want to exchange the values of two variables a and b, you use
# a third temporary variable c and do: c = a, a = b, b = a.
def mirror_flip(drawing):
drawing = np.array([s.replace('/','#') for s in drawing])
drawing = np.array([s.replace('\\','/') for s in drawing])
drawing = np.array([s.replace('#','\\') for s in drawing])
drawing = np.array([s.replace('`','#') for s in drawing])
drawing = np.array([s.replace("'",'`') for s in drawing])
drawing = np.array([s.replace('#',"'") for s in drawing])
drawing = np.array([s.replace(')','#') for s in drawing])
drawing = np.array([s.replace('(',')') for s in drawing])
drawing = np.array([s.replace('#','(') for s in drawing])
drawing = np.array([s.replace('[','#') for s in drawing])
drawing = np.array([s.replace(']','[') for s in drawing])
drawing = np.array([s.replace('#',']') for s in drawing])
drawing = np.array([s.replace('>','#') for s in drawing])
drawing = np.array([s.replace('<','>') for s in drawing])
drawing = np.array([s.replace('#','<') for s in drawing])
drawing = np.array([s[::-1] for s in drawing])
return drawing
self.collisions = np.array([([Scene.AIR*len(c[0])] * (Scene.HEIGHT-len(c)))+c for c in self.collisions])
self.success_collisions = np.array([[Paint.fg_yellow.format(_) for _ in collision] for collision in self.collisions])
self.failure_collisions = np.array([[Paint.fg_light_gray.format(_) for _ in collision] for collision in self.collisions])
self.swords = np.concatenate((self.swords, [[_ for _ in mirror_flip(self.swords[0])]]), axis=0)
self.swords = np.concatenate((self.swords, [[Paint.fg_cyan.format(_) for _ in self.swords[0]]]), axis=0)
self.swords = np.concatenate((self.swords, [[Paint.fg_red.format(_) for _ in self.swords[1]]]), axis=0)
def position_players(self):
for player in self.players:
player.pos_x = int(self.scene_layout.find(str(player.who+1))\
* (Scene.WIDTH-2*HumanPlayer.WIDTH+2)\
/len(self.scene_layout))
player.pos_y = player.height
def update_scoreboard(self, by_whom=-1):
scores = "| "+str(self.players[0].score)+" | "+str(self.players[1].score) + " |"
if by_whom == 0:
p1 = (self.swords[2][0]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[1][0]).center(Scene.WINDOW_WIDTH)
scoreboard = (self.swords[2][1]+(' '*5)+scores+(' '*5)+self.swords[1][1]).center(Scene.WINDOW_WIDTH)
p2 = (self.swords[2][2]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[1][2]).center(Scene.WINDOW_WIDTH)
elif by_whom == 1:
p1 = (self.swords[0][0]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[3][0]).center(Scene.WINDOW_WIDTH)
scoreboard = (self.swords[0][1]+(' '*5)+scores+(' '*5)+self.swords[3][1]).center(Scene.WINDOW_WIDTH)
p2 = (self.swords[0][2]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[3][2]).center(Scene.WINDOW_WIDTH)
elif by_whom == -1:
p1 = (self.swords[0][0]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[1][0]).center(Scene.WINDOW_WIDTH)
scoreboard = (self.swords[0][1]+(' '*5)+scores+(' '*5)+self.swords[1][1]).center(Scene.WINDOW_WIDTH)
p2 = (self.swords[0][2]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[1][2]).center(Scene.WINDOW_WIDTH)
elif by_whom == 2:
p1 = (self.swords[2][0]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[3][0]).center(Scene.WINDOW_WIDTH)
scoreboard = (self.swords[2][1]+(' '*5)+scores+(' '*5)+self.swords[3][1]).center(Scene.WINDOW_WIDTH)
p2 = (self.swords[2][2]+(' '*5)+('-'*len(scores))+(' '*5)+self.swords[3][2]).center(Scene.WINDOW_WIDTH)
p1 = (' '*((Scene.WINDOW_WIDTH-self.printed_length(p1))//2))+p1+(' '*((Scene.WINDOW_WIDTH-self.printed_length(p1))//2))
scoreboard = (' '*((Scene.WINDOW_WIDTH-self.printed_length(scoreboard))//2))+scoreboard+(' '*((Scene.WINDOW_WIDTH-self.printed_length(scoreboard))//2))
p2 = (' '*((Scene.WINDOW_WIDTH-self.printed_length(p2))//2))+p2+(' '*((Scene.WINDOW_WIDTH-self.printed_length(p2))//2))
self.scoreboard = p1+scoreboard+p2
def clear_scene(self):
if os.name == 'posix':
os.system('clear')
else:
os.system('cls')
class GuiScene:
HEIGHT = 233
WIDTH = 144
def __init__(self):
pass
def start(self):
pass
class Game:
IS_INITIALIZED = False
IS_PLAYING = False
IS_PAUSED = False
IS_STOPPED = False
IS_GRAPHICAL = False
PLAYER_SUCCEEDED = 0
PLAYER_FAILED = 0
def __init__(self):
Game.IS_INITIALIZED = True
def start(self):
Game.IS_PAUSED = False
Game.IS_PLAYING = True
self.scene.loop()
def pause(self):
Game.IS_PAUSED = True
Game.IS_PLAYING = False
self.scene.pause()
def stop(self):
Game.IS_PAUSED = False
Game.IS_PLAYING = False
Game.STOPPED = True
@property
def score_unit(self):
return self.__score_unit
@score_unit.setter
def score_unit(self, score_unit):
self.__score_unit = score_unit
def score_plus(self, player):
player.score += self.score_unit
def score_minus(self, player):
player.score -= self.score_unit
def add_player(self, player):
player.game = self
self.scene.add_player(player)
def set_scene(self, scene_file):
scene_layout = Scene.default_scene
with open(scene_file) as file:
scene_layout = file.readline().strip()
self.scene = Scene.get_instance(scene_layout)
return self.scene
class GameManager:
instance = None
init_OK = True
help_string = """
Hi, I am FancyFencing 1.0. Let me introduce myself...
\x1b[42mHELP\033[00m (You called me with option --help)
Usage: python3.9 main.py [-fps <frame_per_second>] [-option_i <arg_i_player_1> <arg_i_player_2>]...
List of required arguments:
--fps <frame_per_second> This controls the speed of the game.
--ms <ms1> <ms2> [movement_speed] Player1 uses <ms1> frames to move in all four directions, Player2 uses <ms2> frames to move in all four directions
--as <as1> <as2> [attacking_speed] Player1 uses <as1> frames for his attacks to become effective, Player2 needs <as2> frames
--ar <ar1> <ar2> [attacking_range] Player1 needs his attacker to be as close as <ar1> from him in order for his (Player1) attacks to be effective
Player2 needs his attacker to be as close as <ar2> from him in order for his attacks to be effective
--bt <bt1> <bt2> [blocking_time] Player1's blockage last for <bt1> frames, while <bt2> is the blocking time for Player2
--scene <path_to_ff.scene.file> A valid .ffscene file.
Optional argument (used alone):
--help For this help message
Here are two valid examples:
\033[93m python3.9 main.py --fps 24 --ms 9 8 --as 12 13 --ar 8 15 --bt 7 9 --scene default.ffscene\033[00m
\033[93m python3.9 main.py --help \033[00m
Version 1.0
Dec 1, 2022, Comlan Amouwotor
Happy Fencing!
"""
def __init__(self):
self.game = Game()
self.params = dict()
def get_instance():
if GameManager.instance is None:
GameManager.instance = GameManager()
return GameManager.instance
def get_params_from_inputs(self):
argv = sys.argv[1:]
if len(argv) != 16:
print("Not enough arguments!")
GameManager.init_OK = False
print(GameManager.help_string)
return
if len(argv) == 1 and argv[0] != '--help':
GameManager.init_OK = False
print(GameManager.help_string)
return
try:
i=0
while i < len(argv):
if argv[i] not in {'--help', '--fps', '--ms', '--as', '--ar', '--bt', '--scene'}:
print(f"argument {arg[i]} not supported")
GameManager.init_OK = False
print(GameManager.help_string)
return
if argv[i] == '--help':
GameManager.init_OK = False
print(GameManager.help_string)
return
elif argv[i] == '--fps':
self.params['--fps'] = int(argv[i+1])
i+=2
elif argv[i] == '--ms':
self.params['--ms'] = int(argv[i+1]), int(argv[i+2])
i+=3
elif argv[i] == '--as':
self.params['--as'] = int(argv[i+1]), int(argv[i+2])
i+=3
elif argv[i] == '--ar':
self.params['--ar'] = int(argv[i+1]), int(argv[i+2])
i+=3
elif argv[i] == '--bt':
self.params['--bt'] = int(argv[i+1]), int(argv[i+2])
i+=3
elif argv[i] == '--scene':
GameManager.instance.set_scene(argv[i+1])
i+=2
except Exception as e:
print(f"\x1b[41m {e}\033[00m")
GameManager.init_OK = False
print(GameManager.help_string)
if len(self.params) == 5:
self.build_players()
else:
GameManager.init_OK = False
print(GameManager.help_string)
def build_players(self):
if GameManager.init_OK:
gameManager = GameManager.get_instance()
player1 = gameManager.player()
player2 = gameManager.player()
for arg, val in self.params.items():
if arg == '--fps':
self.game.scene.fps = val
elif arg == '--ms':
player1.movement_speed = val[0]
player2.movement_speed = val[1]
elif arg == '--as':
player1.attacking_speed = val[0]
player2.attacking_speed = val[1]
elif arg == '--ar':
player1.attacking_range = val[0]
player2.attacking_range = val[1]
elif arg == '--bt':
player1.blocking_time = val[0]
player2.blocking_time = val[1]
def start(self):
self.get_params_from_inputs()
if GameManager.init_OK:
listener = keyboard.Listener(on_press=self.key_pressed, suppress=True)
listener.start()
self.game.start()
else:
print("Maybe next time... :)")
def pause(self):
self.game.pause()
def stop(self):
self.game.stop()
def set_scene(self, scene_file):
self.game.set_scene(scene_file)
def save_game(self):
# Serialization
with open(".game.pickle", "wb") as outfile:
pickle.dump(GameManager.instance, outfile)
print("\x1b[42m Game saved successfully! \033[00")
def load_game(self):
# Deserialization
with open(".game.pickle", "rb") as infile:
loaded = pickle.load(infile)
if not loaded:
print("\x1b[41m No saved game found in file ./game.pickle!\033[00m")
return
else:
print("\x1b[42m Game loaed successfully! You can see after leaving pause mode.\033[00")
GameManager.instance = loaded
self.game.scene.players = GameManager.instance.game.scene.players
def key_pressed(self, key):
key = str(key)
if key == 'Key.esc':
self.stop()
Game.IS_STOPPED = True
return
elif key == 'Key.space':
# We flip from pause to playing with
# with the same key
if not Game.IS_PAUSED:
Game.IS_PAUSED = True
else:
Game.IS_PLAYING = True
return
elif (key == "'n'" or key == "'N'") and Game.IS_PAUSED:
Game.IS_PLAYING = True
self.anew = True
return
elif (key == "'g'" or key == "'G'") and Game.IS_PAUSED:
Game.IS_GRAPHICAL = True
self.anew = True
return
elif key == "Key.enter" and Game.IS_PAUSED:
self.save_game()
return
elif (key == "Key.shift_r" or key == "Key.shift_l") and Game.IS_PAUSED:
self.load_game()
return
#hotkey = keyboard.HotKey([keyboard.Key.ctrl, keyboard.KeyCode(char='s')], lambda : print("<ctrl><s>"))
#hotkey.press(self.listener.canonical(key))
if not self.game.scene.players[0].receive_command(key):
self.game.scene.players[1].receive_command(key)
def player(self):
player = HumanPlayer()
self.game.add_player(player)
return player