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

0

如果我是你,我会制定一个游戏模式。你会发现问自己这样的问题:

真正重要的实体是什么?

在您的情况下,正如您所做的那样,我会考虑 Character、Enemy、Room 和 Map,在适当的时候继承,例如 Character-> Hero 和 Enemy,以及 Room 中的几种类型的房间作为浴室、走廊......

如果我是您,请考虑使用数据结构来表示地图。例如,如果您正在考虑进行文字游戏冒险,您可以将不同的房间视为游戏中的不同状态。如果你在浴室里,你可能会被敌人攻击,如果你在卧室里,你可以恢复你的生命值(生命),所以这些地方可以被认为是不同的状态。

例如,您将一个数组用于对所有不同的房间(状态)进行分组

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

以及您可以考虑的其他房间。

(可能有一个更好的例子,更有效等等,所以这个例子是为了帮助你在遇到问题时不要放弃。

根据这个例子,如果你使用一个数组,你可以为每个房间分配一个值(等于数组中的每个位置)

此外,您需要知道英雄的位置,因此您可以使用 rand() 为其分配一个随机值。您可以阅读以下链接以获取更多信息:

随机文档 python

堆栈溢出答案

最后,您还会发现比较英雄的位置很有用,该位置之前会与您的阵列或房间的每个位置都有一个随机分配的值

在这种情况下,您可以使用 if... elif.. elif... 来比较这些值并根据您的英雄所在的房间做一些事情。

我希望这个答案对你有用。如果您对我的回答有任何疑问,请告诉我。干杯

于 2015-12-06T17:36:04.670 回答