Skip to content

Commit 596e00c

Browse files
authored
Merge pull request #79 from princesatapathy/main
Pull
2 parents aa3ddd1 + b17e73c commit 596e00c

4 files changed

Lines changed: 476 additions & 0 deletions

File tree

Python/Pong_game/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 🏓 Pong Game
2+
3+
A classic Pong game implementation using Python and Pygame. Play against the computer or challenge a friend in this timeless arcade-style paddle game.
4+
5+
## Features
6+
7+
- 🎮 **Two Game Modes**:
8+
- Player vs Computer (PvE)
9+
- Player vs Player (PvP)
10+
- 🎯 **Classic Pong Gameplay** with smooth ball physics
11+
- 📊 **Score Tracking** for both players
12+
- 🎨 **Simple, Clean Interface** with easy-to-use menu system
13+
- ⌨️ **Keyboard Controls** for intuitive gameplay
14+
15+
## Requirements
16+
17+
- Python 3.x
18+
- Pygame library
19+
20+
## Installation
21+
22+
1. Clone or download this repository
23+
24+
2. Install Pygame (if not already installed):
25+
```bash
26+
pip install pygame
27+
```
28+
29+
## How to Run
30+
31+
Simply run the Python script:
32+
```bash
33+
python pong.py
34+
```
35+
36+
## Controls
37+
38+
### Menu
39+
- **1** - Select Player vs Computer mode
40+
- **2** - Select Player vs Player mode
41+
- **ESC** - Quit the game
42+
43+
### During Gameplay
44+
45+
**Player 1 (Right Paddle):**
46+
- **↑ (Up Arrow)** - Move paddle up
47+
- **↓ (Down Arrow)** - Move paddle down
48+
49+
**Player 2 (Left Paddle) - PvP Mode:**
50+
- **W** - Move paddle up
51+
- **S** - Move paddle down
52+
53+
**In-Game:**
54+
- **ESC** - Return to main menu
55+
56+
## Game Modes
57+
58+
### Player vs Computer (PvE)
59+
Play against an AI opponent that follows the ball. The computer controls the left paddle.
60+
61+
### Player vs Player (PvP)
62+
Play with a friend! Each player controls one paddle using different keyboard keys.
63+
64+
## How to Play
65+
66+
1. Start the game and select your preferred mode from the menu
67+
2. Use the arrow keys (or W/S in PvP mode) to move your paddle up and down
68+
3. Hit the ball with your paddle to bounce it back
69+
4. Score points when the ball passes your opponent's side
70+
5. Press ESC anytime to return to the main menu
71+
72+
## Game Settings
73+
74+
- **Screen Size**: 800x600 pixels
75+
- **Frame Rate**: 60 FPS
76+
- **Paddle Speed**: 7 pixels per frame
77+
- **Ball Speed**: 5 pixels per frame (adjustable in code)
78+
79+
Enjoy the game! 🎮
80+

