Text Adventure – Using Control Flow In Python

Python wascreated by Guido van Rossum and first released in 1991.Python, as a programming platform, has gained a huge popularity within a short span of time because of its flexibility and the user-friendly interface. The software can be deployed easily for developing statistical models and machine learning algorithms

 
Text Adventure- Using Control Flow In Python
 

In fact, due to the advent of AI and ML, Python has a language has had a certain kind of rebirth as far as industrial use is concerned. Today, however, the focus is going to be on a particular section of the language, namely the control flow to create a basic system in Python.

 

Using one of the most basic control structures and a few neat loops in Python one can easily develop a simple text game. A text game basically consists of two basic things:

 

  1. User Input – Where the user is asked to give an input.
  2. Choices – Now based on these inputs the narrative of the story takes on different paths

 

Requirements:

 

  • Platform : Jupyter, ipython, cmd etc…
  • Working knowledge of basic text functions, loops and control flow structures

A short note on control flow and input statements before we begin:

    • Conditional flow: In any Language, the control flow are equivalent to the IF-Then-Else statements. An advantage of writing any loops or flow statements in python is that it is devoid of any brackets that confuses users most of the times. A basic indentation is all that is required to write the codes.

 

The Basic syntax of a conditional flow is:

 

if <condition>:

<Do something>

elif <condition>:

<Do something else>

elsse:

<Do something else>

 

  • User input statement: Input statement asks the user for an input value based on which a conditional statement might be executed.

 

import time
 import random
# game function
 def game():
 print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
 print ("Welcome to the cavern of secrets!")
 print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
 time.sleep(3)
 print ("You enter a dark cavern out of curiosity. 
It is dark and you can only make out a small stick on the floor.")
 ch1 = str(input("Do you take it? [y/n]: "))
# STICK TAKEN
 if ch1 in ['y', 'Y', 'Yes', 'YES', 'yes']:
 print("You have taken the stick!")
 time.sleep(2)
 stick = 1
 # STICK NOT TAKEN
 else:
 print("You did not take the stick")
 stick = 0
print ("As you proceed further into the cave, you hear a moaning")
 ch2 = str(input("Do you approach the object? [y/n]"))
# APPROACH Zombie
 if ch2 in ['y', 'Y', 'Yes', 'YES', 'yes']:
 print ("You approach the object...")
 time.sleep(2)
 print ("As you draw closer, you realize that the moaning sound is coming from a...")
 time.sleep(1)
 print ("ZOMBIE!")
 ch3 = str(input("Do you try to fight it? [Y/N]"))
# FIGHT Zombie
 if ch3 in ['y', 'Y', 'Yes', 'YES', 'yes']:
 # WITH STICK
 if stick == 1:
 print ("You only have a stick to fight with!")
 print ("You quickly jab the Zombie in it's eye and gain an advantage")
 time.sleep(2)
 print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
 print (" Fighting... ")
 print (" YOU MUST HIT ABOVE 5 TO KILL THE Zombie ")
 print ("IF THE Zombie HITS HIGHER THAN YOU, YOU WILL DIE")
 print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(2)
if edmg1 > fdmg1:
 print ("The Zombie has dealt more damage than you!")
 complete = 0
 return complete
elif fdmg1 < 5: print ("You didn't do enough damage to kill the Zombie, 
but you manage to escape") 
complete = 1 
return complete
 
else: print ("You killed the Zombie!") 
complete = 1 
return complete 

# WITHOUT STICK
 
else:
 print ("You don't have anything to fight with!") 
time.sleep(2) 
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
print (" Fighting... ") 
print (" YOU MUST HIT ABOVE A 5 TO KILL THE Zombie ") 
print ("IF THE Zombie HITS HIGHER THAN YOU, YOU WILL DIE") 
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 
time.sleep(2)
 fdmg1 = int(random.randint(1, 8)) 
edmg1 = int(random.randint(1, 5)) 
print ("you hit ", fdmg1) 
print ("the Zombie hits ", edmg1) 
time.sleep(2) 
if edmg1 > fdmg1:
 print ("The Zombie has dealt more damage than you!")
 complete = 0
 return complete
elif fdmg1 < 5:
 print ("You didn't do enough damage to kill the Zombie, but you manage to escape")
 complete = 1
 return complete
else:
 print ("You killed the Zombie!")
 complete = 1
 return complete
 #DON'T FIGHT Zombie
 else:
 print ("You choose not to fight the Zombie.")
 time.sleep(1)
 print ("As you turn away, it ambushes you and impales you with it's fangs!!!")
 complete = 0
 return complete
# DON'T APPROACH Zombie
 else:
 print ("You turn away from the moaning sound, and attempt to leave the cave...")
 time.sleep(1)
 print ("But suddenly something breaks your neck from behind. You fall 
to the ground and die.")
 time.sleep(2)
 complete = 0
 return complete

 
…and that’s it. We now have successfully created our own text adventure game with some simple condition flows and loops. Just think how much we can achieve if we modify this existing code a bit more. Who knows, it might become a worthwhile text game to be uploaded on the play store.

 

For excellent R programming courses in Pune, drop by DexLab Analytics. Their R programming certification in Pune is one of the best in India, so shoot for the best!

 

Interested in a career in Data Analyst?

To learn more about Machine Learning Using Python and Spark – click here.
To learn more about Data Analyst with Advanced excel course – click here.
To learn more about Data Analyst with SAS Course – click here.
To learn more about Data Analyst with R Course – click here.
To learn more about Big Data Course – click here.

September 25, 2017 11:00 am Published by , , , , , , , , ,

, , , , , , , ,

Comments are closed here.

...

Call us to know more

×