Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CHAPTER 19 — Practical Project: Building a Mini Python Game
#1
Chapter 19 — Practical Project: Building a Mini Python Game
Now that you understand the fundamentals, it’s time to build a real project — a playable Python game mixing logic, maths, randomness, and your new programming skills.

This project gives beginners a HUGE confidence boost and teaches real-world thinking.

---

19.1 Game Overview

You’ll build:

THE DUNGEON ESCAPE GAME

Features:

• choose a hero 
• fight random enemies 
• use attacks or healing 
• manage health 
• win or lose based on decisions 
• random variation each playthrough 

Uses:

• variables 
• input 
• loops 
• if statements 
• functions 
• lists 
• random module 
• simple OOP (optional upgrade) 

---

19.2 Step 1 — Setup

Create a new file:

dungeon_escape.py

Add:

Code:
import random

print("=== DUNGEON ESCAPE ===")

---

19.3 Step 2 — Player Stats

Code:
player = {
    "name": "",
    "health": 100,
    "potions": 3
}

player["name"] = input("Enter your hero name: ")

---

19.4 Step 3 — Enemy List

Code:
enemies = ["Goblin", "Skeleton", "Bandit", "Wolf"]

---

19.5 Step 4 — Attack Function

Code:
def attack():
    return random.randint(8, 15)

---

19.6 Step 5 — Enemy Attack

Code:
def enemy_attack():
    return random.randint(5, 12)

---

19.7 Step 6 — Main Game Loop

Code:
while player["health"] > 0:
    enemy = random.choice(enemies)
    enemy_health = random.randint(20, 35)

    print(f"\nA wild {enemy} appears with {enemy_health} HP!")

    while enemy_health > 0 and player["health"] > 0:
        print(f"\n{player['name']} HP: {player['health']}")
        print(f"{enemy} HP: {enemy_health}")
        print("1. Attack")
        print("2. Use Potion")
        print("3. Run")

        choice = input("> ")

        if choice == "1":
            damage = attack()
            enemy_health -= damage
            print(f"You hit the {enemy} for {damage} damage!")
        elif choice == "2":
            if player["potions"] > 0:
                player["potions"] -= 1
                player["health"] += 20
                print("You drink a potion and regain 20 HP!")
            else:
                print("No potions left!")
                continue
        elif choice == "3":
            print(f"You escaped from the {enemy}!")
            break
        else:
            print("Invalid choice.")
            continue

        if enemy_health > 0:
            damage = enemy_attack()
            player["health"] -= damage
            print(f"The {enemy} hits you for {damage}!")

---

19.8 Step 7 — Ending the Game

After the loop:

Code:
if player["health"] <= 0:
    print("\nYou have fallen in the dungeon...")
else:
    print("\nYou escaped the dungeon safely!")

---

19.9 Full Working Game (Copy/Paste Ready)

Code:
import random

print("=== DUNGEON ESCAPE ===")

player = {
    "name": "",
    "health": 100,
    "potions": 3
}

player["name"] = input("Enter your hero name: ")

enemies = ["Goblin", "Skeleton", "Bandit", "Wolf"]

def attack():
    return random.randint(8, 15)

def enemy_attack():
    return random.randint(5, 12)

while player["health"] > 0:
    enemy = random.choice(enemies)
    enemy_health = random.randint(20, 35)

    print(f"\nA wild {enemy} appears with {enemy_health} HP!")

    while enemy_health > 0 and player["health"] > 0:
        print(f"\n{player['name']} HP: {player['health']}")
        print(f"{enemy} HP: {enemy_health}")
        print("1. Attack")
        print("2. Use Potion")
        print("3. Run")

        choice = input("> ")

        if choice == "1":
            damage = attack()
            enemy_health -= damage
            print(f"You hit the {enemy} for {damage} damage!")
        elif choice == "2":
            if player["potions"] > 0:
                player["potions"] -= 1
                player["health"] += 20
                print("You drink a potion and regain 20 HP!")
            else:
                print("No potions left!")
                continue
        elif choice == "3":
            print(f"You escaped from the {enemy}!")
            break
        else:
            print("Invalid choice.")
            continue

        if enemy_health > 0:
            damage = enemy_attack()
            player["health"] -= damage
            print(f"The {enemy} hits you for {damage}!")

if player["health"] <= 0:
    print("\nYou have fallen in the dungeon...")
else:
    print("\nYou escaped the dungeon safely!")

---

19.10 Challenge — Advanced Version

Upgrade the game by adding:

• armour items 
• critical hits 
• special enemy types 
• inventory system 
• save/load game using JSON 

BONUS: 
Convert the entire game into an OOP version using a Player and Enemy class.

---

19.11 Chapter Summary

• you created a full playable game 
• applied loops, lists, functions, randomness 
• used a main game loop 
• managed health, items, decisions 
• learned real-world game structure 

Next:
Chapter 20 — Final Project: A Complete Python Application

The last chapter ties EVERYTHING together into a polished, multi-file, multi-feature application worthy of your course finale.

---

Written and Compiled by Lee Johnston — Founder of The Lumin Archive


Forum Jump:


Users browsing this thread: 1 Guest(s)