Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Ballon-flight/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Balloon Flight 🎈

A simple, endless side-scrolling arcade game built with Python and [Pygame Zero](https://pygame-zero.readthedocs.io/). Navigate your hot air balloon through a world of obstacles, avoid the birds and buildings, and compete for the high score!

## 🎮 Game Features

* **Endless Gameplay:** The game continues as long as you survive.
* **Gravity Physics:** Click to fly up, release to fall.
* **Randomized Obstacles:** Dodge birds flying at different heights, and avoid houses and trees on the ground.
* **High Score System:** Scores are automatically saved to a local file.
* **Restart Function:** Quickly restart after a crash without closing the window.

## 🛠️ Prerequisites

To run this game, you need:

1. **Python 3.x** installed on your system.
2. **Pygame Zero** library.

## 📦 Installation

1. **Clone the repository** (or download the files):
```bash
git clone https://github.com/yourusername/balloon-flight.git
cd balloon-flight
```

2. **Install dependencies**:
```bash
pip install pgzero
```
*or using the requirements file:*
```bash
pip install -r requirements.txt
```

3. **Asset Setup**:
Pygame Zero requires images to be placed in an `images/` directory. Ensure your project structure looks like this:

```text
balloon-flight/
├── game.py
├── high-scores.txt
├── README.md
├── requirements.txt
└── images/
├── background.png
├── balloon.png
├── bird-up.png
├── bird-down.png
├── house.png
└── tree.png
```

> **Note:** You will need to provide your own image assets (PNG format) with the names listed above inside the `images/` folder.

## 🚀 How to Run

Execute the game using the `pgzrun` command:

```bash
pgzrun game.py
```

## 🕹️ Controls

* **Left Mouse Click (Hold):** Fly Up ⬆️
* **Release Mouse:** Fall Down (Gravity) ⬇️
* **Objective:** Survive as long as possible and avoid hitting obstacles or the edges of the screen.

## 🏆 High Scores

The game tracks your top scores locally in the `high-scores.txt` file. When you crash, the top scores are displayed on the screen.

## 📝 License

This project is open source. Feel free to modify and improve it!
182 changes: 182 additions & 0 deletions Ballon-flight/balloon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import pgzrun
from random import randint

# Screen size
WIDTH = 800
HEIGHT = 600

# Balloon
balloon = Actor("balloon")
balloon.pos = 400, 300

# Obstacles
bird = Actor("bird-up")
bird.pos = randint(800, 1600), randint(10, 200)

house = Actor("house")
house.pos = randint(800, 1600), 460

tree = Actor("tree")
tree.pos = randint(800, 1600), 450

# Restart button (appears when game over)
restart_btn = Rect((350, 350), (100, 40))

# Game variables
bird_up = True
up = False
game_over = False
score = 0
number_of_updates = 0
scores = []


# Step 8: Update High Scores
def update_high_scores():
global score, scores
filename = "high-scores.txt"
scores = []

with open(filename, "r") as file:
line = file.readline()
high_scores = line.split()
for high_score in high_scores:
if score > int(high_score):
scores.append(str(score) + " ")
score = int(high_score)
else:
scores.append(str(high_score) + " ")

with open(filename, "w") as file:
for high_score in scores:
file.write(high_score)


# Step 9: Display High Scores
def display_high_scores():
screen.draw.text("HIGH SCORES", (340, 150), color="black")
y = 175
position = 1
for high_score in scores:
screen.draw.text(
f"Position {position}: {high_score}",
(350, y),
color="black",
)
y += 25
position += 1

# Restart button
screen.draw.filled_rect(restart_btn, "lightblue")
screen.draw.text("Restart", (restart_btn.x + 15,
restart_btn.y + 10), color="black")


# Step 10: Draw function
def draw():
screen.blit("background", (0, 0))
if not game_over:
balloon.draw()
bird.draw()
house.draw()
tree.draw()
screen.draw.text(f"Score: {score}", (700, 5))
else:
display_high_scores()


# Step 11: Mouse control
def on_mouse_down(pos):
global up
if not game_over:
up = True
balloon.y -= 50
else:
# Check if restart clicked
if restart_btn.collidepoint(pos):
restart_game()


def on_mouse_up():
global up
up = False


# Step 12: Bird flapping
def flap():
global bird_up
if bird_up:
bird.image = "bird-down"
bird_up = False
else:
bird.image = "bird-up"
bird_up = True


# Step 13: Update loop
def update():
global game_over, score, number_of_updates

if not game_over:
# Balloon gravity
if not up:
balloon.y += 1

# Bird movement
if bird.x > 0:
bird.x -= 4
if number_of_updates == 9:
flap()
number_of_updates = 0
else:
number_of_updates += 1
else:
bird.x = randint(800, 1600)
bird.y = randint(10, 200)
score += 1
number_of_updates = 0

# House movement
if house.right > 0:
house.x -= 2
else:
house.x = randint(800, 1600)
score += 1

# Tree movement
if tree.right > 0:
tree.x -= 2
else:
tree.x = randint(800, 1600)
score += 1

# Check for collision or out of bounds
if balloon.top < 0 or balloon.bottom > 560:
game_over = True
update_high_scores()

if (
balloon.collidepoint(bird.x, bird.y)
or balloon.collidepoint(house.x, house.y)
or balloon.collidepoint(tree.x, tree.y)
):
game_over = True
update_high_scores()


# Restart Game Function
def restart_game():
global game_over, score, up, balloon, bird, house, tree, number_of_updates

score = 0
up = False
game_over = False
number_of_updates = 0

balloon.pos = (400, 300)
bird.pos = (randint(800, 1600), randint(10, 200))
house.pos = (randint(800, 1600), 460)
tree.pos = (randint(800, 1600), 450)


pgzrun.go()
1 change: 1 addition & 0 deletions Ballon-flight/high-scores.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4 1 0
Binary file added Ballon-flight/images/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Ballon-flight/images/balloon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Ballon-flight/images/bird-down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Ballon-flight/images/bird-up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Ballon-flight/images/house.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Ballon-flight/images/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.