Write a Complete Program to create a guess the number game in python GUI.
Here is a step by step guide to create Guess the Number game in Python.
- Program to Create Snake Game in Python
- Write a Program to Create Hangman Game in Python
- Program to Create Tic Tac Toe Game in Python
- Program to Create BlackJack Game in Python
Introduction:
Guess the number is a simple guessing game in which the player has to guess a randomly generated number. In this game player gets a limited number of chances to guess the number. The game can be played using a command-line interface or a simple GUI.
In this tutorial we will create Guess the Number game in Python. We will start with a simple command line interface and then create a GUI version of the game using the Tkinter library.
Step 1: Importing Required Libraries
In the first step we need to import the required libraries for the game. We will use random module to generate a random number for the game.
import random
Step 2: Generating a Random Number
In the second step we will generate a random number for game. The random number will in between 1 and 100. We will be using the randint() function of the random module to generate the random number.
number = random.randint(1, 100)
Step 3: Setting the Number of Chances
In the third step we will set number of chances the player will have to guess the number. We will give player a maximum of 5 chances to guess the number.
chances = 5
Step 4: Starting the Game Loop
In the fourth step we will start game loop. We will be asking player to enter a number and then check if the number is the same as the random number generated in Step 2. If the number is not the same we will provide a hint to the player and reduce the number of chances the player has.
while chances > 0:
guess = int(input("Enter your guess (1-100): "))
if guess == number:
print("Congratulations! You guessed the number.")
break
elif guess < number:
print("The number is higher than your guess.")
else:
print("The number is lower than your guess.")
chances -= 1
Step 5: Ending the Game
In the fifth step we will end the game. If the player is not able to guess the number within the given number of chances we will inform player that they have lost the game.
if chances == 0:
print("You have lost the game. The number was", number)
Step 6: Creating a GUI Version of the Game using Tkinter
Now that we have a working command line version of game let’s create a graphical user interface (GUI) using Tkinter library in Python. We will use labels to display the messages an entry box for the user’s input and a button to submit their guess.
We start by importing the Tkinter library and creating the main window:
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Guess the Number")
root.geometry("300x150")
Next we create a label to display the game instructions:
# Create a label for the instructions
instructions = tk.Label(root, text="I'm thinking of a number between 1 and 100. Can you guess what it is?", font=("Helvetica", 12))
instructions.pack(pady=10)
We create another label to display the result of the user’s guess:
# Create a label for the result of the guess
result = tk.Label(root, text="", font=("Helvetica", 12))
result.pack(pady=10)
Next we create entry box for the user to input their guess:
# Create an entry box for the user's guess
guess = tk.Entry(root, font=("Helvetica", 12), width=4)
guess.pack(pady=10)
Finally we will create a button that the user can click to submit their guess:
# Create a button to submit the guess
def check_guess():
guess_num = int(guess.get())
if guess_num == number:
result.config(text="You guessed it! The number was {}.".format(number))
elif guess_num < number:
result.config(text="Too low! Guess again.")
else:
result.config(text="Too high! Guess again.")
submit = tk.Button(root, text="Submit", font=("Helvetica", 12), command=check_guess)
submit.pack()
And thats all Run the code to see the GUI version of the game.
Here’s the complete code for the GUI version:
import tkinter as tk
import random
# Create the main window
root = tk.Tk()
root.title("Guess the Number")
root.geometry("300x150")
# Generate a random number between 1 and 100
number = random.randint(1, 100)
# Create a label for the instructions
instructions = tk.Label(root, text="I'm thinking of a number between 1 and 100. Can you guess what it is?", font=("Helvetica", 12))
instructions.pack(pady=10)
# Create a label for the result of the guess
result = tk.Label(root, text="", font=("Helvetica", 12))
result.pack(pady=10)
# Create an entry box for the user's guess
guess = tk.Entry(root, font=("Helvetica", 12), width=4)
guess.pack(pady=10)
# Create a button to submit the guess
def check_guess():
guess_num = int(guess.get())
if guess_num == number:
result.config(text="You guessed it! The number was {}.".format(number))
elif guess_num < number:
result.config(text="Too low! Guess again.")
else:
result.config(text="Too high! Guess again.")
submit = tk.Button(root, text="Submit", font=("Helvetica", 12), command=check_guess)
submit.pack()
# Start the game loop
root.mainloop()
Conclusion:
In this tutorial we learned how to create a Guess the Number game in Python. We started by creating a simple commandline version of the game and gradually added more features such as a limit on the number of guesses and a GUI version of the game using Tkinter. We also saw how to handle errors and validate user inputs to prevent the program from crashing.
Python offers a lot of flexibility and power for game development and creating a simple game like Guess the Number can be a great way to get started. We hope this tutorial has been helpful and that you now have a better understanding of how to create a game in Python.