# #------------------------------------------------------------------------# # | Name : Guess the number | # | Purpose : 11DGT | # | Author: Hayden De Rohan | # | Created : 11/03/2024 | # | Finished : | # | Copyright : © Hayden De Rohan 28/02/2024 | # #------------------------------------------------------------------------# # Imports import random import time # Variables User_Name = str Player_Number_2Guess = 0 COMP_Number_2Guess = 0 COMP_Score = 0 Player_Score = 0 # FUNCTIONS def GameScores() : Spacing() global COMP_Score global Player_Score print("{} has a score of ".format(User_Name), "[{}]".format(Player_Score)) print("I has a score of [{}]".format(COMP_Score)) # function for the player to input their number def PlayersNumber() : while True : try : Spacing() UsersNumber = int(input("Give a number between 1 and 1000 -- ")) if UsersNumber < 1 or UsersNumber > 1000 : fade_in_print("A number between 1 and 1000 [{}]".format(User_Name)) else : Spacing() fade_in_print("Good choice [{}] is a good one".format(UsersNumber)) fade_in_print("I'll forget it and try to guess your number now") global Player_Number_2Guess Player_Number_2Guess = UsersNumber break except : fade_in_print("Try an integer [{}]".format(User_Name)) # The computer guessing code - it isn't smart unlucky! def ComputerGuessing(): global Player_Score global COMP_Score global Player_Number_2Guess for i in range(1,11): Sleep() Computer_Guess = random.randint(1,1000) if Computer_Guess != Player_Number_2Guess : if Computer_Guess < Player_Number_2Guess : SmallSpacing() fade_in_print("I guessed [{}], and my value was too low ".format(Computer_Guess)) elif Computer_Guess > Player_Number_2Guess : SmallSpacing() fade_in_print("I guessed [{}], and my value was too high ".format(Computer_Guess)) else: SmallSpacing() fade_in_print("I guessed your number and I gain a point!") COMP_Score += 1 break SmallSpacing() Sleep() Sleep() fade_in_print("Aww, I didn't get it. Now I'll think of a number and you'll have to guess it {}".format(User_Name)) Sleep() # How the computer chooses a number def ComputerChooseNum() : global COMP_Number_2Guess Num = random.randint(1,1000) COMP_Number_2Guess = Num SmallSpacing() ###################################################################################################################### - So I won't forget where this is for debugging #print("The computers number is {} ".format(COMP_Number_2Guess)) fade_in_print("I have choosen my number and you may guess it now [{}]".format(User_Name)) # How the player chooses a number def PlayerGuessing(): global COMP_Number_2Guess global Player_Score global COMP_Score for i in range(1, 11): while True: try: SmallSpacing() Players_Guess = int(input("Your guess -- ")) if Players_Guess < 1 or Players_Guess > 1000: SmallSpacing() fade_in_print("A number between 1 and 1000") else: SmallSpacing() fade_in_print("You guessed {}".format(Players_Guess)) break except: SmallSpacing() fade_in_print("Try an integer") if Players_Guess != COMP_Number_2Guess: if Players_Guess < COMP_Number_2Guess: SmallSpacing() fade_in_print("You guessed [{}], and your value was too low".format(Players_Guess)) elif Players_Guess > COMP_Number_2Guess: SmallSpacing() fade_in_print("You guessed [{}], and your value was too high".format(Players_Guess)) else: SmallSpacing() Sleep() fade_in_print("You guessed my number, you get a point :(") Sleep() Player_Score += 1 break fade_in_print("Your remaining guesses [{}]".format(10 - i)) if i == 10 and Players_Guess != COMP_Number_2Guess: fade_in_print("You ran out of guesses so I get a point :D") fade_in_print("My number was [{}]".format(COMP_Number_2Guess)) COMP_Score += 1 # Asks for a input by user as a name and validates def Name() : while True : Spacing() Player_Name = str(input("What is our users name? -- ")) if Player_Name.isalpha() == True : global User_Name User_Name = Player_Name break else : Spacing() fade_in_print("Plase try a name without spacing or numbers") # Introduction to the game + other stuff before the actual game starts def Intro() : Spacing() fade_in_print("Welcome [{}], lets play a game!".format(User_Name)) Sleep() fade_in_print("We're going to play a game where we'll guess each others numbers ") Sleep() fade_in_print("We'll have 10 Guesses to get the other persons number, getting it within 10 will give them a point") Sleep() fade_in_print("However, I'm not too good so if you DON'T get my number I'll gain a point") Sleep() fade_in_print("We're playing BEST of 3 [{}]".format(User_Name)) Sleep() fade_in_print("You'll choose a random number between 1-1000, and I'll do the same") Sleep() fade_in_print("Good luck [{}]".format(User_Name)) Sleep() # Prints blank lines for a better look def Spacing() : print() print() # Smaller print def SmallSpacing() : print() # Quick access for time.sleep() def Sleep() : time.sleep(0.75) # Function for making text fade in def fade_in_print(text, delay=0.0175): for i in range(len(text)): print(text[i], end='', flush=True) time.sleep(delay) print() # The main function for the game Calls other important functions def MainFunc() : global Player_Score global COMP_Score Name() Intro() while True : PlayersNumber() ComputerGuessing() GameScores() ComputerChooseNum() PlayerGuessing() GameScores() if Player_Score == 2 or COMP_Score == 2: Sleep() SmallSpacing() fade_in_print("Our winner has been decided") break else: Sleep() SmallSpacing() fade_in_print("Now I'll be guessing again") SmallSpacing() # Calls the main function if __name__ == "__main__" : SmallSpacing() fade_in_print(" -- Game Worked :D -- ") SmallSpacing() MainFunc() else : print("Some funky error happened relaunch the program :3")