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(). Любая помощь приветствуется!
положить его в функцию и просто вызовите функцию – SirParselot
Там довольно несколько учебников о написании текста приключения на сети: https://www.google.com/search?q=python+text+adventure Вы можете найти некоторые из них полезными; более общие рекомендации могут привести к хорошим идеям для вашего собственного кода. –
вам нужно узнать о циклах и функциях. ... –