-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·325 lines (316 loc) · 15.3 KB
/
main.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
from __future__ import print_function
import signal
import time
import os
from bomberman import Bomberman
from board import Board
from alarmexception import AlarmException
from getchunix import GetchUnix
from enemy import Enemy
from bomb import Bomb
from bricks import Bricks
disp = [['X' for x in range(90)]
for y in range(40)] # The main board which is printed
getch = GetchUnix()
def alarmHandler(signum, frame):
raise AlarmException
def input_to(timeout=1): # Input function
signal.signal(signal.SIGALRM, alarmHandler)
signal.alarm(timeout)
try:
text = getch()
signal.alarm(0)
return text
except AlarmException:
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return ''
player = Bomberman() # different objects being created
b = Board()
b.display(disp)
e = Enemy()
u = Bomb()
s = Bricks()
e.generate_enemies(disp, 10) # enemies being generated
for i in range(10): # if you want more enemies just change the number 10 in this and above line
e.change(i, disp)
s.generate_bricks(disp) # brikcs being generated
player.start(disp)
b.output(disp)
planted = 0 # different variables for various purposes
count = 0
flag = 0
die = 0
won = 1
score = 0
enemieskilled = 0
t = 1
done = [0 for i in range(100)]
# array for whether we have checked enemy number i
dons = [0 for i in range(100)]
# array for whether we have checked brick number i
while 1: # the main loop begins
length = e.enemies.__len__()
lenbricks = s.all_bricks.__len__()
if t == 1:
now = time.time()
t = 0
if time.time() > now + 0.1: # loop for movement of enemy
i = 0
while i < length / 2:
e.move(i, disp, player)
i = i + 1
t = 1
won = 1
score = 0
bricksdestroyed = 0
for i in range(length): # for calcuating scores
if e.enemies[i] != -1:
won = 0
else:
if done[i] == 0:
enemieskilled = enemieskilled + 1
done[i] = 1
if i == length - 1:
score = (enemieskilled / 2) * 100
for i in range(lenbricks):
if s.all_bricks[i] == -1:
bricksdestroyed = bricksdestroyed + 1
score = score + (bricksdestroyed / 2) * 20
if won == 1: # if all the enemies are killed then you won the game
print("You Won")
exit(0)
c = input_to() # input of the character
if c == 's': # down movement
if player.left_x <= 28 and player.left_y <= 80:
if planted == 0 or (flag == 0 and planted == 1):
if disp[player.left_x + 2][player.left_y] != 'X':
if disp[player.left_x + 2][player.left_y] != '%':
disp[player.left_x + 2][player.left_y + 1] = 'B'
disp[player.left_x + 2][player.left_y + 2] = 'B'
disp[player.left_x + 2][player.left_y + 3] = 'B'
disp[player.left_x + 2][player.left_y] = 'B'
disp[player.left_x + 3][player.left_y] = 'B'
disp[player.left_x + 3][player.left_y + 1] = 'B'
disp[player.left_x + 3][player.left_y + 2] = 'B'
disp[player.left_x + 3][player.left_y + 3] = 'B'
disp[player.left_x][player.left_y] = ' '
disp[player.left_x][player.left_y + 1] = ' '
disp[player.left_x][player.left_y + 2] = ' '
disp[player.left_x][player.left_y + 3] = ' '
disp[player.left_x + 1][player.left_y] = ' '
disp[player.left_x + 1][player.left_y + 1] = ' '
disp[player.left_x + 1][player.left_y + 2] = ' '
disp[player.left_x + 1][player.left_y + 3] = ' '
player.left_x = player.left_x + 2
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if player.left_x <= 28 and player.left_y <= 80 and planted == 1 and flag == 1:
if disp[player.left_x + 2][player.left_y] != 'X':
if disp[player.left_x + 2][player.left_y] != '%':
disp[player.left_x + 2][player.left_y + 1] = 'B'
disp[player.left_x + 2][player.left_y + 2] = 'B'
disp[player.left_x + 2][player.left_y + 3] = 'B'
disp[player.left_x + 2][player.left_y] = 'B'
disp[player.left_x + 3][player.left_y] = 'B'
disp[player.left_x + 3][player.left_y + 1] = 'B'
disp[player.left_x + 3][player.left_y + 2] = 'B'
disp[player.left_x + 3][player.left_y + 3] = 'B'
disp[player.left_x][player.left_y] = '3'
disp[player.left_x][player.left_y + 1] = '3'
disp[player.left_x][player.left_y + 2] = '3'
disp[player.left_x][player.left_y + 3] = '3'
disp[player.left_x + 1][player.left_y] = '3'
disp[player.left_x + 1][player.left_y + 1] = '3'
disp[player.left_x + 1][player.left_y + 2] = '3'
disp[player.left_x + 1][player.left_y + 3] = '3'
player.left_x = player.left_x + 2
die = 0
flag = 0
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if c == 'w': # up movement
if player.left_x >= 2 and player.left_y <= 80:
if planted == 0 or (flag == 0 and planted == 1):
if disp[player.left_x - 2][player.left_y] != 'X':
if disp[player.left_x - 2][player.left_y] != '%':
disp[player.left_x - 2][player.left_y + 1] = 'B'
disp[player.left_x - 2][player.left_y + 2] = 'B'
disp[player.left_x - 2][player.left_y + 3] = 'B'
disp[player.left_x - 2][player.left_y] = 'B'
disp[player.left_x - 1][player.left_y] = 'B'
disp[player.left_x - 1][player.left_y + 1] = 'B'
disp[player.left_x - 1][player.left_y + 2] = 'B'
disp[player.left_x - 1][player.left_y + 3] = 'B'
disp[player.left_x][player.left_y] = ' '
disp[player.left_x][player.left_y + 1] = ' '
disp[player.left_x][player.left_y + 2] = ' '
disp[player.left_x][player.left_y + 3] = ' '
disp[player.left_x + 1][player.left_y] = ' '
disp[player.left_x + 1][player.left_y + 1] = ' '
disp[player.left_x + 1][player.left_y + 2] = ' '
disp[player.left_x + 1][player.left_y + 3] = ' '
player.left_x = player.left_x - 2
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if player.left_x >= 2 and player.left_y <= 80 and planted == 1 and flag == 1:
if disp[player.left_x - 2][player.left_y] != 'X':
if disp[player.left_x - 2][player.left_y] != '%':
disp[player.left_x - 2][player.left_y + 1] = 'B'
disp[player.left_x - 2][player.left_y + 2] = 'B'
disp[player.left_x - 2][player.left_y + 3] = 'B'
disp[player.left_x - 2][player.left_y] = 'B'
disp[player.left_x - 1][player.left_y] = 'B'
disp[player.left_x - 1][player.left_y + 1] = 'B'
disp[player.left_x - 1][player.left_y + 2] = 'B'
disp[player.left_x - 1][player.left_y + 3] = 'B'
disp[player.left_x][player.left_y] = '3'
disp[player.left_x][player.left_y + 1] = '3'
disp[player.left_x][player.left_y + 2] = '3'
disp[player.left_x][player.left_y + 3] = '3'
disp[player.left_x + 1][player.left_y] = '3'
disp[player.left_x + 1][player.left_y + 1] = '3'
disp[player.left_x + 1][player.left_y + 2] = '3'
disp[player.left_x + 1][player.left_y + 3] = '3'
player.left_x = player.left_x - 2
flag = 0
die = 0
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if c == 'd': # right movement
if player.left_x <= 28 and player.left_y <= 80:
if planted == 0 or (flag == 0 and planted == 1):
if disp[player.left_x][player.left_y + 4] != 'X':
if disp[player.left_x][player.left_y + 4] != '%':
disp[player.left_x][player.left_y + 7] = 'B'
disp[player.left_x][player.left_y + 6] = 'B'
disp[player.left_x][player.left_y + 5] = 'B'
disp[player.left_x][player.left_y + 4] = 'B'
disp[player.left_x + 1][player.left_y + 4] = 'B'
disp[player.left_x + 1][player.left_y + 5] = 'B'
disp[player.left_x + 1][player.left_y + 6] = 'B'
disp[player.left_x + 1][player.left_y + 7] = 'B'
disp[player.left_x][player.left_y] = ' '
disp[player.left_x][player.left_y + 1] = ' '
disp[player.left_x][player.left_y + 2] = ' '
disp[player.left_x][player.left_y + 3] = ' '
disp[player.left_x + 1][player.left_y] = ' '
disp[player.left_x + 1][player.left_y + 1] = ' '
disp[player.left_x + 1][player.left_y + 2] = ' '
disp[player.left_x + 1][player.left_y + 3] = ' '
player.left_y = player.left_y + 4
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if player.left_x <= 28 and player.left_y <= 80 and planted == 1 and flag == 1:
if disp[player.left_x][player.left_y + 4] != 'X':
if disp[player.left_x][player.left_y + 4] != '%':
disp[player.left_x][player.left_y + 7] = 'B'
disp[player.left_x][player.left_y + 6] = 'B'
disp[player.left_x][player.left_y + 5] = 'B'
disp[player.left_x][player.left_y + 4] = 'B'
disp[player.left_x + 1][player.left_y + 4] = 'B'
disp[player.left_x + 1][player.left_y + 5] = 'B'
disp[player.left_x + 1][player.left_y + 6] = 'B'
disp[player.left_x + 1][player.left_y + 7] = 'B'
disp[player.left_x][player.left_y] = '3'
disp[player.left_x][player.left_y + 1] = '3'
disp[player.left_x][player.left_y + 2] = '3'
disp[player.left_x][player.left_y + 3] = '3'
disp[player.left_x + 1][player.left_y] = '3'
disp[player.left_x + 1][player.left_y + 1] = '3'
disp[player.left_x + 1][player.left_y + 2] = '3'
disp[player.left_x + 1][player.left_y + 3] = '3'
player.left_y = player.left_y + 4
flag = 0
die = 0
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if c == 'a': # left movement
if player.left_x <= 28 and player.left_y >= 4:
if planted == 0 or (flag == 0 and planted == 1):
if disp[player.left_x][player.left_y - 1] != 'X':
if disp[player.left_x][player.left_y - 1] != '%':
disp[player.left_x][player.left_y - 2] = 'B'
disp[player.left_x][player.left_y - 3] = 'B'
disp[player.left_x][player.left_y - 4] = 'B'
disp[player.left_x][player.left_y - 1] = 'B'
disp[player.left_x + 1][player.left_y - 1] = 'B'
disp[player.left_x + 1][player.left_y - 2] = 'B'
disp[player.left_x + 1][player.left_y - 3] = 'B'
disp[player.left_x + 1][player.left_y - 4] = 'B'
disp[player.left_x][player.left_y] = ' '
disp[player.left_x][player.left_y + 1] = ' '
disp[player.left_x][player.left_y + 2] = ' '
disp[player.left_x][player.left_y + 3] = ' '
disp[player.left_x + 1][player.left_y] = ' '
disp[player.left_x + 1][player.left_y + 1] = ' '
disp[player.left_x + 1][player.left_y + 2] = ' '
disp[player.left_x + 1][player.left_y + 3] = ' '
player.left_y = player.left_y - 4
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if player.left_x <= 28 and player.left_y >= 4 and planted == 1 and flag == 1:
if disp[player.left_x][player.left_y - 1] != 'X':
if disp[player.left_x][player.left_y - 1] != '%':
disp[player.left_x][player.left_y - 2] = 'B'
disp[player.left_x][player.left_y - 3] = 'B'
disp[player.left_x][player.left_y - 4] = 'B'
disp[player.left_x][player.left_y - 1] = 'B'
disp[player.left_x + 1][player.left_y - 1] = 'B'
disp[player.left_x + 1][player.left_y - 2] = 'B'
disp[player.left_x + 1][player.left_y - 3] = 'B'
disp[player.left_x + 1][player.left_y - 4] = 'B'
disp[player.left_x][player.left_y] = '3'
disp[player.left_x][player.left_y + 1] = '3'
disp[player.left_x][player.left_y + 2] = '3'
disp[player.left_x][player.left_y + 3] = '3'
disp[player.left_x + 1][player.left_y] = '3'
disp[player.left_x + 1][player.left_y + 1] = '3'
disp[player.left_x + 1][player.left_y + 2] = '3'
disp[player.left_x + 1][player.left_y + 3] = '3'
player.left_y = player.left_y - 4
flag = 0
die = 0
os.system('clear')
b.output(disp)
print("Your Score is %d" % score)
if c == 'b': # for putting bomb
u.plant(player, disp)
planted = 1
flag = 1
die = 1
x = player.left_x
y = player.left_y
cur = time.time()
p = 2
if planted == 1: # for printing time left in bomb crack
if time.time() > cur + 0.2:
count = count + 1
disp[x][y] = p
disp[x][y + 1] = p
disp[x][y + 2] = p
disp[x][y + 3] = p
disp[x + 1][y] = p
disp[x + 1][y + 1] = p
disp[x + 1][y + 2] = p
disp[x + 1][y + 3] = p
p = p - 1
cur = time.time()
if count == 3:
u.explode(player, e, s, disp, die)
u.clear(disp)
planted = 0
count = 0
if c == 'q': # to stop the game
exit(1)
time.sleep(0.3)
# print("lauda") # print("bahar")