-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject1.txt
643 lines (549 loc) · 21.1 KB
/
project1.txt
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
import turtle
import math
import random
import time #shamsa
import sys #shamsa
from collections import deque#shamsa
alive, win = True, False #shamsa
window = turtle.Screen()
window.bgcolor('white')
window.title("Corona Minus")
window.setup(700,700)
window.tracer(0) # Quickens the speed
#creating button
#creating button
turtle.hideturtle()
turtle.penup()
turtle.goto(250, 240 )
turtle.pendown()
turtle.forward(50)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(30)
turtle.left(90)
turtle.penup()
turtle.goto(260, 245)
turtle.write('help', font=('Arial', 12, 'normal'))
##Register shapes
shapes = ["doctor_right.gif", "doctor_left.gif","stetho.gif", "mask.gif", "shopping_bag.gif","right_mannn.gif","home.gif", "dot.gif","title.gif","corona.gif","girl_left.gif", "girl_right.gif", "wall2.gif","san.gif" ]
for i in shapes:
turtle.register_shape(i)
class Pen(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("white")
self.penup() #usually pen is down becuase it writes we dont want that so pen up
self.speed(0)
class Green(turtle.Turtle): #shamsa
def __init__(self):
turtle.Turtle.__init__(self)
self.hideturtle()
self.penup()
self.speed(0)
class Yellow(turtle.Turtle): #shamsa
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("dot.gif")
self.penup()
self.speed(0)
class Player(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("girl_right.gif")
self.penup() #usually pen is down becuase it writes we dont want that so pen up
self.speed(0)
self.gold = 0
def go_up(self):
if (self.xcor(), self.ycor()+24) not in walls:
self.goto(self.xcor(), self.ycor() +24)#When you go up x coordinate doesnt change but y gets +24 pixels
def go_down(self): #similar explaination for others
if (self.xcor(), self.ycor()-24) not in walls:
self.goto(self.xcor(), self.ycor()-24)
def go_left(self):
move_to_x = player.xcor() - 24
move_to_y = player.ycor()
self.shape("girl_left.gif")
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_right(self):
move_to_x = player.xcor() + 24
move_to_y = player.ycor()
self.shape("girl_right.gif")
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def is_collision(self, something):#to check if player collides with treasure or any object
a = self.xcor()-something.xcor()
b = self.ycor()-something.ycor()
#checking the distance to see if they are colse enough to consider a collision
distance = math.sqrt((a**2) + (b**2))
if distance<5:
return True
if distance>5:
return False
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Doctor(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("doctor_right.gif")
self.penup() #usually pen is down becuase it writes we dont want that so pen up
self.speed(0)
self.gold = 0
def go_up(self):
if (self.xcor(), self.ycor()+24) not in walls:
self.goto(self.xcor(), self.ycor() +24)#When you go up x coordinate doesnt change but y gets +24 pixels
def go_down(self): #similar explaination for others
if (self.xcor(), self.ycor()-24) not in walls:
self.goto(self.xcor(), self.ycor()-24)
def go_left(self):
move_to_x = player.xcor() - 24
move_to_y = player.ycor()
self.shape("doctor_left.gif")
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def go_right(self):
move_to_x = player.xcor() + 24
move_to_y = player.ycor()
self.shape("doctor_right.gif")
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
def is_collision(self, something):#to check if player collides with treasure or any object
a = self.xcor()-something.xcor()
b = self.ycor()-something.ycor()
#checking the distance to see if they are colse enough to consider a collision
distance = math.sqrt((a**2) + (b**2))
if distance<5:
return True
if distance>5:
return False
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Treasure(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("san.gif")
self.penup()
self.speed(0)
self.gold = 100
self.goto(x,y)
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Mask(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("mask.gif")
self.penup()
self.speed(0)
self.gold = 50
self.goto(x, y)
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Stethoscope(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("stetho.gif")
self.penup()
self.speed(0)
self.gold = 150
self.goto(x, y)
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Shopping_Bag(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("shopping_bag.gif")
self.penup()
self.speed(0)
self.gold = 100
self.goto(x,y)
class Home(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("home.gif")
self.penup()
self.speed(0)
#self.gold = 100
self.goto(x,y)
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Enemy(turtle.Turtle):
def __init__(self, x, y):
turtle.Turtle.__init__(self)
self.shape("corona.gif")
self.penup()
self.speed(0)
self.gold = 25
self.goto(x,y)
self.direction= random.choice(["up", "down", "left", "right"])
def move(self):
if self.direction =="up":
dx=0
dy=24 #changes by Haania
elif self.direction == "down":
dx=0
dy = -24
elif self.direction == "left":
dx=-24
dy = 0
elif self.direction == "right":
dx=24
dy =0
else:
dx=0
dy=0
if self.is_close(player):#check if player is. close, if so , go in that direction
if player.xcor()<self.xcor():
self.direction= "left"
if player.xcor()>self.xcor():
self.direction="right"
if player.ycor()<self.ycor():
self.direction="down"
if player.ycor()>self.ycor():
self.direction="up"
#Calculate the spot
move_to_x = self.xcor() + dx
move_to_y = self.ycor() + dy
#Now we dont want enemy to pass through the wall
if (move_to_x, move_to_y) not in walls:
self.goto(move_to_x, move_to_y)
else:
#We will randomly choose another direction
self.direction= random.choice(["up", "down", "left", "right"])
turtle.ontimer(self.move, t= random.randint(100, 300)) #100 - 300 ms
def is_close(self,other):
a=self.xcor()-other.xcor()
b=self.ycor()-other.ycor()
distance= math.sqrt((a**2)+(b**2))
if distance< 75: return True
else: return False
def destroy(self):
self.goto(2000,2000)
self.hideturtle()
class Score(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("square")
self.color("black")
self.penup()
self.speed(0)
self.goto(-296,240)
self.write("Score: {}".format(0), align="left", font=("Arial", 20, "normal"))
self.hideturtle()
class Die(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.penup()
self.speed(0)
self.hideturtle()
# self.goto(-10,-50)
#self.write("Score: {}".format(0), align="left", font=("Arial", 20, "normal"))
#this is the list for levels. we are keeping the first element of the list as empty list so that
#is id easier. if we call level[1] we gett the first level
levels=[""]
#this is our first level
level_1 =[
" C",
" ", #h
" ", #h
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XP XXXXXT XXXX",
"X XXXXXX XXXXXX XXXXX",
"X XX XXXXXX XXXXX",
"X MXX XXX XX",
"XXXXXX XX XXX XX",
"XXXXXX XX XXXXXX XXXXX",
"XXXXXX XX XXXX XXXXX",
"X XXX XXXXH XXXX",
"X XXX XXXXXXXXXXXXXXXXX",
"X XXXXXXXXXXXXXXX",
"XE XXXXXXXX",
"XXXXXXXXXXXX XXXXX X",
"XXXXXXXXXXXXXXX XXXXX X",
"XXX XXXXXXXXXXE X",
"XXX X",
"XXXE XXXXXXXXXXXXX",
"XXXXXXXXXX XXXXXXXXXXXXX",
"XXXXXXXXXX X",
"XX XXXXXX X",
"XX XXXXXXXXXXXXXX XXXXX",
"XX XXXXXXXXXXXXX XXXXX",
"XXE X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]
level_2 =[
" C",
" ", #h
" ", #h
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XP X X",
"X XXXXXXXXXX XXXXXXXXXX",
"X X",
"X XXXXXXX XXXXXXXXXXXXX",
"X X X X X",
"X X X X X X XXXX X",
"X X XS X X X X X",
"X X XXXX X XXXXXXXXXX",
"X X X EXX",
"X XXXX X XXXXXXX XXXXX",
"X X X MX X",
"XXXX X XXXXXXXXXX XXXXX",
"X X X X",
"X X XXXX XXXXXXXXXXXXX",
"X X XE X X X",
"X X X XXXXXX XXXX X",
"X X",
"X XXXXX X X",
"X XXXXX XXXXX XXXXXXXX",
"X X XX XXX",
"XE XXXX XXX XXX XXX",
"XXXXXXXT X X XXXE HXX",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
]
level_3=[" C",
" ", #h
" ", #h
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XX XX XX",
"XX XX XX",
"XXXXXXXXX XXXXXXXXXX XX",
"XXXXXXXXX XXXXXXXXXX XX",
"XX XX",
"X XX",
"X XXXXXXXXXXX XXXXXXX",
"XX XX X XX",
"XX XX XX XX",
"XXXXXX XX XX",
"XX XX XX",
"XXXXXXXXXXX XXXXXXXXXXXX",
"XXXXXXXXXXX XXXXXXXXXXXX",
"X XX XX",
"X XX XX",
"XXXX XX XXXXXXXXXX",
"X XX XXXXXXXXXX",
"X XXXXXXXXX XX XX",
"X XX XX",
"X XXXXXXX XX",
"XXXXXXXXXX XXXXXXX XX",
"XX XX XX",
"XX XX",
"XXXXXXXXXXXXXXXXXXXXXXXXX"]
levels.append(level_1)
levels.append(level_2)
levels.append(level_3)
treasures = []
masks=[]
enimies=[]
homes=[]
stethoscopes=[]
shopping=[]
#create function that sets up the maze for a particular level
def setup_maze(level):
global start_x, start_y, end_x, end_y #i think ye na bhi karen tou chale ga #shamsa
for y in range(len(level)):
for x in range(len(level[y])):
#now we will get the character at each x,y coordinate
character=level[y][x]
#we will calculate the screens x,y coordinate for this coordinates
#note that the screen starts from 0,0 and our maze is 600 by 600 so the right most coordinate will be -288,288 because each box is of size 24.
screen_x = -288 + (x*24)
screen_y = 288 - (y*24)
#checking if it is an X(which represents that there we need to place a brick for wall)
if character == "X":
pen.goto(screen_x, screen_y)
pen.shape("wall2.gif")
pen.stamp()
walls.append((screen_x, screen_y)) #add coordinates to walls list
if character == " " or character == "H":#shamsa
path.append((screen_x, screen_y)) # add " " and H to path list#shamsa
if character== "S":
stethoscopes.append(Stethoscope(screen_x, screen_y))
if character == "P":
start_x , start_y = screen_x,screen_y
player.goto(screen_x,screen_y)
if character == "M":
masks.append(Mask(screen_x, screen_y))
if character == "T":
treasures.append(Treasure(screen_x, screen_y))
if character == "E":
enimies.append(Enemy(screen_x, screen_y))
if character == "C":
pen.goto(screen_x, screen_y)
pen.shape("title.gif")
pen.stamp()
if character == "H":
end_x , end_y = screen_x, screen_y
homes.append(Home(screen_x, screen_y))
if character=="V":
shopping.append(Shopping_Bag(screen_x,screen_y))
#Global variables #shamsa
#pen.goto(screen_x, screen_y)
#pen.shape("home.gif")
#pen.stamp()
return start_x , start_y , end_x, end_y #shamsa
#we are creating class instances. so if we use pen it calls the class Pen
pen = Pen()
green = Green() #shamsa
yellow = Yellow()#shamsa
die=Die()
score=Score()
player=Player()
walls = []
path = []#shamsa
visited = set()#shamsa
frontier = deque()#shamsa
solution = {} #shamsa
# solution dictionary
start_x , start_y , end_x, end_y = setup_maze(levels[1])
maze="level1"
def button_click(x, y):
if x>=250 and x<=300 and y>=240 and y<=270: # borders of button
if alive and player.gold>=5: # if the player has not died
#shamsa : I had to do it in a way that the current position gets calculated and
#then waheen se BFS for that I tried :
#start_x , start_y = player.pos()[0] , player.pos()[1]
#But its not working that way
search(start_x,start_y)
backRoute(end_x, end_y)
else:
print('you donot have enough immunity points')
#################################################################
def search(x,y):
frontier.append((x, y))
solution[x,y] = x,y
while len(frontier) > 0: # exit while loop when frontier queue equals zero
time.sleep(0)
x, y = frontier.popleft() # pop next entry in the frontier queue an assign to x and y location
if(x - 24, y) in path and (x - 24, y) not in visited: # check the cell on the left
cell = (x - 24, y)
solution[cell] = x, y # backtracking routine [cell] is the previous cell. x, y is the current cell
frontier.append(cell) # add cell to frontier list
visited.add((x-24, y)) # add cell to visited list
if (x, y - 24) in path and (x, y - 24) not in visited: # check the cell down
cell = (x, y - 24)
solution[cell] = x, y
frontier.append(cell)
visited.add((x, y - 24))
if(x + 24, y) in path and (x + 24, y) not in visited: # check the cell on the right
cell = (x + 24, y)
solution[cell] = x, y
frontier.append(cell)
visited.add((x +24, y))
if(x, y + 24) in path and (x, y + 24) not in visited: # check the cell up
cell = (x, y + 24)
solution[cell] = x, y
frontier.append(cell)
visited.add((x, y + 24))
green.goto(x,y)
def backRoute(x, y):
yellow.goto(x, y)
yellow.stamp()
while (x, y) != (start_x, start_y): # stop loop when current cells == start cell
yellow.goto(solution[x, y]) # move the yellow sprite to the key value of solution ()
yellow.stamp()
x, y = solution[x, y] # "key value" now becomes the new key
#######################################
turtle.onscreenclick(button_click,1) #1 means right click 0 means left click
#The functions that we created for up/down/left/right now we need to call it on
#key i.e. Keyboard Binding
turtle.listen()
turtle.onkey(player.go_left, "Left") #Quotations show the arrow key on keyboard
turtle.onkey(player.go_right, "Right")
turtle.onkey(player.go_down, "Down")
turtle.onkey(player.go_up, "Up")
#turns off screen updates
window.tracer(0)
for enemy in enimies:
turtle.ontimer(enemy.move, t=250) #It will move after every 250 milisecond
#main game loop so that it keeps running
while True:
#check for collision with treasure.we will iterate through treasure list for all the treasure
for treasure in treasures:
if treasure!='*' and player.is_collision(treasure):
player.gold += treasure.gold
win = True #shamsa
score.clear()
score.write("Score: {}".format(player.gold), align="left", font=("Arial", 20, "normal"))
print("Immunity Points: {}".format(player.gold))
treasure.destroy()
treasures.remove(treasure)
for stethoscope in stethoscopes:
if stethoscope!='*' and player.is_collision(stethoscope):
player.gold += stethoscope.gold
win = True #shamsa
score.clear()
score.write("Score: {}".format(player.gold), align="left", font=("Arial", 20, "normal"))
print("Immunity Points: {}".format(player.gold))
stethoscope.destroy()
stethoscopes.remove(stethoscope)
for mask in masks:
if player.is_collision(mask):
player.gold += mask.gold
win = True
score.clear()
score.write("Score: {}".format(player.gold), align="left", font=("Arial", 20, "normal"))
print("Immunity Points: {}".format(player.gold))
mask.destroy()
masks.remove(mask)
for enemy in enimies:
if player.is_collision(enemy):
alive = False #shamsa
die.goto(-10,-50)
die.write("PLAYER DIES!",True, align="center", font=("Arial",30,"bold"))
die.hideturtle()
turtle.clear()
print("Player Dies!")
player.destroy()
for home in homes:
if player.is_collision(home):
walls.clear()
pen.clear()
for treasure in treasures:
Treasure.destroy(treasure)
treasures.remove(treasure)
for mask in masks:
Mask.destroy(mask)
masks.remove(mask)
for enemy in enimies:
enemy.destroy()
enimies.remove(enemy)
for home in homes:
home.destroy()
homes.remove(home)
player.destroy()
if maze=="level1":
print ("level passed")
player=Doctor()
start_x , start_y , end_x, end_y = setup_maze(levels[2])
maze="level2"
turtle.onscreenclick(button_click,1) #1 means right click 0 means left click
#The functions that we created for up/down/left/right now we need to call it on
#key i.e. Keyboard Binding
turtle.listen()
turtle.onkey(player.go_left, "Left") #Quotations show the arrow key on keyboard
turtle.onkey(player.go_right, "Right")
turtle.onkey(player.go_down, "Down")
turtle.onkey(player.go_up, "Up")
#turns off screen updates
window.tracer(0)
for enemy in enimies:
turtle.ontimer(enemy.move, t=250) #It will move after every 250 milisecond
elif maze=="level2":
print ("level passed")
start_x , start_y , end_x, end_y = setup_maze(levels[3])
maze="level3"
else:
print ("Congratulations! You have saved yourself from the virus")
#if player.is_collision(homes[-1]):
# break
#Update screen
window.update()