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,
'opsheild': 0,
}
# 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):
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)
if random.randint(1, 100) == 1:
pu_type = random.choices(
["double_jump", "speed_boost", "shield", "supershield", "opsheild"],
weights=[30, 30, 30, 1, 0.1], # opsheild very rare ~0.1%
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 += 1
# Track consecutive shields
if pu.power_type == "shield":
consecutive_shields_collected += 1
if consecutive_shields_collected == 6:
# Grant opsheild immediately
player.opsheild_active = True
player.opsheild_time_left = OPSHEILD_DURATION_FRAMES
player.opsheild_hits_left = OPSHEILD_HITS
powerup_timers['opsheild'] = OPSHEILD_DURATION_FRAMES
consecutive_shields_collected = 0
elif pu.power_type == "opsheild":
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.opsheild_active:
player.opsheild_hits_left -= 1
if player.opsheild_hits_left <= 0:
player.opsheild_active = False
powerup_timers['opsheild'] = 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
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 == 'opsheild':
player.opsheild_active = False
player.opsheild_hits_left = 0
score_timer += 1
if score_timer >= 10:
score += 1
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.opsheild_active:
draw_text_with_outline(screen, f"OPSheild Active ({player.opsheild_hits_left} hits left)", font_small, OPSHEILD_COLOR, WHITE, WIDTH - 220, 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 is mostly well-structured and appears functional for a simple 2D Pygame platformer-style game with power-ups and hazards. Here are some observations, suggestions, and a few issues you might want to consider addressing:
General Comments
Code Organization: The code is organized nicely with the use of classes (Player, Floor, PowerUp, KillBlock) and well-named variables.
Game States: Handling of multiple states (MENU, PLAYING, PAUSED, etc.) is done properly.
Power-ups and Timers: The timers dict to track power-up durations is a good approach.
Drawing With Outline: The utility function draw_text_with_outline is useful for readability.
Settings & Pause: Implementation of settings and pause toggle looks solid.
Possible Issues or Improvements
1. Spelling Consistency
You have OPSHEILD, which presumably should be OPSHIELD (a shield power-up). This spelling is inconsistent or incorrect:
Variables: OPSHEILD_COLOR, opsheild_active, OPSHEILD_DURATION_FRAMES, etc.
Affecting PowerUp type "opsheild".
Suggestion: Rename all opsheild to opshield. This prevents confusion when reading and maintaining.
2. Power-Up Spawn Probability
In the game loop under STATE_PLAYING, these lines:
The weights add up to 91.1, which is fine but maybe could be normalized for clarity.
The opsheild is extremely rare which is fine but consider user experience if the power-up never appears.
3. Double move_speed_current and Speed Boost
You do:
python
if powerup_timers['speed_boost'] > 0:
dx *= 2
But earlier:
python
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
dx is set based on move_speed_current, which starts at base_move_speed. Then you multiply dx by 2 if speed_boost is active. But move_speed_current never gets updated to reflect this.
Suggestion: Instead of multiplying dx again, you could update move_speed_current when speed boost activates or simply keep as is if you prefer this approach. Otherwise, this could cause confusion.
4. PowerUp Timers Update Logic
In the timer update you do:
python
for key in powerup_timers:
if powerup_timers[key] > 0:
powerup_timers[key] -= 1
if powerup_timers[key] == 0:
# deactivate powerups accordingly
This is fine. Just make sure that powerup_timers doesn't get into the negative value if not carefully managed. Your code inherently prevents decrementing below zero, so this is good.
5. Player Falling Through Floors?
Your Player.update() does this:
python
if collided_floor and self.dy == 0: # landed, damage floor
if collided_floor.damage():
floors.remove(collided_floor)
You check if player collided with a floor and only if dy == 0 (i.e., vertical speed zero - landed), then damage that floor and remove it if health reaches 0.
Also, on_floor flag is set if collision is detected.
This logic seems fine, but watch out for any potential issues of the player sinking into the floor if collision detection is inconsistent.
6. PowerUp apply() Method Accessing Timers
Your PowerUp.apply(player, timers) method requires passing timers dict explicitly.
This is fine, but you also store player.supershield_time_left and player.opsheild_time_left in the player object (though you don’t seem to decrement it in your main loop, only the timers dict). This redundancy can be confusing.
Suggestion: Pick one mechanism to track timers—either in the player object or in the powerup_timers dict to avoid confusion and keep code consistent.
7. No Sound or Music
Your code doesn't seem to include any sound effects or music. Consider adding some for better user experience.
8. No Comments for Key Methods / Functions
Adding comments for complex logic bits would improve readability, especially around power-up collections and interactions.
Suggested Fixes for Naming (opshield rename example)