-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghost_inky.py
207 lines (167 loc) · 6.55 KB
/
ghost_inky.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
import os.path
import pygame
from rect_object import PacRects
import random
class Inky(PacRects):
def __init__(self, x, y, width, height, image, screen, pacman):
super().__init__(x, y, width, height, image)
# ============== Testing Random Movement ==============
self.m_index = 0
self.m_counter = 0
# =====================================================
# current
self.rect.x = x
self.rect.y = y
self.pac = pacman
self.ghost_speed_x = 5
self.ghost_speed_y = 0
self.ghost_timer = 0
self.screen = screen
# default position for Inky using maze
self.defualt_x = self.pac.maze.inky_default_x
self.default_y = self.pac.maze.inky_default_y
# ghost alive
self.ghost_alive = True
# ghost fear mode
self.ghost_fear = False
self.w_collision = False
# direction
self.direction = "right"
# animation images
self.images = [pygame.image.load(image),
pygame.image.load(os.getcwd()+ '/images/inky_2.png')]
self.a_index = 0
self.counter = 250
# death animation images
self.death_image = pygame.image.load(os.getcwd() + '/images/ghost_eyes.png')
# fear animation
self.fear_images = [pygame.image.load(os.getcwd() + '/images/ghost_fear_1.png'),
pygame.image.load(os.getcwd() + '/images/ghost_fear_2.png'),
pygame.image.load(os.getcwd() + '/images/ghost_fear_3.png'),
pygame.image.load(os.getcwd() + '/images/ghost_fear_4.png')]
self.f_index = 0
self.w_collision = False
self.reset_ghost()
def reset_ghost(self):
self.rect.x = self.defualt_x
self.rect.y = self.default_y
self.direction = "right"
self.w_collision = False
def increase_a_index(self):
self.ghost_timer += 1
if self.ghost_timer % 10 == 0:
self.ghost_timer = 0
self.a_index += 1
if self.a_index >= len(self.images):
self.a_index = 0
def increase_f_index(self):
self.ghost_timer += 1
if self.ghost_timer % 10 == 0:
self.ghost_timer = 0
self.f_index += 1
if self.f_index >= len(self.images):
self.f_index = 0
# figure out how to slow down animation with timer maybe?
def draw_(self):
img = self.images[self.a_index]
self.screen.blit(img, (self.rect.x, self.rect.y))
self.increase_a_index()
def pac_coll(self):
# collision with fear ghost and pacman
if self.rect.colliderect(self.pac.rect) and self.pac.ghost_fear and self.ghost_alive:
self.pac.pacman_alive = True
self.ghost_alive = False
self.pac.stats.score += 200
self.ghost_fear = False
# collision with pac-man and regular ghost
elif self.rect.colliderect(self.pac.rect) and not self.pac.ghost_fear and self.ghost_alive:
self.pac.pacman_alive = False
def death(self):
if self.counter > 0:
self.screen.blit(self.death_image, (self.rect.x, self.rect.y))
self.counter -= 1
if self.counter <= 0:
self.pac.ghost_fear = False
if not self.ghost_alive:
self.ghost_alive = True
self.counter = 250
# have to implement timer so fear disapears after some time
# also must figure out a way to switch between images 1,2 with images 3,4
# to signal warning also with a timer
def fear(self):
if self.counter > 0:
img = self.fear_images[self.f_index]
self.screen.blit(img, (self.rect.x, self.rect.y))
self.increase_f_index()
self.counter -= 1
if self.counter <= 0:
self.pac.ghost_fear = False
if not self.ghost_alive:
self.ghost_alive = True
self.counter = 250
# self.fear_warning()
def update_movement(self):
if self.direction == "up":
self.ghost_speed_x = 0
self.ghost_speed_y = -5
if self.direction == "down":
self.ghost_speed_y = 5
self.ghost_speed_x = 0
if self.direction == "left":
self.ghost_speed_x = -5
self.ghost_speed_y = 0
if self.direction == "right":
self.ghost_speed_x = 5
self.ghost_speed_y = 0
self.rect.x += self.ghost_speed_x
self.rect.y += self.ghost_speed_y
self.handle_w_coll()
# will take a direction from the A* algorithm
def ai_movement(self):
if self.m_counter % 10 == 0:
self.m_index = random.randrange(1, 4)
self.m_counter = 0
self.m_counter += 1
if self.m_index == 1:
self.direction = "left"
self.ghost_speed_x = -5
self.ghost_speed_y = 0
if self.m_index == 2:
self.direction = "right"
self.ghost_speed_x = 5
self.ghost_speed_y = 0
if self.m_index == 3:
self.direction = "up"
self.ghost_speed_x = 0
self.ghost_speed_y = -5
if self.m_index == 4:
self.direction = "down"
self.ghost_speed_y = 5
self.ghost_speed_x = 0
self.handle_w_coll()
def handle_w_coll(self):
# checks if ghost updated position collides with wall
for wall in self.pac.maze.walls:
if self.rect.colliderect(wall.rect):
# print("wall collision")
self.w_collision = True
break
# if there is a collision fix the position
if self.w_collision:
if self.direction == "up":
self.rect.y -= self.ghost_speed_y
self.direction = "down"
# print("direction switched from up => down")
elif self.direction == "left":
self.rect.x -= self.ghost_speed_x
self.direction = "right"
# print("direction switched from left => right")
elif self.direction == "down":
self.rect.y -= self.ghost_speed_y
self.direction = "up"
# print("direction switched from down => up")
elif self.direction == "right":
self.rect.x -= self.ghost_speed_x
self.direction = "left"
# print("direction switched from right => left")
self.w_collision = False