/
201125005_robot.py
425 lines (412 loc) · 17.4 KB
/
201125005_robot.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
import curses
from curses import *
import random
# -----------------------------------------------------------------------------
class curses_screen:
""" Creates A Screen """
def __enter__(self):
self.stdscr = curses.initscr()
curses.cbreak()
self.stdscr.clear()
curses.noecho()
self.stdscr.keypad(1)
self.stdscr.border(0)
return self.stdscr
def __exit__(self,a,b,c):
curses.echo()
curses.endwin()
self.stdscr.getch()
self.stdscr.clear()
self.stdscr.refresh()
# -----------------------------------------------------------------------------
class curses_window:
""" Creates A Window """
def __init__(self):
curses.curs_set(0)
curses.noecho()
self.win=curses.newwin(LINES-4,COLS-4,2,2)
self.win.clear()
self.win.refresh()
self.win.keypad(1)
#------------------------------------------------------------------------------
class Gun:
""" Generates Firing Machines """
def __init__(self,Arena,Y,X,Pos,LINES,COLS,Level):
""" Initializing Gun """
if LEVEL==2:
self.Y=Y
self.X=X+3 if Pos==0 else X-1
list(map(lambda x: [Arena.addch(Y+y,X+x,'#') for y in range(3)],list(range(3))))
if Pos==0:
Arena.addch(Y,X+2,' ')
Arena.addch(Y+2,X+2,' ')
if Pos==1:
Arena.addch(Y,X,' ')
Arena.addch(Y+2,X,' ')
if LEVEL==3:
if Pos ==0: list(map(lambda x: Arena.addstr(1,x,"### "),list(range(3,COLS-7,4))))
else: list(map(lambda x: Arena.addstr(LINES-1,x,"### "),list(range(3,COLS-7,4))))
self.yx=list(range(4,COLS-3,4))
def fire_small(self,Pos):
""" Calculate Position Of Small Bullet """
if Pos==0:
return (self.X+2) if self.X<COLS-2 else 4
else:
return (self.X-2) if self.X>1 else COLS-5
def getBullPos(self,Arena,Pos):
""" Firing Small Bullet """
if Arena.inch(self.Y+1,self.X)&255!=ord('D'):
Arena.addch(self.Y+1,self.X,' ')
self.X=self.fire_small(Pos)
if Pos==0:
if Arena.inch(self.Y+1,self.X)&255!=ord('D'):
Arena.addch(self.Y+1,self.X,'>')
elif Pos==1:
if Arena.inch(self.Y+1,self.X)&255!=ord('D'):
Arena.addch(self.Y+1,self.X,'<')
return self.Y+1,self.X
def fire_big(self,Arena,flag,x_loc,y_loc,Pos):
""" Calculate Position Of Big Bullet """
if flag==0:
x_loc=random.choice(self.yx)
if Pos==0:
if Arena.inch(y_loc,x_loc)&255!=ord('D'):
Arena.addstr(y_loc,x_loc,'/')
if Arena.inch(y_loc+1,x_loc)&255!=ord('D'):
Arena.addstr(y_loc+1,x_loc,'\\')
return y_loc+2,x_loc
else:
if Arena.inch(y_loc,x_loc)&255!=ord('D'):
Arena.addstr(y_loc,x_loc,'/')
if Arena.inch(y_loc-1,x_loc)&255!=ord('D'):
Arena.addstr(y_loc-1,x_loc,'\\')
return y_loc-2,x_loc
def clear(self,Arena,x_loc,Pos):
""" Clearing Game Window """
L=list(range(2,LINES-1))
if Pos==0:
for i in L:
if Arena.inch(i,x_loc)&255!=ord('D'):
Arena.addstr(i,x_loc,' ')
else:
L.reverse()
for i in L:
if Arena.inch(i,x_loc)&255!=ord('D'):
Arena.addstr(i,x_loc,' ')
#+-----------------------------------------------------------------------------
def Print_File(self,file,y,x,LINES,COLS):
""" Printing ASCII Art """
F=open(file)
F=F.readlines()
for lines in F:
self.addstr(y,x,lines);
y+=1
if y==LINES-1: break
return y
def Pause2(self,time):
""" Simple Pause """
for i in range(25):
self.timeout(time)
getKey=self.getch()
class MyGame(curses_window):
""" Main Game Class """
def Pause(self):
""" Pause = Press P or p """
while True:
if self.win.getch() in [80,112]:
break
def Display_Hash(self,LINES,COLS,x_loc,y_loc):
""" Display Animated Hash """
a,b,x,y,flag,ctr=LINES-2,1,1,COLS-2,0,3
while True:
self.win.timeout(ctr)
getKey=self.win.getch()
if len([(l,m) for l in range(y_loc-4,y_loc+4*2) for m in range(x_loc-5,x_loc+5*2) if (l,m) ==(a,b)])==0:
self.win.addch(a,b,'#')
if len([(l,m) for l in range(y_loc-4,y_loc+4*2) for m in range(x_loc-5,x_loc+5*2) if (l,m) ==(x,y)])==0:
self.win.addch(x,y,'#')
if flag==0:
if b==(COLS-2):
a,flag,x=a-1,1,x+1
else:
b,y=b+1,y-1
else:
if b==1:
a,x,flag=a-1,x+1,0
else:
b,y=b-1,y+1
if (abs(b-y)==1 or b==y) and a==x:
ctr=1
if a==0:
break
def Display_Crash(self,Game,x_loc,y_loc):
""" Display Flashing Effect """
j,flag=3,0
for i in range(5):
Game.getch()
curses.flash()
Game.clear()
def Start(self,Game,LINES,COLS,LEVEL):
""" Initialize The Game @ Given Level -- Heart Of Program"""
Game.clear()
Game.border('|','|','-','-','+','+','+','+')
x_loc=random.randint(8,COLS-10) # Generating Random Coordinates
y_loc=random.randint(8,LINES-10)
ROBO.Place(y_loc,x_loc,Game) #Placing The Robot
DIFFUSE_C,N_Codes,DC_xy,Key,B_xy=0,10,{},KEY_LEFT,{} #Specifying Constants Which Can Be Altered By The Programmer
flag=0
bull_y,bull_x=LINES//5+1,2
l,m=(random.randint(4,LINES-5),random.randint(4,COLS-6))
for x in range(3):
for y in range(3):
B_xy[(l+x,m+y)]=1
for x in range(N_Codes): DC_xy[(random.randint(1,LINES-5),random.randint(4,COLS-6))]=x
list(map(lambda x: Game.addch(x[0],x[1],'D'),DC_xy)) #Placing Diffuce Codes & Bomb
list(map(lambda x: Game.addch(x[0],x[1],'0'),B_xy))
global Score
if LEVEL==1:
Score=0
Limit=401
if LEVEL==2:
Gun1=Gun(Game,LINES//5,1,0,LINES,COLS,LEVEL) #Creating Gun Objects
Gun2=Gun(Game,3*LINES//5,1,0,LINES,COLS,LEVEL)
Gun3=Gun(Game,2*LINES//5,COLS-4,1,LINES,COLS,LEVEL)
Gun4=Gun(Game,4*LINES//5,COLS-4,1,LINES,COLS,LEVEL)
Limit=601
if LEVEL==3:
MultiGun1=Gun(Game,1,COLS,0,LINES,COLS,LEVEL)#Creating Lightening Objects
MultiGun2=Gun(Game,1,COLS,1,LINES,COLS,LEVEL)
La=[(1,x) for x in range(COLS)]
mxa_loc,mya_loc=4,2
Lb=[(LINES-1,x) for x in range(COLS)]
mxb_loc,myb_loc=LINES-2,COLS-2
flag2=0
Limit=801
time=0
REASON=""
while True:
if Key==27:
self.End(self.win,Score,LEVEL,LINES,COLS)
return -1,REASON
if time==Limit:
list(map(lambda x: [Game.addch(l+x,m+y,'0') for y in range(3)],list(range(3))))
Game.addch(l+3//2,m+3//2,'0')
for i in range(3):
Game.addstr(l-2,m-2,"BEEP!")
Pause2(self.win,25)
Game.addstr(l-2,m-2," ")
Pause2(self.win,25)
Pause2(self.win,3)
Game.addstr(l-2,m,"KABOOM!")
self.Display_Hash(LINES,COLS,m,l)
REASON="*****TIME FINISHED & BOMB EXPLODED*****"
time+=1
if flag==0:
list(map(lambda x: [Game.addch(l+x,m+y,' ') for y in range(3)],list(range(3))))
Game.addch(l+3//2,m+3//2,'0')
flag=1
else:
list(map(lambda x: [Game.addch(l+x,m+y,'0') for y in range(3)],list(range(3))))
Game.addch(l+3//2,m+3//2,' ')
flag=0
Game.addstr(0,36," ")
Game.addstr(0,5,"NUMBER OF DIFFUSE CODES LEFT: "+str(N_Codes-DIFFUSE_C))
Game.addstr(0,COLS-4,"")
Game.addstr(0,COLS-16,"TIME LEFT: "+str(Limit-time))
Game.addstr(0,COLS//2+7,"")
Game.addstr(0,COLS//2,"SCORE: "+str(Score))
Game.timeout(180)
getKey=Game.getch()
if getKey in [80,112]:
Game.addstr(LINES-2,2,"GAME PAUSED")
self.Pause()
Game.addstr(LINES-2,2," ")
Key=Key if getKey not in [258,259,260,261,27] else getKey
x_loc,y_loc=ROBO.Move(y_loc,x_loc,Game,Key)
if LEVEL==2:
L=[]
L.append(Gun1.getBullPos(Game,0))
L.append(Gun2.getBullPos(Game,0))
L.append(Gun3.getBullPos(Game,1))
L.append(Gun4.getBullPos(Game,1))
if LEVEL==3:
if flag2==0:
L=[(mya_loc,mxa_loc),(myb_loc,mxb_loc)]
mya_loc,mxa_loc=MultiGun1.fire_big(Game,flag2,mxa_loc,2,0)
myb_loc,mxb_loc=MultiGun2.fire_big(Game,flag2,mxb_loc,LINES-2,1)
flag2=1
else:
mya_loc,mxa_loc=MultiGun1.fire_big(Game,flag2,mxa_loc,mya_loc,0)
myb_loc,mxb_loc=MultiGun2.fire_big(Game,flag2,mxb_loc,myb_loc,1)
L[len(L):]=[(mya_loc,mxa_loc),(myb_loc,mxb_loc)]
if abs(mya_loc-LINES)<3:
flag2=0
MultiGun1.clear(Game,mxa_loc,0)
MultiGun2.clear(Game,mxb_loc,1)
for x in range(x_loc,x_loc+5):
for y in range(y_loc,y_loc+4):
if (y,x) in B_xy:
if DIFFUSE_C<N_Codes:
self.Display_Hash(LINES,COLS,x_loc,y_loc)
REASON="****YOU DIDN'T HAVE ENOUGH DIFFUSE CODES!****"
return 0,REASON
else:
return 1,REASON
if (y,x) in DC_xy:
DIFFUSE_C+=1
Score+=DIFFUSE_C*(Limit-time)
DC_xy.pop((y,x))
if LEVEL==2:
if (y,x) in L:
curses.flash()
self.Display_Hash(LINES,COLS,x_loc,y_loc)
REASON="****YOUR ROBOT GOT SCREWED!****"
return 0,REASON
if LEVEL==3:
if (y,x) in L or (y,x) in La+Lb:
curses.flash()
REASON="****YOUR ROBOT WAS STRUCK BY LIGHTENING!****"
self.Display_Hash(LINES,COLS,x_loc,y_loc)
return 0,REASON
if x_loc in [0,COLS-5] or y_loc in [0,LINES-4]:
self.Display_Crash(Game,x_loc,y_loc)
REASON="****YOUR ROBOT WENT OUT OF BOUNDS!****"
return 0,REASON
def End(Arena,self,Score,LEVEL,LINES,COLS):
""" Ending The Game """
self.clear()
F=open("GameOver.txt")
F=F.readlines()
y,x=2,2
for lines in F:
self.addstr(y,x,lines)
y+=1
self.border('|','|','-','-','+','+','+','+')
self.addstr(LINES//2,COLS//2,"Your Score :"+str(Score))
self.addstr(LINES//2+1,COLS//2,"You Completed "+str(LEVEL-1)+" Level(s)!")
Key=self.getch()
while True:
self.addstr(LINES//2+2,COLS//2,"Press Escape To Exit...")
if Key==27: break
Key=self.getch()
def Loading(self,LINES,COLS):
""" Loading A Particular Level """
self.win.addch(LINES-2,2,'[')
self.win.addch(LINES-2,COLS-2,']')
x=3
while x!=COLS-2:
self.win.addch(LINES-2,x,'#')
x+=1
Pause2(self.win,1)
def Display_Choice(Arena,self,REASON,LINES,COLS):
""" Display Choices When Robot Dies """
self.clear()
self.border('|','|','-','-','+','+','+','+')
self.addstr(LINES//2-10,COLS//2-10,REASON)
self.addstr(LINES//2-9,COLS//2-10,"Choices Available: ")
self.addstr(LINES//2-8,COLS//2-10,"1: Restart Level")
self.addstr(LINES//2-7,COLS//2-10,"2: Exit Game")
Key=self.getch()
while True:
if Key in [49,50]:
return Key-48
elif Key!=-1:
self.addstr(LINES//2-5,COLS//2-10,"Press 1 or 2 Only!")
Key=self.getch()
# -----------------------------------------------------------------------------
#ROBOT_STYLES:
R=[[[' ','i',' ','i',' '],['[','*','_','*',']'],['/','|','_','|','\\'],[' ','/',' ','\\',' ']],
[[' ','!',' ','!',' '],['[','+','_','+',']'],['!','.','_','.','!'],['_','|',' ','|','_']],
[[' ','|','_','|',' '],['d','O','_','O','b'],[' ','[','_',']',' '],[' ','^',' ','^',' ']],
[[' ','\\',' ','/',' '],['[','@','_','@',']'],['/','|','_','|','\\'],[' ','d',' ','b',' ']],
[['\\','^',' ','^','/'],['[','o',' ','o',']'],['$','[','_',']','$'],['_','|',' ','|','_']],
[['\\','_','*','_','/'],['|','^','_','^','|'],['<','|',' ','|','>'],[' ','0',' ','0',' ']],
[[' ','M',' ','M',' '],['<','@','_','@','>'],['\\','\"','_','\"','/'],[' ','%',' ','%',' ']],
[[' ','*','_','*',' '],['[','0','_','0',']'],['/','(','+',')','\\'],['_','|',' ','|','_']],
[[' ','$','^','$',' '],['{','x','_','x','}'],['/',')','_','(','\\'],[' ','u',' ','u',' ']],
[['_','|','^','|','_'],['[','p','_','p',']'],['-','|','_','|','-'],['\\','0','_','0','/']]]
class Robot:
""" ROBOT Definitions """
def __init__(self,choice):
""" Initialize Robot On Selection Criteria """
self.type=R[choice]
def Place(self,y_loc,x_loc,scr):
""" Place Robot @ Given Coordinates """
list(map(lambda i: [scr.addch(i,j,self.type[i-y_loc][j-x_loc]) for j in range(x_loc,x_loc+5)],list(range(y_loc,y_loc+4))))
def Move(self,y_loc,x_loc,scr,Key):
""" Move Robot @ Given Coordinates """
x_a,y_a=(Key==KEY_LEFT and -1 or Key==KEY_RIGHT and 1),(Key==KEY_UP and -1 or Key==KEY_DOWN and 1)
x_r,y_r=(Key==KEY_LEFT and 5 or Key==KEY_RIGHT and -1),(Key==KEY_UP and 4 or Key==KEY_DOWN and -1)
start=(((Key==KEY_LEFT or Key==KEY_RIGHT) and y_loc+y_a) or ((Key==KEY_UP or Key==KEY_DOWN) and x_loc+x_a))
end=(((Key==KEY_LEFT or Key==KEY_RIGHT) and y_loc+y_a+4) or ((Key==KEY_UP or Key==KEY_DOWN) and x_loc+x_a+5))
self.Place(y_loc+y_a,x_loc+x_a,scr)
if Key==KEY_LEFT or Key==KEY_RIGHT:
list(map(lambda i: scr.addch(i,x_loc+x_a+x_r,' '),list(range(start,end))))
if Key==KEY_UP or Key==KEY_DOWN:
list(map(lambda i: scr.addch(y_loc+y_a+y_r,i,' '),list(range(start,end))))
return x_loc+x_a,y_loc+y_a
# -----------------------------------------------------------------------------
with curses_screen() as welcome:
""" Welcome Is An Object Of The Class: curses_screen """
LINES,COLS=welcome.getmaxyx()
y=Print_File(welcome,"Robot2.txt",2,2,LINES,COLS)
y=Print_File(welcome,"Robot1.txt",2,COLS//2,LINES,COLS)
welcome.border(0)
welcome.addstr(y-3,COLS//2-30,"WELCOME TO ROBO-BOMB-DEFUSER!")
welcome.addstr(y-2,COLS//2-30,"Press Any Key To Continue...")
# -----------------------------------------------------------------------------
with curses_screen() as Select_Robot:
""" Select_Robot Is An Object Of The Class: curses_screen """
LINES,COLS=Select_Robot.getmaxyx()
Select_Robot.addstr(2,(COLS-18)//2,"SELECT YOUR ROBOT!")
MyRobo={}
for i in range(10):
MyRobo[i]=Robot(i)
MyRobo[i].Place(LINES//2,(((COLS-50)//11)+5)*(i+1),Select_Robot)
Select_Robot.addstr(LINES//2+5,(((COLS-50)//11)+5)*(i+1),"Robot "+"%c"%(i+ord('A')))
Select_Robot.addstr(LINES//2+10,COLS//2-32,"Enter Your Selection: ")
global ROBO
Key=Select_Robot.getch()
SELECTED=False
while True:
if Key in range(65,75):
Select_Robot.addch(LINES//2+10,COLS//2-10,Key)
ROBO=Robot(Key-ord('A'))
ROBO.Place(LINES//2+7,COLS//2,Select_Robot)
SELECTED=True
else:
Select_Robot.addstr(LINES-2,COLS//2-30,"Press Keys Between A-J Only!")
Key=Select_Robot.getch()
if Key==10 and SELECTED==True: break
Select_Robot.addstr(LINES-2,COLS//2-30,"Press Any Key To Continue...")
# -----------------------------------------------------------------------------
Game=MyGame() # Creating an object of MyGame Class
Arena=Game.win
Arena.bkgd(curses.A_REVERSE)
LINES,COLS=Arena.getmaxyx()
LEVEL=1
while True:
Arena.clear()
Arena.refresh()
Arena.border('|','|','-','-','+','+','+','+')
Print_File(Arena,"Loading.txt",LINES//2-10,COLS//2-30,LINES,COLS)
Print_File(Arena,'Level'+str(LEVEL)+'.txt',LINES//2-10+8,COLS//2-30,LINES,COLS)
Arena.border('|','|','-','-','+','+','+','+')
Game.Loading(LINES,COLS)
Key=Arena.getch()
global Score
while Key==-1:
Arena.addstr(LINES-10,COLS//2-20,"Press Any Key To Continue...")
Key=Arena.getch()
Exit_Status,REASON=Game.Start(Arena,LINES,COLS,LEVEL)
if Exit_Status==-1: break
elif Exit_Status==1:
LEVEL+=1
else:
Exit_Status=Game.Display_Choice(Arena,REASON,LINES,COLS)
if Exit_Status==1: Score=0
if Exit_Status==2 or LEVEL==4:
Game.End(Arena,Score,LEVEL,LINES,COLS)
break
Game.win.clear() # Exiting Quietly!
curses.endwin()