|
| 1 | +import pygame |
| 2 | +import random |
| 3 | +import math |
| 4 | + |
| 5 | +# Initialize Pygame |
| 6 | +pygame.init() |
| 7 | + |
| 8 | +# Screen dimensions |
| 9 | +SCREEN_WIDTH = 800 |
| 10 | +SCREEN_HEIGHT = 600 |
| 11 | + |
| 12 | +# Colors |
| 13 | +WHITE = (255, 255, 255) |
| 14 | +BLACK = (0, 0, 0) |
| 15 | + |
| 16 | +# Create the screen |
| 17 | +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) |
| 18 | +pygame.display.set_caption("Infinite Runner with Customizable Obstacles") |
| 19 | + |
| 20 | +# Load sound effects and background music |
| 21 | +pygame.mixer.music.load("background_music.mp3") |
| 22 | +jump_sound = pygame.mixer.Sound("jump_sound.wav") |
| 23 | +collision_sound = pygame.mixer.Sound("collision_sound.wav") |
| 24 | + |
| 25 | +# Scoring |
| 26 | +score = 0 |
| 27 | +font = pygame.font.Font(None, 36) |
| 28 | + |
| 29 | +# Player attributes |
| 30 | +player_width = 50 |
| 31 | +player_height = 50 |
| 32 | +player_x = SCREEN_WIDTH // 2 - player_width // 2 |
| 33 | +player_y = SCREEN_HEIGHT - player_height - 20 |
| 34 | +player_speed = 5 |
| 35 | + |
| 36 | +# Obstacle attributes |
| 37 | +obstacle_width = 100 |
| 38 | +obstacle_height = 20 |
| 39 | +obstacle_speed = 5 |
| 40 | +obstacles = [] |
| 41 | + |
| 42 | +# Game loop |
| 43 | +running = True |
| 44 | +clock = pygame.time.Clock() |
| 45 | +pygame.mixer.music.play(-1) |
| 46 | + |
| 47 | +while running: |
| 48 | + for event in pygame.event.get(): |
| 49 | + if event.type == pygame.QUIT: |
| 50 | + running = False |
| 51 | + |
| 52 | + # Input handling - left and right arrow keys |
| 53 | + keys = pygame.key.get_pressed() |
| 54 | + if keys[pygame.K_LEFT] and player_x > 0: |
| 55 | + player_x -= player_speed |
| 56 | + if keys[pygame.K_RIGHT] and player_x < SCREEN_WIDTH - player_width: |
| 57 | + player_x += player_speed |
| 58 | + |
| 59 | + # Jumping with Spacebar |
| 60 | + if keys[pygame.K_SPACE] and player_y == SCREEN_HEIGHT - player_height - 20: |
| 61 | + player_y -= 10 |
| 62 | + jump_sound.play() |
| 63 | + |
| 64 | + # Apply gravity |
| 65 | + if player_y < SCREEN_HEIGHT - player_height - 20: |
| 66 | + player_y += 5 |
| 67 | + |
| 68 | + # Add a new obstacle randomly |
| 69 | + if random.randint(0, 100) < 2: |
| 70 | + obstacle_type = random.choice(["rect", "tri", "circle"]) |
| 71 | + obstacle_x = random.randint(0, SCREEN_WIDTH - obstacle_width) |
| 72 | + |
| 73 | + if obstacle_type == "circle": |
| 74 | + obstacle_x = random.randint(0, SCREEN_WIDTH - obstacle_width - 50) |
| 75 | + |
| 76 | + obstacles.append((obstacle_x, -obstacle_height, obstacle_type)) |
| 77 | + |
| 78 | + # Update obstacle positions |
| 79 | + for i, (obstacle_x, obstacle_y, obstacle_type) in enumerate(obstacles): |
| 80 | + if obstacle_type == "rect": |
| 81 | + # Rectangular obstacle: move straight down |
| 82 | + obstacles[i] = (obstacle_x, obstacle_y + obstacle_speed) |
| 83 | + elif obstacle_type == "tri": |
| 84 | + # Triangular obstacle: move diagonally |
| 85 | + obstacles[i] = (obstacle_x + obstacle_speed, obstacle_y + obstacle_speed) |
| 86 | + elif obstacle_type == "circle": |
| 87 | + # Rotating circular obstacle: update the angle |
| 88 | + angle = 0.1 # Adjust the rotation speed |
| 89 | + rotated_obstacle_x = obstacle_x + (obstacle_width // 2) |
| 90 | + rotated_obstacle_y = obstacle_y + (obstacle_height // 2) |
| 91 | + obstacles[i] = (rotated_obstacle_x - obstacle_width // 2 * math.cos(angle), |
| 92 | + rotated_obstacle_y - obstacle_height // 2 * math.sin(angle), |
| 93 | + "circle") |
| 94 | + |
| 95 | + # Remove obstacles that are off the screen |
| 96 | + if obstacle_y > SCREEN_HEIGHT: |
| 97 | + del obstacles[i] |
| 98 | + |
| 99 | + # Check for collision with obstacles |
| 100 | + if obstacle_y + obstacle_height > player_y and obstacle_y < player_y + player_height: |
| 101 | + if obstacle_x + obstacle_width > player_x and obstacle_x < player_x + player_width: |
| 102 | + # Game over |
| 103 | + collision_sound.play() |
| 104 | + running = False |
| 105 | + |
| 106 | + # Save high score to a file (you can choose a different file path) |
| 107 | + with open("high_score.txt", "w") as file: |
| 108 | + file.write(str(score)) |
| 109 | + |
| 110 | + # Increase the score |
| 111 | + score += 1 |
| 112 | + |
| 113 | + # Clear the screen |
| 114 | + screen.fill(WHITE) |
| 115 | + |
| 116 | + # Draw the player |
| 117 | + pygame.draw.rect(screen, (0, 0, 255), (player_x, player_y, player_width, player_height)) |
| 118 | + |
| 119 | + # Draw the obstacles |
| 120 | + for obstacle_x, obstacle_y, obstacle_type in obstacles: |
| 121 | + if obstacle_type == "rect": |
| 122 | + pygame.draw.rect(screen, (255, 0, 0), (obstacle_x, obstacle_y, obstacle_width, obstacle_height)) |
| 123 | + elif obstacle_type == "tri": |
| 124 | + pygame.draw.polygon(screen, (0, 255, 0), [(obstacle_x, obstacle_y), (obstacle_x + obstacle_width, obstacle_y), |
| 125 | + (obstacle_x + obstacle_width // 2, obstacle_y + obstacle_height)]) |
| 126 | + elif obstacle_type == "circle": |
| 127 | + pygame.draw.circle(screen, (0, 0, 255), (int(obstacle_x + obstacle_width // 2), |
| 128 | + int(obstacle_y + obstacle_height // 2)), obstacle_width // 2) |
| 129 | + |
| 130 | + # Draw the score |
| 131 | + score_text = font.render("Score: " + str(score), True, BLACK) |
| 132 | + screen.blit(score_text, (10, 10)) |
| 133 | + |
| 134 | + # Update the screen |
| 135 | + pygame.display.update() |
| 136 | + |
| 137 | + # Set the frame rate |
| 138 | + clock.tick(60) |
| 139 | + |
| 140 | +# Quit the game |
| 141 | +pygame.quit() |
0 commit comments