19
Command line game
The following is a project of an excellent online course I am taking at codingnomads.com. I am a civil engineer who is a beginner in programing and now I am learning Python. I am in the first course, and as part of one of my assignments I had to do a Command-Line game. The basic instructions of the game is:
• Ask the player for their name.
• Display a message that greets them and introduces them to the game world.
• Present them with a choice between two doors.
• If they choose the left door, they'll see an empty room.
• If they choose the right door, then they encounter a dragon.
• In both cases, they have the option to return to the previous room or interact further.
• When in the seemingly empty room, they can choose to look around. If they do so, they will find a sword. They can choose to take it or leave it.
• When encountering the dragon, they have the choice to fight it.
• If they have the sword from the other room, then they will be able to defeat it and win the game.
• If they don't have the sword, then they will be eaten by the dragon and lose the game.
So first I propose this code, which was functional but susceptible to errors:
name=input("Hello! thanks for playing, what is your name? ")
sword=False
print(f"Hello {name}! Welcome to the game!")
• Display a message that greets them and introduces them to the game world.
• Present them with a choice between two doors.
• If they choose the left door, they'll see an empty room.
• If they choose the right door, then they encounter a dragon.
• In both cases, they have the option to return to the previous room or interact further.
• When in the seemingly empty room, they can choose to look around. If they do so, they will find a sword. They can choose to take it or leave it.
• When encountering the dragon, they have the choice to fight it.
• If they have the sword from the other room, then they will be able to defeat it and win the game.
• If they don't have the sword, then they will be eaten by the dragon and lose the game.
So first I propose this code, which was functional but susceptible to errors:
name=input("Hello! thanks for playing, what is your name? ")
sword=False
print(f"Hello {name}! Welcome to the game!")
Win_Game=False
while Win_Game==False:
choice=input("Please choose between this 2 doors, Left or Right? ")
choice=input("Please choose between this 2 doors, Left or Right? ")
if choice=="Left":
print(f"Al right {name}! you are in an empty room now! ")
choice_left=input("You want to look around or go back? Yes(look around) or No(Go back)? ")
if choice_left=="Yes":
choice_sword=input("Hey, there is a sword there! Do you want to take it before you go back?? Yes or No? ")
if choice_sword=="Yes":
sword=True
print("Alright, nothing can stop you now, you have a good sword")
elif choice_sword=="No":
print("Alright then! Walk without the sword ")
if choice=="Right":
print(f"My God {name}, there is a dragon here! ")
choice_right=input("You want to kill the dragon or go back? Yes or No? ")
if choice_right=="Yes":
if sword:
print(f"Hell Yeah {name}! You kill the dragon! ")
Win_Game=True
break
else:
print("Oh No! You are eaten by the dragon, you lost the game :/ ")
break
if Win_Game:
print("Hey you won the game!")
print("Hey you won the game!")
quit()
So with the advice of my tutor and friend, he pointed out 3 points I can strength:
{
name=input("Hello! thanks for playing, what is your name? ")
sword=False
print(f"Hello {name}! Welcome to the game!")
name=input("Hello! thanks for playing, what is your name? ")
sword=False
print(f"Hello {name}! Welcome to the game!")
Win_Game=False
while Win_Game==False:
choice=str(input("Please choose between this 2 doors, left or right? ")).lower()
choice=str(input("Please choose between this 2 doors, left or right? ")).lower()
if choice=="left":
print(f"Al right {name}! you are in an empty room now! ")
choice_left=input("You want to look around or go back? Yes(look around) or No(Go back)? ").lower()
if choice_left=="yes":
choice_sword=input("Hey, there is a sword there! Do you want to take it before you go back?? Yes or No? ").lower()
if choice_sword=="yes":
sword=True
print("Alright, nothing can stop you now, you have a good sword")
elif choice_sword=="no":
print("Alright then! Walk without the sword ")
else:
print("Well it seems that you are not sure of what to do, you better go back to the 2 doors")
continue
elif choice_left=="no":
continue
else:
print("Well it seems that you are not sure of what to do, you better go back to the 2 doors")
if choice=="right":
print(f"My God {name}, there is a dragon here! ")
choice_right=input("You want to kill the dragon or go back? Yes or No? ").lower()
if choice_right=="yes":
if sword:
print(f"Hell Yeah {name}! You kill the dragon! ")
Win_Game=True
#break
else:
print("Oh No! You are eaten by the dragon, you lost the game :/ ")
quit() #new position to exit the loop and the game
#break
elif choice_right=="no":
print("Alright run back then!")
continue
else:
print("Well it seems that you are not sure of what to do, you better go back to the 2 doors")
print("Hey you won the game!")
}
Making these changes, now the game still works, but can receive answers different than yes or no, and it will not collapse. Thanks to the people that helped me with this small projects of my course, and I am looking forward to keep learning.
19