In: Computer Science
Can someone add comments on this explaining the code and also I could not figure out how to code the finalbattle to make it where if u use a lowercase "a" for the final attack you lose.
from sys import exit
print ("The city needs your help, you must save them!")
print ("What is your name?")
name=input("Enter your name:")
print ("Ok, are you ready to begin?")
def decisions():
decision = input("yes or no?\n")
if decision == "yes":
print ("You made the right decision, may your blade keep you
safe.")
elif decision == "no":
print ("You have let the people down and the city will be
destroyed.")
exit(0)
decisions()
def choosepath():
print ("You must leave the town in order to save the
people.")
print ("You may go either north or south to destroy the enemies
raiding the city.")
print ("The paths to the east and west have been destroyed and it
is up to you to stop this from happening elsewhere.")
path = ''
while path !='north' and path !='south'and path !='east' and path
!='west':
print ("which path would you like to take?")
print ("choose north or south:")
path = input()
if path =="north":
print ("You head north towards the enemy approching the front
gates.")
elif path=="south":
print ("You head south to sneak up on the enemy and attack from
behind.")
choosepath()
print("You arrive at the battle and start planning your
attack!")
print("You spot an enemy and must make a wise decision to start
your fight...")
print("Before picking your target you must choose your
weapon!")
def weapon():
weapon = ''
while weapon != 'sword' and weapon !='axe':
print("Choose your sword or axe!")
weapon = input ()
if weapon == "sword":
print("You chose sword!")
elif weapon =="axe":
print ("You chose axe!")
weapon()
print("Now that you have chosen your weapon it is time to
attack!")
print("You look around for a brief moment...")
print("You start to have doubts, now is your chance to turn
around.")
print("Would you like to turn around and let the people die to save
your own life?")
final_decision = input ("yes or no?\n")
if final_decision == "yes":
print("You have let the people down...")
exit(0)
elif final_decision =="no":
print("You are a brave knight.")
print("Now is your time to strike!")
print("Quickly! the enemy is approaching, it is now or never!")
enemy = 0
a=50
A=50
enemy_approaches = input ("Press a to attack!")
if enemy_approaches=='a' or enemy_approaches =='A':
enemy = enemy + 50
enemy_approaches = input ("The enemy is getting weaker keep
attacking!")
if enemy_approaches=='a' or enemy_approaches =='A':
enemy = enemy + 50
enemy_approaches = input ("Time for the final blow!")
if enemy_approaches=='a' or enemy_approaches =='A':
enemy = enemy+ 50
if enemy >= 150:
print ("Congratulations, you have defeated the enemy!")
def finalbattle():
print("You defeated a soldier now it is time to take out the
leader!")
print("You must be careful great knight, he if very
powerful!")
print("You will need to use power attacks to defeat him!")
print("Use capital A for a power attack.")
print("Use basic attacks until he is stunned, when the time is
right kill him with a powerful blow!")
print("...")
print("NOW! IT IS TIME TO ATTACK!")
finalbattle()
enemy = 0
a=50
A=100
enemy_approaches = input ("Press a to attack!")
if enemy_approaches=='a':
enemy = enemy + 50
enemy_approaches = input ("One more and you will have him
stunned!")
if enemy_approaches=='a':
enemy = enemy + 50
enemy_approaches = input ("You stunned him! Use your powerful
strike")
if enemy_approaches =='A':
enemy = enemy+ 100
if enemy >= 200:
print("You have defeated the boss and saved the city!")
print("YOU WIN!")
CODE for the above game (PYTHON):
from sys import exit #Importing modules
print ("The city needs your help, you must save them!") #This is the output statement visible on the screen
print ("What is your name?")
name=input("Enter your name:") #Prompting the user to enter his/her name
print ("Ok, are you ready to begin?") #Output statement to the screen
def decisions(): #"Decisions" function for taking the decision from the Player(user)
decision = input("yes or no?\n") #Prompting the player to take a decision (yes or no)
if decision == "yes": #If the decision taken by the player is "yes"
print ("You made the right decision, may your blade keep you safe.") #Output this statement to the screen
elif decision == "no": #If the decision taken by the use is "no"
print ("You have let the people down and the city will be destroyed.") #Output this statement to the screen
exit(0) #Exiting the game (program)
decisions() #Calling the "decisions" function
def choosepath(): #"choosepath" is a function for choosing a path by the use
print ("You must leave the town in order to save the people.") #The below 3 statements are outputted to the screen
print ("You may go either north or south to destroy the enemies raiding the city.")
print ("The paths to the east and west have been destroyed and it is up to you to stop this from happening elsewhere.")
path = '' #Inititalising "path" variable as an empty string
while path !='north' and path !='south'and path !='east' and path !='west': #while loop continues until the player enters any path (north or south)
print ("which path would you like to take?")
print ("choose north or south:")
path = input() #Prompting the player to take a path (north or south)
if path =="north": #If the chosen path is "north"
print ("You head north towards the enemy approching the front gates.") #Output this statement
elif path=="south": #If the chosen path is "south"
print ("You head south to sneak up on the enemy and attack from behind.") #Output this statement
choosepath() #Calling the "choospath" function
print("You arrive at the battle and start planning your attack!") #Below 3 lines are outputted to the screen.
print("You spot an enemy and must make a wise decision to start your fight...")
print("Before picking your target you must choose your weapon!")
def weapon(): #"weapon" method for letting the player to choose a weapon ("sword" or "axe")
weapon = '' #Initialising the weapon variable as an empty string
while weapon != 'sword' and weapon !='axe': #While loop continues until the player chooses any weapon ("sword" or "axe")
print("Choose your sword or axe!")
weapon = input () #Prompting the player to choose a weapon
if weapon == "sword": #If the player chooses "sword" as the weapon
print("You chose sword!")
elif weapon =="axe": #If the player chooses "axe" as the weapon
print ("You chose axe!")
weapon() #Calling the weapon function
print("Now that you have chosen your weapon it is time to attack!") #Below 4 lines are outputted to the screen.
print("You look around for a brief moment...")
print("You start to have doubts, now is your chance to turn around.")
print("Would you like to turn around and let the people die to save your own life?")
final_decision = input ("yes or no?\n") #Prompting the player to take the final decision
if final_decision == "yes": #If the final decision is "yes"
print("You have let the people down...") #The game is over....:(
exit(0) #Exiting the program
elif final_decision =="no": #If the final decision is "no"
print("You are a brave knight.") #Outputs the below 3 statements to the screen
print("Now is your time to strike!")
print("Quickly! the enemy is approaching, it is now or never!")
enemy = 0 #Initialising enemy variable (amount of scaring) to 0
a=50 #Initialisng variable a to 50
A=50
enemy_approaches = input ("Press a to attack!") #Prompting the player to attack..
if enemy_approaches=='a' or enemy_approaches =='A': #If the player attacks (i.e., when the player enters "a" or "A")
enemy = enemy + 50 #Increasing the amount of scaring of the enemy to 50
enemy_approaches = input ("The enemy is getting weaker keep attacking!") #Prompting the player to attack again
if enemy_approaches=='a' or enemy_approaches =='A': #If the player attacks:
enemy = enemy + 50 #Again increase the amount of scaring of the enemy..
enemy_approaches = input ("Time for the final blow!") #Right time to give the final blow...
if enemy_approaches=='a' or enemy_approaches =='A': #If the player attacks again:
enemy = enemy+ 50 #Increasing the enemy variable again 50 (amount of scaring)
if enemy >= 150: #If the amount of scaring of the enemy is greater than 150 (i.e.,
# the enemy is scared very much so it will be easy for the player to defeat the enemy..)
print ("Congratulations, you have defeated the enemy!") #Outputting this statement to the screen
def finalbattle(): # "finalbattle" function is fighting with the boss
print("You defeated a soldier now it is time to take out the leader!") #The below 7 lines are outputted to the screen
print("You must be careful great knight, he if very powerful!")
print("You will need to use power attacks to defeat him!")
print("Use capital A for a power attack.")
print("Use basic attacks until he is stunned, when the time is right kill him with a powerful blow!")
print("...")
print("NOW! IT IS TIME TO ATTACK!")
finalbattle() #Calling the "finalbattle" function
enemy = 0 #Initialising enemy variable to 0
a=50
A=100
enemy_approaches = input ("Press a to attack!") #Prompting the player to attack..
if enemy_approaches=='a': #If the player enters "a"
enemy = enemy + 50 #Incrementing the enemy variable
enemy_approaches = input ("One more and you will have him stunned!") #Prompting the player to attack again
if enemy_approaches=='a': #If the player again enter "a"
enemy = enemy + 50 #Incrementing the enemy variable again
enemy_approaches = input ("You stunned him! Use your powerful strike") #Prompting the player to take powerfull strike...
if enemy_approaches == 'a': #If the player enters "a" instead of "A":
print("You have let the people down...") #Boss (ENEMY) defeats the Player(player)
print("YOU LOSE") #Player has lost the game
if enemy_approaches =='A': #If the player enters "A"
enemy = enemy+ 100 #Incrementing the enemy variable
if enemy >= 200: #If the enemy variable is greater than 200:
print("You have defeated the boss and saved the city!") #The Player wins...(i.e., Player has defeated the boss)
print("YOU WIN!") #Player "WINS"
#END OF THE CODE
Above CODE implementation:




Above CODE output:
#Below is the output When the player hits (enters) " A " for the final strike:
#When the player hits " A "
the player "WINS"
# Below is the output when the player hits " a " for the final strike:
# When the player hits (enters) " a " for the final strike the player gets DEFEATED (LOSES )
NOTE:
# Be aware of the indentation while copying the above code into the editor or idle by observing the above uploaded CODE pictures..
#Any questions or clarifications regarding this problem can be asked in the comments section..