-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.py
321 lines (287 loc) · 15.2 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
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
import Items
import pygame
import random
from operator import *
import math
from Constants import *
class Ghost:
def __init__(self, x, y, color, scatter_coord, personality,generation):
#if ghosts are disabled, trap them in box
if turnOffGhosts and generation%disableGhostsEvery==1:
x = house_x
y = house_y
self.generation = generation
# CONSTANTS
self.offset = block_size * 2
self.size = 24
self.base_color = color
self.step_len = block_size / 16 # normal movement speed
self.slow_step = block_size / 24 # movement speed when turned blue
self.step = self.step_len
self.personality = personality
self.scatter_time = 5
self.chase_time = 20
# MOVEMENT
self.scatter_coord = scatter_coord
self.COORD_DIR = {0: [1, 0], 1: [0, 1], 2: [-1, 0], 3: [0, -1]}
self.look_dir = 3
self.move_dir = 3
# LOCATION
self.array_coord = [x, y]
self.x = x * block_size + block_size / 2 # px
self.y = y * block_size + block_size / 2 # px
# SETUP VARS
self.mode = "house"
self.blue = False
self.respawn_time = 3
# TIMERS
# Initialise multiple timers to avoid accidental collisions
self.turn_timer = 0
self.respawn_timer = 0
self.timer = 0
self.blue_timer = 0
def move(self, player, maze, display_width, tick_counter, blinky_coord):
def find_distance(a_pos, b_pos):
a = pow(abs(a_pos[0] - b_pos[0]), 2)
b = pow(abs(a_pos[1] - b_pos[1]), 2)
return math.sqrt(a + b)
def find_closest(facing, target_pos):
return_dir = facing
next_pos = list(map(add, self.array_coord, self.COORD_DIR[facing]))
dir_min = find_distance(next_pos, target_pos)
# check left turn
if maze.can_move(self, left_turn(facing)):
next_pos = list(map(add, self.array_coord, self.COORD_DIR[left_turn(facing)]))
next_dir = find_distance(next_pos, target_pos)
if next_dir < dir_min:
dir_min = next_dir
return_dir = left_turn(facing)
# check right turn
if maze.can_move(self, right_turn(facing)):
next_pos = list(map(add, self.array_coord, self.COORD_DIR[right_turn(facing)]))
next_dir = find_distance(next_pos, target_pos)
if next_dir < dir_min:
return_dir = right_turn(facing)
return return_dir
def left_turn(facing):
return abs((facing - 1) % 4)
def right_turn(facing):
return abs((facing + 1) % 4)
# Set step length based on current state
step = self.step_len
if self.blue:
if self.blue_timer >= player.power_time:
self.blue = False
else:
self.blue_timer += 1
step = self.slow_step
if self.mode == "dead":
self.step = self.step_len * 2
step = self.step
if self.mode == "normal" or self.mode == "dead":
# Update current position in array
self.array_coord = [int(self.x / block_size), int(self.y / block_size)]
# Only try changing direction if within bounds of maze array
if block_size < self.x < display_width - block_size:
my_row = int(self.y / block_size)
my_col = int(self.x / block_size)
player_row = int(player.y / block_size)
player_col = int(player.x / block_size)
player_dir = 0
# FRIGHTENED MODE
if self.blue:
# Check whether the player is visible from this ghost's perspective
# If they are on the same row or column, check all tiles in between
# If there are no walls, the ghost can see the player
see_player = False
if my_row == player_row or my_col == player_col:
wall = False # flag for whether there is an obstruction between ghost and player
if my_col == player_col and my_row == player_row:
wall = True
elif my_row == player_row:
if my_col > player_col:
player_dir = LEFT
for i in range(0, my_col - player_col):
if maze.maze_array[my_row][i + player_col] == 1:
wall = True
elif player_col == my_col:
wall = True
else:
player_dir = RIGHT
for i in range(0, player_col - my_col):
if maze.maze_array[my_row][i + my_col] == 1:
wall = True
elif my_col == player_col:
if my_row > player_row:
player_dir = UP
for i in range(0, my_row - player_row):
if maze.maze_array[i + player_row][my_col] == 1:
wall = True
elif player_row == my_row:
wall = True
else:
player_dir = DOWN
for i in range(0, player_row - my_row):
if maze.maze_array[i + my_row][my_col] == 1:
wall = True
if not wall:
see_player = True
# Only attempt turn if more than 1 tick since last turn
if self.turn_timer > 2:
# Run away from the player if it is visible
# If it is able to continue in the direction it is facing it will
# do so, so long as it does not go towards the player
if see_player:
if self.look_dir == player_dir or not maze.can_move(self, self.look_dir):
self.look_dir = random.choice([left_turn(left_turn(player_dir)),
left_turn(player_dir), right_turn(player_dir)])
self.turn_timer = 0
# if player not visible, pick a random movement direction
else:
self.look_dir = random.choice([self.move_dir, left_turn(self.move_dir),
right_turn(self.move_dir)])
self.turn_timer = 0
# set target position based on current behaviour
if self.mode == "normal":
# Immediately exit house
if self.array_coord == [house_x, house_y]:
target_coord = [house_x, house_y-2]
elif self.array_coord in([house_x, house_y-1], [house_x, house_y-2]) and self.move_dir == UP:
target_coord = [house_x-1, house_y-2]
# Scatter
elif (tick_counter / 60) % (self.chase_time + self.scatter_time) < self.scatter_time:
target_coord = self.scatter_coord
# Chase Pac-Man
else:
target_coord = player.array_coord
if self.personality == "speedy":
target_coord = \
[player.array_coord[0] + player.COORD_DIR[player.move_dir][0] * 2,
player.array_coord[1] + player.COORD_DIR[player.move_dir][1] * 2]
if self.personality == "pokey" and find_distance(self.array_coord, player.array_coord) < 3:
target_coord = self.scatter_coord
if self.personality == "bashful":
target_coord = \
[player.array_coord[0] + player.COORD_DIR[player.move_dir][0] * 2,
player.array_coord[1] + player.COORD_DIR[player.move_dir][1] * 2]
vect = list(map(sub, target_coord, blinky_coord))
target_coord = list(map(add, target_coord, vect))
# if dead, move back to ghost house
elif self.mode == "dead":
target_coord = [house_x, house_y]
# move towards target, only attempt a turn at an intersection
if step < self.x % block_size < block_size - step \
and step < self.y % block_size < block_size - step and self.turn_timer > 2:
if maze.can_move(self, left_turn(self.look_dir)) \
or maze.can_move(self, right_turn(self.look_dir)):
self.look_dir = find_closest(self.look_dir, target_coord)
self.turn_timer = 0
if not maze.can_move(self, self.look_dir):
self.look_dir = random.choice([left_turn(self.move_dir), right_turn(self.move_dir)])
self.turn_timer = 0
# change move direction to match look direction if possible
if self.look_dir != self.move_dir:
if maze.can_move(self, self.look_dir):
self.move_dir = self.look_dir
# if in a dead end, flip direction
if not (maze.can_move(self, self.move_dir)) \
and not (maze.can_move(self, left_turn(self.move_dir))) \
and not (maze.can_move(self, right_turn(self.move_dir))):
self.look_dir = left_turn(left_turn(self.move_dir))
self.move_dir = self.look_dir
# do movement
if maze.can_move(self, self.move_dir):
self.x += step * self.COORD_DIR[self.move_dir][0]
self.y += step * self.COORD_DIR[self.move_dir][1]
# If outside maze, keep moving forwards until wrapped to the other side of the screen
else:
if self.move_dir == LEFT:
self.x -= self.step_len
maze.center(self, "y", self.y)
if self.move_dir == RIGHT:
self.x += self.step_len
maze.center(self, "y", self.y)
# screen wrap
if self.x < -self.size:
self.x = int(0.5*block_size + MapSizeX*block_size) #display_width
if self.x > self.size + display_width:
self.x = -int(0.5*block_size) #-self.size
# respawn if way found back to house
if self.mode == "dead" and self.array_coord == [house_x, house_y]:
self.mode = "normal"
self.turn_timer += 1
# Ghost stays in the house and paces left and right
elif self.mode == "house":
if self.look_dir == DOWN or self.look_dir == UP:
self.look_dir = random.choice([LEFT, RIGHT])
self.move_dir = self.look_dir
if not (maze.can_move(self, self.move_dir)):
self.look_dir = left_turn(left_turn(self.move_dir))
self.move_dir = self.look_dir
self.x += step * self.COORD_DIR[self.move_dir][0]
def draw(self, display, player, tick_counter):
def draw_body(col):
pygame.draw.ellipse(display, col, (self.x - self.size / 2, self.y + self.offset - self.size / 2,
self.size, self.size * 0.95))
pygame.draw.rect(display, col, (self.x - self.size / 2, self.y + self.offset,
self.size, self.size / 4))
# alternate wobble shape
if 0 < tick_counter % 20 < 10:
pygame.draw.ellipse(display, col, (
self.x - self.size / 2, self.y + self.size / 6 + self.offset - 1, self.size / 3, self.size / 3))
pygame.draw.ellipse(display, col, (
self.x - self.size / 6, self.y + self.size / 6 + self.offset - 1, self.size / 3, self.size / 3))
pygame.draw.ellipse(display, col, (
self.x + self.size / 6, self.y + self.size / 6 + self.offset - 1, self.size / 3, self.size / 3))
else:
pygame.draw.ellipse(display, col, (
self.x - self.size / 6 * 2, self.y + self.size / 6 + self.offset - 1, self.size / 3, self.size / 3))
pygame.draw.ellipse(display, col, (
self.x, self.y + self.size / 6 + self.offset - 1, self.size / 3, self.size / 3))
def draw_eyes(move_dir):
eye_width = self.size / 3
eye_height = eye_width * 3 / 2
pupil_diam = eye_width * 3 / 4
eye_separation = self.size * 0.1
y_pos = self.y - eye_height / 2 + self.offset
x_off = 0
y_off = 0
if move_dir == RIGHT:
x_off = 1
elif move_dir == LEFT:
x_off = -1
elif move_dir == UP:
y_off = -1
elif move_dir == DOWN:
y_off = 1
# eye whites
pygame.draw.ellipse(display, (255, 255, 255),
(self.x - eye_width - (eye_separation / 2) + x_off, y_pos + y_off,
eye_width, eye_height))
pygame.draw.ellipse(display, (255, 255, 255),
(self.x + (eye_separation / 2) + x_off, y_pos + y_off,
eye_width, eye_height))
# eye pupils
pygame.draw.circle(display, (0, 0, 0), (round(self.x - eye_width / 2 - eye_separation / 2 + x_off * 2),
round(y_pos + eye_height / 2 + y_off * 2)), round(pupil_diam / 2))
pygame.draw.circle(display, (0, 0, 0), (round(self.x + eye_width / 2 + eye_separation / 2 + x_off * 2),
round(y_pos + eye_height / 2 + y_off * 2)), round(pupil_diam / 2))
if self.mode != "dead":
if self.blue and player.powered_up:
# blink blue and white in the last 2 seconds of power up time
if 0 < self.blue_timer % 40 < 20 \
and self.blue_timer + (2 * 60) >= player.power_time:
color = (200, 200, 255) # very light blue
else:
color = (50, 50, 200) # dark blue
else:
color = self.base_color
draw_body(color)
draw_eyes(self.move_dir)
else:
draw_eyes(self.move_dir)
def collide(self, player):
dist_x = abs(self.x - player.x)
dist_y = abs(self.y - player.y)
touch_distance = self.size / 2
return dist_x < touch_distance and dist_y < touch_distance and self.mode != "dead"