/
bombman.py
executable file
·5557 lines (3955 loc) · 213 KB
/
bombman.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/env python
# coding=utf-8
#
# Bombman - free and open-source Bomberman clone
#
# Copyright (C) 2016 Miloslav Číž
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# ========================== A FEW COMMENTS ===========================
#
# Version numbering system:
#
# major.minor
#
# Major number increases with significant new features added (multiplayer, ...),
# minor number increases with small changes (bug fixes, AI improvements, ...) and
# it does so in this way: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 91, 92, 93, etc.
#
# ---------------------------------------------------------------------
#
# Map string format (may contain spaces and newlines, which will be ignored):
#
# <environment>;<player items>;<map items>;<tiles>
#
# <environment> - Name of the environment of the map (affects only visual appearance).
# <player items> - Items that players have from the start of the game (can be an empty string),
# each item is represented by one letter (the same letter can appear multiple times):
# f - flame
# F - superflame
# b - bomb
# k - kicking shoe
# s - speedup
# p - spring
# d - disease
# m - multibomb
# r - random
# x - boxing glove
# e - detonator
# t - throwing glove
# <map items> - Set of items that will be hidden in block on the map. This is a string of the
# same format as in <player items>. If there is more items specified than there is
# block tiles, then some items will be left out.
# <tiles> - Left to right, top to bottom sequenced array of map tiles:
# . - floor
# x - block (destroyable)
# # - wall (undestroyable)
# A - teleport A
# B - teleport B
# T - trampoline
# V - lava
# u - arrow up, floor tile
# r - arrow right, floor tile
# d - arrow down, floor tile
# l - arrow left, floor tile
# U - arrow up, under block tile
# R - arrow right, under block tile
# D - arrow down, under block tile
# L - arrow left, under block tile
# <0-9> - starting position of the player specified by the number
import sys
import pygame
import os
import math
import copy
import random
import re
import time
DEBUG_PROFILING = False
DEBUG_FPS = False
DEBUG_VERBOSE = False
#------------------------------------------------------------------------------
def debug_log(message):
if DEBUG_VERBOSE:
print(message)
#==============================================================================
class Profiler(object):
SHOW_LAST = 10
#----------------------------------------------------------------------------
def __init__(self):
self.sections = {}
#----------------------------------------------------------------------------
def measure_start(self, section_name):
if not DEBUG_PROFILING:
return
if not (section_name in self.sections):
self.sections[section_name] = [0.0 for i in range(Profiler.SHOW_LAST)]
section_values = self.sections[section_name]
section_values[0] -= pygame.time.get_ticks()
#----------------------------------------------------------------------------
def measure_stop(self, section_name):
if not DEBUG_PROFILING:
return
if not section_name in self.sections:
return
section_values = self.sections[section_name]
section_values[0] += pygame.time.get_ticks()
#----------------------------------------------------------------------------
def end_of_frame(self):
for section_name in self.sections:
section_values = self.sections[section_name]
section_values.pop()
section_values.insert(0,0)
#----------------------------------------------------------------------------
def get_profile_string(self):
result = "PROFILING INFO:"
section_names = list(self.sections.keys())
section_names.sort()
for section_name in section_names:
result += "\n" + section_name.ljust(25) + ": "
section_values = self.sections[section_name]
for i in range(len(section_values)):
result += str(section_values[i]).ljust(5)
result += " AVG: " + str(sum(section_values) / float(len(section_values)))
return result
#==============================================================================
## Something that has a float position on the map.
class Positionable(object):
#----------------------------------------------------------------------------
def __init__(self):
self.position = (0.0,0.0)
#----------------------------------------------------------------------------
def set_position(self,position):
self.position = position
#----------------------------------------------------------------------------
def get_position(self):
return self.position
#----------------------------------------------------------------------------
def get_neighbour_tile_coordinates(self):
tile_coordinates = self.get_tile_position()
top = (tile_coordinates[0],tile_coordinates[1] - 1)
right = (tile_coordinates[0] + 1,tile_coordinates[1])
down = (tile_coordinates[0],tile_coordinates[1] + 1)
left = (tile_coordinates[0] - 1,tile_coordinates[1])
return (top,right,down,left)
#----------------------------------------------------------------------------
def get_tile_position(self):
return Positionable.position_to_tile(self.position)
#----------------------------------------------------------------------------
## Moves the object to center of tile (if not specified, objects current tile is used).
def move_to_tile_center(self, tile_coordinates=None):
if tile_coordinates != None:
self.position = tile_coordinates
self.position = (math.floor(self.position[0]) + 0.5,math.floor(self.position[1]) + 0.5)
#----------------------------------------------------------------------------
## Converts float position to integer tile position.
@staticmethod
def position_to_tile(position):
return (int(math.floor(position[0])),int(math.floor(position[1])))
#----------------------------------------------------------------------------
def is_near_tile_center(self):
position_within_tile = (self.position[0] % 1,self.position[1] % 1)
limit = 0.2
limit2 = 1.0 - limit
return (limit < position_within_tile[0] < limit2) and (limit < position_within_tile[1] < limit2)
#==============================================================================
class Player(Positionable):
# possible player states
STATE_IDLE_UP = 0
STATE_IDLE_RIGHT = 1
STATE_IDLE_DOWN = 2
STATE_IDLE_LEFT = 3
STATE_WALKING_UP = 4
STATE_WALKING_RIGHT = 5
STATE_WALKING_DOWN = 6
STATE_WALKING_LEFT = 7
STATE_IN_AIR = 8
STATE_TELEPORTING = 9
STATE_DEAD = 10
DISEASE_NONE = 0
DISEASE_DIARRHEA = 1
DISEASE_SLOW = 2
DISEASE_REVERSE_CONTROLS = 3
DISEASE_SHORT_FLAME = 4
DISEASE_SWITCH_PLAYERS = 5
DISEASE_FAST_BOMB = 6
DISEASE_NO_BOMB = 7
DISEASE_EARTHQUAKE = 8
INITIAL_SPEED = 3
SLOW_SPEED = 1.5
MAX_SPEED = 10
SPEEDUP_VALUE = 1
DISEASE_TIME = 20000
JUMP_DURATION = 2000
TELEPORT_DURATION = 1500
#----------------------------------------------------------------------------
def __init__(self):
super(Player,self).__init__()
self.number = 0 ##< player's number
self.team_number = 0 ##< team number, determines player's color
self.state = Player.STATE_IDLE_DOWN
self.state_time = 0 ##< how much time (in ms) has been spent in current state
self.speed = Player.INITIAL_SPEED ##< speed in tiles per second
self.bombs_left = 1 ##< how many more bombs the player can put at the time
self.flame_length = 1 ##< how long the flame is in tiles
self.items = {} ##< which items and how many the player has, format: [item code]: count
self.has_spring = False ##< whether player's bombs have springs
self.has_shoe = False ##< whether player has a kicking shoe
self.disease_time_left = 0
self.disease = Player.DISEASE_NONE
self.has_multibomb = False
self.has_boxing_glove = False
self.has_throwing_glove = False
self.boxing = False
self.detonator_bombs_left = 0 ##< what number of following bombs will have detonators
self.detonator_bombs = [] ##< references to bombs to be detonated
self.wait_for_special_release = False ##< helper used to wait for special key release
self.wait_for_bomb_release = False
self.throwing_time_left = 0 ##< for how longer (in ms) the player will be in a state of throwing (only for visuals)
self.state_backup = Player.STATE_IDLE_UP ##< used to restore previous state, for example after jump
self.jumping_to = (0,0) ##< coordinates of a tile the player is jumping to
self.teleporting_to = (0,0)
self.wait_for_tile_transition = False ##< used to stop the destination teleport from teleporting the player back immediatelly
self.invincible = False ##< can be used to make the player immortal
self.info_board_update_needed = True
self.kills = 0
self.wins = 0
self.items[GameMap.ITEM_BOMB] = 1
self.items[GameMap.ITEM_FLAME] = 1
#----------------------------------------------------------------------------
def get_kills(self):
return self.kills
#----------------------------------------------------------------------------
def set_kills(self, kills):
self.kills = kills
self.info_board_update_needed = True
#----------------------------------------------------------------------------
def get_wins(self):
return self.wins
#----------------------------------------------------------------------------
def set_wins(self, wins):
self.wins = wins
self.info_board_update_needed = True
#----------------------------------------------------------------------------
def info_board_needs_update(self):
if self.info_board_update_needed:
self.info_board_update_needed = False
return True
return False
#----------------------------------------------------------------------------
## Makes the player not react to bomb key immediatelly, but only after it
# has been released and pressed again.
def wait_for_bomb_action_release(self):
self.wait_for_bomb_release = True
#----------------------------------------------------------------------------
## Makes the player not react to special key immediatelly, but only after it
# has been released and pressed again.
def wait_for_special_action_release(self):
self.wait_for_special_release = True
#----------------------------------------------------------------------------
def is_walking(self):
return self.state in [Player.STATE_WALKING_UP,Player.STATE_WALKING_RIGHT,Player.STATE_WALKING_DOWN,Player.STATE_WALKING_LEFT]
#----------------------------------------------------------------------------
def is_boxing(self):
return self.boxing
## Checks if there are any bombs waiting to be detonated with detonator by
# the player.
#----------------------------------------------------------------------------
def detonator_is_active(self):
return len(self.detonator_bombs) > 0
#----------------------------------------------------------------------------
def kill(self, game_map):
if self.invincible:
return
self.info_board_update_needed = True
self.state = Player.STATE_DEAD
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_DEATH)
random_animation = random.choice((
Renderer.ANIMATION_EVENT_DIE,
Renderer.ANIMATION_EVENT_EXPLOSION,
Renderer.ANIMATION_EVENT_RIP,
Renderer.ANIMATION_EVENT_SKELETION))
game_map.add_animation_event(random_animation,Renderer.map_position_to_pixel_position(self.position,(0,-15)))
game_map.give_away_items(self.get_items())
#----------------------------------------------------------------------------
def is_enemy(self, another_player):
return self.team_number != another_player.get_team_number()
#----------------------------------------------------------------------------
## Returns a number that says which way the player is facing (0 - up, 1 - right,
# 2 - down, 3 - left).
def get_direction_number(self):
if self.state in [Player.STATE_IDLE_UP, Player.STATE_WALKING_UP]:
return 0
elif self.state in [Player.STATE_IDLE_RIGHT, Player.STATE_WALKING_RIGHT]:
return 1
elif self.state in [Player.STATE_IDLE_DOWN, Player.STATE_WALKING_DOWN]:
return 2
else:
return 3
#----------------------------------------------------------------------------
def is_dead(self):
return self.state == Player.STATE_DEAD
#----------------------------------------------------------------------------
## Returns a number of bomb the player can currently lay with multibomb (if
# the player doesn't have multibomb, either 1 or 0 will be returned).
def get_multibomb_count(self):
if not self.has_multibomb:
return 1 if self.bombs_left > 0 else 0
return self.bombs_left
#----------------------------------------------------------------------------
## Initialises the teleporting of the player with teleport they are standing on (if they're
# not standing on a teleport, nothing happens).
def teleport(self, game_map):
if self.wait_for_tile_transition:
return
current_tile = self.get_tile_position()
destination_coordinates = game_map.get_tile_at(current_tile).destination_teleport
if destination_coordinates == None:
return
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_TELEPORT)
self.move_to_tile_center()
self.teleporting_to = destination_coordinates
self.state_backup = self.state
self.state = Player.STATE_TELEPORTING
self.state_time = 0
self.wait_for_tile_transition = True
#----------------------------------------------------------------------------
def get_items(self):
result = []
for item in self.items:
result += [item for i in range(self.items[item])]
return result
#----------------------------------------------------------------------------
def send_to_air(self, game_map):
if self.state == Player.STATE_IN_AIR:
return
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_TRAMPOLINE)
self.state_backup = self.state
self.state = Player.STATE_IN_AIR
self.jumping_from = self.get_tile_position()
landing_tiles = [] # potential tiles to land on
# find a landing tile
for y in range (self.jumping_from[1] - 3,self.jumping_from[1] + 4):
for x in range (self.jumping_from[0] - 3,self.jumping_from[0] + 4):
tile = game_map.get_tile_at((x,y))
if tile != None and game_map.tile_is_walkable((x,y)) and tile.special_object == None:
landing_tiles.append((x,y))
if len(landing_tiles) == 0: # this should practically not happen
self.jumping_to = (self.jumping_from[0],self.jumping_from[1] + 1)
else:
self.jumping_to = random.choice(landing_tiles)
self.state_time = 0
#----------------------------------------------------------------------------
def get_state_time(self):
return self.state_time
#----------------------------------------------------------------------------
def get_teleport_destination(self):
return self.teleporting_to
#----------------------------------------------------------------------------
def get_jump_destination(self):
return self.jumping_to
#----------------------------------------------------------------------------
def is_teleporting(self):
return self.state == Player.STATE_TELEPORTING
#----------------------------------------------------------------------------
def is_in_air(self):
return self.state == Player.STATE_IN_AIR
#----------------------------------------------------------------------------
def is_throwing(self):
return self.throwing_time_left > 0
#----------------------------------------------------------------------------
def can_box(self):
return self.has_boxing_glove
#----------------------------------------------------------------------------
def can_throw(self):
return self.has_throwing_glove
#----------------------------------------------------------------------------
def get_item_count(self, item):
if not item in self.items:
return 0
return self.items[item]
#----------------------------------------------------------------------------
## Gives player an item with given code (see GameMap class constants). game_map
# is needed so that sounds can be made on item pickup - if no map is provided,
# no sounds will be generated.
def give_item(self, item, game_map=None):
self.items[item] = 1 if not item in self.items else self.items[item] + 1
self.info_board_update_needed = True
if item == GameMap.ITEM_RANDOM:
item = random.choice((
GameMap.ITEM_BOMB,
GameMap.ITEM_FLAME,
GameMap.ITEM_SUPERFLAME,
GameMap.ITEM_MULTIBOMB,
GameMap.ITEM_SPRING,
GameMap.ITEM_SHOE,
GameMap.ITEM_SPEEDUP,
GameMap.ITEM_DISEASE,
GameMap.ITEM_BOXING_GLOVE,
GameMap.ITEM_DETONATOR,
GameMap.ITEM_THROWING_GLOVE
))
sound_to_make = SoundPlayer.SOUND_EVENT_CLICK
if item == GameMap.ITEM_BOMB:
self.bombs_left += 1
elif item == GameMap.ITEM_FLAME:
self.flame_length += 1
elif item == GameMap.ITEM_SUPERFLAME:
self.flame_length = max(GameMap.MAP_WIDTH,GameMap.MAP_HEIGHT)
elif item == GameMap.ITEM_MULTIBOMB:
self.has_multibomb = True
elif item == GameMap.ITEM_DETONATOR:
self.detonator_bombs_left = 3
elif item == GameMap.ITEM_SPRING:
self.has_spring = True
sound_to_make = SoundPlayer.SOUND_EVENT_SPRING
elif item == GameMap.ITEM_SPEEDUP:
self.speed = min(self.speed + Player.SPEEDUP_VALUE,Player.MAX_SPEED)
elif item == GameMap.ITEM_SHOE:
self.has_shoe = True
elif item == GameMap.ITEM_BOXING_GLOVE:
self.has_boxing_glove = True
elif item == GameMap.ITEM_THROWING_GLOVE:
self.has_throwing_glove = True
elif item == GameMap.ITEM_DISEASE:
chosen_disease = random.choice([
(Player.DISEASE_SHORT_FLAME,SoundPlayer.SOUND_EVENT_DISEASE),
(Player.DISEASE_SLOW,SoundPlayer.SOUND_EVENT_SLOW),
(Player.DISEASE_DIARRHEA,SoundPlayer.SOUND_EVENT_DIARRHEA),
(Player.DISEASE_FAST_BOMB,SoundPlayer.SOUND_EVENT_DISEASE),
(Player.DISEASE_REVERSE_CONTROLS,SoundPlayer.SOUND_EVENT_DISEASE),
(Player.DISEASE_SWITCH_PLAYERS,SoundPlayer.SOUND_EVENT_DISEASE),
(Player.DISEASE_NO_BOMB,SoundPlayer.SOUND_EVENT_DISEASE),
(Player.DISEASE_EARTHQUAKE,SoundPlayer.SOUND_EVENT_EARTHQUAKE)
])
if chosen_disease[0] == Player.DISEASE_SWITCH_PLAYERS:
if game_map != None:
players = filter(lambda p: not p.is_dead(), game_map.get_players())
player_to_switch = self
if len(players) > 1: # should always be true
while player_to_switch == self:
player_to_switch = random.choice(players)
my_position = self.get_position()
self.set_position(player_to_switch.get_position())
player_to_switch.set_position(my_position)
elif chosen_disease[0] == Player.DISEASE_EARTHQUAKE:
if game_map != None:
game_map.start_earthquake()
else:
self.disease = chosen_disease[0]
self.disease_time_left = Player.DISEASE_TIME
sound_to_make = chosen_disease[1]
if game_map != None and sound_to_make != None:
game_map.add_sound_event(sound_to_make)
#----------------------------------------------------------------------------
def lay_bomb(self, game_map, tile_coordinates=None):
new_bomb = Bomb(self)
if tile_coordinates != None:
new_bomb.set_position(tile_coordinates)
new_bomb.move_to_tile_center()
game_map.add_bomb(new_bomb)
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_BOMB_PUT)
self.bombs_left -= 1
if self.disease == Player.DISEASE_SHORT_FLAME:
new_bomb.flame_length = 1
elif self.disease == Player.DISEASE_FAST_BOMB:
new_bomb.explodes_in = Bomb.EXPLODES_IN_QUICK
if self.detonator_bombs_left > 0:
new_bomb.detonator_time = Bomb.DETONATOR_EXPIRATION_TIME
self.detonator_bombs.append(new_bomb)
self.detonator_bombs_left -= 1
#----------------------------------------------------------------------------
def get_bombs_left(self):
return self.bombs_left
#----------------------------------------------------------------------------
def has_kicking_shoe(self):
return self.has_shoe
#----------------------------------------------------------------------------
def get_disease(self):
return self.disease
#----------------------------------------------------------------------------
def get_disease_time(self):
return self.disease_time_left
#----------------------------------------------------------------------------
def set_disease(self, disease, time_left):
self.disease = disease
self.disease_time_left = time_left
#----------------------------------------------------------------------------
def bombs_have_spring(self):
return self.has_spring
#----------------------------------------------------------------------------
## Says how many of a given item the player has.
def how_many_items(self, item):
if not item in self.items:
return 0
return self.items[item]
#----------------------------------------------------------------------------
def set_number(self, number):
self.number = number
#----------------------------------------------------------------------------
def set_team_number(self, number):
self.team_number = number
#----------------------------------------------------------------------------
## Must be called when this player's bomb explodes so that their bomb limit is increased again.
def bomb_exploded(self):
self.bombs_left += 1
#----------------------------------------------------------------------------
def get_number(self):
return self.number
#----------------------------------------------------------------------------
def get_team_number(self):
return self.team_number
#----------------------------------------------------------------------------
def get_state(self):
return self.state
#----------------------------------------------------------------------------
def get_state_time(self):
return self.state_time
#----------------------------------------------------------------------------
def get_flame_length(self):
return self.flame_length
#----------------------------------------------------------------------------
## Gets a direction vector (x and y: 0, 1 or -1) depending on where the player is facing.
def get_direction_vector(self):
if self.state in [Player.STATE_WALKING_UP, Player.STATE_IDLE_UP]:
return (0,-1)
elif self.state in [Player.STATE_WALKING_RIGHT, Player.STATE_IDLE_RIGHT]:
return (1,0)
elif self.state in [Player.STATE_WALKING_DOWN, Player.STATE_IDLE_DOWN]:
return (0,1)
else: # left
return (-1,0)
#----------------------------------------------------------------------------
def get_forward_tile_position(self):
direction_vector = self.get_direction_vector()
position = self.get_tile_position()
return (position[0] + direction_vector[0],position[1] + direction_vector[1])
#----------------------------------------------------------------------------
def __manage_input_actions(self, input_actions, game_map, distance_to_travel):
moved = False # to allow movement along only one axis at a time
detonator_triggered = False
special_was_pressed = False
bomb_was_pressed = False
for item in input_actions:
if item[0] != self.number:
continue # not an action for this player
input_action = item[1]
if self.disease == Player.DISEASE_REVERSE_CONTROLS:
input_action = PlayerKeyMaps.get_opposite_action(input_action)
if not moved:
if input_action == PlayerKeyMaps.ACTION_UP:
self.position[1] -= distance_to_travel
self.state = Player.STATE_WALKING_UP
moved = True
elif input_action == PlayerKeyMaps.ACTION_DOWN:
self.position[1] += distance_to_travel
self.state = Player.STATE_WALKING_DOWN
moved = True
elif input_action == PlayerKeyMaps.ACTION_RIGHT:
self.position[0] += distance_to_travel
self.state = Player.STATE_WALKING_RIGHT
moved = True
elif input_action == PlayerKeyMaps.ACTION_LEFT:
self.position[0] -= distance_to_travel
self.state = Player.STATE_WALKING_LEFT
moved = True
if input_action == PlayerKeyMaps.ACTION_BOMB:
bomb_was_pressed = True
if not self.wait_for_bomb_release and self.bombs_left >= 1 and not game_map.tile_has_bomb(self.position) and not self.disease == Player.DISEASE_NO_BOMB:
self.putting_bomb = True
if input_action == PlayerKeyMaps.ACTION_BOMB_DOUBLE: # check multibomb
if self.has_throwing_glove:
self.throwing = True
elif self.has_multibomb:
self.putting_multibomb = True
if input_action == PlayerKeyMaps.ACTION_SPECIAL:
special_was_pressed = True
if not self.wait_for_special_release:
while len(self.detonator_bombs) != 0: # find a bomb to ddetonate (some may have exploded by themselves already)
self.info_board_update_needed = True
bomb_to_check = self.detonator_bombs.pop()
if bomb_to_check.has_detonator() and not bomb_to_check.has_exploded and bomb_to_check.movement != Bomb.BOMB_FLYING:
game_map.bomb_explodes(bomb_to_check)
detonator_triggered = True
self.wait_for_special_release = True # to not detonate other bombs until the key is released and pressed again
break
if not detonator_triggered and self.has_boxing_glove:
self.boxing = True
if moved:
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_WALK)
if not special_was_pressed:
self.wait_for_special_release = False
if not bomb_was_pressed:
self.wait_for_bomb_release = False
#----------------------------------------------------------------------------
def __manage_kick_box(self, game_map, collision_happened):
if collision_happened:
bomb_movement = Bomb.BOMB_NO_MOVEMENT
bomb_movement = {
Player.STATE_WALKING_UP: Bomb.BOMB_ROLLING_UP,
Player.STATE_WALKING_RIGHT: Bomb.BOMB_ROLLING_RIGHT,
Player.STATE_WALKING_DOWN: Bomb.BOMB_ROLLING_DOWN,
Player.STATE_WALKING_LEFT: Bomb.BOMB_ROLLING_LEFT
}[self.state]
direction_vector = self.get_direction_vector()
forward_tile = self.get_forward_tile_position()
if (self.has_shoe or self.has_boxing_glove) and game_map.tile_has_bomb(forward_tile):
# kick or box happens
bomb_hit = game_map.bomb_on_tile(forward_tile)
if self.boxing:
destination_tile = (forward_tile[0] + direction_vector[0] * 3,forward_tile[1] + direction_vector[1] * 3)
bomb_hit.send_flying(destination_tile)
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_KICK)
elif self.has_shoe:
# align the bomb in case of kicking an already moving bomb
bomb_position = bomb_hit.get_position()
if bomb_movement == Bomb.BOMB_ROLLING_LEFT or bomb_movement == Bomb.BOMB_ROLLING_RIGHT:
bomb_hit.set_position((bomb_position[0],math.floor(bomb_position[1]) + 0.5))
else:
bomb_hit.set_position((math.floor(bomb_position[0]) + 0.5,bomb_position[1]))
bomb_hit.movement = bomb_movement
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_KICK)
#----------------------------------------------------------------------------
def __resolve_collisions(self, game_map, distance_to_travel, previous_position):
collision_type = game_map.get_position_collision_type(self.position)
collision_happened = False
if collision_type == GameMap.COLLISION_TOTAL:
self.position = previous_position
collision_happened = True
else:
helper_mapping = {
GameMap.COLLISION_BORDER_UP: (Player.STATE_WALKING_UP, [Player.STATE_WALKING_LEFT, Player.STATE_WALKING_RIGHT], (0,distance_to_travel)),
GameMap.COLLISION_BORDER_DOWN: (Player.STATE_WALKING_DOWN, [Player.STATE_WALKING_LEFT, Player.STATE_WALKING_RIGHT], (0,-1 * distance_to_travel)),
GameMap.COLLISION_BORDER_RIGHT: (Player.STATE_WALKING_RIGHT, [Player.STATE_WALKING_UP, Player.STATE_WALKING_DOWN], (- 1 * distance_to_travel,0)),
GameMap.COLLISION_BORDER_LEFT: (Player.STATE_WALKING_LEFT, [Player.STATE_WALKING_UP, Player.STATE_WALKING_DOWN], (distance_to_travel,0))
}
if collision_type in helper_mapping:
helper_values = helper_mapping[collision_type]
if self.state == helper_values[0]: # walking against the border won't allow player to pass
self.position = previous_position
collision_happened = True
elif self.state in helper_values[1]: # walking along the border will shift the player sideways
self.position[0] += helper_values[2][0]
self.position[1] += helper_values[2][1]
return collision_happened
#----------------------------------------------------------------------------
## Sets the state and other attributes like position etc. of this player accoording to a list of input action (returned by PlayerKeyMaps.get_current_actions()).
def react_to_inputs(self, input_actions, dt, game_map):
if self.state == Player.STATE_DEAD or game_map.get_state() == GameMap.STATE_WAITING_TO_PLAY:
return
if self.state in [Player.STATE_IN_AIR,Player.STATE_TELEPORTING]:
self.state_time += dt
if self.state_time >= (Player.JUMP_DURATION if self.state == Player.STATE_IN_AIR else Player.TELEPORT_DURATION):
self.state = self.state_backup
self.state_time = 0
self.jumping_to = None
self.teleporting_to = None
else:
return
current_speed = self.speed if self.disease != Player.DISEASE_SLOW else Player.SLOW_SPEED
distance_to_travel = dt / 1000.0 * current_speed
self.throwing_time_left = max(0,self.throwing_time_left - dt)
self.position = list(self.position) # in case position was tuple
old_state = self.state
if self.state in (Player.STATE_WALKING_UP,Player.STATE_IDLE_UP):
self.state = Player.STATE_IDLE_UP
elif self.state in (Player.STATE_WALKING_RIGHT,Player.STATE_IDLE_RIGHT):
self.state = Player.STATE_IDLE_RIGHT
elif self.state in (Player.STATE_WALKING_DOWN,Player.STATE_IDLE_DOWN):
self.state = Player.STATE_IDLE_DOWN
else:
self.state = Player.STATE_IDLE_LEFT
previous_position = copy.copy(self.position) # in case of collision we save the previous position
self.putting_bomb = False
self.putting_multibomb = False
self.throwing = False
self.boxing = False
if self.disease == Player.DISEASE_DIARRHEA:
input_actions.append((self.number,PlayerKeyMaps.ACTION_BOMB)) # inject bomb put event
self.__manage_input_actions(input_actions, game_map, distance_to_travel)
# resolve collisions:
check_collisions = True
current_tile = self.get_tile_position()
previous_tile = Positionable.position_to_tile(previous_position)
transitioning_tiles = current_tile != previous_tile
if transitioning_tiles:
self.wait_for_tile_transition = False
if game_map.tile_has_bomb(current_tile): # first check if the player is standing on a bomb
if not transitioning_tiles:
check_collisions = False # no transition between tiles -> let the player move
collision_happened = False
if check_collisions:
collision_happened = self.__resolve_collisions(game_map, distance_to_travel, previous_position)
if self.putting_bomb and not game_map.tile_has_bomb(self.get_tile_position()) and not game_map.tile_has_teleport(self.position):
self.lay_bomb(game_map)
# check if bomb kick or box happens
self.__manage_kick_box(game_map, collision_happened)
if self.throwing:
bomb_thrown = game_map.bomb_on_tile(current_tile)
game_map.add_sound_event(SoundPlayer.SOUND_EVENT_THROW)
if bomb_thrown != None:
forward_tile = self.get_forward_tile_position()
direction_vector = self.get_direction_vector()
destination_tile = (forward_tile[0] + direction_vector[0] * 3,forward_tile[1] + direction_vector[1] * 3)
bomb_thrown.send_flying(destination_tile)
self.wait_for_bomb_release = True
self.throwing_time_left = 200
elif self.putting_multibomb: # put multibomb
current_tile = self.get_tile_position()
if self.state in (Player.STATE_WALKING_UP,Player.STATE_IDLE_UP):
tile_increment = (0,-1)
elif self.state in (Player.STATE_WALKING_RIGHT,Player.STATE_IDLE_RIGHT):
tile_increment = (1,0)
elif self.state in (Player.STATE_WALKING_DOWN,Player.STATE_IDLE_DOWN):
tile_increment = (0,1)
else: # left
tile_increment = (-1,0)
i = 1
while self.bombs_left > 0:
next_tile = (current_tile[0] + i * tile_increment[0],current_tile[1] + i * tile_increment[1])
if not game_map.tile_is_walkable(next_tile) or game_map.tile_has_player(next_tile):
break
self.lay_bomb(game_map,next_tile)
i += 1
# check disease
if self.disease != Player.DISEASE_NONE:
self.disease_time_left = max(0,self.disease_time_left - dt)
if self.disease_time_left == 0:
self.disease = Player.DISEASE_NONE
self.info_board_update_needed = True
if old_state == self.state:
self.state_time += dt
else: