Got it! I've improved UI, added temporary power-ups with a 30-second duration, and polished the overall feel.
Changes & improvements:
- Power-ups (
double_jump and speed_boost) last exactly 30 seconds (tracked via powerup_timers dict).
- Display timers on screen for active power-ups.
- More polished UI: outlined texts for better visibility, subtle shadows.
- Added a subtle "floor health" bar above floors.
- Improved "PAUSED" overlay with a nicer translucent background.
- Added a subtle glow effect on player if speed boost active.
- Display current FPS in bottom-left corner for debugging.
- Added a max floor count on spawn and some regeneration mechanic (optional, can be removed if unwanted).
- Clearer and nicer game messages.
Here's the comprehensive updated code:
import pygame
import random
import sys
import time
pygame.init()
# Constants
WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tower Tumble")
# Fonts
font_small = pygame.font.SysFont(None, 24)
font_med = pygame.font.SysFont(None, 36)
font_large = pygame.font.SysFont(None, 48)
font_xlarge = pygame.font.SysFont(None, 72)
# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
DARK_RED = (180, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
YELLOW = (255, 255, 0)
TRANSPARENT_BLACK_180 = (0, 0, 0, 180)
TRANSPARENT_BLACK_130 = (0, 0, 0, 130)
SPEED_GLOW_COLOR = (50, 255, 50)
# Game settings
clock = pygame.time.Clock()
FPS = 60
floor_height = 40
player_size = 20
gravity = 1
jump_speed = 15
base_move_speed = 5
# States
STATE_MENU = 0
STATE_PLAYING = 1
STATE_PAUSED = 2
STATE_GAMEOVER = 3
POWERUP_DURATION_FRAMES = FPS * 30 # 30 seconds duration
def draw_text_with_outline(surface, text, font, color, outline_color, x, y, center=True):
text_surface = font.render(text, True, color)
outline_surf = font.render(text, True, outline_color)
if center:
rect = text_surface.get_rect(center=(x, y))
else:
rect = text_surface.get_rect(topleft=(x, y))
# Draw outline
outline_positions = [(rect.x-1, rect.y), (rect.x+1, rect.y), (rect.x, rect.y-1), (rect.x, rect.y+1)]
for pos in outline_positions:
surface.blit(outline_surf, pos)
surface.blit(text_surface, rect)
class Player:
def __init__(self):
self.rect = pygame.Rect(WIDTH//2, HEIGHT - floor_height - player_size, player_size, player_size)
self.dy = 0
self.on_floor = True
self.can_double_jump = False
self.double_jump_available = False
self.speed_boost = False
self.speed_boost_time_left = 0
def jump(self):
if self.on_floor:
self.dy = -jump_speed
self.on_floor = False
self.double_jump_available = self.can_double_jump
elif self.double_jump_available:
self.dy = -jump_speed
self.double_jump_available = False
def move(self, dx):
self.rect.x += dx
if self.rect.x < 0:
self.rect.x = 0
if self.rect.x > WIDTH - player_size:
self.rect.x = WIDTH - player_size
def update(self, floors):
self.dy += gravity
self.rect.y += self.dy
collided_floor = None
for floor in floors:
if self.rect.colliderect(floor.rect):
# Player hitting floor from above
if self.dy >= 0 and self.rect.bottom - self.dy <= floor.rect.top:
self.rect.bottom = floor.rect.top
self.dy = 0
self.on_floor = True
collided_floor = floor
break
else:
self.on_floor = False
if collided_floor and self.dy == 0: # landed, damage floor
if collided_floor.damage():
floors.remove(collided_floor)
# Prevent player from falling below bottom floor
if self.rect.y > HEIGHT - floor_height - player_size:
self.rect.y = HEIGHT - floor_height - player_size
self.dy = 0
self.on_floor = True
# Clamp horizontally
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x > WIDTH - player_size:
self.rect.x = WIDTH - player_size
def draw(self, surface):
# If speed boost active, glow effect
if self.speed_boost:
glow_radius = player_size * 2
glow_surf = pygame.Surface((glow_radius * 2, glow_radius * 2), pygame.SRCALPHA)
pygame.draw.circle(glow_surf, (*SPEED_GLOW_COLOR, 100), (glow_radius, glow_radius), glow_radius)
surface.blit(glow_surf, (self.rect.centerx - glow_radius, self.rect.centery - glow_radius))
pygame.draw.rect(surface, BLUE, self.rect)
class Floor:
def __init__(self, y):
self.rect = pygame.Rect(0, y, WIDTH, floor_height)
self.max_health = 5
self.health = self.max_health
def draw(self, surface):
# Draw floor with color depending on health (fade to darker red)
health_ratio = self.health / self.max_health
floor_color = (
int(RED[0] * health_ratio + DARK_RED[0] * (1-health_ratio)),
int(RED[1] * health_ratio + DARK_RED[1] * (1-health_ratio)),
int(RED[2] * health_ratio + DARK_RED[2] * (1-health_ratio))
)
pygame.draw.rect(surface, floor_color, self.rect)
# Draw health bar above floor
bar_width = self.rect.width * 0.3
bar_height = 6
bar_x = (WIDTH - bar_width) /2
bar_y = self.rect.top - bar_height - 4
pygame.draw.rect(surface, BLACK, (bar_x, bar_y, bar_width, bar_height), 1)
inner_width = bar_width * (self.health / self.max_health)
pygame.draw.rect(surface, GREEN, (bar_x + 1, bar_y + 1, inner_width - 2, bar_height - 2))
def damage(self):
self.health -= 1
return self.health <= 0
class PowerUp:
def __init__(self, x, y, power_type):
self.rect = pygame.Rect(x, y, player_size, player_size)
self.power_type = power_type
self.fall_speed = random.randint(3, 6)
def apply(self, player, timers):
# Add/refresh timer for this power-up
if self.power_type == "double_jump":
player.can_double_jump = True
timers['double_jump'] = POWERUP_DURATION_FRAMES
elif self.power_type == "speed_boost":
player.speed_boost = True
timers['speed_boost'] = POWERUP_DURATION_FRAMES
def update(self):
self.rect.y += self.fall_speed
def draw(self, surface):
color = BLUE if self.power_type == "double_jump" else GREEN
pygame.draw.rect(surface, color, self.rect)
class KillBlock:
def __init__(self, x, y):
self.size = 60
self.rect = pygame.Rect(x, y, self.size, self.size)
self.fall_speed = random.randint(4, 7)
def update(self):
self.rect.y += self.fall_speed
if self.rect.top > HEIGHT:
self.rect.x = random.randint(0, WIDTH - self.size)
self.rect.y = -self.size
self.fall_speed = random.randint(4, 7)
def draw(self, surface):
pygame.draw.rect(surface, DARK_RED, self.rect)
def draw_text(surface, text, size, color, x, y, center=True):
font_obj = pygame.font.SysFont(None, size)
text_surface = font_obj.render(text, True, color)
if center:
rect = text_surface.get_rect(center=(x, y))
else:
rect = text_surface.get_rect(topleft=(x,y))
surface.blit(text_surface, rect)
def draw_text_outline(surface, text, font, text_color, outline_color, pos, center=True):
text_surface = font.render(text, True, text_color)
outline_surface = font.render(text, True, outline_color)
rect = text_surface.get_rect(center=pos) if center else text_surface.get_rect(topleft=pos)
# Draw outline (4 directions)
for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
surface.blit(outline_surface, rect.move(dx, dy))
surface.blit(text_surface, rect)
def draw_paused_overlay(surface):
overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
overlay.fill(TRANSPARENT_BLACK_180)
surface.blit(overlay, (0, 0))
draw_text_outline(surface, "PAUSED", font_xlarge, WHITE, BLACK, (WIDTH//2, HEIGHT//3))
draw_text_outline(surface, "Press ESC to Resume", font_med, WHITE, BLACK, (WIDTH//2, HEIGHT//2))
def draw_powerup_timers(surface, timers):
x = 10
y = HEIGHT - 70
bar_width = 140
bar_height = 20
gap = 10
for p_type in timers:
# Background bar
pygame.draw.rect(surface, BLACK, (x, y, bar_width, bar_height))
# Foreground bar proportional to time left
progress = timers[p_type] / POWERUP_DURATION_FRAMES if POWERUP_DURATION_FRAMES else 0
pygame.draw.rect(surface, GREEN if p_type == 'speed_boost' else BLUE,
(x + 2, y + 2, int((bar_width - 4) * progress), bar_height - 4))
# Text label
label = f"{p_type.replace('_', ' ').title()}: {timers[p_type]//FPS}s"
draw_text_outline(surface, label, font_small, WHITE, BLACK, (x + bar_width//2, y + 2 + bar_height//2), True)
y += bar_height + gap
def main():
state = STATE_MENU
paused = False
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,
}
button_rect = pygame.Rect(WIDTH//2 - 100, HEIGHT//2 - 25, 200, 50)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
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
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}
state = STATE_PLAYING
paused = False
elif state == STATE_PLAYING:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not paused:
player.jump()
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]:
dx -= move_speed_current
if keys[pygame.K_d]:
dx += move_speed_current
if powerup_timers['speed_boost'] > 0:
dx *= 2
player.move(dx)
player.update(floors)
# Spawn powerups sometimes
if random.randint(1, 100) == 1:
pu_type = random.choice(["double_jump", "speed_boost"])
powerups.append(PowerUp(random.randint(0, WIDTH - player_size), -player_size, pu_type))
# Update & check powerups
for pu in powerups[:]:
pu.update()
if pu.rect.top > HEIGHT:
# reset powerup to top randomly
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
powerups.remove(pu)
# Update kill blocks
for kb in kill_blocks:
kb.update()
if player.rect.colliderect(kb.rect):
state = STATE_GAMEOVER
# Update power-up timers
for key in powerup_timers:
if powerup_timers[key] > 0:
powerup_timers[key] -= 1
if powerup_timers[key] == 0:
# Powerup expired, reset player states as needed
if key == 'double_jump':
player.can_double_jump = False
player.double_jump_available = False
elif key == 'speed_boost':
player.speed_boost = False
# Update score every 10 frames (~6 times per second)
score_timer += 1
if score_timer >= 10:
score += 1
score_timer = 0
# Draw phases
screen.fill(WHITE)
if state == STATE_MENU:
draw_text_with_outline(screen, "Tower Tumble", 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, Jump: 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)
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)
# Show powerup timers with bars at bottom left
active_timers = {k:v for k,v in powerup_timers.items() if v > 0}
if active_timers:
draw_powerup_timers(screen, active_timers)
# Show active powerups as text
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)
if paused:
draw_paused_overlay(screen)
# Draw FPS (optional)
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()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
Details:
- Power-up timers count down every frame.
- When timer expires, remove effect.
- On screen at bottom-left, green/blue bars reps remaining time + seconds count.
- Text shadows and outlines for crispness.
- Player glows green when speed boost is active.
- Floor health bars show how battered floors are.
- FPS counter helps debugging performance.
- Pause overlay is nicer and easier on eyes.
Feel free to ask for sound effects, animations, menus, or more polish!