-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost.py
More file actions
210 lines (196 loc) · 9.83 KB
/
ghost.py
File metadata and controls
210 lines (196 loc) · 9.83 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
import pygame
from pygame.locals import *
from spritesheet import *
class ghost(pygame.sprite.Sprite):
def __init__(self, gs, mode, gmap, board, color):
super(ghost, self).__init__()
self.gs = gs
self.mode = mode
self.gmap = gmap
self.board = board
self.color = color
self.yratio = (self.board.rect.height - 34.0)/len(self.gmap)
self.xratio = (self.board.rect.width -10)/len(self.gmap[0])
self.stuck = False
self.dead = False
self.spritefile = "pacman.sprite"
ss = spritesheet(self.spritefile)
self.black = (0,0,0)
self.ghostImages = dict()
# Below we load a sprite twice to keep the order in which it moves. We sacrifice memory for easier coding
if(color == "RED"):
iBaseY = 0;
elif(color == "PINK"):
iBaseY = 18;
elif(color == "CYAN"):
iBaseY = 36;
elif(color == "ORANGE"):
iBaseY = 54;
else:
raise Exception("Not a proper color")
self.ghostImages["RIGHT"] = ss.images_at(((3, 125+iBaseY, 14, 13), (20, 125+iBaseY, 14, 13), (3, 125+iBaseY, 14, 13), (20, 125+iBaseY, 14, 13)), colorkey=self.black)
self.ghostImages["LEFT"] = ss.images_at(((37, 125+iBaseY, 14, 13), (54,125+iBaseY,14,13),(37,125+iBaseY,14,13),(54,125+iBaseY,14,13)), colorkey=self.black)
self.ghostImages["UP"] = ss.images_at(((71,125+iBaseY,14,13), (88,125+iBaseY,14,13),(71,125+iBaseY,14,13),(88,125+iBaseY,14,13)), colorkey=self.black)
self.ghostImages["DOWN"] = ss.images_at(((105,125+iBaseY,14,13),(122,125+iBaseY,14,13),(105,125+iBaseY,14,13),(122,125+iBaseY,14,13)),colorkey=self.black)
self.ghostSpooked = dict()
self.ghostSpooked["V1"] = ss.images_at(((3,197,14,13),(20,197,14,13),(3,197,14,13),(20,197,14,13)),colorkey=self.black) # Just blue
self.ghostSpooked["V2"] = ss.images_at(((3,197,14,13),(20,197,14,13),(37,197,14,13),(54,197,14,13)),colorkey=self.black) # Flicker between blue and white
# Scale images
for key, imageL in self.ghostImages.iteritems():
for n in range(4):
rect = imageL[n].get_rect()
w=round(rect.width*self.board.ratio*0.8)
h=round(rect.height*self.board.ratio*0.8)
imageL[n] = pygame.transform.scale(imageL[n], (int(w), int(h)))
for key, imageL in self.ghostSpooked.iteritems():
for n in range(4):
rect = imageL[n].get_rect()
w=round(rect.width*self.board.ratio*0.8)
h=round(rect.height*self.board.ratio*0.8)
imageL[n] = pygame.transform.scale(imageL[n], (int(w), int(h)))
self.moving = True
self.spooked = False
self.trapped = True # Inside the middle room
self.blink = False
self.rect = [0,0,0,0]
self.innerTick = 0
self.posx = 9
self.posy = 11
#self.posx = 11
#self.posy = 21
self.baseX = self.board.rect.x + 10
self.baseY = self.board.rect.y + 18
self.boardMaxY = len(self.gmap)
self.boardMaxX = len(self.gmap[0])
self.movingDirection = "RIGHT"
self.setDirection = "RIGHT"
self.iter = 0
self.image = self.ghostImages[self.movingDirection][1]
self.toTick = True
def calcRect(self):
self.rect = self.image.get_rect()
xoffset = 0
yoffset = 0
if(self.movingDirection == "RIGHT"):
xoffset = round(self.innerTick*self.xratio/4.0)
elif(self.movingDirection == "LEFT"):
xoffset = 0-round(self.innerTick*self.xratio/4.0)
elif(self.movingDirection == "UP"):
yoffset = 0-round(self.innerTick*self.yratio/4.0)
elif(self.movingDirection == "DOWN"):
yoffset = round(self.innerTick*self.yratio/4.0)
self.rect.x = round(self.posx*self.xratio) + self.baseX + xoffset
self.rect.y = round(self.posy*self.yratio) + self.baseY + yoffset
def updateSetDirection(self, direction):
if self.mode == 1:
#print("Ghost:Updating direction to: ",direction)
#print("moving direction is: ", self.movingDirection)
self.setDirection = direction
def release(self):
#print("Releasing")
self.trapped = False
def update(self):
if(self.mode == 1):
if(self.moving):
self.innerTick = (self.innerTick + 1) % 4
if self.innerTick == 0: # special case 1
if(self.moving):
if(self.movingDirection == "UP"):
self.posy -= 1
elif(self.movingDirection == "DOWN"):
self.posy += 1
elif(self.movingDirection == "LEFT"):
self.posx = (self.posx-1)%(self.boardMaxX+1)
elif(self.movingDirection == "RIGHT"):
self.posx = (self.posx+1)%(self.boardMaxX+1)
if(self.movingDirection != self.setDirection):
if self.setDirection == "UP":
#print("set direction is up")
if(self.posy > 0):
#print("post is greater 0")
if( self.gmap[self.posy-1][self.posx] ==1): # not a wall
self.movingDirection = "UP"
#print("changing moving direction")
elif(self.gmap[self.posy-1][self.posx]==2 and not self.trapped):
self.movingDirection = "UP"
else:
pass # Do nothing
elif self.setDirection == "DOWN":
if(self.posy < len(self.gmap) -1):
if(self.gmap[self.posy+1][self.posx] ==1): # not a wall
self.movingDirection = "DOWN"
else:
pass # Do nothing
elif self.setDirection == "RIGHT":
if(self.posx != self.boardMaxX):
if(self.posx < len(self.gmap[0]) -1):
if( self.gmap[self.posy][self.posx+1] ==1): # not a wall
self.movingDirection = "RIGHT"
elif( self.gmap[self.posy][self.posx+1] == 3): # portal to the other side
self.movingDirection = "RIGHT"
else:
pass # Do nothing
else:
pass
elif self.setDirection == "LEFT":
if(self.posx != 0):
if(self.posx > 0):
if(self.gmap[self.posy][self.posx-1] ==1): # not a wall
self.movingDirection = "LEFT"
elif( self.gmap[self.posy][self.posx-1] == 3): # portal
self.movingDirection = "LEFT"
else:
pass # Do nothing
else:
pass
if self.movingDirection == "UP":
if( self.posy > 0):
if(self.gmap[self.posy-1][self.posx] ==1): # not a wall
self.moving = True
elif(self.gmap[self.posy-1][self.posx] == 2 and not self.trapped):
self.moving = True
else:
self.moving = False
else:
self.moving = False
elif self.movingDirection == "DOWN":
if(self.posy < len(self.gmap)-1):
if (self.gmap[self.posy+1][self.posx] ==1 ): # not a wall
self.moving = True
else:
self.moving = False
else:
self.moving = False
elif self.movingDirection == "RIGHT":
if(self.posx < (len(self.gmap[0]) -1)):
##print(self.gmap[self.posx+1])
if( self.gmap[self.posy][self.posx+1] ==1 or self.gmap[self.posy][self.posx+1] == 3): # not a wall
self.moving = True
#print("self.posx", self.posx)
else:
self.moving = False
else:
if(self.gmap[self.posy][self.posx] == 3):
self.moving = True
else:
self.moving = False
elif self.movingDirection == "LEFT":
if(self.posx > 0):
if(self.gmap[self.posy][self.posx-1] ==1 or self.gmap[self.posy][self.posx-1] == 3): # not a wall
self.moving = True
else:
self.moving = False
else:
if(self.gmap[self.posy][self.posx] == 3):
self.moving = True
else:
self.moving = False
if not self.spooked:
self.image = self.ghostImages[self.movingDirection][self.innerTick]
elif self.spooked and not self.blink:
self.image = self.ghostSpooked["V1"][self.innerTick]
elif self.spooked and self.blink:
self.image = self.ghostSpooked["V2"][self.innerTick]
self.calcRect()
def setMode(self, mode):
self.mode = mode