0

いくつかの教材を通じて、テキスト アドベンチャー ゲームに以下の構造 (クラス) を使用するように依頼され、ヒーローと敵との戦いのための単純な戦闘システムを追加する必要があります。

現在、各部屋に敵を作成し、開始部屋 (廊下) とバスルームの間を移動して戻ることができますが、この時点で立ち往生しています。「ヒーロー」を作成する場所や、健康属性などに必要な変更をどのように伝えるかを判断できません.

コードを別の方法で構成できれば、ゲームを完成させることができますが、コードのさまざまなセクションが相互に通信できるようにする方法について、理解にギャップがあります。

ありがとう、

デイブ

# 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()
4

1 に答える 1