-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGhost.py
More file actions
261 lines (206 loc) · 8.13 KB
/
Copy pathGhost.py
File metadata and controls
261 lines (206 loc) · 8.13 KB
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
import pygame
import math
from random import randint
from Strategy import LoudBooStrategy
from Strategy import GentleBooStrategy
from Strategy import BrightLightsStrategy
# Store strategy functions
loud_boo = LoudBooStrategy()
gentle_boo = GentleBooStrategy()
ten_seconds = BrightLightsStrategy()
# Global Variables
STEP_SIZE = 15
WINDOW_WIDTH = 54 * STEP_SIZE
WINDOW_HEIGHT = 40 * STEP_SIZE
MAX_VELOCITY = 5
MIN_DISTANCE = 15
GHOST_COLOURS = ["ghost1.png", "ghost2.png", "ghost3.png"]
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Ghost(object):
x = 0
y = 0
velocityx = 0
velocityy = 0
direction = 0
orientation = 0
colours = ["ghost1.png"]
def __init__(self, boo_strategy, light_strategy):
self._ghost_surf = pygame.image.load(self.colours[0]).convert()
self.velocityx = randint(1, 10) / 10.0
self.velocityy = randint(1, 10) / 10.0
self._boo_strategy = boo_strategy
self._light_strategy = light_strategy
# Define strategy functions for the Ghost class
def boo(self):
self._boo_strategy.boo()
# Define strategy functions for the Ghost class
def lights_on(self):
self._light_strategy.lights_on()
# Set starting coordinates for x
def setX(self, x):
self.x = x
# Set starting coordinates for y
def setY(self, y):
self.y = y
# Set colour
def setColours(self, colours):
self.colours = colours
# Function draws the object to screen
def draw(self, surface, image):
length = len(self.colours)
if length == 1:
self._ghost_surf = pygame.image.load(self.colours[0]).convert()
else:
self._ghost_surf = pygame.image.load(self.colours[randint(0, length-1)]).convert()
surface.blit(image, (self.x, self.y))
# Function calculates the distance between the ghost and another object
def distance(self, ghost):
distx = self.x - ghost.x
disty = self.y - ghost.y
return math.sqrt(distx * distx + disty * disty)
# Function controls the movement of the bird object
def move(self):
if abs(self.velocityx) > MAX_VELOCITY or abs(self.velocityy) > MAX_VELOCITY:
scaleFactor = MAX_VELOCITY / max(abs(self.velocityx), abs(self.velocityy))
self.velocityx *= scaleFactor
self.velocityy *= scaleFactor
self.x += self.velocityx
self.y += self.velocityy
# Function determines whether two objects have collided by comparing where the origin corners of
# the shape are and the total size of each object
def isCollision(self, x1, y1, x2, y2, bsize):
if x1 < x2 + bsize and x1 + bsize > x2 and y1 < y2 + bsize and y1 + bsize > y2:
return True
return False
# Function makes the ghost chase the player
def target(self, dx, dy):
distancex = 0
distancey = 0
# If the chaser and chasee aren't touching, continue chasing by reducing the
# distance between the two
if dx != self.x and dy != self.y:
distancex += (self.x - dx)
distancey += (self.y - dy)
# If they have collided, the chaser is on top of the target
if self.isCollision(self.x, self.y, dx, dy, MIN_DISTANCE):
self.x = dx
self.y = dy
# Speed for the chase
self.velocityx -= distancex
self.velocityy -= distancey
# Function makes the ghost run away from the player
def evade(self, dx, dy):
distancex = 0
distancey = 0
# If the chaser and chasee aren't touching, continue running by increasing the
# distance between the two
if dx != self.x and dy != self.y:
distancex += (self.x + dx) * .001
distancey += (self.y + dy) * .001
# If the mouse is to the bottom or right of the ghost
if dx > self.x and dy > self.y:
self.velocityx -= distancex
self.velocityy -= distancey
# If the mouse is to the top or left of the ghost
if dx < self.x and dy < self.y:
self.velocityx += distancex
self.velocityy += distancey
# Function makes the ghost seek the player
def seek(self, dx, dy):
distancex = 0
distancey = 0
# If the chaser and chasee aren't touching, continue chasing by reducing the
# distance between the two
if dx != self.x and dy != self.y:
distancex += (self.x - dx)
distancey += (self.y - dy)
# Speed for the chase
self.velocityx -= distancex
self.velocityy -= distancey
# Function makes the ghost NPC wander around
def wander(self):
# Randomly select the next direction to go to
nextdirection = randint(0, 7)
# Number of steps to go in that direction
numberofsteps = randint(0, 3)
# Cases for each direction
if nextdirection == 0:
dx = self.x
dy = MIN_DISTANCE - self.y
# Seek to the new direction
self.seek(dx, dy)
# Move to the direction wandered to
if nextdirection == 0: # North
dx = self.x
dy = self.y - (MIN_DISTANCE * numberofsteps)
# Seek to the new direction
self.seek(dx, dy)
elif nextdirection == 1: # North East
dx = self.x + (MIN_DISTANCE * numberofsteps)
dy = self.y - (MIN_DISTANCE * numberofsteps)
# Seek to the new direction
self.seek(dx, dy)
elif nextdirection == 2: # East
dx = self.x + (MIN_DISTANCE * numberofsteps)
dy = self.y
# Seek to the new direction
self.seek(dx, dy)
elif nextdirection == 3: # South East
dx = self.x + (MIN_DISTANCE * numberofsteps)
dy = self.y + (MIN_DISTANCE * numberofsteps)
# Seek to the new direction
self.seek(dx, dy)
elif nextdirection == 4: # South
dx = self.x
dy = self.y + (MIN_DISTANCE * numberofsteps)
# Seek to the new direction
self.seek(dx, dy)
elif nextdirection == 5: # South West
dx = self.x - (MIN_DISTANCE * numberofsteps)
dy = self.y + (MIN_DISTANCE * numberofsteps)
# Seek to the new direction
self.seek(dx, dy)
elif nextdirection == 6: # West
dx = self.x - (MIN_DISTANCE * numberofsteps)
dy = self.y
# Seek to the new direction
self.seek(dx, dy)
else: # North West
dx = self.x - (MIN_DISTANCE * numberofsteps)
dy = self.y - (MIN_DISTANCE * numberofsteps)
# Seek to the new direction
self.seek(dx, dy)
# AnnoyingGhost which has a boo strategy but no light strategy
class AnnoyingGhost(Ghost):
def __init__(self):
super(AnnoyingGhost, self).__init__(loud_boo, None)
self.colours = GHOST_COLOURS
def go_home(self, dx, dy):
print("Wait for me!")
self.seek(dx, dy)
def be_flashy(self, colours):
self.colours = colours
# ShyGhost which has a boo strategy and a light strategy
class ShyGhost(Ghost):
def __init__(self):
super(ShyGhost, self).__init__(gentle_boo, ten_seconds)
def run(self, dx, dy):
print("Leave me alone!")
self.evade(dx, dy)
# CulturedGhost which has a boo strategy but no light strategy
class CulturedGhost(Ghost):
def __init__(self):
super(CulturedGhost, self).__init__(gentle_boo, None)
def go_home(self):
print("I need a rest back at the art gallery.")
# RobotGhost which has a boo strategy and a light strategy
class RobotGhost(Ghost):
def __init__(self):
super(RobotGhost, self).__init__(loud_boo, ten_seconds)
def run(self):
print("Searching!")
self.wander()
def be_flashy(self, colours):
self.colours = colours