Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b25ce3d

Browse files
Merge pull request avinashkranjan#2650 from andoriyaprashant/branch28
Infinite Runner with Obstacles Script Added
2 parents bfb1e89 + 31a905e commit b25ce3d

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Infinite Runner with Customizable Obstacles
2+
3+
This is a simple Infinite Runner game implemented in Python using the Pygame library. The player runs endlessly through a procedurally generated landscape while avoiding various obstacles. What sets this game apart is the ability for the player to encounter multiple types of obstacles: rectangular, triangular, and rotating circular obstacles.
4+
5+
## Features
6+
7+
- Endless runner gameplay with customizable obstacles.
8+
- Three types of obstacles: rectangular, triangular, and rotating circular.
9+
- Player can move left, right, and jump using arrow keys and spacebar.
10+
- Scoring system to keep track of player progress.
11+
- Background music, sound effects for jumping and collisions.
12+
13+
## Prerequisites
14+
15+
- Python
16+
- Pygame library (`pip install pygame`)
17+
18+
## How to Run
19+
20+
1. Install the required Pygame library using `pip`:
21+
22+
```bash
23+
pip install pygame
24+
```
25+
26+
2. Download or clone the repository.
27+
28+
3. Run the game script:
29+
30+
```bash
31+
python infinite_runner.py
32+
```
33+
34+
5. Use the left and right arrow keys to move the player character.
35+
6. Press the spacebar to make the player character jump.
36+
37+
## Gameplay
38+
39+
- The objective of the game is to survive for as long as possible without colliding with any obstacles.
40+
- Rectangular obstacles move straight down, triangular obstacles move diagonally, and circular obstacles rotate while moving down.
41+
- The player can collect points by running and avoiding collisions with obstacles.
42+
43+
## Customization
44+
45+
- You can customize the appearance and movement of obstacles by modifying the obstacle generation and update logic in the script.
46+
- Feel free to add more obstacle types or enhance the gameplay to suit your preferences.
47+
48+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pygame
2+
random
3+
math

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /