0

Through some educational materials I've been tasked to use the below structure (the classes) for a text adventure game and am required to add a simple combat system for battle between a hero and an enemy.

Currently I am able to have an enemy created in each room and move between the start room(corridor) to the bathroom and back, but at this point I'm stuck. I can't determine where I should be creating my 'hero' or how I'd communicate the changes I'd need to make to the health attributes etc.

If I could structure the code in another way, I would be able to complete the game, but as it is there is a gap in my understanding of how to enable various sections of code communicate with each other.

Thanks,

Dave

# text based adventure game

import random
import time
from sys import exit

class Game(object):

    def __init__(self, room_map):
        self.room_map = room_map

    def play(self):
        current_room = self.room_map.opening_room()

        while True:
            next_room_name = current_room.enter()
            current_room = self.room_map.next_room(next_room_name)


class Character(object):
    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack


class Hero(Character):
    def __init__(self, name):
        super(Hero, self).__init__(name, 10, 2)

    def __str__(self):
        rep = "You, " + self.name + ", have " + str(self.health) + " health and " + \
              str(self.attack) + " attack."

        return rep



class Enemy(Character):

    ENEMIES = ["Troll", "Witch", "Ogre", "Jeremy Corbyn"]

    def __init__(self):
        super(Enemy, self).__init__(random.choice(self.ENEMIES),
                                    random.randint(4, 6), random.randint(2, 4))

    def __str__(self):
        rep = "The " + self.name + " has " + str(self.health) + \
        " health, and " + str(self.attack) + " attack."

        return rep




class Room(object):
    def __init__(self):
        self.commands = ["yes", "no"]
        self.rooms = ["\'corridor\'", "\'bathroom\'", "\'bedroom\'"]
        self.enemy = Enemy()


    def command_list(self):
        print("Commands: ", ", ".join(self.commands))


    def enter_room_question(self):
            print("Which room would you like to enter?")
            print("Rooms:", ", ".join(self.rooms))

    def leave_room_question(self):
        print("Do you want to leave this room?")
        print("Commands:", ", ".join(self.commands))





class Bathroom(Room):
    def enter(self):
        print("You enter the bathroom. But, wait! There is an", \
              self.enemy.name, "!")
        print(self.enemy)

        print("You are in the bathroom. Need to take a dump? ")
        self.command_list()
        response = input("> ")

        while response not in self.commands:
            print("Sorry I didn't recognise that answer")
            print("You are in the bathroom. Need to take a dump?")
            self.command_list()
            response = input("> ")

        if response == "yes":
            print("Not while I'm here!")
            return "death"

        elif response == "no":
            print("Good.")
            self.leave_room_question()
            response = input("> ")

            if response == "yes":
                return "corridor"
            else:
                return "death"



class Bedroom(Room):
    def enter(self):
        pass


class Landing(Room):
    def enter(self):
        pass

class Corridor(Room):
    def enter(self):
        print("You are standing in the corridor. There are two rooms available to enter.")
        self.enter_room_question()
        response = input("> ")
        if response == "corridor":
            print("You're already here silly.")
        else:
            return response


class Death(Room):

    QUIPS = ["Off to the man in sky. You are dead",
             "You died, no-one cried.",
             "Lolz. You're dead!"]
    def enter(self):
        time.sleep(1)
        print(random.choice(Death.QUIPS))
        exit()



class Map(object):

    ROOMS = {"corridor": Corridor(),
             "bathroom": Bathroom(),
             "death": Death(),
             "landing": Landing(),
             "bedroom": Bedroom()}

    def __init__(self, start_room):
        self.start_room = start_room
        self.hero = hero


    def next_room(self, room_name):
        return Map.ROOMS.get(room_name)


    def opening_room(self):
        return self.next_room(self.start_room)

a_hero = Hero("Dave")
a_map = Map("corridor")
a_game = Game(a_map, a_hero)
a_game.play()
Michael Johnson
  • 470
  • 6
  • 18

1 Answers1

0

If I were you, I would set out a game schema. You could find out asking yourself questions like this:

What are the really important entities?

In your case, as you have done, I would consider Character, Enemy, Room and Map, inheriting when it would be appropiate, like Character-> Hero and Enemy, and several types of room from Room as Bathroom, Corridor, ...

If I were you a consider using a data structure to represent the Map. For example, if you are considering do a text game adventure, you could think in different rooms as different states in your game. If you are in the bathroom, you could be attacked by an enemy and if you are in the bedroom, you can retrieve your hitpoints (life) so these places can be thought as different states.

As an example, you would an array for group all your different rooms (states)

rooms = ["bedroom", "bathroom", "corridor", "kitchen", "living_room"] 

and other rooms that you can think about.

(there is probably a better example, more efficient and so on, so this example is to help you about not giving up when you gets stuck about an problem.

According to this example, if you use an array, you can assign a value to each room (equal to each position in the array)

Moreover, you will need to know the hero's position, so you could assign a random value to it using rand(). You can read links below for more information:

random docs python

stackoverflow answer

At last, you also would find useful to compare the hero's position, which would have a random assigned value previously with every position of your array or rooms

In this cases, you could use a if... elif.. elif... to compare those values and do something according to the room where your hero would be.

I hope this answer will be useful for you. Let me know if you have any doubt about my answer. Cheers

Community
  • 1
  • 1
Rafael VC
  • 406
  • 5
  • 12
  • Hey. Thanks for the reply. I'll look into that first chance I get. – Michael Johnson Dec 06 '15 at 18:25
  • Hi again. I'm sorry but I still don't understand. I can't seem to get past how my hero and enemy can interact when the Room classes use the return value of the their enter() to move to the next room. How do I work with that? Spawning an enemy in each room, the way I have, is that correct? If so, where do I go from here? Cheers, Dave – Michael Johnson Dec 07 '15 at 13:22