-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghosts.py
156 lines (131 loc) · 4.88 KB
/
ghosts.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
import tkinter as tk
import random
from bounds import *
from constants import *
class Ghost():
def __init__(self, window, pac, ghost_imgs):
self.window = window
self.pac = pac
self.orig_imgs = ghost_imgs
self.imgs = ghost_imgs[2:]
self.ghosts = []
self.speeds = []
self.movement_types = []
self.dirs = []
self.atCorner = []
self.count = 0
self.img_count = len(ghost_imgs)
self.movements = [
{'x': 0, 'y': -1}, # up
{'x': 0, 'y': 1}, # down
{'x': 1, 'y': 0}, # right
{'x': -1, 'y': 0}, # left
]
def create_ghost(self, speed, movement_type):
if len(self.imgs) == 0:
self.imgs = self.orig_imgs[2:]
if movement_type == 'follow':
selected_img = self.orig_imgs[0]
elif movement_type == 'ambush':
selected_img = self.orig_imgs[1]
else:
selected_img = random.choice(self.imgs)
self.imgs.remove(selected_img)
ghost = tk.Label(self.window, image=selected_img, bd=0)
ghost_x, ghost_y = random_poss(object='ghost')
ghost.place(x=ghost_x, y=ghost_y)
self.count += 1
self.ghosts.append(ghost)
self.dirs.append(random.choice(self.movements))
self.speeds.append(speed)
self.movement_types.append(movement_type)
self.atCorner.append(False)
def move_ghosts(self, pac_dir):
for i in range(self.count):
ghost_x, ghost_y = float(self.ghosts[i].place_info()['x']), float(self.ghosts[i].place_info()['y'])
pac_x, pac_y = float(self.pac.place_info()['x']), float(self.pac.place_info()['y'])
if ghost_x <= -24 or ghost_x >= 777:
ghost_x = 777 - abs(ghost_x)
if self.movement_types[i] == 'random':
# IF IT HITS A CORNER
if not self.atCorner[i]:
ghost_x_mid = ghost_x + ghost_size[0]/2
ghost_y_mid = ghost_y + ghost_size[1]/2
for corner in corners:
corner_x, corner_y = corner
dx = abs(corner_x - ghost_x_mid)
dy = abs(corner_y - ghost_y_mid)
if (dx <= 5 or dy <= 5) and dx < 30 and dy < 30:
self.atCorner[i] = True
self.dirs[i] = random.choice(self.movements)
ghost_x += self.speeds[i] * self.dirs[i]['x']
ghost_y += self.speeds[i] * self.dirs[i]['y']
# IF IT HITS THE WALL
while outOfBound((ghost_x, ghost_y), object='ghost'):
ghost_x -= self.speeds[i] * self.dirs[i]['x']
ghost_y -= self.speeds[i] * self.dirs[i]['y']
self.dirs[i] = random.choice(self.movements)
ghost_x += self.speeds[i] * self.dirs[i]['x']
ghost_y += self.speeds[i] * self.dirs[i]['y']
self.atCorner[i] = False
elif self.movement_types[i] == 'follow' or self.movement_types[i] == 'ambush':
new_dirs = self.movements[:]
new_dirs.remove({'x': -self.dirs[i]['x'], 'y': -self.dirs[i]['y']})
dirs = []
ds = []
for dir in new_dirs:
step_x = ghost_x + self.speeds[i] * dir['x']
step_y = ghost_y + self.speeds[i] * dir['y']
if not outOfBound((step_x, step_y), object='ghost'):
if self.movement_types[i] == 'follow':
target_x, target_y = pac_x, pac_y
elif self.movement_types[i] == 'ambush':
target_x = pac_x + pac_dir['x']*ambush_distance
target_y = pac_y + pac_dir['y']*ambush_distance
d = (target_x - step_x) ** 2 + (target_y - step_y) ** 2
dirs.append(dir)
ds.append(d)
if len(set(ds)) == len(ds):
try:
d_min = min(ds)
except:
self.dirs[i] = {'x': -self.dirs[i]['x'], 'y': -self.dirs[i]['y']}
ghost_x += self.speeds[i] * self.dirs[i]['x']
ghost_y += self.speeds[i] * self.dirs[i]['y']
self.ghosts[i].place(x=ghost_x, y=ghost_y)
continue
self.dirs[i] = dirs[ds.index(d_min)]
else:
d_max = max(ds)
i_to_remove = ds.index(d_max)
ds.pop(i_to_remove)
dirs.pop(i_to_remove)
if {'x': 0, 'y': -1} in dirs:
self.dirs[i] = {'x': 0, 'y': -1}
elif {'x': -1, 'y': 0} in dirs:
self.dirs[i] = {'x': -1, 'y': 0}
elif {'x': 0, 'y': 1} in dirs:
self.dirs[i] = {'x': 0, 'y': 1}
else:
self.dirs[i] = {'x': 1, 'y': 0}
ghost_x = ghost_x + self.speeds[i] * self.dirs[i]['x']
ghost_y = ghost_y + self.speeds[i] * self.dirs[i]['y']
if ghost_x <= -24 or ghost_x >= 800:
ghost_x = 800 - abs(ghost_x)
self.ghosts[i].place(x=ghost_x, y=ghost_y)
def check_collision(self):
pac_x, pac_y = float(self.pac.place_info()['x']), float(self.pac.place_info()['y'])
for ghost in self.ghosts:
ghost_x, ghost_y = float(ghost.place_info()['x']), float(ghost.place_info()['y'])
dx, dy = ghost_x - pac_x, ghost_y - pac_y
if dx < 23 and dx > -19 and dy < 21 and dy > -21:
return True
return False
def recreate(self, initial_n_of_ghosts=2):
for ghost in self.ghosts:
ghost.destroy()
starting_ghosts_speeds = self.speeds[:initial_n_of_ghosts]
starting_ghosts_movement_types = self.movement_types[:initial_n_of_ghosts]
self.__init__(self.window, self.pac, self.orig_imgs)
for i in range(initial_n_of_ghosts):
self.create_ghost(starting_ghosts_speeds[i], starting_ghosts_movement_types[i])