通过一些教育材料,我的任务是使用以下结构(课程)进行文字冒险游戏,我需要为英雄和敌人之间的战斗添加一个简单的战斗系统。
目前,我可以在每个房间创建一个敌人,并在起始房间(走廊)和浴室之间移动,然后返回,但此时我被卡住了。我无法确定我应该在哪里创建我的“英雄”,或者我如何传达我需要对健康属性进行的更改等。
如果我可以用另一种方式构建代码,我将能够完成游戏,但是因为我对如何使各个代码段相互通信的理解存在差距。
谢谢,
戴夫
# 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()
答案 0 :(得分:0)
如果我是你,我会制定游戏架构。你可以发现自己问这样的问题:
真正重要的实体是什么?
在你的情况下,正如你所做的那样,我会考虑角色,敌人,房间和地图,继承适当的时候,比如Character->英雄和敌人,以及房间作为浴室,走廊,...的几种类型的房间。
如果我是你,请考虑使用数据结构来表示地图。例如,如果您正在考虑进行文字游戏冒险,您可以将不同的房间视为游戏中的不同状态。如果你在浴室里,你可能会受到敌人的攻击,如果你在卧室里,你可以找回你的生命值(生命),这样这些地方可以被认为是不同的状态。
例如,您将为所有不同的房间(州)组成一个数组
rooms = ["bedroom", "bathroom", "corridor", "kitchen", "living_room"]
以及您可以考虑的其他房间。
(可能有一个更好的例子,效率更高等等,所以这个例子是为了帮助你在遇到问题时不放弃。
根据这个例子,如果你使用一个数组,你可以为每个房间分配一个值(等于数组中的每个位置)
此外,您需要知道英雄的位置,因此您可以使用rand()为其指定一个随机值。您可以阅读以下链接以获取更多信息:
最后,你会发现有用的比较英雄的位置,这个位置以前会随机分配给你的阵列或房间的每个位置
在这种情况下,您可以使用if ... elif .. elif ...来比较这些值并根据您的英雄所在的房间做一些事情。
我希望这个答案对你有用。 如果您对我的答案有任何疑问,请告诉我。 干杯