/
bomberman.py
233 lines (182 loc) · 7.01 KB
/
bomberman.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
from person import Person
from input import _Getch, _GetchUnix
from termcolor import colored, cprint
from board import Board, b
from brick import all_bricks
from walls import add_walls
from bomb import Bomb
from enemy import Enemy, enemy_array, ini
import os
import time
'''Bomber man main class inherited person class'''
class Bomberman(Person):
# Constructor
def __init__(self, x=1, y=1):
Person.__init__(self, x, y)
self.bombs = 0
# To check condition so that person can not plant more than 1 bomb
b.game[x][y] = 'P'
'''Function to check if the coordinates are in bounds of the board or not
This uses the board copordinates from the b object
'''
def check_bound(self, x, y):
if x >= b.up + 1 and x <= b.down - 1:
if y >= b.left + 1 and y <= b.right - 1:
return True
else:
return False
else:
return False
'''Function to move the bomberman with change in x and y given as parameters
Return values -1 if person is there 0 if move was unsuccessful 1 for successful move
'''
def move_bomberman(self, dx, dy):
x = self.x + dx
y = self.y + dy
if self.check_bound(
x,
y) and b.game[x][y] != '/' and b.game[x][y] != 'X' and b.game[x][y] != 'B':
if b.game[x][y] == 'E':
return -1
b.game[self.x][self.y] = ' '
b.game[x][y] = 'P'
self.x = x
self.y = y
return 1
else:
return 0
''' Main move function of the bomberman
Main fucntion of the code:Features-
1. Scan characters from the stdin buffer
2. Also decreses the bomb counter
3. Handles the explosion of the bomb
'''
def move(self, level):
#Character read from imput function'''
getch = _Getch()
key = getch()
return_value = 0
'''Movement accordind to keys'''
if key is 'w':
return_value = self.move_bomberman(-1, 0)
elif key is 's':
return_value = self.move_bomberman(+1, 0)
elif key is 'a':
return_value = self.move_bomberman(0, -1)
elif key is 'd':
return_value = self.move_bomberman(0, +1)
elif key is 'q':
return -2
'''If pressed q exit the game right there'''
'''Function to handle the bomb and its timers'''
if self.bombs == 1:
# Decrease yhe bomb timer
self.p_bomb.tick_tick(self.x, self.y, level)
b.game[self.p_bomb.x][self.p_bomb.y] = 'B'
# If time is over call the neighbour function which displays
# explosion around the bomb
if(self.p_bomb.time_bomb == 0):
# Displays the board and calls the neighbour also
# call the function so that it calls the explosion and checks
# for different objects around the bomb
temp = self.p_bomb.tick_tick(self.x, self.y, level)
# Set the bombs to 0 and del the bomb object
self.bombs = 0
del self.p_bomb
if temp is -1:
return -1
elif (key is 'b') and self.bombs == 0:
'''Add new bomb if the key is b and bombs are 0'''
self.p_bomb = Bomb(self.x, self.y, time.time())
b.game[self.p_bomb.x][self.p_bomb.y] = 'B'
self.bombs = 1
# Returns return_value which stores the status of the moves
return return_value
def move_poly(obj_type, level, x=0, y=0, type_easy=0):
if isinstance(obj_type, Bomberman):
return obj_type.move(level)
else:
return obj_type.move(level, x, y, type_easy)
''' Main function which maintains levels,lives and all the objects of different classes'''
def main():
'''Initialisation:
level - to store current level
lives to store number of lives
type_easy - easy bot
type_hard-hard bot'''
level = 1
lives = 0
type_easy = 1
type_hard = 2
while(level <= 3):
'''Create bomberman and reinitialise the board as well as bricks and walls'''
p = Bomberman()
b.initialise_board()
all_bricks()
add_walls()
ini(level)
b.renderer(level)
lives = 0
while(lives <= 2):
return_value = 0
lives = lives + 1
while(True):
# if all the enemies are killed break the loop
if len(enemy_array) == 0:
break
# move the bomberman
return_value = move_poly(p, level)
# -1 tells that enemy has caught the person
if return_value is -1:
break
# -2 tells that user has enetered q
if return_value is -2:
return
flag = 0
# Move all the enemies one by one
# From second level onwards every fourth enemy is a medium bot
for i in enemy_array:
if enemy_array.index(i) % 4 != 1:
temp = move_poly(i, level, p.x, p.y, type_easy)
else:
temp = move_poly(i, level, p.x, p.y, type_hard)
if temp is -1:
flag = 1
# To display hard enemies with different colours replaced E
# with e and took into account in the renderer function
for i in enemy_array:
if enemy_array.index(i) % 4 == 1 and level >= 2:
b.game[i.x][i.y] = 'e'
# Add the time left for explosion to the b.game string which is
# accountes in the renderer function
if p.bombs == 1 and p.p_bomb.time_bomb > 1:
b.game[p.p_bomb.x][p.p_bomb.y] = str(
p.p_bomb.time_bomb - 1)
# Call to renderer after all the moves have been done
b.renderer(level)
'''Replacing e with E as all the functions have E for enemy'''
for i in enemy_array:
if enemy_array.index(i) % 4 != 1:
pass
else:
b.game[i.x][i.y] = 'E'
# Flag is 1 only when an emey catches the bomberman or bomber
# man moves towards the enemy
if flag is 1:
break
# No. of lives left
print("Lives left are", 3 - lives, ' and score is ', b.score)
'''Vacate the current position of the bomberman and reinitialise the bomberman at the starting point'''
b.game[p.x][p.y] = ' '
p.x = 1
p.y = 1
b.game[1][1] = 'P'
'''Some sleep so that player could see how it got killed'''
time.sleep(1.5)
'''If level is complete move to next level'''
if len(enemy_array) == 0:
level = level + 1
break
if lives == 3:
break
main()