diff --git a/Ballon-flight/README.md b/Ballon-flight/README.md new file mode 100644 index 00000000000..8cffceca5ee --- /dev/null +++ b/Ballon-flight/README.md @@ -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! \ No newline at end of file diff --git a/Ballon-flight/balloon.py b/Ballon-flight/balloon.py new file mode 100644 index 00000000000..83357d2ac56 --- /dev/null +++ b/Ballon-flight/balloon.py @@ -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() diff --git a/Ballon-flight/high-scores.txt b/Ballon-flight/high-scores.txt new file mode 100644 index 00000000000..2a87e4bebd3 --- /dev/null +++ b/Ballon-flight/high-scores.txt @@ -0,0 +1 @@ +4 1 0 \ No newline at end of file diff --git a/Ballon-flight/images/background.png b/Ballon-flight/images/background.png new file mode 100644 index 00000000000..ab0b7c2b698 Binary files /dev/null and b/Ballon-flight/images/background.png differ diff --git a/Ballon-flight/images/balloon.png b/Ballon-flight/images/balloon.png new file mode 100644 index 00000000000..e80b1c86257 Binary files /dev/null and b/Ballon-flight/images/balloon.png differ diff --git a/Ballon-flight/images/bird-down.png b/Ballon-flight/images/bird-down.png new file mode 100644 index 00000000000..54e82dff716 Binary files /dev/null and b/Ballon-flight/images/bird-down.png differ diff --git a/Ballon-flight/images/bird-up.png b/Ballon-flight/images/bird-up.png new file mode 100644 index 00000000000..155797c8318 Binary files /dev/null and b/Ballon-flight/images/bird-up.png differ diff --git a/Ballon-flight/images/house.png b/Ballon-flight/images/house.png new file mode 100644 index 00000000000..4740fce73da Binary files /dev/null and b/Ballon-flight/images/house.png differ diff --git a/Ballon-flight/images/tree.png b/Ballon-flight/images/tree.png new file mode 100644 index 00000000000..cc2c1d2cffe Binary files /dev/null and b/Ballon-flight/images/tree.png differ