-->

Python Text Adventure Script

Text adventures are an excellent way to build your Python coding skills and have some fun at the same time. This example that we created will start you on the path to making a classic text adventure; where it will end is up to you.

Python Text Adventure Script
Python Text Adventure Script


Python Text Adventure Script


Python Text Adventure Script


ADVENTURE.PY

The Adventure game uses just the time module to begin with, creating pauses between print functions. There’s a help system in place to expand upon, as well as the story itself.

  1. import time
  2. print(“\n” * 200)
  3. print(“>>>>>>>>>>Awesome Adventure<<<<<<<<<<\n”)
  4. print(“\n” * 3)
  5. time.sleep(3)
  6. print(“\nA long time ago, a warrior strode forth from
  7. the frozen north.”)
  8. time.sleep(1)
  9. print(“Does this warrior have a name?”)
  10. name=input(“> “)
  11. print(name, “the barbarian, sword in hand and looking
  12. for adventure!”)
  13. time.sleep(1)
  14. print(“However, evil is lurking nearby....”)
  15. time.sleep(1)
  16. print(“A pair of bulbous eyes regards the hero...”)
  17. time.sleep(1)
  18. print(“Will”, name, “prevail, and win great fortune...”)
  19. time.sleep(1)
  20. print(“Or die by the hands of great evil...?”)
  21. time.sleep(1)
  22. print(“\n” *3)
  23. print(“Only time will tell...”)
  24. time.sleep(1)
  25. print(‘...’)
  26. time.sleep(1)
  27. print(‘...’)
  28. time.sleep(1)
  29. print(‘...’)
  30. time.sleep(1)
  31. print(‘...’)
  32. time.sleep(5)
  33.  
  34. print(“\n” *200)
  35.  
  36. print(‘’’ You find yourself at a small inn. There’s
  37.     little gold in your purse but your sword is sharp,
  38.     and you’re ready for adventure.
  39.     With you are three other customers.
  40.     A ragged looking man, and a pair of dangerous
  41.     looking guards.’’’)
  42. def start():
  43.     print(“\n ----------”)
  44.     print(“Do you approach the...”)
  45.     print(“\n”)
  46.     print(“1. Ragged looking man”)
  47.     print(“2. Dangerous looking guards”)
  48.  
  49.     cmdlist=[“1”, “2”]
  50.  
  51.     cmd=getcmd(cmdlist)
  52.  
  53.     if cmd == “1”:
  54.         ragged()
  55.     elif cmd == “2”:
  56.         guards()
  57. def ragged():
  58.     print(“\n” * 200)
  59.     print(‘’’You walk up to the ragged looking man and
  60.     greet him.
  61.     He smiles a toothless grin and, with a strange
  62.     accent, says.
  63.     “Buy me a cup of wine, and I’ll tell you of
  64.     great treasure...’’’)
  65.     time.sleep(2)
  66. def guards():
  67.     print(“\n” *200)
  68.     print(‘’’You walk up to the dangerous looking guards
  69.     and greet them.
  70.     The guards look up from their drinks and
  71.     snarl at you.
  72.     “What do you want, barbarian?” One guard reaches
  73.     for the hilt of his sword...’’’)
  74.  
  75.     time.sleep(2)
  76. def getcmd(cmdlist):
  77.     cmd = input(name+”>”)
  78.     if cmd in cmdlist:
  79.         return cmd
  80.     elif cmd == “help”:
  81.         print(“\nEnter your choices as detailed in
  82.         the game.”)
  83.         print(“or enter ‘quit’ to leave the game”)
  84.         return getcmd(cmdlist)
  85.     elif cmd == “quit”:
  86.         print(“\n-----------”)
  87.         time.sleep(1)
  88.     print(“Sadly you return to your homeland without
  89.         fame or fortune...”)
  90.         time.sleep(5)
  91.             exit()
  92.     if _ _ name _ _ ==”_ _ main _ _”:
  93.             start()

Adventure Time

This, as you can see, is just the beginning of the adventure and takes up a fair few lines of code. When you expand it, and weave the story along, you’ll find that you can repeat certain instances such as a chance meeting with an enemy or the like.

We’ve created each of the two encounters as a defined set of functions, along with a list of possible choices under the cmdlist list, and cmd variable, of which is also a defined function. Expanding on this is quite easy, just map out each encounter and choice and create a defined function around it.

Providing the user doesn’t enter quit into the adventure, they can keep playing. There’s also room in the adventure for a set of variables designed for combat, luck, health, endurance and even an inventory or amount of gold earned. Each successful combat situation can reduce the main character’s health but increase their combat skills or endurance. Plus, they could loot the body and gain gold, or earn gold through quests.

Finally, how about introducing the random module. This will enable you to include an element of chance in the game. For example, in combat, when you strike an enemy you will do a random amount of damage as will they. You could even work out the maths behind improving the chance of a better hit based on your or your opponent’s combat skills, current health, strength and endurance.

You could create a game of dice in the inn, to see if you win or lose gold (again, improve the chances of winning by working out your luck factor into the equation).

Needless to say, your text adventure can grow exponentially and prove to be a work of wonder. Good luck, and have fun with your adventure.

Python Text Adventure Script


0 Response to "Python Text Adventure Script"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel