forked from meet-projects/meet2017y1final-proj
/
ghost_code.py
144 lines (128 loc) · 3.26 KB
/
ghost_code.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
import turtle
import random
turtle.tracer(1,0)
enemy = turtle.clone()
enemy.penup()
enemy.goto(200,0)
turtle.register_shape("ghost_F.gif")
enemy.shape("ghost_F.gif")
#enemy.shape("circle")
#screan size
SIZE_X = 1028
SIZE_Y = 800
turtle.setup(SIZE_X, SIZE_Y)
#square size
SQUARE_SIZE = 20
#installing arrows
UP_ARROW = "Up"
DOWN_ARROW = "Down"
RIGHT_ARROW = "Right"
LEFT_ARROW = "LEFT"
TIME_STEP = 100
#lists
pos_list = []
enemy_pos_list = []
turtle.penup()
UP = 0
DOWN = 1
RIGHT = 2
LEFT = 3
direction = UP
gdirection = UP
turtle.shape("square")
def up():
global direction
direction = UP
print("UP")
def down():
global direction
direction = DOWN
print("DOWN")
def right():
global direction
direction = RIGHT
print("RIGHT")
def left():
global direction
direction = LEFT
def gup():
global gdirection
gdirection = UP
def gdown():
global gdirection
gdirection = DOWN
def gleft():
global gdirection
gdirection = LEFT
def gright():
global gdirection
gdirection = RIGHT
def move_ghost():
global TIME_STEP
en_pos = enemy.pos()
en_x_pos = en_pos[0]
en_y_pos = en_pos[1]
randNum = (random.random()) * 100
if randNum <= 25 :
gdirection = UP
elif randNum >= 25 and randNum<=50:
gdirection = DOWN
elif randNum >= 50 and randNum<= 75:
gdirection = RIGHT
elif randNum >= 75:
gdirection = LEFT
if gdirection == UP:
enemy.goto(en_x_pos, en_y_pos + (1.5*SQUARE_SIZE))
if gdirection == DOWN:
enemy.goto(en_x_pos, en_y_pos - (1.5*SQUARE_SIZE))
if gdirection == RIGHT:
enemy.goto(en_x_pos + (1.5*SQUARE_SIZE), en_y_pos)
if gdirection == LEFT:
enemy.goto(en_x_pos - (1.5*SQUARE_SIZE), en_y_pos)
en_pos = enemy.pos()
enemy_pos_list.append(en_pos)
if en_x_pos >= 400 or en_x_pos <= -400:
enemy.reset()
enemy.penup()
if en_y_pos >= 250 or en_y_pos <= -250:
enemy.reset()
enemy.penup()
turtle.ontimer(move_ghost, TIME_STEP)
move_ghost()
turtle.onkeypress(left, "Left")
turtle.onkeypress(right, "Right")
turtle.onkeypress(up, "Up")
turtle.onkeypress(down, "Down")
turtle.listen()
def move_player():
my_pos = turtle.pos()
x_pos = my_pos[0]
y_pos = my_pos[1]
if direction == RIGHT:
turtle.goto(x_pos + (1.5*SQUARE_SIZE), y_pos)
print("you moved to the right!")
elif direction == LEFT:
turtle.goto(x_pos - (1.5*SQUARE_SIZE), y_pos)
print("you moved to the left!")
elif direction == UP:
turtle.goto(x_pos, y_pos + (1.5*SQUARE_SIZE))
print("you moved UP")
elif direction == DOWN:
turtle.goto(x_pos, y_pos - (1.5*SQUARE_SIZE))
print("you moved DOWN")
my_pos = turtle.pos()
pos_list.append(my_pos)
print(pos_list[-1])
global TIME_STEP
turtle.ontimer(move_player,TIME_STEP)
#limiting the player in the border
if x_pos >= 400 or x_pos <= -400:
quit()
if y_pos >= 250 or y_pos <= -250:
quit()
if -0.1 <enemy.pos()[0] - turtle.pos()[0] < 0.1 or -0.1 < enemy.pos()[1] - turtle.pos()[1] < 0.1:
turtle.register_shape("ghost.gif")
enemy.shape("ghost.gif")
if turtle.pos() in (0,0):
writer.write("You Won!")
move_player()