In this guide, we’ll walk you through the steps to create a basic game with Chatgpt 4o using Python. This game will feature difficulty levels, limited guesses, hints, high scores, and a play-again option. By the end of this tutorial, you’ll have a fully functional game and a deeper understanding of Python programming.

Create a basic game with Chatgpt 4o - AI generatered image

Step 1: Setting Up Your Environment

Ensure you have Python installed on your windows or Mac compture. You can download it from the official website. Use a text editor or an Integrated Development Environment (IDE) like Visual Studio Code or PyCharm to write your code.

______________

Step 2: Starting with the Basics

Begin by importing the necessary modules and setting up the main function:

import random
import time

def guess_the_number():
print(“Welcome to ‘Guess the Number’!”)
high_scores = []

______________

Step 3: Adding Difficulty Levels

levels = {
‘easy’: 50,
‘medium’: 100,
‘hard’: 200
}

while True:
difficulty = input(“Choose a difficulty level (easy, medium, hard): “).lower()
if difficulty in levels:
max_number = levels
break
else:
print(“Invalid choice. Please choose ‘easy’, ‘medium’, or ‘hard’.”)

______________

Step 4: Generating the Number and Initializing Variables

Generate a random number within the chosen range and set up variables to keep track of guesses:

print(f”I’m thinking of a number between 1 and {max_number}.”)
number_to_guess = random.randint(1, max_number)
guess = None
number_of_guesses = 0

______________

Step 5: Limiting the Number of Guesses

Define the maximum number of guesses allowed based on the difficulty level:

max_guesses = {
‘easy’: 10,
‘medium’: 7,
‘hard’: 5
}
allowed_guesses = max_guesses
start_time = time.time()

______________

Step 6: Main Game Loop with Hints

Create a loop that continues until the player guesses the number or runs out of guesses. Provide hints when the player has two guesses left:

while guess != number_to_guess and number_of_guesses < allowed_guesses:
guess = input(f”Take a guess (You have {allowed_guesses – number_of_guesses} guesses left): “)

if not guess.isdigit():
print(“Please enter a valid number.”)
continue

guess = int(guess)
number_of_guesses += 1

if guess < number_to_guess:
print(“Your guess is too low.”)
elif guess > number_to_guess:
print(“Your guess is too high.”)
else:
end_time = time.time()
total_time = round(end_time – start_time, 2)
print(f”Good job! You guessed the number in {number_of_guesses} tries and {total_time} seconds.”)

name = input(“Enter your name for the high score list: “)
high_scores.append((name, number_of_guesses, total_time))
high_scores = sorted(high_scores, key=lambda x: (x[1], x[2]))[:5] # Keep top 5 scores
break

if number_of_guesses == allowed_guesses – 2:
hint = “even” if number_to_guess % 2 == 0 else “odd”
print(f”Hint: The number is {hint}.”)

______________

Step 7: Handling Game Over and Displaying High Scores

Inform the player if they run out of guesses and display the high scores:

if guess != number_to_guess:
print(f”Sorry, you’ve used all your guesses. The number was {number_to_guess}.”)print(“\nHigh Scores:”)
for score in high_scores:
print(f”{score[0]} – {score[1]} guesses, {score[2]} seconds”)

______________

Step 8: Adding a Play Again Option

Ask the player if they want to play again:

play_again = input(“Do you want to play again? (yes or no): “).lower()
if play_again != ‘yes’:
break

______________

Step 9: Running the Game

Finally, call the guess_the_number() function when the script is executed:

if __name__ == “__main__”:
guess_the_number()

______________

Full Code

Here’s the complete code for the enhanced “Guess the Number” game:

import random
import time

def guess_the_number():
print(“Welcome to ‘Guess the Number’!”)
high_scores = []

while True:
levels = {
‘easy’: 50,
‘medium’: 100,
‘hard’: 200
}

while True:
difficulty = input(“Choose a difficulty level (easy, medium, hard): “).lower()
if difficulty in levels:
max_number = levels
break
else:
print(“Invalid choice. Please choose ‘easy’, ‘medium’, or ‘hard’.”)

print(f”I’m thinking of a number between 1 and {max_number}.”)
number_to_guess = random.randint(1, max_number)
guess = None
number_of_guesses = 0

max_guesses = {
‘easy’: 10,
‘medium’: 7,
‘hard’: 5
}
allowed_guesses = max_guesses
start_time = time.time()

while guess != number_to_guess and number_of_guesses < allowed_guesses:
guess = input(f”Take a guess (You have {allowed_guesses – number_of_guesses} guesses left): “)

if not guess.isdigit():
print(“Please enter a valid number.”)
continue

guess = int(guess)
number_of_guesses += 1

if guess < number_to_guess:
print(“Your guess is too low.”)
elif guess > number_to_guess:
print(“Your guess is too high.”)
else:
end_time = time.time()
total_time = round(end_time – start_time, 2)
print(f”Good job! You guessed the number in {number_of_guesses} tries and {total_time} seconds.”)

name = input(“Enter your name for the high score list: “)
high_scores.append((name, number_of_guesses, total_time))
high_scores = sorted(high_scores, key=lambda x: (x[1], x[2]))[:5] # Keep top 5 scores
break

if number_of_guesses == allowed_guesses – 2:
hint = “even” if number_to_guess % 2 == 0 else “odd”
print(f”Hint: The number is {hint}.”)

if guess != number_to_guess:
print(f”Sorry, you’ve used all your guesses. The number was {number_to_guess}.”)

print(“\nHigh Scores:”)
for score in high_scores:
print(f”{score[0]} – {score[1]} guesses, {score[2]} seconds”)

play_again = input(“Do you want to play again? (yes or no): “).lower()
if play_again != ‘yes’:
break

if __name__ == “__main__”:
guess_the_number()

______________

Congratulations! You’ve built a comprehensive “Guess the Number” game in Python. This project covers a variety of programming concepts, including loops, conditionals, functions, input validation, and lists. You can further enhance the game by adding more features or refining the existing ones. Happy coding!