Python Game

Mohammed Mahmoud

Web Developer
import time
import random
def time_pause():
time.sleep(2)
def print_pause(message, delay=0):
time.sleep(delay)
print(message)
my_variable = 0
def increment():
global my_variable # Declare the use of the global variable
my_variable += 1 # Increment the variable
print(f"Your score is: {my_variable}")
def decrement():
global my_variable # Declare the use of the global variable
my_variable -= 1 # Decrement the variable
print(f"Your score is: {my_variable}")
def score_win_or_lose():
if my_variable < -3:
print_pause(f"You LOST. GAME OVER. Your score is: {my_variable}",2)
exit()
elif my_variable > 3:
print_pause(f"You WON. GAME OVER. Your score is: {my_variable}",2)
exit()
#this is to restart the game
def restart():
game()
def torestart():
score_win_or_lose()
def validate_with_recursion_call_1():
print("To restart press 1. To exit press 2")
choice = input("Choose one 1/2: ")
if choice == "1":
restart()
elif choice == "2":
print_pause("You LEFT the game.",1)
#to terminate program
exit()
else:
validate_with_recursion_call_1() # recall the function
validate_with_recursion_call_1()
#to validate the input with a loop
#to define this game
def tiger_game():
def tiger1():
print_pause("You entered the underground mysterious place.",2)
print_pause("You have a rusty old dagger in your hand.")
print("You see the tiger.")
while True:
print("To attack it press 1 and to runaway press 2.")
input2 = input()
if input2 =="1":
print_pause("you attacked the tiger.",2)
print_pause("The tiger killed you because the knife broke.",2)
decrement()
torestart()
break
elif input2 == "2":
print_pause("You did not attack the tiger and you ranaway.",2)
print_pause("You did not die.",1.5)
increment()
torestart()
break
else:
print("Invalid input. Pleasetry again.")
def tiger2():
print_pause("You did not enter the underground mysterious place.",2)
print_pause("You did not die.",2)
increment()
torestart()
#this function is to validate the choices
def validate_with_recursion_call():
choice = input("Choose one 1/2: ")
if choice == "1":
tiger1()
elif choice == "2":
tiger2()
else:
validate_with_recursion_call() # recall the function
def enter():
print("To enter this place, press 1. To not enter it, press 2.")
validate_with_recursion_call()
print_pause("You find yourself in a large desert. You were exploring the desert and got lost.",2)
print_pause("Rumor has it that there is a large Tiger inside, terrifying his whole village in this desert.",2)
print("You found a weird stair leading underground.")
time_pause()
#calling function enter
enter()
#defining this game
def lion_game():
def lion_1():
print("You have a spear in your hand.")
print("You suddenly see the mythical lion.")
#this loop handles invalid and valid input
while True:
print_pause("To attack it press 1 and to runaway press 2.",2)
input2 = input()
if input2 =="1":
print_pause("you attacked the lion.",2)
print_pause("You killed the lion and survived You became the hero of the village.",2)
print_pause("You did not die.",2)
increment()
torestart()
break
elif input2 == "2":
print("You did not attack the lion. You tried to runaway, but the lion outpaced you and killed you.")
time_pause()
print("You died.")
decrement()
time_pause()
torestart()
break
#this is when the input is invalid
else:
print("Invalid input. Please try again.")
def lion_2():
print_pause("You did not die.",2)
increment()
torestart()
def enter():
print("To enter this cave, press 1. To not enter it, press 2.")
def validate_with_recursion_call_2():
choice = input("Choose one 1/2: ")
if choice == "1":
print_pause("You entered the mysterious cave.",2)
lion_1()
elif choice == "2":
print("You did not enter the cave and returned to your village.")
lion_2()
else:
validate_with_recursion_call_2() # recall the function
validate_with_recursion_call_2()
print("You find yourself in a large forest. You were exploring the forest and got lost.")
time_pause()
print_pause("Rumor has it that there is an enormous mythcal Lion inside, terrifying your whole village in this forest.",2)
print_pause("You found a weird and creepy cave inside a mountain.",2)
enter() #calling this function
#defining this game
def polar_bear_game():
def polar_1():
print_pause("You have a cold old sword in your hand.",2)
print("You see the Polar Bear.")
while True:
print("To attack it press 1 and to runaway press 2.")
input2 = input()
if input2 =="1":
print_pause("You attacked the Bear.",2)
print("The Bear killed you because the sword broke.")
print_pause("You lost the game.",2)
decrement()
torestart()
#to stop the loop
break
elif input2 == "2":
print_pause("You did not attack the bear and you ranaway.",2)
print("You did not die and you WON the game.")
increment()
#recalling the function
torestart()
break
else:
print("Invalid input. Pleasetry again.")
def polar_2():
print_pause("You did not die. You won this game.",2)
increment()
torestart()
print_pause("You find yourself in a large area of ice. You were exploring the North pole and got lost.",3)
print_pause("There is a large Polar Bear around, terrifying your whole village in the North Pole.",2.5)
print_pause("You found cave inside a large ice mountain.",2)
while True:
print("To continue this journey, press 1. To return to your village, press 2.")
input1 = input()
#to know the user's input
if input1 == "1":
print("You continued your jouney.")
polar_1()
break
elif input1 == "2":
print("You returned to your village.")
polar_2()
break
#to see if the input is valid or not
else:
print("Invalid input. Please try again.")
#this is to choose a game randomly
def game():
numberslist = ["1", "2", "3"]
number = (random.choice(numberslist))
# this is for the polarbeargame
if number == "1":
polar_bear_game()
#this is for the tigergame
elif number == "2":
tiger_game()
#this is for the liongame
elif number == "3":
lion_game()
game()
#tiger_game()
#lion_game()
#polar_bear_game()
Partner With Mohammed
View Services

More Projects by Mohammed