python

Now that you’ve created the framework for a game, complete with different maps and scenes, enemies, items, and a currency system, we can start applying the techniques we’ve learned along the way to expand our game experience. In this activity, you’ll be adding three new components to add gameplay value to your game.
Step 1: Make a Change of Scenery

First, create at least one new scene for your game. This could be an alternative pathway, a secret room, or anything else you might come up with! Be sure to give the room a textual introduction when the player enters, and be sure they are able to navigate to and from the room freely using player input.
Step 2: A New Foe Has Appeared!

Within one of your new rooms, create a new enemy or obstacle to challenge the player in some way. The script for your enemy should provide a sort of gameplay loop, either in the form of answering/solving a puzzle or attacking an enemy to lower their stamina. Once the enemy has been defeated, the player should be rewarded with a random amount of currency, as we designed in our last activity.
Step 3: Create Lore

Finally, we’ll navigate to our library. Add a new user input option to allow them to read/collect a new book item. This one won’t be used to defeat an enemy, though; rather, it will be used to communicate lore, or storyline, to the player. Once the player picks up the book, a line of text should display a passage from the new, fictional book revealing lore about the game’s location or history. Perhaps there will be a warning of the dangers that lie ahead!

1 answer

Certainly! Let's create a simple text-based game framework where we can add a new scene, a new enemy, and a new book for lore. Below is a complete example of how to implement these features in Python.

Game Framework

import random

class Game:
    def __init__(self):
        self.scenes = {
            'entrance': Scene("You stand at the entrance of a dark cave. Paths diverge in multiple directions."),
            'secret_room': Scene("You stumble upon a secret room filled with ancient treasures and a mysterious foe."),
        }
        self.current_scene = self.scenes['entrance']
        self.currency = 0

    def start(self):
        print("Welcome to the Adventure Game!")
        while True:
            print(self.current_scene.description)
            command = input("What would you like to do? (navigate, attack, check currency, read book): ").strip().lower()
            if command == 'navigate':
                self.navigate()
            elif command == 'attack' and self.current_scene.enemy:
                self.current_scene.enemy.attack_player()
            elif command == 'check currency':
                print(f"You have {self.currency} gold.")
            elif command == 'read book':
                self.read_book()
            else:
                print("Invalid command.\n")

    def navigate(self):
        if self.current_scene == self.scenes['entrance']:
            self.current_scene = self.scenes['secret_room']
        elif self.current_scene == self.scenes['secret_room']:
            self.current_scene = self.scenes['entrance']
    
    def read_book(self):
        if self.current_scene == self.scenes['secret_room']:
            print("You found an old book titled 'Legends of the Abyss'.")
            print("Passage: 'Beware, brave traveler. The shadows hold secrets, and the edge of darkness is where the nightmares awaken.'")
        else:
            print("There are no books to read here.")

class Scene:
    def __init__(self, description):
        self.description = description
        self.enemy = None

    def set_enemy(self, enemy):
        self.enemy = enemy

class Enemy:
    def __init__(self, name, health, damage):
        self.name = name
        self.health = health
        self.damage = damage

    def attack_player(self):
        if self.health > 0:
            print(f"A wild {self.name} appears! It has {self.health} health.")
            action = input("Do you wish to attack? (yes/no): ").strip().lower()
            if action == 'yes':
                damage_dealt = random.randint(1, 5)  # Player damage
                self.health -= damage_dealt
                print(f"You dealt {damage_dealt} damage to {self.name}!")
                if self.health <= 0:
                    self.defeated()
                else:
                    self.counter_attack()
            else:
                print("You choose to flee!")

    def counter_attack(self):
        print(f"{self.name} attacks you for {self.damage} damage!")
        self.health = 0  # For simplicity, we'll just remove the enemy once attacked.

    def defeated(self):
        reward = random.randint(10, 50)
        print(f"You have defeated the {self.name}! You earn {reward} gold.")
        game.currency += reward
        self.health = 0  # Mark the enemy as defeated

# Setting up the game
game = Game()

# Create a new enemy for the secret room
enemy = Enemy("Shadow Beast", 10, 2)
game.scenes['secret_room'].set_enemy(enemy)

# Start the game
game.start()

Breakdown of the Code:

  1. Game Class: This manages the overall game state, including scenes and player currency.

  2. Scene Class: Represents different locations in the game, holding descriptions and any enemies present in the scene.

  3. Enemy Class: Represents an enemy with health, damage capabilities, and methods to handle combat mechanics.

  4. Navigating Scenes: The player can navigate back and forth between the entrance and the secret room.

  5. Combat System: When the player decides to attack an enemy, they can deal damage to it based on a random value. If the enemy is defeated, the player receives a random reward in currency.

  6. Lore Book: The player can access a fictional book in the secret room, which provides lore about the game setting.

Feel free to expand upon these mechanics and flesh out the scenes, characters, and more complex combat systems!