-
Notifications
You must be signed in to change notification settings - Fork 4
/
blocks.py
279 lines (214 loc) · 9.18 KB
/
blocks.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import pygame
import random
# Screen dimensions
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
PURPLE = (150, 0, 255)
BUTTON_ACTIVE = (150, 150, 150)
BUTTON_INACTIVE = (200, 200, 200)
class Platform(pygame.sprite.Sprite):
def __init__(self, width, height, zone):
super().__init__()
self.zone = zone
# City skin
if self.zone == "City":
if width <= 150:
self.image = pygame.image.load("./resources/img/city-plat.png").convert_alpha()
else:
self.image = pygame.image.load("./resources/img/city-plat-long.png").convert_alpha()
# Swamp skin
elif self.zone == "Swamp":
if width <= 150:
self.image = pygame.image.load("./resources/img/swamp-plat.png").convert_alpha()
else:
self.image = pygame.image.load("./resources/img/swamp-plat-long.png").convert_alpha()
# Underwater skin
elif self.zone == "Underwater":
if width <= 150:
self.image = pygame.image.load("./resources/img/underwater-plat.png").convert_alpha()
else:
self.image = pygame.image.load("./resources/img/underwater-plat-long.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (width, height))
self.rect = self.image.get_rect()
class MovingPlatform(Platform):
def __init__(self, width, height, zone, player):
# Call parent constructor to skin and construct platform dimensions
super().__init__(width, height, zone)
self.change_x = 0
self.change_y = 0
self.boundary_top = 0
self.boundary_bottom = 0
self.boundary_left = 0
self.boundary_right = 0
self.player = player
def update(self):
# Move left/right
self.rect.x += self.change_x
# See if we hit the player
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
# We did hit the player. Shove the player around and
# assume he/she won't hit anything else.
# If we are moving right, set our right side
# to the left side of the item we hit
if self.change_x < 0:
self.player.rect.right = self.rect.left
else:
# Otherwise if we are moving left, do the opposite.
self.player.rect.left = self.rect.right
# Move up/down
self.rect.y += self.change_y
# Check and see if we the player
hit = pygame.sprite.collide_rect(self, self.player)
if hit:
# We did hit the player. Shove the player around and
# assume he/she won't hit anything else.
# Reset our position based on the top/bottom of the object.
if self.change_y < 0:
self.player.rect.bottom = self.rect.top
else:
self.player.rect.top = self.rect.bottom
# Check the boundaries and see if we need to reverse
# direction.
if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
self.change_y *= -1
if self.rect.x < self.boundary_left or self.rect.x > self.boundary_right:
self.change_x *= -1
class Wall(Platform):
def __init__(self, width, height, zone):
# Call parent constructor to skin and construct platform dimensions
super().__init__(width, height, zone)
self.zone = zone
# City skin
if self.zone == "City":
self.image = pygame.image.load("./resources/img/city-wall.png").convert_alpha()
elif self.zone == "Swamp":
self.image = pygame.image.load("./resources/img/swamp-wall.png").convert_alpha()
elif self.zone == "Underwater":
self.image = pygame.image.load("./resources/img/underwater-wall.png").convert_alpha()
self.image = pygame.transform.scale(self.image, (width, height))
class Bullet(pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([4, 4])
self.image.fill(BLACK)
self.direction = "R"
self.rect = self.image.get_rect()
def update(self):
""" Move the bullet. """
if self.direction == "R":
self.rect.x += 9
if self.direction == "L":
self.rect.x -= 9
class Target(pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.target_states = []
self.width = 37
self.height = 80
self.direction = "R"
self.change_x = 1
self.change_y = 0
self.move_start = 0
self.move_end = 0
target_img = pygame.image.load("./resources/img/initial.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)
target_img = pygame.image.load("./resources/img/initial.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)
self.image = self.target_states[0]
self.rect = self.image.get_rect()
def change_directions(self):
self.change_x *= -1
if self.direction == "R":
self.direction = "L"
elif self.direction == "L":
self.direction = "R"
def update(self):
# Walk only between edges of current platform
if (self.rect.x + self.width == self.move_end or self.rect.x == self.move_start) and 0 != self.change_x:
self.change_directions()
# Handling for spawn glitched enemies
if self.move_end - self.move_start <= 10 + self.width:
self.change_x = 0
if self.rect.x < self.move_start or self.rect.x + self.width > self.move_end:
self.change_x = 0
# State changing based on direction
if self.direction == "R":
self.image = self.target_states[0]
if self.direction == "L":
self.image = self.target_states[1]
# Move left/right
self.rect.x += self.change_x
self.rect.y += self.change_y
class Cop(Target):
def __init__(self):
Target.__init__(self)
self.target_states.clear()
target_img = pygame.image.load("./resources/img/cop.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)
target_img = pygame.image.load("./resources/img/cop2.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)
class Pogomonkey(Target):
def __init__(self):
Target.__init__(self)
self.jump_start = None
self.target_states.clear()
self.jump = False
target_img = pygame.image.load("./resources/img/pogomonkey.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)
self.target_states.append(target_img)
def update(self):
# Gravity
if self.change_y == 0:
self.change_y = 1
else:
self.change_y += .35
# Check for grounding
if self.rect.y >= self.jump_start - self.rect.height:
self.change_y = 0
self.jump = True
self.rect.y = self.jump_start - self.rect.height
if self.jump:
i = random.randrange(0, 2)
if i == 1:
self.change_y = -6
self.jump = False
# Walk only between edges of current platform
# Right walk handling
if self.rect.x + self.width == self.move_end or self.rect.x == self.move_start:
self.change_x *= -1
self.change_directions()
elif self.rect.x + self.width > self.move_end or self.rect.x < self.move_start:
self.change_x = 0
# State changing based on direction
if self.direction == "R":
self.image = self.target_states[0]
if self.direction == "L":
self.image = self.target_states[1]
# Move left/right
self.rect.x += self.change_x
self.rect.y += self.change_y
class Bikefish(Target):
def __init__(self):
Target.__init__(self)
self.target_states.clear()
self.width = 87
target_img = pygame.image.load("./resources/img/bikefish.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)
target_img = pygame.image.load("./resources/img/bikefish2.png").convert_alpha()
target_img = pygame.transform.scale(target_img, (self.width, self.height))
self.target_states.append(target_img)