Python - 30

Python - 30: Build a Mini Adventure Game

This final project combines classes, user input, conditions, random events, and object data in one small adventure.

Your challenge: Create a hero, choose a location, trigger an event, update the hero's health or gold, and display the final stats.

✦ Python Editor
▶ Output

Store the hero's data

The Hero class keeps the name, health, and gold together inside one object.

The show_stats() method displays the hero's current values.

Create random events

Inside the forest, random.randint(1, 2) chooses one of two possible events.

One event can increase hero.gold, while another can reduce hero.health.

Update object values

You can change data stored inside the object:

hero.gold = hero.gold + 50

or:

hero.health = hero.health - 20

Keep health under control

If the village heals the hero, check that health does not go above 100.

Try it: Add another location with its own reward or danger.

Good Python habit: Keep related game data inside an object instead of creating separate variables for every hero property.

👀 Show Example Solution
import random

class Hero:

    def __init__(self, name):
        self.name = name
        self.health = 100
        self.gold = 0

    def show_stats(self):
        print("===================")
        print("Hero:", self.name)
        print("Health:", self.health)
        print("Gold:", self.gold)
        print("===================")

hero_name = input("Enter your hero name: ")

hero = Hero(hero_name)

print("Welcome to the Adventure!")

hero.show_stats()

choice = input(
    "Choose a path: forest, cave, or village: "
)

if choice == "forest":

    print("You entered the forest.")

    event = random.randint(1, 2)

    if event == 1:
        print("You found hidden treasure!")
        hero.gold = hero.gold + 50

    else:
        print("A wild creature attacked!")
        hero.health = hero.health - 20

elif choice == "cave":

    print("You entered the cave.")
    print("You discovered glowing crystals!")

    hero.gold = hero.gold + 30

elif choice == "village":

    print("You arrived at the village.")
    print("The villagers healed you.")

    hero.health = hero.health + 20

    if hero.health > 100:
        hero.health = 100

else:

    print("You got lost on the road.")

print()

print("Adventure Complete!")

hero.show_stats()

This final project combines many ideas from the course: classes, objects, input, conditions, random numbers, methods, and changing stored values.