/
moving_walls_game.py
162 lines (126 loc) · 3.58 KB
/
moving_walls_game.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
from turtle import *
from personal_project_ball import *
from mazegame import *
import time
import random
import math
import turtle
import threading
ghost = turtle.clone()
go = turtle.clone()
#go.color("red")
door = turtle.clone()
right_wall = turtle.clone()
left_wall = turtle.clone()
rx = right_wall.xcor()
lx = left_wall.xcor()
temp=turtle.clone()
isSafe = True
def second_game():
tracer(0)
# ghost = turtle.clone()
ghost.penup()
# go = turtle.clone()
go.ht()
turtle.register_shape("small_ghost.gif")
ghost.shape("small_ghost.gif")
# door = turtle.clone()
turtle.register_shape("Door.gif")
door.shape("Door.gif")
door.penup()
door.goto(0, 325)
ghost.goto(0,-400)
turtle.ht()
turtle.speed(0)
# right_wall = turtle.clone()
right_wall.color("red")
right_wall.st()
right_wall.penup()
right_wall.shape("square")
right_wall.speed(1000000)
right_wall.shapesize(60)
right_wall.goto(1000,0)
# left_wall = turtle.clone()
left_wall.color("red")
left_wall.st()
left_wall.penup()
left_wall.shape("square")
left_wall.speed(100000)
left_wall.shapesize(60)
left_wall.goto(-1000,0)
# rx = right_wall.xcor()
# lx = left_wall.xcor()
isSafe = True
#temp=turtle.clone()
temp.st()
temp.penup()
temp.shape("square")
temp.goto(0,0)
def collide(ghost, left_wall, right_wall):
global isSafe
if (left_wall.xcor()+600) >= ghost.xcor() or (right_wall.xcor()-600) <= ghost.xcor() :
print("collided")
isSafe = False
return True
return False
def col_with_door(ghost, door):
if ghost == door:
return False
distance = math.sqrt(math.pow(ghost.xcor() - door.xcor(), 2) + math.pow(ghost.ycor() - door.ycor(), 2))
if distance <= 30:
return True
else:
return False
def walls_move():
global isSafe
rx = right_wall.xcor()
lx = left_wall.xcor()
while isSafe:
collide(ghost,left_wall,right_wall)
right_wall.goto(rx,0)
rx = rx - 1
left_wall.goto(lx,0)
lx = lx + 1
ghost.clear()
update()
time.sleep(0.01)
if collide(ghost, left_wall, right_wall):
go.write("GAME OVER!!! TRY AGAIN IN 1000 YEARS!!!", align = "center", font = ("david", 30, "normal"))
time.sleep(4)
#print("Reached")
quit()
if col_with_door(ghost, door):
#turtle.clearscreen()
# turtle.bgpic("heaven_bg1.png")
# turtle.write("CONGRATS! YOU'VE MADE IT TO HEAVEN!!", align = "center", font = ("david", 30, "normal"))
isSafe = False
ghost.hideturtle()
door.hideturtle()
left_wall.hideturtle()
right_wall.hideturtle()
third_game()
# t = threading.Thread(name = 'walls', target = walls_move)
#t.start()
def key1():
ghost.setheading(90)
ghost.fd(4.5)
update()
def key2():
ghost.setheading(180)
ghost.fd(4.5)
update()
def key3():
ghost.setheading(270)
ghost.fd(4.5)
update()
def key4():
ghost.setheading(360)
ghost.fd(4.5)
update()
Screen().onkey(key1, "Up")
Screen().onkey(key2, "Left")
Screen().onkey(key3, "Down")
Screen().onkey(key4, "Right")
Screen().listen()
walls_move()
#second_game()