-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
560 lines (354 loc) · 12.9 KB
/
Copy pathmain.py
File metadata and controls
560 lines (354 loc) · 12.9 KB
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
548
549
550
551
552
553
#import libraries
import pygame.freetype
import pygame.mixer
import random
import pygame
#initialise pygame
pygame.init()
#game window dimensions
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
#create game winodw
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Jump Man v0.4')
#set frame rate
clock = pygame.time.Clock()
FPS = 90
#game vars
JUMP_VELOCITY = -24
GRAVITY = 2
MAX_PLATFORMS = 10
SCROLL_THRESH = 200
scroll = 0
bg_scroll = 0
game_over = False
score = 0
score1 = 0
hscore = 0
hsx = 300
fade_counter = 0
paused = False
sx = 40
hsx1 = 40
music_paused = False
check_click = False
crossed_high_score = False
last_player_position = None
on_settings_page = False
#colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PANEL = (0, 0, 0)
PURPLE = (35, 11, 77)
#define font
title_font_path = 'assets/font/SedgwickAveDisplay-Regular.ttf'
font_small_path = 'assets/font/Quicksand-Medium.ttf'
font_big_path = 'assets/font/Quicksand-Medium.ttf'
font_small = pygame.font.Font(font_small_path, 14)
font_big = pygame.font.Font(font_big_path, 24)
font_huge = pygame.font.Font(font_big_path, 34)
font_gyatt = pygame.font.Font(font_big_path, 50)
title_font_huge = pygame.font.Font(title_font_path, 85)
# Initialize mixer
pygame.mixer.init()
# Load song
music_file = "assets/music/bedtime-after-coffee.mp3"
pygame.mixer.music.load(music_file)
# Set volume (increase if necessary)
pygame.mixer.music.set_volume(0.7) # 1.0 means 100% volume
# Set the music end event
MUSIC_END = pygame.USEREVENT + 1
pygame.mixer.music.set_endevent(MUSIC_END)
# Play music
pygame.mixer.music.play()
#load images
player_image = pygame.image.load('assets/player.png').convert_alpha()
bg_image = pygame.image.load('assets/bg.png').convert_alpha()
platform_image = pygame.image.load('assets/platform.png').convert_alpha()
pause_button_image = pygame.image.load('assets/pause.png').convert_alpha()
play_button_image = pygame.image.load('assets/play.png').convert_alpha()
restart_button_image = pygame.image.load('assets/restart.png').convert_alpha()
city_image = pygame.image.load('assets/city.png').convert_alpha()
start_button_image = pygame.image.load('assets/startbutton.png').convert_alpha()
target_image = pygame.image.load('assets/fade.png').convert_alpha()
#screen text function
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
screen.blit(img, (x, y))
#info panel function
def draw_panel():
pygame.draw.rect(screen, PANEL, (0, 0, SCREEN_WIDTH, 20))
pygame.draw.line(screen, WHITE, (0,20), (SCREEN_WIDTH, 20), 2)
draw_text('SCORE: ' + str(score), font_small, WHITE, 0, 3)
draw_text('HIGHSCORE: ' + str(hscore), font_small, WHITE, hsx, 3)
def draw_city():
screen.blit(city_image, (0, 0))
#function backgroung
def draw_bg(bg_scroll):
#draw background
screen.blit(bg_image, (0, 0 + bg_scroll))
screen.blit(bg_image, (0, -600 + bg_scroll))
#player class
class Player():
def __init__(self, x, y):
self.image = pygame.transform.scale(player_image, (40, 68))
self.width = 30
self.height = 70
self.rect = pygame.Rect(0, 0, self.width, self.height)
self.rect.center = (x, y)
self.vel_y = 0
self.flip = True
def move(self):
# Reset variables
scroll = 0
dx = 0
dy = 0
# Process key presses
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
dx = -13
self.flip = False
if keys[pygame.K_RIGHT]:
dx = 13
self.flip = True
if keys[pygame.K_UP] and not self.is_jumping:
self.vel_y = JUMP_VELOCITY
self.is_jumping = True
# Gravity
self.vel_y += GRAVITY
dy += self.vel_y
# Don't go off the screen
if self.rect.left + dx < 0:
dx = -self.rect.left
if self.rect.right + dx > SCREEN_WIDTH:
dx = SCREEN_WIDTH - self.rect.right
# Collision with platforms
for platform in platform_group:
if platform.rect.colliderect(self.rect.x, self.rect.y + dy, self.width, self.height):
if self.rect.bottom < platform.rect.centery:
if self.vel_y > 0:
self.rect.bottom = platform.rect.top
self.vel_y = 0
dy = 0
self.is_jumping = False
# Check if player is near the top of the screen
if self.rect.top <= SCROLL_THRESH:
if self.vel_y < 0:
scroll = -dy
# Update rect position
self.rect.x += dx
self.rect.y += dy + scroll
return scroll
def draw(self):
screen.blit(pygame.transform.flip(self.image, self.flip, False), (self.rect.x - 5, self.rect.y + 25))
#platform class
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width) :
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(platform_image, (width, 60))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self, scroll):
self.rect.y += scroll
#check if platform has gone off screen
if self.rect.top > SCREEN_HEIGHT:
self.kill()
#player instance
player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150)
#create sprite groups
platform_group = pygame.sprite.Group()
#create starting plat
platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100)
platform_group.add(platform)
def draw_restart_button():
screen.blit(pygame.transform.scale(restart_button_image, (75, 75)), (165, 450))
def draw_settings_page():
screen.fill(PURPLE) # Fill the screen with the background color
# Display settings title
draw_text("Settings", font_huge, WHITE, 20, 20)
# Display settings options
draw_text("Music:", font_big, WHITE, 50, 100)
draw_text("Sound Effects:", font_big, WHITE, 50, 150)
# Display toggle buttons for music and sound effects
# You can use rectangles or images to represent buttons
# For example:
# pygame.draw.rect(screen, WHITE, (200, 100, 50, 30)) # Music toggle button
# pygame.draw.rect(screen, WHITE, (200, 150, 50, 30)) # Sound effects toggle button
# Display a back button to return to the game
draw_text("Back", font_big, WHITE, 20, 500)
#start screen background
def draw_start_bg():
pygame.draw.rect(screen, PURPLE, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT))
#def clickable area
image_x = 165
image_y = 450
image_width = 75
image_height = 75
image_width2 = 90
image_height2 = 90
clickable_rect = pygame.Rect(image_x, image_y, image_width, image_height)
clickable_rect2 = pygame.Rect(image_x, image_y, image_width2, image_height2)
start_fade_alpha = 255
def draw_start_button():
screen.blit(pygame.transform.scale(start_button_image, (90, 90)), (165, 450))
def draw_music_button():
if music_paused:
# If music is paused, display the play button
screen.blit(pygame.transform.scale(play_button_image, (30, 30)), (185, -5))
else:
# If music is playing, display the pause button
screen.blit(pygame.transform.scale(pause_button_image, (30, 30)), (185, -5))
def start_fade_out(start_color):
start_fade_alpha = 0
fading_to_black = False
while start_fade_alpha <= 255:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if fading_to_black:
screen.fill(BLACK) # Fill the screen with black
else:
screen.fill(start_color) # Fill the screen with the starting color
start_fade_alpha += 2 # Increase alpha by 2 (adjust the value as needed)
# Create a surface with the current alpha value
fade_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
fade_surface.set_alpha(start_fade_alpha)
screen.blit(fade_surface, (0, 0))
pygame.display.flip()
clock.tick(FPS)
# Check if we should transition to fading into black
if start_fade_alpha >= 255:
fading_to_black = True
# Usage example
#start screen loop
start = True
while start:
draw_start_bg()
draw_start_button()
draw_text("A13c's:", font_big, WHITE, 30, 90)
draw_text("Jump Man", title_font_huge, WHITE, 35, 130)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# Check if the left mouse button was clicked
mouse_x, mouse_y = pygame.mouse.get_pos()
print(f"Mouse Click Position: ({mouse_x}, {mouse_y})")
print(f"Clickable Rect: {clickable_rect}")
if clickable_rect.collidepoint(mouse_x, mouse_y):
start = False
start_fade_out(PURPLE)
# Now, you can enter the game loop
if event.type == pygame.QUIT:
pygame.quit()
key = pygame.key.get_pressed()
if key[pygame.K_ESCAPE]:
pygame.quit()
#update display window
pygame.display.update()
#game loop
run = True
while run:
clock.tick(FPS)
if not paused:
clock.tick(FPS)
if game_over == False :
clock.tick(FPS)
#move player
scroll = player.move()
#draw bg
bg_scroll += scroll
if bg_scroll >= 600:
bg_scroll = 0
draw_bg(bg_scroll)
#draw city
draw_city()
#platform gen
if len(platform_group) < MAX_PLATFORMS:
p_w = random.randint(40, 60)
p_x = random.randint(0, SCREEN_WIDTH - p_w)
p_y = platform.rect.y - random.randint(80, 120)
platform = Platform(p_x, p_y, p_w)
platform_group.add(platform)
#update plats
platform_group.update(scroll)
#update score
if scroll > 0:
score += scroll
#highscore
if score > hscore:
hscore = score
#hide cursor
pygame.mouse.set_visible(False)
#draw sprites
platform_group.draw(screen)
player.draw()
#draw panel
draw_panel()
draw_music_button()
#check game over
if player.rect.top > SCREEN_HEIGHT:
game_over = True
last_player_position = (player.rect.x, player.rect.y)
#highscore x check
if hscore >= 100:
hsx = 290
if hscore >= 1000:
hsx = 280
if hscore >= 10000:
hsx = 270
if event.type == MUSIC_END:
pygame.mixer.music.play() # Restart the music when it ends
else:
pygame.mouse.set_visible(True)
score1 = score
check_click = True
if fade_counter < SCREEN_WIDTH:
fade_counter += 7
for y in range(0, 6, 2):
pygame.draw.rect(screen, PURPLE, (0, y * 100, fade_counter, 100))
pygame.draw.rect(screen, PURPLE, (SCREEN_WIDTH - fade_counter, (y + 1) * 100, SCREEN_WIDTH, 100))
draw_text('GAME OVER!', font_gyatt, WHITE, 50, 60)
draw_text('SCORE: ' + str(score), font_huge, WHITE, sx, 350)
draw_text('HIGHSCORE: ' + str(hscore), font_huge, WHITE, hsx1, 200)
draw_restart_button()
#escape quit
key = pygame.key.get_pressed()
if key[pygame.K_ESCAPE]:
run = False
#event handler
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
if event.key == pygame.K_p:
# Toggle music play/pause
if music_paused:
pygame.mixer.music.unpause()
music_paused = False
else:
pygame.mixer.music.pause()
music_paused = True
# Check for mouse events
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# Check if the left mouse button was clicked
mouse_x, mouse_y = pygame.mouse.get_pos()
if clickable_rect.collidepoint(mouse_x, mouse_y) and check_click == True:
# Execute your code when the image is clicked
#reset var
fade_counter = 0
game_over = False
score = 0
scroll = 0
#repostion player
player.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT - 150)
#reset plats
platform_group.empty()
#create starting plat
platform = Platform(SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT - 50, 100)
platform_group.add(platform)
check_click = False
if event.type == pygame.QUIT:
run = False
#update display window
pygame.display.update()
pygame.quit()