import pygame #this helps create games, draw pics, play sounds, and detect keyboard presses
import random #acts like randomizer
import sys #helps control the program
import os #helps work with files and folders on the computer
# Initialize Pygame completely
[Link]() #this automatically initializes all imported Pygame modules at once
[Link]() #turns on sound and music
# Setting Screen size in pixels
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
screen = [Link].set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
[Link].set_caption("no name") # naming the game
# fps Control
clock = [Link]() # helps control the speed
FPS = 60 #frames per second
#Color Values
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (50, 205, 50) # Normal Platform
BLUE = (30, 144, 255) # Moving Platform
ORANGE = (255, 140, 0) # Breaking Platform
RED = (220, 20, 60) # Jetpack
CHARACTER_DARK = (30, 30, 35) #CHARACTER
# --- PHYSICS SPEED CONFIGURATION ---
GRAVITY_BASE = 0.4 # how strong the gravity pulls the character down
JUMP_BASE = -12 #conrols how high the character jumps
SPEED_X_BASE = 6 #controls how the character moves left and right
PLAT_SPEED_MIN = 1.5 #slowest speed a moving platform can have
PLAT_SPEED_MAX = 2.5 #fastest speed a moving platform can have
score = 0
high_score = 0 #creates the score counter
game_over = False
#Font Initialization
try:
font = [Link](None, 30)
except Exception:
[Link]()
font = [Link]("sans-serif", 24)
# for saving score
HIGH_SCORE_FILE = "[Link]"
if [Link](HIGH_SCORE_FILE):
try:
with open(HIGH_SCORE_FILE, "r") as f:
high_score = int([Link]().strip())
except Exception:
high_score = 0
# adding sounds
try:
jump_sound = [Link]("[Link]")
jetpack_sound = [Link]("[Link]")
gameover_sound = [Link]("game [Link]")
except Exception:
jump_sound = None
jetpack_sound = None
gameover_sound = None
if jump_sound:
jump_sound.set_volume(0.4)
# adding background music
music_loaded = False
try:
[Link]("[Link]")
[Link].set_volume(1) #music volume
[Link](-1) #loops the music infinitely
music_loaded = True
except Exception:
music_loaded = False #backup if music fails
class Player:
def __init__(self):
[Link] = 30
[Link] = 30
self.x = SCREEN_WIDTH // 2 - [Link] // 2
self.y = SCREEN_HEIGHT - 150
self.vel_y = 0
self.is_flying = False
self.flight_timer = 0
def move(self, dt_multiplier):
keys = [Link].get_pressed()
current_speed_x = SPEED_X_BASE * dt_multiplier
if keys[pygame.K_LEFT] or keys[pygame.K_a]:
self.x -= current_speed_x
if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
self.x += current_speed_x
if self.x < -[Link]:
self.x = SCREEN_WIDTH
elif self.x > SCREEN_WIDTH:
self.x = -[Link]
if self.is_flying:
self.vel_y = JUMP_BASE
self.flight_timer -= 1 * dt_multiplier
if self.flight_timer <= 0:
self.is_flying = False
else:
self.vel_y += GRAVITY_BASE * dt_multiplier
self.y += self.vel_y * dt_multiplier
def draw(self):
[Link](screen, CHARACTER_DARK, (int(self.x), int(self.y), [Link],
[Link]), border_radius=4)
[Link](screen, RED, (int(self.x), int(self.y + 4), [Link], 6))
class Platform:
def __init__(self, x, y, plat_type="normal"):
[Link] = 60
[Link] = 12
self.x = x
self.y = y
self.plat_type = plat_type
self.has_item = False
[Link] = [Link]([-1, 1])
[Link] = [Link](PLAT_SPEED_MIN, PLAT_SPEED_MAX)
self.is_stepped_on = False
self.break_timer = 15
self.is_broken = False
def update(self, dt_multiplier):
if self.plat_type == "moving":
self.x += ([Link] * [Link]) * dt_multiplier
if self.x <= 0 or self.x + [Link] >= SCREEN_WIDTH:
[Link] *= -1
elif self.plat_type == "breaking" and self.is_stepped_on:
self.break_timer -= 1 * dt_multiplier
if self.break_timer <= 0:
self.is_broken = True
def draw(self):
if self.is_broken:
return
color = GREEN
if self.plat_type == "moving":
color = BLUE
elif self.plat_type == "breaking":
color = ORANGE
[Link](screen, color, (int(self.x), int(self.y), [Link], [Link]),
border_radius=4)
if self.has_item:
[Link](screen, RED, (int(self.x + [Link] // 2 - 6), int(self.y - 16), 12,
16),
border_radius=2)
def reset_game():
global score, game_over
score = 0
game_over = False
player.x = SCREEN_WIDTH // 2 - [Link] // 2
player.y = SCREEN_HEIGHT - 150
player.vel_y = JUMP_BASE
player.is_flying = False
if music_loaded:
[Link](-1)
[Link]()
[Link](Platform(SCREEN_WIDTH // 2 - 30, SCREEN_HEIGHT - 50, "normal"))
for i in range(7):
px = [Link](0, SCREEN_WIDTH - 60)
py = (SCREEN_HEIGHT - 120) - (i * 80)
[Link](Platform(px, py, "normal"))
player = Player()
platforms = []
reset_game()
# --- Main Runtime Loop ---
while True:
[Link](WHITE)
dt = [Link](FPS) / 1000.0
dt_multiplier = dt * 60.0
for event in [Link]():
if [Link] == [Link]:
[Link]()
[Link]()
if [Link] == [Link]:
if game_over and [Link] == pygame.K_SPACE:
reset_game()
if not game_over:
[Link](dt_multiplier)
for plat in platforms:
[Link](dt_multiplier)
if player.y < SCREEN_HEIGHT // 2:
diff = SCREEN_HEIGHT // 2 - player.y
player.y = SCREEN_HEIGHT // 2
score += int(diff)
for plat in platforms:
plat.y += diff
for plat in platforms:
if plat.y > SCREEN_HEIGHT:
highest_y = min([p.y for p in platforms])
plat.y = highest_y - [Link](70, 100)
plat.x = [Link](0, SCREEN_WIDTH - [Link])
plat.is_stepped_on = False
plat.break_timer = 15
plat.is_broken = False
rand = [Link]()
if score > 10000 and rand < 0.25:
plat.plat_type = "breaking"
plat.has_item = False
elif rand < min(0.50, score / 50000):
plat.plat_type = "moving"
plat.has_item = False
else:
plat.plat_type = "normal"
plat.has_item = [Link]() < 0.12
p_rect = [Link](player.x, player.y, [Link], [Link])
for plat in platforms:
if plat.is_broken:
continue
if plat.has_item:
i_rect = [Link](plat.x + [Link] // 2 - 6, plat.y - 16, 12, 16)
if p_rect.colliderect(i_rect):
plat.has_item = False
player.is_flying = True
player.flight_timer = 120
if jetpack_sound:
jetpack_sound.play()
if player.vel_y > 0:
if (player.x + [Link] > plat.x and
player.x < plat.x + [Link] and
player.y + [Link] >= plat.y and
player.y + [Link] <= plat.y + 16):
player.vel_y = JUMP_BASE
if jump_sound and not player.is_flying:
jump_sound.play()
if plat.plat_type == "breaking":
plat.is_stepped_on = True
if player.y > SCREEN_HEIGHT:
game_over = True
if music_loaded:
[Link]()
if gameover_sound:
gameover_sound.play()
if score > high_score:
high_score = score
try:
with open(HIGH_SCORE_FILE, "w") as f:
[Link](str(high_score))
except Exception:
pass
for plat in platforms:
[Link]()
[Link]()
#Score Text
score_surf = [Link](f"Score: {score}", True, BLACK)
[Link](score_surf, (10, 10))
if game_over:
[Link](screen, CHARACTER_DARK, (40, 200, SCREEN_WIDTH - 80, 200),
border_radius=6)
t1 = [Link]("GAME OVER", True, WHITE)
t2 = [Link](f"High Score: {high_score}", True, WHITE)
t3 = [Link]("SPACE to Restart", True, WHITE)
[Link](t1, (SCREEN_WIDTH // 2 - t1.get_width() // 2, 230))
[Link](t2, (SCREEN_WIDTH // 2 - t2.get_width() // 2, 280))
[Link](t3, (SCREEN_WIDTH // 2 - t3.get_width() // 2, 330))
[Link]()