import pygame import math import random pygame.init() # ---------------- SETTINGS ---------------- WIDTH = 1000 HEIGHT = 650 FPS = 60 WORLD_WIDTH = 3200 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Platformer | Cameron McLeod") clock = pygame.time.Clock() WHITE = (245, 245, 255) PURPLE = (160, 100, 255) DARK = (12, 15, 35) GREEN = (75, 205, 100) font = pygame.font.SysFont("arial", 23) big_font = pygame.font.SysFont("arial", 60, bold=True) # ---------------- LEVEL ---------------- platforms = [ pygame.Rect(0, 540, 550, 150), pygame.Rect(640, 470, 280, 180), pygame.Rect(1010, 400, 320, 250), pygame.Rect(1430, 485, 300, 165), pygame.Rect(1810, 410, 340, 240), pygame.Rect(2240, 340, 300, 310), pygame.Rect(2630, 465, 570, 185), pygame.Rect(280, 420, 120, 22), pygame.Rect(1120, 310, 120, 22), pygame.Rect(1900, 315, 120, 22), pygame.Rect(2350, 245, 120, 22) ] # ---------------- PLAYER ---------------- player = pygame.Rect(100, 400, 42, 65) speed_x = 0 speed_y = 0 ACCELERATION = 0.65 MAX_SPEED = 5.5 FRICTION = 0.80 GRAVITY = 0.65 MAX_FALL = 14 JUMP_POWER = -14 grounded = False jump_buffer = 0 coyote_timer = 0 facing = 1 animation_timer = 0 camera_x = 0 # ---------------- GAME OBJECTS ---------------- coins = [ pygame.Rect(x, y, 22, 28) for x, y in [ (180, 460), (260, 460), (350, 370), (700, 390), (780, 390), (850, 390), (1080, 330), (1160, 245), (1240, 245), (1510, 405), (1600, 405), (1880, 330), (1980, 250), (2050, 250), (2290, 280), (2380, 180), (2700, 390), (2800, 390), (2900, 390), (3050, 390) ] ] enemies = [ pygame.Rect(400, 500, 42, 38), pygame.Rect(1080, 360, 42, 38), pygame.Rect(1900, 370, 42, 38), pygame.Rect(2750, 425, 42, 38) ] enemy_speeds = [2, -2, 2, -2] score = 0 lives = 3 invincible = 0 state = "menu" paused = False # ---------------- DRAWING HELPERS ---------------- def text(message, size, colour, x, y, centre=False): image = pygame.font.SysFont("arial", size, bold=True).render( message, True, colour ) rect = image.get_rect() if centre: rect.center = (x, y) else: rect.topleft = (x, y) screen.blit(image, rect) def draw_background(): screen.fill((13, 17, 48)) # Stars for i in range(100): x = (i * 137 - camera_x // 4) % WIDTH y = (i * 83) % 350 pygame.draw.circle( screen, (120, 140, 210), (x, y), 2 ) # Moon pygame.draw.circle( screen, (215, 220, 255), (780, 130), 55 ) pygame.draw.circle( screen, (180, 190, 235), (765, 115), 9 ) pygame.draw.circle( screen, (180, 190, 235), (800, 150), 7 ) # Distant mountains for i in range(-2, 10): x = i * 190 - int(camera_x * 0.2) % 190 pygame.draw.polygon( screen, (35, 42, 100), [(x, 490), (x + 95, 220), (x + 190, 490)] ) # Castle castle_x = 680 - int(camera_x * 0.12) pygame.draw.rect( screen, (22, 22, 58), (castle_x, 260, 180, 220) ) for x in [castle_x + 15, castle_x + 75, castle_x + 135]: pygame.draw.rect( screen, (22, 22, 58), (x, 200, 35, 100) ) pygame.draw.polygon( screen, (22, 22, 58), [(x - 8, 200), (x + 17, 145), (x + 43, 200)] ) pygame.draw.rect( screen, PURPLE, (x + 13, 225, 9, 22) ) def draw_platform(rect): x = rect.x - camera_x if x + rect.width < 0 or x > WIDTH: return # Dirt pygame.draw.rect( screen, (95, 57, 55), (x, rect.y + 15, rect.width, rect.height - 15) ) # Dirt texture for px in range(int(x) + 10, int(x + rect.width), 35): pygame.draw.rect( screen, (125, 75, 67), (px, rect.y + 35, 16, 12) ) pygame.draw.rect( screen, (75, 45, 48), (px + 15, rect.y + 65, 13, 10) ) # Grass pygame.draw.rect( screen, (40, 150, 75), (x, rect.y, rect.width, 17) ) pygame.draw.rect( screen, (105, 235, 105), (x, rect.y, rect.width, 6) ) for px in range(int(x), int(x + rect.width), 22): pygame.draw.rect( screen, (70, 195, 85), (px, rect.y + 7, 12, 8) ) # ---------------- PLAYER ANIMATION ---------------- def draw_player(): global animation_timer x = player.x - camera_x y = player.y moving = abs(speed_x) > 0.5 if moving and grounded: animation_timer += abs(speed_x) * 0.13 # Walking animation swing = math.sin(animation_timer) if moving and grounded else 0 leg_a = int(swing * 9) leg_b = int(-swing * 9) arm_a = int(-swing * 7) arm_b = int(swing * 7) if not grounded: leg_a = -5 leg_b = 5 arm_a = -5 arm_b = 5 # Shadow pygame.draw.ellipse( screen, (8, 10, 25), (x - 3, y + 59, 48, 10) ) # Legs pygame.draw.line( screen, (25, 35, 60), (x + 15, y + 43), (x + 14 + leg_a, y + 60), 9 ) pygame.draw.line( screen, (25, 35, 60), (x + 29, y + 43), (x + 29 + leg_b, y + 60), 9 ) # Shoes pygame.draw.ellipse( screen, (210, 215, 235), (x + 5 + leg_a, y + 55, 18, 10) ) pygame.draw.ellipse( screen, (210, 215, 235), (x + 22 + leg_b, y + 55, 19, 10) ) # Body pygame.draw.rect( screen, (28, 32, 55), (x + 9, y + 22, 27, 28), border_radius=5 ) # Jacket detail pygame.draw.line( screen, (95, 105, 140), (x + 22, y + 25), (x + 22, y + 46), 2 ) # Arms pygame.draw.line( screen, (30, 35, 60), (x + 11, y + 27), (x + 5 + arm_a, y + 39), 8 ) pygame.draw.line( screen, (30, 35, 60), (x + 34, y + 27), (x + 39 + arm_b, y + 39), 8 ) # Hands pygame.draw.circle( screen, (235, 175, 140), (x + 5 + arm_a, y + 40), 5 ) pygame.draw.circle( screen, (235, 175, 140), (x + 39 + arm_b, y + 40), 5 ) # Head pygame.draw.rect( screen, (225, 160, 125), (x + 9, y + 2, 27, 26), border_radius=6 ) # Hair pygame.draw.rect( screen, (65, 38, 32), (x + 6, y, 33, 12), border_radius=5 ) pygame.draw.rect( screen, (75, 43, 32), (x + 7, y + 5, 8, 10) ) # Eyes eye_x = x + (27 if facing == 1 else 13) pygame.draw.rect( screen, (20, 20, 25), (eye_x, y + 14, 4, 5) ) # ---------------- COINS AND ENEMIES ---------------- def draw_coin(coin): bounce = math.sin(pygame.time.get_ticks() * 0.006 + coin.x) * 3 x = coin.x - camera_x y = coin.y + bounce pygame.draw.ellipse( screen, (180, 110, 5), (x, y, 22, 28) ) pygame.draw.ellipse( screen, (255, 190, 20), (x + 3, y + 2, 16, 24) ) pygame.draw.line( screen, (255, 245, 140), (x + 9, y + 5), (x + 9, y + 21), 3 ) def draw_enemy(enemy): x = enemy.x - camera_x pygame.draw.ellipse( screen, (30, 120, 55), (x, enemy.y + 22, 42, 16) ) pygame.draw.ellipse( screen, (180, 45, 80), (x, enemy.y, 42, 34) ) pygame.draw.circle( screen, (255, 220, 80), (x + 12, enemy.y + 15), 5 ) pygame.draw.circle( screen, (255, 220, 80), (x + 30, enemy.y + 15), 5 ) pygame.draw.circle( screen, DARK, (x + 12, enemy.y + 15), 2 ) pygame.draw.circle( screen, DARK, (x + 30, enemy.y + 15), 2 ) # ---------------- HUD ---------------- def draw_hud(): pygame.draw.rect( screen, (20, 23, 48), (18, 18, 300, 55), border_radius=12 ) text(f"SCORE: {score:05d}", 23, WHITE, 35, 32) for i in range(lives): pygame.draw.circle( screen, (245, 55, 90), (835 + i * 35, 43), 12 ) text(f"COINS: {20 - len(coins):02d}", 20, (255, 205, 65), 850, 90) # ---------------- RESET ---------------- def reset_game(): global player, speed_x, speed_y global score, lives, coins, enemies global enemy_speeds, camera_x global grounded, invincible global jump_buffer, coyote_timer player = pygame.Rect(100, 400, 42, 65) speed_x = 0 speed_y = 0 score = 0 lives = 3 coins = [ pygame.Rect(x, y, 22, 28) for x, y in [ (180, 460), (260, 460), (350, 370), (700, 390), (780, 390), (850, 390), (1080, 330), (1160, 245), (1240, 245), (1510, 405), (1600, 405), (1880, 330), (1980, 250), (2050, 250), (2290, 280), (2380, 180), (2700, 390), (2800, 390), (2900, 390), (3050, 390) ] ] enemies = [ pygame.Rect(400, 500, 42, 38), pygame.Rect(1080, 360, 42, 38), pygame.Rect(1900, 370, 42, 38), pygame.Rect(2750, 425, 42, 38) ] enemy_speeds = [2, -2, 2, -2] camera_x = 0 grounded = False invincible = 0 jump_buffer = 0 coyote_timer = 0 # ---------------- MAIN LOOP ---------------- running = True reset_game() while running: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if state == "menu": if event.key == pygame.K_SPACE: state = "playing" elif state in ("won", "lost"): if event.key == pygame.K_r: reset_game() state = "playing" elif state == "playing": if event.key == pygame.K_SPACE: jump_buffer = 9 if event.key == pygame.K_ESCAPE: paused = not paused if event.key == pygame.K_r: reset_game() # MENU if state == "menu": draw_background() text("CAMERON'S", 65, WHITE, WIDTH // 2, 210, True) text("PLATFORMER", 65, PURPLE, WIDTH // 2, 275, True) text("PRESS SPACE TO START", 25, WHITE, WIDTH // 2, 390, True) text("A / D TO MOVE SPACE TO JUMP", 19, WHITE, WIDTH // 2, 455, True) # END SCREENS elif state in ("won", "lost"): screen.fill(DARK) message = "LEVEL COMPLETE!" if state == "won" else "GAME OVER" colour = GREEN if state == "won" else (255, 80, 100) text(message, 60, colour, WIDTH // 2, 230, True) text(f"FINAL SCORE: {score}", 28, WHITE, WIDTH // 2, 310, True) text("PRESS R TO RESTART", 23, PURPLE, WIDTH // 2, 390, True) # GAMEPLAY elif state == "playing": if not paused: keys = pygame.key.get_pressed() # HORIZONTAL MOVEMENT if keys[pygame.K_a] or keys[pygame.K_LEFT]: speed_x -= ACCELERATION facing = -1 elif keys[pygame.K_d] or keys[pygame.K_RIGHT]: speed_x += ACCELERATION facing = 1 else: speed_x *= FRICTION speed_x = max(-MAX_SPEED, min(MAX_SPEED, speed_x)) player.x += round(speed_x) player.left = max(0, player.left) player.right = min(WORLD_WIDTH, player.right) # COYOTE TIME if grounded: coyote_timer = 8 else: coyote_timer = max(0, coyote_timer - 1) # JUMP BUFFER if jump_buffer > 0: jump_buffer -= 1 if jump_buffer > 0 and coyote_timer > 0: speed_y = JUMP_POWER grounded = False coyote_timer = 0 jump_buffer = 0 # VARIABLE JUMP HEIGHT if not keys[pygame.K_SPACE] and speed_y < -5: speed_y += 0.5 # GRAVITY speed_y = min(speed_y + GRAVITY, MAX_FALL) # HORIZONTAL COLLISION for platform in platforms: if player.colliderect(platform): if speed_x > 0: player.right = platform.left elif speed_x < 0: player.left = platform.right speed_x = 0 # VERTICAL COLLISION player.y += round(speed_y) grounded = False for platform in platforms: if player.colliderect(platform): if speed_y > 0: player.bottom = platform.top grounded = True elif speed_y < 0: player.top = platform.bottom speed_y = 0 # FALLING if player.top > HEIGHT + 100: lives -= 1 player.topleft = (100, 350) speed_x = 0 speed_y = 0 if lives <= 0: state = "lost" # COLLECT COINS for coin in coins[:]: if player.colliderect(coin): coins.remove(coin) score += 10 # ENEMY MOVEMENT for i, enemy in enumerate(enemies): enemy.x += enemy_speeds[i] if enemy.x < 350 + i * 350 or enemy.x > 520 + i * 350: enemy_speeds[i] *= -1 if player.colliderect(enemy) and invincible == 0: if speed_y > 0 and player.bottom - enemy.top < 25: enemies.pop(i) enemy_speeds.pop(i) speed_y = -9 score += 50 break else: lives -= 1 invincible = 90 speed_x = -facing * 5 speed_y = -7 if lives <= 0: state = "lost" break if invincible > 0: invincible -= 1 # CAMERA SMOOTHING target_camera = player.centerx - WIDTH // 2 camera_x += (target_camera - camera_x) * 0.10 camera_x = max(0, min(WORLD_WIDTH - WIDTH, camera_x)) # FINISH if player.x > WORLD_WIDTH - 100: state = "won" # DRAW draw_background() for platform in platforms: draw_platform(platform) for coin in coins: draw_coin(coin) for enemy in enemies: draw_enemy(enemy) # PLAYER if invincible == 0 or pygame.time.get_ticks() // 100 % 2 == 0: draw_player() draw_hud() text("A/D MOVE SPACE JUMP ESC PAUSE R RESTART", 15, WHITE, 20, HEIGHT - 28) if paused: overlay = pygame.Surface( (WIDTH, HEIGHT), pygame.SRCALPHA ) overlay.fill((5, 5, 20, 190)) screen.blit(overlay, (0, 0)) text("PAUSED", 60, WHITE, WIDTH // 2, 270, True) text("PRESS ESC TO CONTINUE", 23, PURPLE, WIDTH // 2, 340, True) pygame.display.flip() pygame.quit()