-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghosts.py
172 lines (133 loc) · 4.74 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import pygame
from pygame.locals import *
from vector import Vector2
from constants import *
from entity import Entity
from modes import ModeController
from sprites import GhostSprites
class Ghost(Entity):
def __init__(self, node, pacman=None, blinky=None):
Entity.__init__(self, node)
self.name = GHOST
self.points = 200
self.goal = Vector2()
self.directionMethod = self.goalDirection
self.pacman = pacman
self.mode = ModeController(self)
self.blinky = blinky
self.homeNode = node
def reset(self):
Entity.reset(self)
self.points = 200
self.directionMethod = self.goalDirection
def update(self, dt):
self.sprites.update(dt)
self.mode.update(dt)
if self.mode.current is SCATTER:
self.scatter()
elif self.mode.current is CHASE:
self.chase()
Entity.update(self, dt)
def scatter(self):
self.goal = Vector2()
def chase(self):
self.goal = self.pacman.position
def spawn(self):
self.goal = self.spawnNode.position
def setSpawnNode(self, node):
self.spawnNode = node
def startSpawn(self):
self.mode.setSpawnMode()
if self.mode.current == SPAWN:
self.setSpeed(150)
self.directionMethod = self.goalDirection
self.spawn()
def startFreight(self):
self.mode.setFreightMode()
if self.mode.current == FREIGHT:
self.setSpeed(50)
self.directionMethod = self.randomDirection
def normalMode(self):
self.setSpeed(65)
self.directionMethod = self.goalDirection
self.homeNode.denyAccess(DOWN, self)
class Blinky(Ghost):
def __init__(self, node, pacman=None, blinky=None):
Ghost.__init__(self, node, pacman, blinky)
self.name = BLINKY
self.color = RED
self.sprites = GhostSprites(self)
class Pinky(Ghost):
def __init__(self, node, pacman=None, blinky=None):
Ghost.__init__(self, node, pacman, blinky)
self.name = PINKY
self.color = PINK
self.sprites = GhostSprites(self)
def scatter(self):
self.goal = Vector2(TILEWIDTH*NCOLS, 0)
def chase(self):
self.goal = self.pacman.position + self.pacman.directions[self.pacman.direction] * TILEWIDTH * 4
class Inky(Ghost):
def __init__(self, node, pacman=None, blinky=None):
Ghost.__init__(self, node, pacman, blinky)
self.name = INKY
self.color = TEAL
self.sprites = GhostSprites(self)
def scatter(self):
self.goal = Vector2(TILEWIDTH*NCOLS, TILEHEIGHT*NROWS)
def chase(self):
vec1 = self.pacman.position + self.pacman.directions[self.pacman.direction] * TILEWIDTH * 2
vec2 = (vec1 - self.blinky.position) * 2
self.goal = self.blinky.position + vec2
class Clyde(Ghost):
def __init__(self, node, pacman=None, blinky=None):
Ghost.__init__(self, node, pacman, blinky)
self.name = CLYDE
self.color = ORANGE
self.sprites = GhostSprites(self)
def scatter(self):
self.goal = Vector2(0, TILEHEIGHT*NROWS)
def chase(self):
d = self.pacman.position - self.position
ds = d.magnitudeSquared()
if ds <= (TILEWIDTH * 8)**2:
self.scatter()
else:
self.goal = self.pacman.position + self.pacman.directions[self.pacman.direction] * TILEWIDTH * 4
class GhostGroup(object):
def __init__(self, node, pacman):
self.blinky = Blinky(node, pacman)
self.pinky = Pinky(node, pacman)
self.inky = Inky(node, pacman, self.blinky)
self.clyde = Clyde(node, pacman)
self.ghosts = [self.blinky, self.pinky, self.inky, self.clyde]
def __iter__(self):
return iter(self.ghosts)
def update(self, dt):
for ghost in self:
ghost.update(dt)
def startFreight(self):
for ghost in self:
ghost.startFreight()
self.resetPoints()
def setSpawnNode(self, node):
for ghost in self:
ghost.setSpawnNode(node)
def updatePoints(self):
for ghost in self:
ghost.points *= 2
def resetPoints(self):
for ghost in self:
ghost.points = 200
def hide(self):
for ghost in self:
ghost.visible = False
def show(self):
for ghost in self:
ghost.visible = True
def reset(self):
for ghost in self:
ghost.reset()
def render(self, screen):
for ghost in self:
ghost.render(screen)