Python/Pong_game/pong.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import pygame
2+
import sys
3+
4+
# Initialize pygame
5+
pygame.init()
6+
7+
# Screen setup
8+
WIDTH, HEIGHT = 800, 600
9+
screen = pygame.display.set_mode((WIDTH, HEIGHT))
10+
pygame.display.set_caption("🏓 Pong Game")
11+
12+
# Colors
13+
WHITE = (255, 255, 255)
14+
BLACK = (0, 0, 0)
15+
16+
# Fonts
17+
font_large = pygame.font.Font(None, 100)
18+
font_medium = pygame.font.Font(None, 60)
19+
font_small = pygame.font.Font(None, 40)
20+
21+
# Game settings
22+
PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100
23+
BALL_SIZE = 20
24+
PADDLE_SPEED = 7
25+
BALL_SPEED_X, BALL_SPEED_Y = 5, 5
26+
27+
# Create paddles and ball
28+
player = pygame.Rect(WIDTH - 20, HEIGHT//2 - 50, PADDLE_WIDTH, PADDLE_HEIGHT)
29+
opponent = pygame.Rect(10, HEIGHT//2 - 50, PADDLE_WIDTH, PADDLE_HEIGHT)
30+
ball = pygame.Rect(WIDTH//2 - 10, HEIGHT//2 - 10, BALL_SIZE, BALL_SIZE)
31+
32+
# Score
33+
player_score = 0
34+
opponent_score = 0
35+
36+
clock = pygame.time.Clock()
37+
38+
# Game state
39+
menu_active = True
40+
mode_selected = None # "pve" or "pvp"
41+
42+
def draw_text(text, font, color, surface, x, y):
43+
"""Helper function to draw text centered."""
44+
textobj = font.render(text, True, color)
45+
textrect = textobj.get_rect(center=(x, y))
46+
surface.blit(textobj, textrect)
47+
48+
def main_menu():
49+
"""Display start menu."""
50+
global menu_active, mode_selected
51+
while menu_active:
52+
screen.fill(BLACK)
53+
draw_text("PONG", font_large, WHITE, screen, WIDTH/2, HEIGHT/4)
54+
draw_text("1. Player vs Computer", font_medium, WHITE, screen, WIDTH/2, HEIGHT/2 - 30)
55+
draw_text("2. Player vs Player", font_medium, WHITE, screen, WIDTH/2, HEIGHT/2 + 40)
56+
draw_text("ESC to Quit", font_small, WHITE, screen, WIDTH/2, HEIGHT - 50)
57+
58+
pygame.display.flip()
59+
60+
for event in pygame.event.get():
61+
if event.type == pygame.QUIT:
62+
pygame.quit()
63+
sys.exit()
64+
if event.type == pygame.KEYDOWN:
65+
if event.key == pygame.K_1:
66+
mode_selected = "pve"
67+
menu_active = False
68+
elif event.key == pygame.K_2:
69+
mode_selected = "pvp"
70+
menu_active = False
71+
elif event.key == pygame.K_ESCAPE:
72+
pygame.quit()
73+
sys.exit()
74+
75+
def game_loop(mode):
76+
"""Main game loop."""
77+
global player_score, opponent_score
78+
ball_speed_x = BALL_SPEED_X
79+
ball_speed_y = BALL_SPEED_Y
80+
player_score = 0
81+
opponent_score = 0
82+
83+
# Reset positions
84+
player.centery = HEIGHT // 2
85+
opponent.centery = HEIGHT // 2
86+
ball.center = (WIDTH // 2, HEIGHT // 2)
87+
88+
running = True
89+
while running:
90+
for event in pygame.event.get():
91+
if event.type == pygame.QUIT:
92+
pygame.quit()
93+
sys.exit()
94+
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
95+
running = False # Return to menu
96+
97+
# Paddle movement
98+
keys = pygame.key.get_pressed()
99+
# Player 1
100+
if keys[pygame.K_UP] and player.top > 0:
101+
player.y -= PADDLE_SPEED
102+
if keys[pygame.K_DOWN] and player.bottom < HEIGHT:
103+
player.y += PADDLE_SPEED
104+
105+
# Opponent / Player 2
106+
if mode == "pve":
107+
# Simple AI: follow the ball
108+
if opponent.centery < ball.centery:
109+
opponent.y += PADDLE_SPEED
110+
if opponent.centery > ball.centery:
111+
opponent.y -= PADDLE_SPEED
112+
else: # PvP
113+
if keys[pygame.K_w] and opponent.top > 0:
114+
opponent.y -= PADDLE_SPEED
115+
if keys[pygame.K_s] and opponent.bottom < HEIGHT:
116+
opponent.y += PADDLE_SPEED
117+
118+
# Ball movement
119+
ball.x += ball_speed_x
120+
ball.y += ball_speed_y
121+
122+
# Collision with top/bottom
123+
if ball.top <= 0 or ball.bottom >= HEIGHT:
124+
ball_speed_y *= -1
125+
126+
# Collision with paddles
127+
if ball.colliderect(player) or ball.colliderect(opponent):
128+
ball_speed_x *= -1
129+
130+
# Score update
131+
if ball.left <= 0:
132+
player_score += 1
133+
ball.center = (WIDTH//2, HEIGHT//2)
134+
ball_speed_x *= -1
135+
if ball.right >= WIDTH:
136+
opponent_score += 1
137+
ball.center = (WIDTH//2, HEIGHT//2)
138+
ball_speed_x *= -1
139+
140+
# Drawing
141+
screen.fill(BLACK)
142+
pygame.draw.rect(screen, WHITE, player)
143+
pygame.draw.rect(screen, WHITE, opponent)
144+
pygame.draw.ellipse(screen, WHITE, ball)
145+
pygame.draw.aaline(screen, WHITE, (WIDTH//2, 0), (WIDTH//2, HEIGHT))
146+
147+
# Scores
148+
player_text = font_medium.render(str(player_score), True, WHITE)
149+
opponent_text = font_medium.render(str(opponent_score), True, WHITE)
150+
screen.blit(player_text, (WIDTH//2 + 40, 20))
151+
screen.blit(opponent_text, (WIDTH//2 - 80, 20))
152+
153+
# Escape hint
154+
draw_text("ESC to return to menu", font_small, WHITE, screen, WIDTH/2, HEIGHT - 40)
155+
156+
pygame.display.flip()
157+
clock.tick(60)
158+
159+
# Run the game
160+
while True:
161+
main_menu()
162+
game_loop(mode_selected)
163+
menu_active = True

0 commit comments

Comments
 (0)