-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghost.py
199 lines (172 loc) · 7.27 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
import pygame
import tilemap
import tilemap_objects
from random import choice, randint
from abc import ABC, abstractmethod
from moving_objects import MovingObject
# abstract class
class Ghost(MovingObject, ABC):
def __init__(self, img):
super().__init__(img)
self.id = -1
self.speed = 3
self.x = randint(5, tilemap.WIDTH - 5) * tilemap.TILE_SIZE + 0.5 * tilemap.TILE_SIZE
self.y = randint(5, tilemap.HEIGHT - 5) * tilemap.TILE_SIZE + 0.5 * tilemap.TILE_SIZE
self.x_vec = choice([-1, 1])
self.y_vec = choice([-1, 1])
self.x_ind = tilemap.row_num(self.x)
self.y_ind = tilemap.col_num(self.y)
def check_move(self):
x_ind = tilemap.row_num(self.x + self.img_width / 2)
y_ind = tilemap.col_num(self.y + self.img_height / 2)
self.update_position(x_ind, y_ind)
collisions = 0
collides = self.check_collision()
while collides:
self.handle_collision(collides)
# if ghost is stuck - freeze him
if collisions == 8:
self.speed = 0
self.img = pygame.image.load('images/frozen_ghost.png')
collides = False
else:
collisions += 1
collides = self.check_collision()
def update_position(self, new_x, new_y):
old_x, old_y = self.x_ind, self.y_ind
tilemap.tile_map[old_y][old_x] = 0
tilemap.tile_map[new_y][new_x] = self.id
self.x_ind, self.y_ind = new_x, new_y
@abstractmethod
def check_collision(self):
pass
@abstractmethod
def handle_collision(self, colliding_vec):
pass
class BlueGhost(Ghost):
def __init__(self, img='images/blue_ghost.png'):
super().__init__(img)
def check_collision(self):
# next field horizontally
if tilemap.tile_map[self.y_ind][self.x_ind + self.x_vec] > 0:
return self.x_vec, 0
# next field vertically
elif tilemap.tile_map[self.y_ind + self.y_vec][self.x_ind] > 0:
return 0, self.y_vec
# next corner
elif tilemap.tile_map[self.y_ind + self.y_vec][self.x_ind + self.x_vec] > 0:
return self.x_vec, self.y_vec
else:
return ()
def handle_collision(self, colliding_vec):
x_col, y_col = colliding_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [1, 2]:
# change moving direction
if x_col:
self.x_vec = -self.x_vec
if y_col:
self.y_vec = -self.y_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 3:
tilemap_objects.kill_player()
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [11, 12, 13, 14]:
tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] = 0
class RedGhost(BlueGhost):
def __init__(self):
super().__init__('images/red_ghost.png')
def handle_collision(self, colliding_vec):
x_col, y_col = colliding_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 1:
# change moving direction
if x_col:
self.x_vec = -self.x_vec
if y_col:
self.y_vec = -self.y_vec
# destroy hit occupied tiles
tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] = 0
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 2:
# change moving direction
if x_col:
self.x_vec = -self.x_vec
if y_col:
self.y_vec = -self.y_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 3:
tilemap_objects.kill_player()
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [11, 12, 13, 14]:
tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] = 0
class GreenGhost(Ghost):
def __init__(self):
super().__init__('images/green_ghost.png')
self.x_ind = tilemap.WIDTH - 2
self.y_ind = tilemap.HEIGHT - 2
self.x_vec = choice([-1, 0])
self.y_vec = 0 if self.x_vec else -1
self.x = self.x_ind * tilemap.TILE_SIZE + self.x_vec * self.speed
self.y = self.y_ind * tilemap.TILE_SIZE + self.y_vec * self.speed
self.changed_dir = 0
# moves along occupied fields
def check_collision(self):
if tilemap.tile_map[self.y_ind + self.y_vec][self.x_ind + self.x_vec] > 0:
return self.x_vec, self.y_vec
elif not self.changed_dir and tilemap.tile_map[self.y_ind - self.x_vec][self.x_ind + self.y_vec] == 0:
return self.y_vec, -self.x_vec
else:
self.changed_dir = 0
return ()
def handle_collision(self, colliding_vec):
x_col, y_col = colliding_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 0:
# change moving direction
if y_col:
self.y_vec = -self.x_vec
self.x_vec = 0
if x_col:
self.x_vec = self.y_vec
self.y_vec = 0
self.changed_dir = 1
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [1, 2]:
# change moving direction
if x_col:
self.y_vec = self.x_vec
self.x_vec = 0
if y_col:
self.x_vec = -self.y_vec
self.y_vec = 0
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 3:
tilemap_objects.kill_player()
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [11, 12, 13, 14]:
tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] = 0
class OrangeGhost(Ghost):
def __init__(self, x_ind, y_ind):
super().__init__('images/orange_ghost.png')
self.id = 4
self.x_ind = x_ind
self.y_ind = y_ind
self.x = x_ind * tilemap.TILE_SIZE + 0.5 * tilemap.TILE_SIZE
self.y = y_ind * tilemap.TILE_SIZE + 0.5 * tilemap.TILE_SIZE
# bounces off unoccupied fields and screen edges
def check_collision(self):
if tilemap.tile_map[self.y_ind][self.x_ind + self.x_vec] in [0, 2]:
return self.x_vec, 0
elif tilemap.tile_map[self.y_ind + self.y_vec][self.x_ind] in [0, 2]:
return 0, self.y_vec
elif tilemap.tile_map[self.y_ind + self.y_vec][self.x_ind + self.x_vec] in [0, 2]:
return self.x_vec, self.y_vec
else:
return ()
def handle_collision(self, colliding_vec):
x_col, y_col = colliding_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [0, 2]:
# change moving direction
if x_col:
self.x_vec = -self.x_vec
if y_col:
self.y_vec = -self.y_vec
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] == 3:
tilemap_objects.kill_player()
if tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] in [11, 12, 13, 14]:
tilemap.tile_map[self.y_ind + y_col][self.x_ind + x_col] = 1
def update_position(self, new_x, new_y):
old_x, old_y = self.x_ind, self.y_ind
tilemap.tile_map[old_y][old_x] = 1
tilemap.tile_map[new_y][new_x] = self.id
self.x_ind, self.y_ind = new_x, new_y