player = Player()
floors = [Floor(HEIGHT - (i + 1) * floor_height) for i in range(10)]
powerups = []
kill_blocks = []
for i in range(3):
kb_x = random.randint(0, WIDTH - 60)
kb_y = random.randint(-600, -60)
kill_blocks.append(KillBlock(kb_x, kb_y))
score = 0
score_timer = 0
move_speed_current = base_move_speed
powerup_timers = {
'double_jump': 0,
'speed_boost': 0,
'shield': 0,
'supershield': 0,
'opshield': 0,
'double_points': 0, # Added new timer entry
}
# Track consecutive shields collected in a row
consecutive_shields_collected = 0
button_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 - 25, 200, 50)
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if state == STATE_MENU:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse_pos = pygame.mouse.get_pos()
if button_rect.collidepoint(mouse_pos):
# Reset game state
player = Player()
floors = [Floor(HEIGHT - (i + 1) * floor_height) for i in range(10)]
powerups = []
kill_blocks = []
for i in range(3):
kb_x = random.randint(0, WIDTH - 60)
kb_y = random.randint(-600, -60)
kill_blocks.append(KillBlock(kb_x, kb_y))
score = 0
score_timer = 0
move_speed_current = base_move_speed
powerup_timers = {k: 0 for k in powerup_timers}
consecutive_shields_collected = 0
state = STATE_PLAYING
paused = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player = Player()
floors = [Floor(HEIGHT - (i + 1) * floor_height) for i in range(10)]
powerups = []
kill_blocks = []
for i in range(3):
kb_x = random.randint(0, WIDTH - 60)
kb_y = random.randint(-600, -60)
kill_blocks.append(KillBlock(kb_x, kb_y))
score = 0
score_timer = 0
move_speed_current = base_move_speed
powerup_timers = {k: 0 for k in powerup_timers}
consecutive_shields_collected = 0
state = STATE_PLAYING
paused = False
elif event.key == pygame.K_s:
state = STATE_SETTINGS
elif state == STATE_SETTINGS:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
state = STATE_MENU
elif event.key == pygame.K_f:
show_fps = not show_fps
elif state == STATE_PLAYING:
if event.type == pygame.KEYDOWN and not paused:
if event.key in (pygame.K_SPACE, pygame.K_w, pygame.K_UP):
player.jump()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
paused = not paused
elif state == STATE_GAMEOVER:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
state = STATE_MENU
keys = pygame.key.get_pressed()
if state == STATE_PLAYING and not paused:
dx = 0
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
dx -= move_speed_current
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
dx += move_speed_current
if powerup_timers['speed_boost'] > 0:
dx *= 2
player.move(dx)
player.update(floors)
# Chance to spawn powerup: Added 10% for double_points, others changed slightly for balance
if random.randint(1, 100) == 1:
pu_type = random.choices(
["double_jump", "speed_boost", "shield", "supershield", "opshield", "double_points"],
weights=[25, 25, 25, 1, 0.1, 10], # weights balanced, double_points 10% rare but much more common than supershield/opshield
k=1
)[0]
powerups.append(PowerUp(random.randint(0, WIDTH - player_size), -player_size, pu_type))
for pu in powerups[:]:
pu.update()
if pu.rect.top > HEIGHT:
pu.rect.x = random.randint(0, WIDTH - player_size)
pu.rect.y = -player_size
pu.fall_speed = random.randint(3, 6)
elif player.rect.colliderect(pu.rect):
pu.apply(player, powerup_timers)
# Score gain doubled if double_points active
points_earned = 2 if player.double_points_active else 1
score += points_earned
# Track consecutive shields
if pu.power_type == "shield":
consecutive_shields_collected += 1
if consecutive_shields_collected == 6:
# Grant opshield immediately
player.opshield_active = True
player.opshield_hits_left = OPSHIELD_HITS
powerup_timers['opshield'] = OPSHIELD_DURATION_FRAMES
consecutive_shields_collected = 0
elif pu.power_type == "opshield":
consecutive_shields_collected = 0
else:
consecutive_shields_collected = 0
powerups.remove(pu)
for kb in kill_blocks:
kb.update()
if player.rect.colliderect(kb.rect):
if player.opshield_active:
player.opshield_hits_left -= 1
if player.opshield_hits_left <= 0:
player.opshield_active = False
powerup_timers['opshield'] = 0
kb.rect.x = random.randint(0, WIDTH - kb.size)
kb.rect.y = -kb.size
kb.fall_speed = random.randint(4, 7)
elif player.supershield_active:
player.supershield_hits_left -= 1
if player.supershield_hits_left <= 0:
player.supershield_active = False
powerup_timers['supershield'] = 0
kb.rect.x = random.randint(0, WIDTH - kb.size)
kb.rect.y = -kb.size
kb.fall_speed = random.randint(4, 7)
elif player.shield_active:
player.shield_active = False
powerup_timers['shield'] = 0
kb.rect.x = random.randint(0, WIDTH - kb.size)
kb.rect.y = -kb.size
kb.fall_speed = random.randint(4, 7)
else:
state = STATE_GAMEOVER
# Update timers for power-ups and deactivate when due
for key in powerup_timers:
if powerup_timers[key] > 0:
powerup_timers[key] -= 1
if powerup_timers[key] == 0:
if key == 'double_jump':
player.can_double_jump = False
player.double_jump_available = False
elif key == 'speed_boost':
player.speed_boost = False
elif key == 'shield':
player.shield_active = False
elif key == 'supershield':
player.supershield_active = False
player.supershield_hits_left = 0
elif key == 'opshield':
player.opshield_active = False
player.opshield_hits_left = 0
elif key == 'double_points':
player.double_points_active = False
score_timer += 1
if score_timer >= 10:
# Increment score by 1 every 10 frames, double if double_points active
points_earned = 2 if player.double_points_active else 1
score += points_earned
score_timer = 0
screen.fill(WHITE)
if state == STATE_MENU:
draw_text_with_outline(screen, "Cube Runnin'", font_large, BLACK, WHITE, WIDTH // 2, HEIGHT // 4)
pygame.draw.rect(screen, GRAY, button_rect)
draw_text_with_outline(screen, "PLAY GAME", font_med, BLACK, WHITE, WIDTH // 2, HEIGHT // 2 - 12)
draw_text_with_outline(screen, "Move: A/D or Arrows, Jump: W/Up/Space", font_small, BLACK, WHITE, WIDTH // 2, HEIGHT // 2 + 50)
draw_text_with_outline(screen, "Avoid falling red blocks! Collect powerups!", font_small, BLACK, WHITE, WIDTH // 2, HEIGHT // 2 + 80)
draw_text_with_outline(screen, "Press S for Settings", font_small, BLACK, WHITE, WIDTH // 2, HEIGHT // 2 + 110)
elif state == STATE_SETTINGS:
draw_settings_menu(screen, show_fps)
elif state == STATE_PLAYING:
for floor in floors:
floor.draw(screen)
for kb in kill_blocks:
kb.draw(screen)
for pu in powerups:
pu.draw(screen)
player.draw(screen)
draw_text_with_outline(screen, f"Score: {score}", font_med, BLACK, WHITE, 60, 20, center=False)
active_timers = {k: v for k, v in powerup_timers.items() if v > 0}
if active_timers:
draw_powerup_timers(screen, active_timers)
y_ui = 50
if player.can_double_jump:
draw_text_with_outline(screen, "Double Jump Active", font_small, BLUE, WHITE, WIDTH - 150, y_ui)
y_ui += 24
if player.speed_boost:
draw_text_with_outline(screen, "Speed Boost Active", font_small, GREEN, WHITE, WIDTH - 150, y_ui)
y_ui += 24
if player.shield_active:
draw_text_with_outline(screen, "Shield Active", font_small, YELLOW, WHITE, WIDTH - 150, y_ui)
y_ui += 24
if player.supershield_active:
draw_text_with_outline(screen, f"Super Shield Active ({player.supershield_hits_left} hits left)", font_small, SUPER_SHINY_PINK, WHITE, WIDTH - 200, y_ui)
y_ui += 24
if player.opshield_active:
draw_text_with_outline(screen, f"OPShield Active ({player.opshield_hits_left} hits left)", font_small, OPSHIELD_COLOR, WHITE, WIDTH - 220, y_ui)
y_ui += 24
if player.double_points_active:
draw_text_with_outline(screen, "Double Points Active", font_small, DARK_BLUE, WHITE, WIDTH - 150, y_ui)
y_ui += 24
if paused:
draw_paused_overlay(screen)
if show_fps:
fps = int(clock.get_fps())
draw_text_with_outline(screen, f"FPS: {fps}", font_small, BLACK, WHITE, 10, HEIGHT - 25, center=False)
elif state == STATE_GAMEOVER:
draw_text_with_outline(screen, "GAME OVER", font_xlarge, RED, BLACK, WIDTH // 2, HEIGHT // 3)
draw_text_with_outline(screen, f"Final Score: {score}", font_med, BLACK, WHITE, WIDTH // 2, HEIGHT // 2)
draw_text_with_outline(screen, "Press R to Restart", font_med, BLACK, WHITE, WIDTH // 2, HEIGHT // 2 + 50)
pygame.display.flip()
if name == "main":
main()
Your code provides a complete Pygame-based platformer game called "Cube Runnin'" with various features including multiple power-ups, shields, kill blocks, scoring, pause functionality, and a settings menu.
Here's a quick summary and some notes about your implementation:
Summary of Features:
Player Movement & Jumping: Player can move left/right (A/D or arrow keys) and jump (W/Up/Space). Supports double jump when the power-up is active.
Floors: Multiple destructible floors with health bars.
Kill Blocks: Falling dangerous blocks that reset position when missed, damage player on collision.
Power-ups:
Double Jump (blue)
Speed Boost (green)
Shield (yellow)
Supershield (pink glow, 3 hits)
OPShield (dark pink, 5 hits)
Double Points (dark blue)
Powerup Timers: Display timers for active power-ups.
Scoring: Scores increase gradually and faster with double points active.
Menu & Game Over Screens: Start and restart game UI.
Visual Effects: Add glow and ellipse effects around the player when shields or speed boost are active.
Code Review Notes:
Your power-up duration and timers are well managed.
The consecutive shield collection mechanic to grant OPShield is a nice touch that adds depth.
The shield and supershield logic with hit counts is well implemented.
The speed boost doubling the player speed and score doubling are handled appropriately.
Some possible improvements / suggestions:
Code repetition on game reset:
The reset code in menu click and space press is repeated. You might want to extract that into a function to avoid duplication.
def reset_game():
nonlocal player, floors, powerups, kill_blocks, score, score_timer, move_speed_current, powerup_timers, consecutive_shields_collected
player = Player()
floors = [Floor(HEIGHT - (i + 1) * floor_height) for i in range(10)]
powerups = []
kill_blocks = []
for i in range(3):
kb_x = random.randint(0, WIDTH - 60)
kb_y = random.randint(-600, -60)
kill_blocks.append(KillBlock(kb_x, kb_y))
score = 0
score_timer = 0
move_speed_current = base_move_speed
powerup_timers = {k: 0 for k in powerup_timers}
consecutive_shields_collected = 0
(Then call this function from event handlers.)
Player speed doubling in two places:
In your main loop, dx *= 2 if 'speed_boost' timer is active.
This works but could result in speed doubling stacking unexpectedly if player.speed_boost also modifies elsewhere. Since you keep both player attribute speed_boost and timer, it's consistent, so no issue here.
Floor damage:
When the player lands on a floor, the floor damages itself by 1 health. If health reaches 0, it is removed. This is nice but fairly basic. You could add some floor respawn or variation for longer gameplay.
Power-up spawn rates:
Your weights for power-up spawn are carefully balanced. You could make this data-driven (a dictionary) for easier tweaking.
Pause menu enhancements:
Currently, pressing ESC pauses/resumes. You might want to allow for a pause menu that has options, perhaps by transitioning to a PAUSED state.
Comments and formatting:
There are some comments from the code snippet like:
# def is a keyword used to define a function...
which are explanatory but probably not needed in actual source code unless for educational purposes. You can remove those comment lines in production.
Final notes:
Overall, the game is well structured, functional, and visually clear with good user feedback and progressive difficulty implementation.