Skip to content

Commit

Permalink
Adding PingPong Game folder (#692)
Browse files Browse the repository at this point in the history
## Pull Request for PyVerse 💡

### Requesting to submit a pull request to the PyVerse repository.

---

#### Issue Title
**Please enter the title of the issue related to your pull request.**  
Ping pong game in python folder addition

- [X] I have provided the issue title.

---

#### Info about the Related Issue
**What's the goal of the project?**  

 Adding folder in section of Game development in python.

*Describe the aim of the project.*
Aim is simple. adding new project in game development section of pyverse
to make it more diverse.

- [X] I have described the aim of the project.

---

#### Name
**Please mention your name.**  
Jivan Jamdar

- [X] I have provided my name.

---

#### GitHub ID
**Please mention your GitHub ID.**  
Jivanjamadar

- [X] I have provided my GitHub ID.

---

#### Email ID
**Please mention your email ID for further communication.**  
[email protected]

- [X] I have provided my email ID.

---

#### Identify Yourself
**Mention in which program you are contributing (e.g., WoB, GSSOC, SSOC,
SWOC).**
GSSOC

- [X] I have mentioned my participant role.

---

#### Closes
**Enter the issue number that will be closed through this PR.**  
*Closes: #656*

- [X] I have provided the issue number.

---

#### Describe the Add-ons or Changes You've Made
**Give a clear description of what you have added or modified.**  
In game development section of pyverse, I have added new folder named
PingPong. In which there are 2 files ( main.py & Readme.md). In main.py
( main code of PingPong game in python ) and Readme.md file for giving
description of game and how to setup it.

- [X] I have described my changes.

---

#### Type of Change
**Select the type of change:**  
- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Code style update (formatting, local variables)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

---

#### How Has This Been Tested?
**Describe how your changes have been tested.**  
Simply run the code on any python compiler it will run perfectly. Before
that install pygame liabrary. ( All details are given in Readme.md file
of folder ).

- [X] I have described my testing process.

---

#### Checklist
**Please confirm the following:**  
- [X] My code follows the guidelines of this project.
- [X] I have performed a self-review of my own code.
- [X] I have commented my code, particularly wherever it was hard to
understand.
- [X] I have made corresponding changes to the documentation.
- [X] My changes generate no new warnings.
- [ ] I have added things that prove my fix is effective or that my
feature works.
- [X] Any dependent changes have been merged and published in downstream
modules.
  • Loading branch information
UTSAVS26 authored Oct 20, 2024
2 parents b26db3b + 76abd4e commit ce04787
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Game_Development/PingPong/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 🏓 Pong Game

Welcome to **Pong Game**, a classic arcade game built with Python and Pygame! 🎮

This project includes two paddles (one for you and one for the AI opponent) and a bouncing ball. The objective is simple: **score points** by making the ball pass your opponent's paddle while defending your own side.

---

## ✨ Features
- 🎮 **Player-controlled paddle**: Use the arrow keys to move your paddle up and down.
- 🤖 **AI opponent**: The opponent paddle follows the ball automatically.
- 🎯 **Collision detection**: Ball bounces off paddles and walls with realistic effects.
- 📊 **Score tracking**: Keep track of your wins and losses with an in-game score display.

---

## 🛠️ Requirements
- **Python 3.x**
- **Pygame**

Install Pygame using pip:
```bash
pip install pygame
140 changes: 140 additions & 0 deletions Game_Development/PingPong/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import pygame
import random

# initialize Pygame
pygame.init()

# set up the screen
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")

# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# game variables
player_width, player_height = 10, 100
ball_size = 20
player_speed = 10
ball_speed = 5
player_score = 0
opponent_score = 0

# font
font = pygame.font.Font(None, 48)

# paddle positions
player_pos = [50, HEIGHT // 2 - player_height // 2]
opponent_pos = [WIDTH - 50 - player_width, HEIGHT // 2 - player_height // 2]

# ball position and velocity
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_vel = [ball_speed, ball_speed]

clock = pygame.time.Clock()


def draw_text(text, color, x, y):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=(x, y))
screen.blit(text_surface, text_rect)


def draw_paddle(pos):
pygame.draw.rect(screen, WHITE, (pos[0], pos[1], player_width, player_height))


def draw_ball(pos):
pygame.draw.circle(screen, WHITE, pos, ball_size)


def move_paddle(pos, direction):
pos[1] += direction * player_speed
if pos[1] < 0:
pos[1] = 0
elif pos[1] > HEIGHT - player_height:
pos[1] = HEIGHT - player_height


def move_ball(pos, vel):
pos[0] += vel[0]
pos[1] += vel[1]


def check_collision(ball_pos, ball_vel, player_pos, opponent_pos):
if ball_pos[1] <= ball_size or ball_pos[1] >= HEIGHT - ball_size:
ball_vel[1] = -ball_vel[1]

if ball_pos[0] <= player_pos[0] + player_width and \
player_pos[1] <= ball_pos[1] <= player_pos[1] + player_height and \
ball_vel[0] < 0:
ball_vel[0] = -ball_vel[0]

if ball_pos[0] >= opponent_pos[0] - ball_size and \
opponent_pos[1] <= ball_pos[1] <= opponent_pos[1] + player_height and \
ball_vel[0] > 0:
ball_vel[0] = -ball_vel[0]


def reset_ball():
global ball_pos, ball_vel
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_vel[0] = random.choice([-ball_speed, ball_speed])
ball_vel[1] = random.choice([-ball_speed, ball_speed])


def move_opponent(opponent_pos, ball_pos):
if opponent_pos[1] + player_height // 2 < ball_pos[1]:
opponent_pos[1] += player_speed
elif opponent_pos[1] + player_height // 2 > ball_pos[1]:
opponent_pos[1] -= player_speed


def main():
global player_score, opponent_score

running = True

while running:
screen.fill(BLACK)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
move_paddle(player_pos, -1)
if keys[pygame.K_DOWN]:
move_paddle(player_pos, 1)

move_opponent(opponent_pos, ball_pos)

move_ball(ball_pos, ball_vel)

check_collision(ball_pos, ball_vel, player_pos, opponent_pos)

if ball_pos[0] <= 0:
opponent_score += 1
reset_ball()
elif ball_pos[0] >= WIDTH:
player_score += 1
reset_ball()

draw_paddle(player_pos)
draw_paddle(opponent_pos)
draw_ball(ball_pos)

draw_text(str(player_score), WHITE, WIDTH // 4, 50)
draw_text(str(opponent_score), WHITE, WIDTH * 3 // 4, 50)

pygame.display.flip()
clock.tick(60)

pygame.quit()
quit()


if __name__ == "__main__":
main()

0 comments on commit ce04787

Please sign in to comment.