-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
329 lines (277 loc) · 14.7 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
""" SegwayJump Copyright © 2018 Tom Schimansky """
from settings import *
from segway_jump_sprites.platforms import Platform, SideMovingPlatform, UpMovingPlatform
from segway_jump_sprites.player import Player
from segway_jump_sprites.coin import Coin
from segway_jump_sprites.menubutton import MenuButton
from segway_jump_sprites.items import EndItem, JumpBoost, SpeedDrop
from segway_jump_sprites.background import Background
import os
import json
import random
import pygame
class Game:
def __init__(self):
pygame.init()
pygame.font.init()
pygame.mixer.init()
self.running = True
self.playing = False
self.pause = False
self.current_level = None
self.time = 0
self.command_pressed = False
self.current_path = os.path.dirname(__file__)
self.icon_image = pygame.image.load(self.current_path + "/assets/icons/icon.png")
pygame.display.set_icon(self.icon_image)
pygame.display.set_caption(TITLE)
self.screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.SHOWN)
self.clock = pygame.time.Clock()
# load levels and highscores
self.levels = []
self.load_levels()
self.highscores = []
self.load_highscores()
if len(self.highscores) != len(self.levels):
raise ValueError("highscores and level files do not match")
# load sounds
self.background_music = pygame.mixer.Sound(self.current_path + "/assets/sounds/ambient_background_music_cicada_3301.wav")
self.coin_sound = pygame.mixer.Sound(self.current_path + "/assets/sounds/blup.wav")
self.die_sound = pygame.mixer.Sound(self.current_path + "/assets/sounds/sfx_die.wav")
self.button_sound = pygame.mixer.Sound(self.current_path + "/assets/sounds/button_27.wav")
self.jump_sound = pygame.mixer.Sound(self.current_path + "/assets/sounds/jump_sound.wav")
self.powerup_sound = pygame.mixer.Sound(self.current_path + "/assets/sounds/powerup.wav")
self.win_sound = pygame.mixer.Sound(self.current_path + "/assets/sounds/ding_sound.wav")
# load menu images
self.coins_image = pygame.image.load(self.current_path + "/assets/images/coins.png")
self.endbutton_image = pygame.image.load(self.current_path + "/assets/images/endbutton.png")
self.banner_image = pygame.image.load(self.current_path + "/assets/images/banner.png")
self.level_list_image = pygame.image.load(self.current_path + "/assets/images/level_list.png")
self.menubutton_image = pygame.image.load(self.current_path + "/assets/images/menubutton.png")
self.startbutton_image = pygame.image.load(self.current_path + "/assets/images/startbutton.png")
# load character images
self.character_left_walking_1_image = pygame.image.load(self.current_path + "/assets/images/player_left_1.png")
self.character_left_walking_2_image = pygame.image.load(self.current_path + "/assets/images/player_left_2.png")
self.character_left_walking_3_image = pygame.image.load(self.current_path + "/assets/images/player_left_3.png")
self.character_jump_1_image = pygame.image.load(self.current_path + "/assets/images/player_jump_1.png")
self.character_jump_2_image = pygame.image.load(self.current_path + "/assets/images/player_jump_2.png")
# load game images
self.platform_image = pygame.image.load(self.current_path + "/assets/images/platform.png").convert()
self.floatingplatorm_image = pygame.image.load(self.current_path + "/assets/images/floatingplatform.png")
self.upmovingplatformsocket_image = pygame.image.load(self.current_path + "/assets/images/movingsocketplatform.png")
self.emptyplatform_image = pygame.image.load(self.current_path + "/assets/images/middleplatform.png").convert()
self.sidemovingplatform_image = pygame.image.load(self.current_path + "/assets/images/movingfloatingplatform.png")
self.upmovingplatform_image = pygame.image.load(self.current_path + "/assets/images/movingplatform.png")
self.coin_1_image = pygame.image.load(self.current_path + "/assets/images/coin_1.png")
self.finish_image = pygame.image.load(self.current_path + "/assets/images/finish.png")
self.jumpboost_image = pygame.image.load(self.current_path + "/assets/images/jumpboost.png")
self.speeddrop_image = pygame.image.load(self.current_path + "/assets/images/speeddrop.png")
self.steelpate_1_image = pygame.image.load(self.current_path + "/assets/images/steelplate_1.png")
self.steelpate_2_image = pygame.image.load(self.current_path + "/assets/images/steelplate_2.png")
self.steelpate_3_image = pygame.image.load(self.current_path + "/assets/images/steelplate_3.png")
self.steelpate_4_image = pygame.image.load(self.current_path + "/assets/images/steelplate_4.png")
def load_levels(self):
with open(self.current_path + "/assets/levels/levels.json", "r") as file:
self.levels = json.load(file)
def load_highscores(self):
if os.path.exists(self.current_path + "/assets/highscores/highscores.json"):
with open(self.current_path + "/assets/highscores/highscores.json", "r") as file:
self.highscores = json.load(file)
else:
self.highscores = [0] * len(self.levels)
def save_highscore(self):
with open(self.current_path + "/assets/highscores/highscores.json", "w") as file:
json.dump(self.highscores, file)
def create_text(self, text, size, color, x, y):
font = pygame.font.Font("assets/fonts/Chalkboard_Bold.ttf", size)
surface = font.render(text, True, color)
rect = surface.get_rect()
rect.midtop = (x, y)
self.screen.blit(surface, rect)
def start_screen(self):
self.background_music.stop()
self.background_music.play()
self.screen.fill(BLACK)
self.screen.blit(self.banner_image, (WIDTH / 2 - (self.banner_image.get_width() / 2), 100))
button = MenuButton(self, WIDTH / 2, HEIGHT / 2 + 160, self.menubutton_image, enter=True)
pygame.display.flip()
self.wait([button.check_click])
self.level_screen()
def level_screen(self):
if self.running is False:
return
self.screen.fill(BLACK)
self.create_text("Levels:", 40, COIN_COLOR, WIDTH / 2, 30)
start_buttons = []
for i, level in enumerate(self.levels):
self.screen.blit(self.level_list_image, (WIDTH / 2 - (self.level_list_image.get_width() / 2), 100 + i * (self.level_list_image.get_height() + 30)))
self.create_text(level["name"], 30, COIN_COLOR, WIDTH / 2 - 200, 100 + i * (self.level_list_image.get_height() + 30) + 30)
self.create_text(f"Highscore: {self.highscores[i]}/{level['maxcoins']}", 20, COIN_COLOR, WIDTH / 2 - 50, 100 + i * (self.level_list_image.get_height() + 30) + 30)
start_buttons.append(MenuButton(self, WIDTH / 2 + 180, 100 + i * (self.level_list_image.get_height() + 30) + 30, self.startbutton_image))
pygame.display.flip()
self.current_level = self.wait([button.check_click for button in start_buttons])
def new(self):
self.background_music.stop()
self.background_music.play(-1)
self.current_column_number = 0
self.current_shift = 0
self.score = 0
self.last_background_x_pos = 100
self.player = Player(self)
self.platforms = pygame.sprite.Group()
self.backgrounds = pygame.sprite.Group()
self.coins = pygame.sprite.Group()
self.all_sprites = pygame.sprite.Group()
self.all_sprites.add(self.player)
def run(self):
self.playing = True
while self.playing:
self.events()
self.update()
self.draw()
self.time = self.clock.tick(FPS)
def update(self):
self.all_sprites.update()
level_array = self.levels[self.current_level]["array"]
if len(self.all_sprites) < 50 and self.current_column_number < len(level_array):
level_array_column = level_array[self.current_column_number]
for i in range(len(level_array_column)):
x_position = round(self.current_column_number * 120) - self.current_shift
y_position = HEIGHT - level_array_column[i][0]
if level_array_column[i][1] in (1, 2, 6, 7):
platform = Platform(self, x_position, y_position, level_array_column[i][1])
self.platforms.add(platform)
self.all_sprites.add(platform)
elif level_array_column[i][1] == 3:
platform = SideMovingPlatform(self, x_position, y_position)
self.platforms.add(platform)
self.all_sprites.add(platform)
elif level_array_column[i][1] == 4:
platform = UpMovingPlatform(self, x_position, y_position)
self.platforms.add(platform)
self.all_sprites.add(platform)
elif level_array_column[i][1] == 5:
coin = Coin(self, x_position, y_position)
self.coins.add(coin)
self.all_sprites.add(coin)
elif level_array_column[i][1] == 8:
item = EndItem(self, x_position, y_position)
self.coins.add(item)
self.all_sprites.add(item)
elif level_array_column[i][1] == 9:
item = JumpBoost(self, x_position, y_position)
self.coins.add(item)
self.all_sprites.add(item)
elif level_array_column[i][1] == 10:
item = SpeedDrop(self, x_position, y_position)
self.coins.add(item)
self.all_sprites.add(item)
self.current_column_number += 1
if len(self.backgrounds) < 6:
new_x_pos = self.last_background_x_pos + random.randint(100, 300)
new_background = Background(self, self.last_background_x_pos + random.randint(100, 300), random.randint(100, 600))
self.last_background_x_pos = new_x_pos + new_background.rect.width
self.backgrounds.add(new_background)
if self.player.rect.x >= WIDTH / 2:
self.current_shift += abs(self.player.speed.x)
self.player.rect.x -= abs(self.player.speed.x)
for platform in self.platforms:
platform.rect.right -= abs(self.player.speed.x)
if platform.rect.right < 0:
platform.kill()
for coin in self.coins:
coin.rect.right -= abs(self.player.speed.x)
if coin.rect.right < 0:
coin.kill()
self.last_background_x_pos -= abs(self.player.speed.x) / 2
for background in self.backgrounds:
background.rect.right -= abs(self.player.speed.x) / 2
if background.rect.right < 0:
background.kill()
if self.player.rect.y > HEIGHT:
if NO_DIE is True:
self.player.rect.y = -200
else:
self.playing = False
self.die_sound.play()
if self.player.rect.x < 0:
self.player.rect.x = 0
self.player.speed.x = 0
pos = pygame.mouse.get_pos()
if pygame.mouse.get_pressed()[0] == 1:
if WIDTH - 200 < pos[0] < WIDTH - 200 + 180 and 20 < pos[1] < 20 + 60:
self.playing = False
self.button_sound.play()
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
self.playing = False
if event.type == pygame.KEYDOWN:
if event.key == 310:
self.command_pressed = True
if event.key == 113 and self.command_pressed is True:
self.running = False
self.playing = False
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
self.player.jump()
if event.type == pygame.KEYUP:
if event.key == 310:
self.command_pressed = False
def draw(self):
self.screen.fill(BLACK)
self.backgrounds.draw(self.screen)
self.all_sprites.draw(self.screen)
if SHOW_FPS is True:
self.create_text(f"FPS: {round(self.clock.get_fps())}", 17, WHITE, 245, 20)
self.screen.blit(self.coins_image, (20, 20))
self.screen.blit(self.endbutton_image, (WIDTH - 200, 20))
self.create_text(str(self.score), 40, COIN_COLOR, 90, 25)
if self.player.jumpboost > 0:
self.screen.blit(self.jumpboost_image, (220, 20))
if self.player.speeddrop > 0:
self.screen.blit(self.speeddrop_image, (360, 20))
pygame.display.flip()
def wait(self, wait_functions):
while self.running:
self.clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN:
if event.key == 310:
self.command_pressed = True
if event.type == pygame.KEYUP:
if event.key == 310:
self.command_pressed = False
if event.type == pygame.KEYDOWN:
if event.key == 113 and self.command_pressed is True:
self.running = False
if event.key == 27:
self.running = False
for i, function in enumerate(wait_functions):
if function() is True:
return i
def end_screen(self):
if self.running is False:
return
self.screen.fill(BLACK)
if self.score > self.highscores[self.current_level]:
self.highscores[self.current_level] = self.score
self.create_text("NEW HIGHSCORE", 40, COIN_COLOR, WIDTH / 2 + 150, 300)
self.screen.blit(self.icon_image, (WIDTH / 2 - (self.icon_image.get_width() / 2) - 200, 100))
self.create_text("SCORE: " + str(self.score), 40, COIN_COLOR, WIDTH / 2 + 150, 150)
self.create_text("HIGHSCORE: " + str(self.highscores[self.current_level]), 40, COIN_COLOR, WIDTH / 2 + 150, 220)
button = MenuButton(self, WIDTH / 2, HEIGHT / 2 + 160, self.menubutton_image, enter=True)
pygame.display.flip()
self.wait([button.check_click])
self.level_screen()
if __name__ == "__main__":
game = Game()
game.start_screen()
while game.running:
game.new()
game.run()
game.end_screen()
game.save_highscore()
pygame.quit()