-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
547 lines (468 loc) · 22.1 KB
/
main.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# . .
# :-:--=:-:::. :=-**##*=:
# :=----------. .-%@@@@@@@@@%:
# :-------------: :@@@@@@@@@@@@%.
# :-=-----------==: +@@@@@@@@@@@@@#
# .------------=------. =@@@@@@@@@@@@@#
# :=-=-------===-=-- .+%@@@@@@@@@@@#=
# --=--------==-=-. -*%@@@@@@@@@*-.
# ::----===+- .#%@@@@*.
# -+++=: . :+##+
# -+=====. .=%@@%%%#=
# :-----------:. :+#%%%@@@@@%@%+-
# ----------------- -%%%%%@@@%@@%%@%%*
# .-==----------==--: #%%%@%@@@@@@@@@@%%.
# :-=+----------*=--- =%%%@@%%@@@%%@@@%%%=
# ---=----------*----: .#%%%@@%%@@@%@%@@%%%%
# :-===----------+=---= -#%%%@@%%@%@%@%@@%%%%=
# --=----------=#==+. ==+%@@%%@@@%@%@@*++.
# --=-----------*=--- :===#@@%%@@@%@%%%--=
# -==-----------++--= ---:#@%@@@%%%@@@%--=
# -=------------=:--=. =-- %@%%%%%%@%%%@=-=
# .-+-------------.:---.--: %%%%%%%%@%%@@+==
# :-++*++++++*+***. --=+-- *###########**-=
# --*+++++++++*+++: :--*-: :------=------*-=
# =-*++++++++*+***- .--*-. :-------------+-=
# .--*+++=+*++*+***+ :==*=: -------=------===:
# :=+++++==+++*++**+ -*++=. -------+-------+=:
# -++++=+==**+++*** :-: -------+-------+.
# -+++=++=****+**# -------+=------=
# .++==*=---=*+**+ =------+*------=
# ----= :---= ====-.::+====
# :**#==---=: ----= .. .:::=--=+*%#*--=+***. .--:..
# .=+**#=--== :=--=%@*:.-=+%%*--=: ::+=--+***+=#@%*-=-::.
# :+=--=. :::=--=:.-*#%*--=*---+-+**=--=--=+**+*=**%@%=
# =--= .#%%=--=. +*#%#= +---#%++#=---.+%@%+ .+++*+-
# ==== .:+===: -==+= :===*+: -==== .--:. ..
# =--= ----: .---- :=---
# ---- :---: .=--- .=---
# ---- :---: .=--- =---
# ---: :---: .=--- +---
# +##%. =*##- -%%#: %%%#
# :@@@@- #@@@+ %@@@* :@@@%:
# .====. -++=: =+==- --==.
# @milosnowcat
# Tiles from https://bayat.itch.io/platform-game-assets
# Character from https://arks.itch.io/dino-characters
# Sound effects from https://jdwasabi.itch.io/8-bit-16-bit-sound-effects-pack
# Music from https://chippy01302.itch.io/chippy-music-pack
import pygame
from pygame.locals import *
import utils
from pygame import mixer
pygame.mixer.pre_init(44100, -16, 2, 512)
mixer.init()
pygame.init()
clock = pygame.time.Clock()
fps = 60
screen_width = 960
screen_height = 540
font = pygame.font.Font('assets/fonts/PressStart2P-Regular.ttf', 70)
font_score = pygame.font.Font('assets/fonts/PressStart2P-Regular.ttf', 30)
black = (0, 0, 0)
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Mario')
tile_size = 60
game_over = 0
main_menu = True
level = 1
score = 0
win = False
background = pygame.image.load('assets/img/background.png')
bg_img = pygame.transform.scale(background, (screen_width, screen_height))
img_restart = pygame.image.load('assets/img/button_restart.png')
img_start = pygame.image.load('assets/img/button_start.png')
img_exit = pygame.image.load('assets/img/button_exit.png')
pygame.mixer.music.load('assets/audio/music.wav')
pygame.mixer.music.play(-1, 0.0, 5000)
coin_fx = pygame.mixer.Sound('assets/audio/coin.wav')
coin_fx.set_volume(0.5)
jump_fx = pygame.mixer.Sound('assets/audio/jump.wav')
jump_fx.set_volume(0.5)
dead_fx = pygame.mixer.Sound('assets/audio/dead.wav')
dead_fx.set_volume(0.5)
def draw_text(text, font, color, x, y):
"""
The function `draw_text` takes in parameters for text, font, color, x and y coordinates, and renders
the text onto the screen at the specified coordinates.
:param text: The text parameter is a string that represents the text you want to draw on the screen
:param font: The font parameter is the font object that you want to use for rendering the text. It
should be a pygame.font.Font object
:param color: The color parameter is the color of the text that will be drawn on the screen. It can
be specified using RGB values or by using predefined color names from the pygame library, such as
pygame.Color('red') or pygame.Color(255, 0, 0)
:param x: The x parameter represents the x-coordinate of the top-left corner of the text on the
screen
:param y: The y parameter in the draw_text function represents the vertical position of where the
text will be drawn on the screen. It determines the distance from the top of the screen to the
baseline of the text
"""
img = font.render(text, True, color)
screen.blit(img, (x, y))
def next_level(level):
"""
The function "next_level" resets the game environment and player position for the next level.
:param level: The level parameter is an integer that represents the current level of the game
:return: the game data for the next level.
"""
spike_group.empty()
water_group.empty()
chest_group.empty()
coin_group.empty()
game_data = utils.worlds(level)
player.reset(game_data[1][0], game_data[1][1])
return game_data
# The Button class represents a clickable button with an image that can be drawn on the screen and
# detects when it is clicked.
class Button():
def __init__(self, x, y, image):
"""
The function initializes an object with given coordinates and an image, and sets the initial
state of the object.
:param x: The x-coordinate of the object's position on the screen
:param y: The y parameter represents the y-coordinate of the object's position on the screen
:param image: The "image" parameter is the image that will be displayed for the object. It is
passed as an argument to the constructor when creating an instance of the class
"""
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.clicked = False
def draw(self):
"""
The function checks if the mouse is clicked on a specific object and returns a boolean value
indicating whether an action should be performed.
:return: the value of the variable "action".
"""
action = False
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] and not self.clicked:
action = True
self.clicked = True
if not pygame.mouse.get_pressed()[0]:
self.clicked = False
screen.blit(self.image, self.rect)
return action
# The Player class represents a player character in a game, with methods for updating its position and
# handling user input.
class Player():
def __init__(self, x, y):
"""
The above function is a constructor that initializes the object with the given x and y values.
:param x: The x parameter is used to initialize the x-coordinate of an object
:param y: The parameter "y" is a variable that represents the y-coordinate of a point
"""
self.reset(x, y)
def update(self, game_over):
"""
The `update` function updates the position and behavior of a game character based on user input
and game conditions.
:param game_over: The parameter "game_over" is a variable that indicates the current state of
the game. It is used to control the player's movement and actions based on whether the game is
over or not. If game_over is 0, the player can move and perform actions. If game_over is -1
:return: The variable `game_over` is being returned.
"""
dx = 0
dy = 0
walk_cooldown = 5
if game_over == 0:
key = pygame.key.get_pressed()
if key[pygame.K_SPACE] and not self.jumped and not self.in_air:
jump_fx.play()
self.vel_y = -15
self.jumped = True
if not key[pygame.K_SPACE]:
self.jumped = False
if key[pygame.K_LEFT]:
dx -= 3
self.counter += 1
self.direction = -1
if key[pygame.K_RIGHT]:
dx += 3
self.counter += 1
self.direction = 1
if not key[pygame.K_LEFT] and not key[pygame.K_RIGHT]:
self.counter = 0
self.index = 0
if self.direction == 1:
self.image = self.image_idle_right
if self.direction == -1:
self.image = self.image_idle_left
if self.counter > walk_cooldown:
self.counter = 0
self.index += 1
if self.index >= len(self.images_right):
self.index = 0
if self.direction == 1:
self.image = self.images_right[self.index]
if self.direction == -1:
self.image = self.images_left[self.index]
self.vel_y += 1
if self.vel_y > 10:
self.vel_y = 10
dy += self.vel_y
self.in_air = True
for tile in world.tile_list:
if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height):
dx = 0
if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
if self.vel_y < 0:
dy = tile[1].bottom - self.rect.top
self.vel_y = 0
elif self.vel_y > 0:
dy = tile[1].top - self.rect.bottom
self.in_air = False
else:
if self.vel_y < 0:
if self.direction == 1:
self.image = self.image_jump_right
if self.direction == -1:
self.image = self.image_jump_left
if pygame.sprite.spritecollide(self, spike_group, False):
game_over = -1
dead_fx.play()
if pygame.sprite.spritecollide(self, water_group, False):
game_over = -1
dead_fx.play()
if pygame.sprite.spritecollide(self, chest_group, False):
game_over = 1
self.rect.x += dx
self.rect.y += dy
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
dy = 0
self.in_air = False
if self.rect.top < 0:
self.rect.top = 0
self.vel_y = 0
if self.rect.right > screen_width:
self.rect.right = screen_width
dx = 0
if self.rect.left < 0:
self.rect.left = 0
dx = 0
elif game_over == -1:
if self.direction == 1:
self.image = self.image_dead_right
if self.direction == -1:
self.image = self.image_dead_left
screen.blit(self.image, self.rect)
return game_over
def reset(self, x, y):
"""
The `reset` function initializes the attributes of a Dino object in a game, including loading
and scaling images, setting the initial position and velocity, and setting flags for jumping and
direction.
:param x: The x parameter represents the x-coordinate of the position where the object will be
reset
:param y: The parameter `y` represents the y-coordinate of the position where the object will be
reset
"""
self.images_right = []
self.images_left = []
self.index = 0
self.counter = 0
img_idle_right = pygame.image.load('assets/img/dino.png')
img_idle_right = pygame.transform.scale(img_idle_right, (60, 68))
img_idle_left = pygame.transform.flip(img_idle_right, True, False)
self.image_idle_right = img_idle_right
self.image_idle_left = img_idle_left
img_jump_right = pygame.image.load('assets/img/dino_jump.png')
img_jump_right = pygame.transform.scale(img_jump_right, (60, 68))
img_jump_left = pygame.transform.flip(img_jump_right, True, False)
self.image_jump_right = img_jump_right
self.image_jump_left = img_jump_left
img_dead_right = pygame.image.load('assets/img/dino_dead.png')
img_dead_right = pygame.transform.scale(img_dead_right, (60, 68))
img_dead_left = pygame.transform.flip(img_dead_right, True, False)
self.image_dead_right = img_dead_right
self.image_dead_left = img_dead_left
for num in range(1, 7):
img_right = pygame.image.load(f'assets/img/dino_walk{num}.png')
img_right = pygame.transform.scale(img_right, (60, 72))
img_left = pygame.transform.flip(img_right, True, False)
self.images_right.append(img_right)
self.images_left.append(img_left)
self.image = self.image_idle_right
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = self.image.get_width()
self.height = self.image.get_height()
self.vel_y = 0
self.jumped = False
self.direction = 0
self.in_air = True
# The World class is responsible for creating and drawing tiles based on the data provided.
class World():
def __init__(self, data):
"""
The function initializes a list of tiles based on the given data, where each tile is represented
by an image and its corresponding rectangle.
:param data: The "data" parameter is a 2D list that represents the layout of tiles in a game
level. Each element in the list represents a tile, and its value determines the type of tile
"""
self.tile_list = []
dirt_img = pygame.image.load('assets/img/dirt.png')
grass_img = pygame.image.load('assets/img/grass.png')
row_count = 0
for row in data:
col_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 = col_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 = col_count * tile_size
img_rect.y = row_count * tile_size
tile = (img, img_rect)
self.tile_list.append(tile)
if tile == 3:
spike = Spike(col_count * tile_size + (tile_size / 2), row_count * tile_size + tile_size)
spike_group.add(spike)
if tile == 4:
water = Water(col_count * tile_size, row_count * tile_size + (tile_size / 2))
water_group.add(water)
if tile == 5:
chest = Chest(col_count * tile_size, row_count * tile_size)
chest_group.add(chest)
if tile == 6:
coin = Coin(col_count * tile_size + (tile_size / 2), row_count * tile_size + (tile_size / 2))
coin_group.add(coin)
col_count += 1
row_count += 1
def draw(self):
"""
The function iterates through a list of tiles and blits them onto the screen.
"""
for tile in self.tile_list:
screen.blit(tile[0], tile[1])
# The Spike class is a subclass of pygame.sprite.Sprite that represents a spike object with an image
# and a position.
class Spike(pygame.sprite.Sprite):
def __init__(self, x, y):
"""
The function initializes a sprite object with an image and sets its position.
:param x: The x parameter represents the x-coordinate of the center of the sprite
:param y: The y parameter represents the y-coordinate of the center of the sprite
"""
pygame.sprite.Sprite.__init__(self)
img = pygame.image.load('assets/img/spike.png')
self.image = img
self.rect = self.image.get_rect()
self.rect.center = (x, y)
# The Water class is a sprite that represents a body of water in a game, with a specified position and
# image.
class Water(pygame.sprite.Sprite):
def __init__(self, x, y):
"""
This function initializes a sprite object with an image and sets its position.
:param x: The x parameter represents the x-coordinate of the sprite's position on the screen. It
determines the horizontal position of the sprite
:param y: The parameter `y` represents the y-coordinate of the sprite's position on the screen
"""
pygame.sprite.Sprite.__init__(self)
img = pygame.image.load('assets/img/water.png')
self.image = pygame.transform.scale(img, (tile_size, tile_size * 1.5))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# The `Chest` class represents a chest sprite in a game, with a specified position and image.
class Chest(pygame.sprite.Sprite):
def __init__(self, x, y):
"""
The function initializes a sprite object with an image and sets its position on the screen.
:param x: The x parameter represents the x-coordinate of the chest's position on the screen
:param y: The parameter "y" represents the y-coordinate of the chest's position on the screen
"""
pygame.sprite.Sprite.__init__(self)
img = pygame.image.load('assets/img/chest.png')
self.image = pygame.transform.scale(img, (tile_size, tile_size))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# The Coin class represents a coin object in a game and initializes its image and position.
class Coin(pygame.sprite.Sprite):
def __init__(self, x, y):
"""
The function initializes a sprite object with an image of a coin and sets its position.
:param x: The x parameter represents the x-coordinate of the center of the coin sprite on the
screen
:param y: The parameter `y` represents the y-coordinate of the center of the coin sprite
"""
pygame.sprite.Sprite.__init__(self)
img = pygame.image.load('assets/img/coin.png')
self.image = pygame.transform.scale(img, (tile_size / 2, tile_size / 2))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
game_data = utils.worlds(level)
player = Player(game_data[1][0], game_data[1][1])
spike_group = pygame.sprite.Group()
water_group = pygame.sprite.Group()
chest_group = pygame.sprite.Group()
coin_group = pygame.sprite.Group()
world = World(game_data[0])
restart_button = Button(screen_width / 2 - 32, screen_height / 2 - 32, img_restart)
start_button = Button(screen_width / 2 - 128, screen_height / 2 - 32, img_start)
exit_button = Button(screen_width / 2 + 64, screen_height / 2 - 32, img_exit)
# The above code is a game loop written in Python using the Pygame library. It sets up a game window,
# handles user input, updates game objects, and renders them on the screen.
run = True
while run:
clock.tick(fps)
screen.blit(bg_img, (0, 0))
if main_menu:
if exit_button.draw():
run = False
if start_button.draw():
main_menu = False
if win:
draw_text('YOU WIN!', font, black, screen_width / 2 - (screen_width / 4) , screen_height / 2 - (screen_height / 4))
else:
world.draw()
spike_group.draw(screen)
water_group.draw(screen)
chest_group.draw(screen)
coin_group.draw(screen)
game_over = player.update(game_over)
draw_text('X' + str(score), font_score, black, tile_size, tile_size / 4)
if game_over == 0:
if pygame.sprite.spritecollide(player, coin_group, True):
score += 1
coin_fx.play()
if game_over == -1:
pygame.mixer.music.stop()
if restart_button.draw():
game_data = next_level(level)
world = World(game_data[0])
game_over = 0
score = 0
pygame.mixer.music.play(-1, 0.0, 5000)
draw_text('GAME OVER!', font, black, screen_width / 2 - (screen_width / 3) , screen_height / 2 - (screen_height / 4))
if game_over == 1:
level += 1
if level <= utils.total():
game_data = next_level(level)
world = World(game_data[0])
game_over = 0
else:
level = 1
game_data = next_level(level)
world = World(game_data[0])
game_over = 0
main_menu = True
win = True
score = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()