【发布时间】:2017-07-06 17:11:46
【问题描述】:
我是一个相当新的程序员,几年来一直在苦苦挣扎,因为我曾短暂接触过几种语言。我试图最终牢牢掌握 Python,为此我目前的项目是设计一个简单的基于文本的游戏,例如“Zork”。
我已经成功地为每个房间使用函数制作了一个非常简单的版本,但是这根本无法推广。在我解释我目前做得不好的地方之前,我将链接代码。现在你可以看到,这是一种非常低效的房间建造方式,因为它需要大量的代码,除了这个确切的游戏,我不能用它来做任何事情。
首先我创建了一个列表用作玩家库存
inventory = []
def addToInventory(item):
inventory.append(item)
这是我用来制造武器的类,也是我的麻烦开始的地方。该类工作正常,我什至可以成功地将它的实例添加到我的库存列表中。
class Weapon:
def __init__(self, name, damage, speed, weight):
self.name = name
self.damage = damage
self.speed = speed
self.weight = weight
sword = Weapon("Sword", 7, 5, 5)
knife = Weapon("Knife", 5, 7, 3)
stick = Weapon("Stick", 2, 3, 3)
这是我尝试设计的用于创建房间的类。它尚未实施。现在我面临的问题是无法将 Weapon 类的实例添加到 Roominv 的参数中。例如,我不能使用武器类的剑实例作为房间内容的参数,它告诉我剑是未定义的。
Entrance = Room("You are at an entrance", "N", "Search the ground", sword)
现在我对这个项目的目标真的是学习,所以我不想要任何代码,但我是否走在正确的道路上?如何将武器类的某些实例与某些房间联系起来?我应该改用字典来建造房间吗?如果我应该使用字典,我必须为每个房间创建一个单独的字典还是嵌套字典?
我已经搜索了很多这些问题的答案,因为我确信有人问过类似的问题,但我似乎无法找到一个直接的答案。
class Room:
def __init__(self, description, exits, actions, Roominv): #Runs every time a new room is created
self.description = description
self.exits = exits
self.actions = actions
self.Roominv = Roominv
def GAMEOVER():
print("Oh no, it appears you have died. Press 1 to restart")
choice = input(">>> ")
if choice == "1":
intro()
def introstrt():
print("WELCOME TO FLUBBO'S MAGIC FOREST! HAVE YOU EVER WONDERED WHAT IT WAS LIKE TO")
print("PLAY A VIDEO GAME THAT MORE OR LESS RELIES ENTIRELY ON IMAGINATION?")
print("THEN LOOK NO FURTHER!")
print("BEFORE YOU BEGIN, BE FOREWARNED THAT THIS ADVENTURE WILL BE VIOLENT, CRUDE,")
print("AND ALSO VERY POOR QUALITY OVERALL, BECAUSE CLEARLY")
print("THE DEVELOPER IS A TALENTLESS HACK WITH NO BUSINESS BEHIND A KEYBOARD.")
print("AND NOW, FOR YOUR PLEASURE, FLUBBO'S MAGIC FOREST!!")
print(" A A A A ooo")
print(" vvv vvv vvv vvv ooooo")
print("vvvvv vvvvv vvvvv vvvvv ooo")
print(" H H H H")
print(" A A A A")
print(" vvv vvv vvv vvv | O __")
print("vvvvv vvvvv vvvvv vvvvv +-|-(__) ")
print(" H H H H / \ ")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("PRESS 1 TO BEGIN")
choice = input(">>> ")
if choice == "1":
intro()
else:
print("I can't understand that command")
intro()
def intro():
global inventory
print("You are in a forest, you can hear wildlife all around you. Your sword lies at your feet. There seems to be a clearing in the distance.")
print("You can:")
print("1.Look for sticks")
print("2. Do a backflip")
print("3.Attempt to befriend wildlife")
print("4.Go to Clearing ")
print("5.View Intro")
choice = input(">>> ")
if choice == "1":
print("You don't find any sticks. This is a poorly designed forest.")
intro()
elif choice == "2":
print("Sweet backflip, what now?")
intro()
elif choice == "3":
print("Attempting to befriend what you believe to be a cuddly critter in a nearby bush, you are surprised as a bear appears to your left and mauls you to death.")
print("GAME OVER.")
GAMEOVER()
elif choice == "4":
print("You move towards the clearing.")
clearing()
elif choice == "Pick up sword":
addToInventory(sword)
print("Your bag contains", inventory)
intro()
elif choice == "5":
introstrt()
else:
print("I can't understand that command")
intro()
def clearing():
print("You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating a bright white flower in the center of the clearing. In the distance a harp can be heard.")
print("You can:")
print("1.Pick the flower")
print("2.Go towards the harp")
print("3.Curse the sun whilst masturbating vigorously")
print("4.Go back")
choice = input(">>> ")
if choice == "1":
print("You pick the white flower and put it in your bag. You notice that it glows slightly.")
clearingnofl()
elif choice == "2":
print("You can't tell which direction the music is coming from!.")
clearing()
elif choice == "3":
print("CURSE YOU FIERY BALL OF SATAN!!! You below with fury as you cum all over the clearing.")
print("The sun is deeply offended and goes away. It is dark now.")
clearingdrk()
elif choice == "4":
print("You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.")
intro()
else:
print("I can't understand that command")
clearing()
def clearingnofl():
print("You are in a clearing surrounded by forest. Sunlight is streaming in, illuminating the center of the clearing. In the distance a harp can be heard.")
print("You can:")
print("1.Go towards the harp")
print("2.Curse the sun whilst masturbating vigorously")
print("3.Go back")
choice = input(">>> ")
if choice == "1":
print("You can't tell which direction the music is coming from!.")
clearingnofl()
elif choice == "2":
print("CURSE YOU FIERY BALL OF SATAN!!! You below with fury as you cum all over the clearing.")
print("The sun is deeply offended and goes away. It is dark now.")
print("You notice the flower glowing brightly in your bag.")
clearingnofldrk()
elif choice == "3":
print("You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.")
intronofl()
else:
print("I can't understand that command")
clearingnofl()
def clearingdrk():
print("You are in a clearing surrounded by forest. Moonlight is streaming in, illuminating a bright white flower in the center of the clearing. In the distance a harp can be heard.")
print("You can:")
print("1.Pick the flower")
print("2.Go towards the harp")
print("3.Apologize to the sun, promise that you'll change and things will be like they used to be.")
print("4.Go back")
choice = input(">>> ")
if choice == "1":
print("You pick the white flower and put it in your bag. You notice that it glows brightly in the dark.")
clearingnofldrk()
elif choice == "2":
print("You can't tell which direction the music is coming from!.")
clearingdrk()
elif choice == "3":
print("The sun reluctantly forgives you, it is sunny again.")
clearing()
elif choice == "4":
print("You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.")
introdrk()
else:
print("I can't understand that command")
clearingdrk()
def clearingnofldrk():
print("You are in a clearing surrounded by forest. Moonlight is streaming in, illuminating the center of the clearing. In the distance a harp can be heard.")
print("You can:")
print("1.Go towards the harp")
print("2.Apologize to the sun, promise that you'll change and things will be like they used to be.")
print("3.Go back")
choice = input(">>> ")
if choice == "1":
print("You can't tell which direction the music is coming from!.")
clearingnofldrk()
elif choice == "2":
print("The sun reluctantly forgives you, it is sunny again.")
clearingnofl()
elif choice == "3":
print("You are in a forest, you can hear wildlife all around you. It is dark. There seems to be a clearing in the distance.")
intronofldrk()
else:
print("I can't understand that command")
clearingnofldrk()
def intronofldrk():
print("You are in a forest, you can hear wildlife all around you. It is dark. There seems to be a clearing in the distance.")
print("You can:")
print("1.Look for sticks")
print("2.Do a backflip")
print("3.Attempt to befriend wildlife")
print("4.Go to Clearing ")
choice = input(">>> ")
if choice == "1":
print("You don't find any sticks. This is a poorly designed forest AND it's dark now.")
intronofldrk()
elif choice == "2":
print("Sweet backflip, what now?")
intronofldrk()
elif choice == "3":
print("Attempting to befriend what you believe to be a cuddly critter in a nearby bush, you are surprised as a bear appears to your left and mauls you to death. GAME OVER.")
elif choice == "4":
print("You move towards the clearing.")
clearingnofldrk()
else:
print("I can't understand that command")
intronofldrk()
def introdrk():
print("You are in a forest, you can hear wildlife all around you. It is dark. There seems to be a clearing in the distance.")
print("You can:")
print("1.Look for sticks")
print("2.Do a backflip")
print("3.Attempt to befriend wildlife")
print("4.Go to Clearing ")
choice = input(">>> ")
if choice == "1":
print("You don't find any sticks. This is a poorly designed forest AND it's dark now.")
introdrk()
elif choice == "2":
print("Sweet backflip, what now?")
introdrk()
elif choice == "3":
print("Attempting to befriend what you believe to be a cuddly critter in a nearby bush, you are surprised as a bear appears to your left and mauls you to death. GAME OVER.")
elif choice == "4":
print("You move towards the clearing.")
clearingdrk()
else:
print("I can't understand that command")
introdrk()
def intronofl():
print("You are in a forest, you can hear wildlife all around you. There seems to be a clearing in the distance.")
print("You can:")
print("1.Look for sticks")
print("2.Do a backflip")
print("3.Attempt to befriend wildlife")
print("4.Go to Clearing ")
choice = input(">>> ")
if choice == "1":
print("You don't find any sticks. This is a poorly designed forest.")
intronofl()
elif choice == "2":
print("Sweet backflip, what now?")
intronofl()
elif choice == "3":
print("Attempting to befriend what you believe to be a cuddly critter in a nearby bush, you are surprised as a bear appears to your left and mauls you to death. GAME OVER.")
elif choice == "4":
print("You move towards the clearing.")
clearingnofl()
else:
print("I can't understand that command")
intronofl()
introstrt()
【问题讨论】:
-
是什么阻止你将参数传递给
Room类? -
它告诉我参数是未定义的,例如 Entrance = Room("This is an entry", "N", "Search the ground", Sword) 它会告诉我剑是未定义。
-
@Schrodinger'sStat 如果您要使用全局变量,请确保在第一次使用它们之前定义它们。所以
sword = Weapon("Sword", 7, 5, 5)必须在Entrance = Room("This is an entrance", "N", "Search the ground", sword)之前
标签: python python-3.x class dictionary game-engine