import random
import time
import sys
inventory = []
print "~~~~~~~~~~~~~~~~~~~~~~~~"
print " Welcome "
print "~~~~~~~~~~~~~~~~~~~~~~~~"
print "You find youself waking up in a cold dark room."
print ""
print "You look around and find 3 door ways in front of you: One to the left, one to the right and one in the middle and a chest."
print ""
print "You have to make a choice. Type the direction of the room to enter the room or type 'Search' to search the chest."
print ""
choice = raw_input("Please make your selection:")
if choice == ("Search") or ("search"):
print "You slowly walk towards the chest and reach in and find:"
x = random.randint(1,2)
if x == 1:
print "You find a sword!"
inventory.append("Sword")
if x == 2:
print "You find an axe!"
inventory.append("Axe")
print "You search some more..."
time.sleep(3)
y = random.randint(1,2)
if y == 1:
print "You also found a torch."
inventory.append("Torch")
if y == 2:
print "You also found a shield."
inventory.append("Shield")
choice = raw_input("Please pick a direction you'd like to go:")
if choice == ("Left") or ("left"):
time.sleep(2)
print "You enter the room to the left"
string = "Torch"
if string in inventory:
print "You take your torch and look around."
time.sleep(2)
print"You notice a glimmer in the corner of the room."
time.sleep(2)
print"As you step closer you find that the glimmer starts to grow."
time.sleep(2)
print "As you come feet away, your torch reveales a giant spider!"
string = "Torch"
if string in inventory:
time.sleep(1)
print "You take out your torch!"
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" Fighting... ")
print (" YOU MUST HIT ABOVE A 5 TO KILL THE SPIDER ")
print ("IF THE SPIDER HITS HIGHER THAN YOU, YOU WILL DIE")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(2)
PDmg = int(random.randint(3, 10))
EDmg = int(random.randint(1, 5))
print ("You hit a", PDmg)
print ("The spider hits a", EDmg)
time.sleep(2)
#This part calculates who won
if EDmg > PDmg:
print ("The spider has done more damage and killed you!")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
elif PDmg < 5:
print ("You managed to hit injure the spider, but not kill it.")
print ("Luckily, you escaped to the main room")
choice = raw_input("Please select a room to enter:")
elif PDmg > 5:
print "You killed the spider, congratulations!"
print "You have been rewarded with armour!"
print "You return to the main room!"
spider = 1
inventory.append("Armour")
choice = raw_input("Please select a room to enter:")
else:
print "You cannot see a thing! You squint to try and find any resources"
time.sleep(2)
print "You see a glimmer come closer and closer, what could it be!"
time.sleep(2)
print "It appears to be a giant spider!"
string = "Shield"
if string in inventory:
time.sleep(1)
print "You take out your trusty shield!"
time.sleep(2)
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" Fighting... ")
print (" YOU MUST HIT ABOVE A 5 TO KILL THE SPIDER ")
print ("IF THE SPIDER HITS HIGHER THAN YOU, YOU WILL DIE")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(2)
PDmg = int(random.randint(3, 10))
EDmg = int(random.randint(1, 4))
print ("You hit a", PDmg)
print ("The spider hits a", EDmg)
time.sleep(2)
#This part calculates who won
if EDmg > PDmg:
print ("The spider has done more damage and killed you!")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
elif PDmg < 5:
print ("You managed to hit injure the spider, but not kill it.")
print ("Luckily, you escaped to the main room")
choice = raw_input("Please select a room to enter:")
elif PDmg > 5:
print "You killed the spider, congratulations!"
print "You have been rewarded with armour!"
print "You return to the main room!"
spider = 1
inventory.append("Armour")
choice = raw_input("Please select a room to enter:")
else:
print "You don't have anything in your inventory!"
print "You attempt to fight the spider..."
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" Fighting... ")
print (" YOU MUST HIT ABOVE A 5 TO KILL THE SPIDER ")
print ("IF THE SPIDER HITS HIGHER THAN YOU, YOU WILL DIE")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(2)
PDmg = int(random.randint(1, 2))
EDmg = int(random.randint(1, 50))
print ("You hit a", PDmg)
print ("The spider hits a", EDmg)
if EDmg > PDmg:
print ("The spider has done more damage and killed you!")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
if PDmg > EDmg:
print "Somehow, you managed to tap the spider and run away alive!"
choice = raw_input("Please select a room to enter:")
if choice == ("Right") or ("right"):
time.sleep(1)
print "You walk into the Right room."
time.sleep(1)
print "You find 3 bridges, and a sign:"
print "You may either leave empty handed, or pick the right path for a bounty."
Bchoice = input("Please select which bridge to cross: 1 , 2 , or , 3:")
if Bchoice == 2:
string = "Armour"
if string in inventory:
print "Crossing..."
time.sleep(2)
print "The bridge has not crumbled, you see a glimmering chest."
print "You find an extra sharp blade, it seems to be for cutting stone."
inventory.append("SharpBlade")
choice = raw_input("Please select a room:")
else:
print "Crossing..."
time.sleep(2)
print "The bridge has crumbled, you have fallen to your death."
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
else:
print "Crossing..."
time.sleep(2)
print "The bridge has crumbled, you have fallen to your death."
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
if choice == ("Straight") or ("straight"):
print "You enter the middle room."
time.sleep(1)
print "You find a gargoyle towering 50 feet above you!"
print "You have 3 options, climb, fight or flee."
Fchoice = raw_input("Please decide to Climb, Fight or Flee.")
while Fchoice != "Flee":
while Fchoice == ("Climb") , ("climb"):
print "You have climbed the gargoyle and prepare to cut off the head."
string = "SharpBlade"
if string in inventory:
time.sleep(2)
print "Your extra sharp blade cuts off the head and the gargoyle falls!"
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" YOU WON!!!!!!!!! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit("You have beat the puzzle RPG text based adventure game!")
else:
print "You fail to cut off the head."
print "The gargoyle throws you off and kills you."
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
if Fchoice == "Fight":
print "You un-shealth your sword, only to find the gargoyles hand ontop of your body."
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
print "You try to flee, but the Gargoyle stops you by kicking you against the wall!"
print "You un-shealth your sword, only to find the gargoyles hand ontop of your body."
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" GAME IS OVER! ")
print (" QUITTING PROGRAM ")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(5)
sys.exit()
我正在创建一个基于RPG的文字游戏,只需要一点点战斗。有一个特定的顺序来获取东西和进入房间。
基本上,我知道重复代码很糟糕,但我不确定我必须采取的步骤。我很确定我需要使用函数和def main()。任何帮助表示赞赏!
答案 0 :(得分:0)
你想要比你已经更多地分离你的逻辑。当你这样做时,重复一些事情不是一个好主意就会变得更加清晰。
所以我们从一开始就开始。
您的游戏很容易属于MVC架构。在MVC架构中,您将逻辑划分为模型,视图和控制器。
您的广告资源是一个模型,每次您在屏幕上打印时都应该调用一个视图,控制游戏的所有逻辑都将成为您的控制器。
这样的良好布局是从Game
类开始。这个班级将是你的控制者。接下来,创建一个Interface
类。在Interface
中创建方法,每次打印到屏幕或提示用户时都会调用这些方法。在您的游戏类中,您可以拥有运行应用程序的逻辑,提示用户,在您想要打印到屏幕时调用视图,并在必要时调用库存以进行更新。
以下是一些资源: http://www.tutorialspoint.com/python/python_classes_objects.htm http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/functions.html
这看起来像是一个类似于你想要做的事情的教程: http://letstalkdata.com/2014/08/how-to-write-a-text-adventure-in-python/
欢迎编程!祝你好运!
答案 1 :(得分:0)
我不打算输出整件事,但我认为你会更好地处理&#34;字符&#34;与对象和跟踪惠普,库存等。看看蜘蛛斗争以及对象如何为您完成所有工作。大多数场景设置都可以循环播放,但为了让我感觉不像写出来,我会保持原样。
import random, time, sys
welcome_message = '''
~~~~~~~~~~~~~~~~~~~~~~~~
\tWelcome
~~~~~~~~~~~~~~~~~~~~~~~~
You find youself waking up in a cold dark room.
You look around and find 3 door ways in front of you: One to the left, one to the right and one in the middle and a chest.
You have to make a choice. Type the direction of the room to enter the room or type 'Search' to search the chest.
'''
class Character(object):
def __init__(self, name):
self.name = name
self.hp = 100
self.is_dead = False
def take_damage(self, damage):
self.damage = damage
print '{0} has taken {1} damage!'.format(self, self.damage)
self.hp = self.hp - self.damage
if self.hp <= 0:
self.is_dead = True
def is_alive(self):
if self.is_dead:
print '{0} has died!'.format(self)
return False
return True
def damage_taken(self):
return self.damage
def __repr__(self):
return self.name
class Spider(Character):
def __init__(self, name):
super(Spider, self).__init__(name)
class Adventurer(Character):
def __init__(self, name):
super(Adventurer, self).__init__(name)
self.inventory = []
def has_inventory(self, item):
if item.lower() in self.inventory:
return True
return False
def is_alive(self):
if self.is_dead:
print 'You have died.'
sys.exit()
return True
def show_inventory(self):
return self.inventory
def add_inventory(self, item):
if item.lower() not in self.inventory:
self.inventory.append(item.lower())
adventurer = Adventurer('Tom')
print welcome_message
user_choice = raw_input('Please make your selection:').lower()
if user_choice == 'search':
print 'You slowly walk towards the chest and reach in and find:'
weapon = random.choice(['Axe', 'Sword'])
print 'You find a {0}'.format(weapon)
adventurer.add_to_inventory(weapon)
...
...
user_choice = raw_input('Please pick a direction you\'d like to go:').lower()
if user_choice == 'left':
if adventurer.has_inventory('Torch'):
...
...
spider = Spider('Spider')
while spider.is_alive() and adventurer.is_alive():
spider.take_damage(random.randint(3, 10))
adventurer.take_damage(random.randint(1, 5))