python 地精文字冒险

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 地精文字冒险相关的知识,希望对你有一定的参考价值。

################
# GAME OBJECTS #
################

class GameObject:
  class_name = ""
  desc = ""
  objects = {}

  def __init__(self, name):
    self.name = name
    GameObject.objects[self.class_name] = self

  def get_desc(self):
    return self.class_name + "\n" + self.desc

class Goblin(GameObject):
  class_name = "goblin"
  desc = "A foul creature"

goblin = Goblin("Gobbly")

class Goblin(GameObject):
  def __init__(self, name):
    self.class_name = "goblin"
    self.health = 3
    self._desc = " A foul creature"
    super().__init__(name)

  @property
  def desc(self):
    if self.health >=3:
      return self._desc
    elif self.health == 2:
      health_line = "It has a wound on its knee."
    elif self.health == 1:
      health_line = "Its left arm has been cut off!"
    elif self.health <= 0:
      health_line = "It is dead."
    return self._desc + "\n" + health_line

  @desc.setter
  def desc(self, value):
    self._desc = value


#########
# VERBS #
#########

def say(noun):
  return 'You said "{}"'.format(noun)
  

def examine(noun):
  if noun in GameObject.objects:
    return GameObject.objects[noun].get_desc()
  else:
    return "There is no {} here.".format(noun)

def hit(noun):
  if noun in GameObject.objects:
    thing = GameObject.objects[noun]
    if type(thing) == Goblin:
      thing.health = thing.health - 1
      if thing.health <= 0:
        msg = "You killed the goblin!"
      else: 
        msg = "You hit the {}".format(thing.class_name)
  else:
    msg ="There is no {} here.".format(noun) 
  return msg
  
verb_dict = {
  "say": say,
  "examine": examine,
  "hit": hit
}


#########
# INPUT #
#########

def get_input():
  command = input(": ").split()
  verb_word = command[0]
  if verb_word in verb_dict:
    verb = verb_dict[verb_word]
  else:
    print("Unknown verb {}". format(verb_word))
    return

  if len(command) >= 2:
    noun_word = command[1]
    print (verb(noun_word))
  else:
    print(verb("nothing"))

while True:
  get_input()

以上是关于python 地精文字冒险的主要内容,如果未能解决你的问题,请参考以下文章

swift 文字冒险游戏

html5文字冒险类游戏《归途》从零开始

迪杰斯特拉--记录路径

[Sdoi2010]地精部落

1925: [Sdoi2010]地精部落

Codevs 1523 地精部落