-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
344 lines (293 loc) · 13.1 KB
/
classes.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import pygame
from config import *
class Button:
def __init__(self, x, y, image):
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.clicked = False
def is_mouse_over(self):
mouse_pos = pygame.mouse.get_pos()
return self.rect.collidepoint(mouse_pos)
def is_clicked(self):
if self.is_mouse_over() and pygame.mouse.get_pressed()[0]:
self.clicked = True
else:
self.clicked = False
def draw(self):
screen.blit(self.image, self.rect)
return self.clicked
class Player:
def __init__(self, x, y):
self.has_reset(x, y)
def draw_game(self):
if self.stepIndex >= 9:
self.stepIndex = 0
if self.move_left:
screen.blit(left[self.stepIndex], (self.rect.x, self.rect.y))
self.stepIndex += 1
elif self.move_right:
screen.blit(right[self.stepIndex], (self.rect.x, self.rect.y))
self.stepIndex += 1
else:
screen.blit(standing, (self.rect.x, self.rect.y))
# create an outline around the player
pygame.draw.rect(screen, WHITE, self.rect, 2)
def update_player(self, game_over):
global weapon_picked, dead_img
dy = 0
dx = 0
if game_over == 0:
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_a]:
if self.move_left:
self.dir = -1
else:
self.dir = 1
if weapon_picked == 1:
if len(mainbullets) < 5:
mainbullets.append(Bullet(self.rect.x - 15, self.rect.y))
if keys_pressed[pygame.K_SPACE] and self.isJump == False and self.jumping == False:
self.vel_y = -15
self.isJump = True
if keys_pressed[pygame.K_SPACE] == False:
self.isJump = False
if keys_pressed[pygame.K_LEFT]:
dx -= 5
self.move_left = True
self.move_right = False
if keys_pressed[pygame.K_RIGHT]:
dx += 5
self.move_left = False
self.move_right = True
if keys_pressed[pygame.K_LEFT] == False and keys_pressed[pygame.K_RIGHT] == False:
self.move_left = False
self.move_right = False
self.stepIndex = 0
self.vel_y += 1
if self.vel_y > 15:
self.vel_y = 15
dy += self.vel_y
# check for collision with danger or enemies
if pygame.sprite.spritecollide(self, enemy_group, False):
game_over = -1
print("The enemy killed you...")
print("Game over...")
if pygame.sprite.spritecollide(self, lava_group, False):
game_over = -1
print("You fell into the lava...")
print("Game over...")
if pygame.sprite.spritecollide(self, weapon_group, False):
weapon_picked = 1
# picR = gun_playerR
# picL = gun_playerL
if weapon_picked == 1:
pygame.sprite.spritecollide(self, weapon_group, True)
# check for collision with door to end the game and say player has won.
if pygame.sprite.spritecollide(self, door_group, False):
game_over = 1
print("You have won the game!")
# collision detection
self.jumping = True
for tile in world.tile_list:
tile_rect = pygame.Rect(tile[1]) #(tile stored in 1 and image in 0)
# check collision in x direction
if tile_rect.colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
# if the player is moving right and there is a collision, set player's right to tile's left
if dx > 0:
self.rect.right = tile_rect.left
# if the player is moving left and there is a collision, set player's left to tile's right
elif dx < 0:
self.rect.left = tile_rect.right
dx = 0 # stop the player's movement in x direction
# check collision in y direction
if tile_rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
# if the player is falling and there is a collision, set player's bottom to tile's top
if dy > 0:
self.rect.bottom = tile_rect.top
self.vel_y = 0
self.jumping = False
# if the player is jumping and there is a collision, set player's top to tile's bottom
elif dy < 0:
self.rect.top = tile_rect.bottom + 10
self.vel_y = 0
# set the change in y to 0 or less if there is a collision in y direction
dy = min(dy, 0)
self.rect.x += dx
self.rect.y += dy
for platform in platform_group:
if self.rect.colliderect(platform.rect):
if dx > 0: # Moving right; Hit the left side of the platform
if abs(self.rect.right - platform.rect.left) < 20 and self.rect.bottom > platform.rect.top + 20:
self.rect.right = platform.rect.left
dx = 0
elif dx < 0: # Moving left; Hit the right side of the platform
if abs(self.rect.left - platform.rect.right) < 20 and self.rect.bottom > platform.rect.top + 20:
self.rect.left = platform.rect.right
dx = 0
if dy > 0: # Falling down; Hit the top side of the platform
if abs(self.rect.bottom - platform.rect.top) < 20:
self.rect.bottom = platform.rect.top
dy = 0
self.jumping = False
self.rect.x += platform.dir # Move player with platform
elif dy < 0: # Jumping up; Hit the bottom side of the platform
if abs(self.rect.top - platform.rect.bottom) < 20:
self.rect.top = platform.rect.bottom
dy = 0
self.vel_y = 0
elif game_over == -1:
dead_img = pygame.transform.scale(dead_img, (tile_size, tile_size))
screen.blit(dead_img, (self.rect.x + 5, self.rect.y))
if self.rect.y >= 250:
self.rect.y -= 5
return game_over # returns the value of game_over via the update_player function
def has_reset(self, x, y):
self.stepIndex = 0
self.count = 0
self.imageR = right[self.stepIndex]
self.imageL = left[self.stepIndex]
# self.gunR = gunR[self.stepIndex]
self.rect = standing.get_rect() # GET STANDING IMAGE RECTANGLE
self.rect.x = x
self.rect.y = y
self.width = standing.get_width() # GET WIDTH
self.height = standing.get_height() # GET HEIGHT
self.move_left = False
self.move_right = False
self.vel_y = 0
self.isJump = False # check if player is jumping
self.jumping = False # check if player is in the air
self.dir = 0
class Mountain():
global tile_size
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
screen.blit(mountains, (self.x, self.y))
class World:
def __init__(self, data):
self.tile_list = []
row_count = 0
for row in data:
column_count = 0
for tile in row:
if tile == 1:
img = pygame.transform.scale(dirt_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = column_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 2:
img = pygame.transform.scale(grass_img, (tile_size, tile_size))
img_rect = img.get_rect()
img_rect.x = column_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 3:
enemy = Enemy(column_count * tile_size, row_count * tile_size)
enemy_group.add(enemy)
if tile == 4:
weapon = Weapons(column_count * tile_size, row_count * tile_size)
weapon_group.add(weapon)
if tile == 6:
lava = Danger(column_count * tile_size, row_count * tile_size + int((tile_size / 2)))
lava_group.add(lava)
if tile == 7:
platform = Platform(column_count * tile_size, row_count * tile_size + int((tile_size / 2)), 0, 1)
platform_group.add(platform)
if tile == 8:
door = Door(column_count * tile_size, row_count * tile_size)
door_group.add(door)
# where the instances are on the map
column_count += 1
row_count += 1
def draw(self):
for tile in self.tile_list:
screen.blit(tile[0], tile[1])
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('assets/enemy.png')
self.image = pygame.transform.scale(self.image, (51, 47))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dir = -1
self.counter = 0
def update(self):
self.rect.x += self.dir
self.counter += 1
# pygame.draw.rect(screen, WHITE, self.rect, 2)
if abs(self.counter) > 50:
self.dir *= -1 # turn left when counter is above 50.
# ^ this is done by multiplying dir by -1, therefore if dir is -1 it will turn to +1 (-1 * -1 = 1)
self.counter *= -1
# ^ reset the counter by flipping the counter, so the enemy can move to the left of its origin.
self.image = pygame.transform.flip(self.image, True, False)
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, moveright, moveleft):
pygame.sprite.Sprite.__init__(self)
image = pygame.image.load('assets/platform.png')
self.image = pygame.transform.scale(image, (tile_size, tile_size // 2))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.dir = 1
self.count = 0
self.mover = moveright
self.movel = moveleft
def update(self):
self.rect.x += self.dir
self.count += 1
if abs(self.count) > 25:
self.dir *= -1 # turn left when counter is above 25
# ^ this is done by multiplying dir by -1, therefore if dir is -1 it will turn to +1 (-1 * -1 = 1)
self.count *= -1
# ^ reset the counter by flipping the counter, so the platform can move to the left of its origin.
class Danger(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
img = pygame.image.load('assets/lava.png')
self.image = pygame.transform.scale(img, (tile_size, tile_size // 2))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Door(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
image = pygame.image.load('assets/door.png')
self.image = pygame.transform.scale(image, (tile_size, tile_size))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Weapons(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
image = pygame.image.load('assets/gun.png')
self.image = pygame.transform.scale(image, (tile_size, tile_size))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Bullet():
def __init__(self, x, y):
self.image = pygame.image.load('assets/bulletimg.png')
self.image = pygame.transform.rotate(self.image, 90)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.rect.x = x
self.rect.y = y
self.height = self.image.get_height()
self.width = self.image.get_width()
def draw(self):
screen.blit(self.image, (self.rect.x, self.rect.y))
enemy_group = pygame.sprite.Group()
lava_group = pygame.sprite.Group()
platform_group = pygame.sprite.Group()
door_group = pygame.sprite.Group()
weapon_group = pygame.sprite.Group()
world = World(world_data)