-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ghost.py
234 lines (209 loc) · 8.86 KB
/
Ghost.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
import pygame
import random
from Variables import *
class Ghost:
def __init__(self, name, screen_width, map, position, cell_width):
self.screen = pygame.Surface((screen_width, screen_width), pygame.SRCALPHA)
self.map = map
self.direction_movement = 'U'
self.speed = 1.88
self.pos_x = position[1] #j - index
self.pos_y = position[0] #i - index
self.cell_width = cell_width
self.screen_pos_x = cell_width * self.pos_x - cell_width // 4
self.screen_pos_y = cell_width * self.pos_y - cell_width // 4
self.name = name
self.mode = "Normal"
self.target = None
self.timer = 0
self.last_cell = [self.pos_y, self.pos_x]
if self.name == "Blinky":
self.color = color_red
elif self.name == "Inky":
self.color = color_inky
elif self.name == "Pinky":
self.color = color_pink
elif self.name == "Clyde":
self.color = color_clyde
else:
self.color = color_white
def update(self, pacman, blinky=None):
self.timer += 1
if self.mode == "Scared" and self.timer > 850:
self.mode = "Normal"
target = self.get_target(pacman, blinky)
self.target = target
self.follow_target(target)
self.manage_speed([pacman.pos_x, pacman.pos_y])
self.manage_position()
self.screen.fill(color_transparent)
sprite_path = None
if self.mode == "Normal":
sprite_path = f"Static/Sprites/{str(self.name)}/{str(self.name)}-{self.direction_movement}.png"
else:
sprite_faze = str((self.timer % 30 > 15) + 1)
sprite_path = f"Static/Sprites/Scared/Fear-{sprite_faze}.png"
sprite = pygame.image.load(sprite_path)
self.screen.blit(sprite, (0, 0))
def get_target(self, pacman, blinky=None):
if self.mode == "Scared":
if self.timer % 20 == 1:
return [random.randint(0, 30), random.randint(0, 27)]
else:
return self.target
if self.map[self.pos_y][self.pos_x] == 'U':
return [13, 0]
if self.name == "Blinky":
return [pacman.pos_x, pacman.pos_y]
if self.name == "Pinky":
pacman_direction = pacman.direction_movement
target_x = None
target_y = None
if pacman_direction == 'D':
target_x = pacman.pos_x
target_y = pacman.pos_y + 4
elif pacman_direction == 'L':
target_x = pacman.pos_x - 4
target_y = pacman.pos_y
elif pacman_direction == 'R':
target_x = pacman.pos_x + 4
target_y = pacman.pos_y
elif pacman_direction == 'U':
target_x = pacman.pos_x
target_y = pacman.pos_y - 4
return [target_x, target_y]
if self.name == "Inky":
vector_x = blinky.pos_x - pacman.pos_x
target_x = pacman.pos_x - vector_x
vector_y = blinky.pos_y - pacman.pos_y
target_y = pacman.pos_y - vector_y
return [target_x, target_y]
if self.name == "Clyde":
dist_to_pacman = dist(pacman.pos_x, pacman.pos_y, self.pos_x, self.pos_y)
return [14, 15] if dist_to_pacman <= 8 else [pacman.pos_x, pacman.pos_y]
def follow_target(self, target):
self.update_pos()
i = self.pos_y
j = self.pos_x
potential_directions = []
if self.direction_movement == 'D':
potential_directions.extend(('D', 'L', 'R'))
elif self.direction_movement == 'L':
potential_directions.extend(('D', 'L', 'U'))
elif self.direction_movement == 'R':
potential_directions.extend(('D', 'U', 'R'))
elif self.direction_movement == 'U':
potential_directions.extend(('L', 'U', 'R'))
else:
potential_directions.extend(('D', 'L', 'U', 'R'))
final_directions = []
last_i = self.last_cell[0]
last_j = self.last_cell[1]
for direction in potential_directions:
if (
direction == 'U'
and self.map[i - 1][j] == 'O'
and (last_i != i - 1 or last_j != j)
):
final_directions.append('U')
if (
direction == 'R'
and self.map[i][j + 1] == 'O'
and (last_i != i or last_j != j + 1)
):
final_directions.append('R')
if (
direction == 'D'
and self.map[i + 1][j] == 'O'
and (last_i != i + 1 or last_j != j)
):
final_directions.append('D')
if (
direction == 'L'
and self.map[i][j - 1] == 'O'
and (last_i != i or last_j != j - 1)
):
final_directions.append('L')
if not final_directions:
for direction in potential_directions:
if (
direction == 'U'
and self.map[i - 1][j] != '#'
and (last_i != i - 1 or last_j != j)
):
final_directions.append('U')
if (
direction == 'R'
and self.map[i][j + 1] != '#'
and (last_i != i or last_j != j + 1)
):
final_directions.append('R')
if (
direction == 'D'
and self.map[i + 1][j] != '#'
and (last_i != i + 1 or last_j != j)
):
final_directions.append('D')
if (
direction == 'L'
and self.map[i][j - 1] != '#'
and (last_i != i or last_j != j - 1)
):
final_directions.append('L')
direction_desired = None
shortest_dist = 1000
for direction in final_directions:
if direction == 'D':
if dist(target[0], target[1], j, i + 1) < shortest_dist:
shortest_dist = dist(target[0], target[1], j, i + 1)
direction_desired = 'D'
elif direction == 'L':
if dist(target[0], target[1], j - 1, i) < shortest_dist:
shortest_dist = dist(target[0], target[1], j - 1, i)
direction_desired = 'L'
elif direction == 'R':
if dist(target[0], target[1], j + 1, i) < shortest_dist:
shortest_dist = dist(target[0], target[1], j + 1, i)
direction_desired = 'R'
elif direction == 'U':
if dist(target[0], target[1], j, i - 1) < shortest_dist:
shortest_dist = dist(target[0], target[1], j, i - 1)
direction_desired = 'U'
if direction_desired:
self.direction_movement = direction_desired
def manage_speed(self, target):
if self.pos_x == target[0] and self.pos_y == target[1]:
self.speed = 0
else:
self.speed = 1.88 if self.mode == "Normal" else 1.5
def manage_position(self):
if self.direction_movement == 'D':
self.screen_pos_y += self.speed
elif self.direction_movement == 'L':
self.screen_pos_x -= self.speed
elif self.direction_movement == 'R':
self.screen_pos_x += self.speed
elif self.direction_movement == 'U':
self.screen_pos_y -= self.speed
self.align()
def align(self):
if self.direction_movement in ['U', 'D']:
self.align_horizontal()
if self.direction_movement in ['R', 'L']:
self.align_vertical()
def align_vertical(self):
self.screen_pos_y = self.cell_width * self.pos_y - self.cell_width // 4
def align_horizontal(self):
self.screen_pos_x = self.cell_width * self.pos_x - self.cell_width // 4
def update_pos(self):
new_pos_x = int((self.screen_pos_x + (self.screen.get_width() - self.cell_width // 2) // 2 + self.cell_width // 4) // self.cell_width)
new_pos_y = int((self.screen_pos_y + (self.screen.get_height() - self.cell_width // 2) // 2 + self.cell_width // 4) // self.cell_width)
if new_pos_x != self.pos_x or new_pos_y != self.pos_y:
self.last_cell = [self.pos_y, self.pos_x]
self.pos_x = new_pos_x
self.pos_y = new_pos_y
def go_to_scare_mode(self):
self.mode = "Scared"
self.timer = 0
def dist(x1, y1, x2, y2):
return (abs(x1 - x2)**2 + abs(y1 - y2)**2)**0.5