Sure! Here's a fun and simple Python game: a "Guess the Number" game with difficulty levels, friendly UI on the terminal, and colored text using the colorama library for emphasis. This game is great for beginners but also includes good structure for learning.
🎮 Game: Guess the Number
✅ Features:
- Difficulty levels: Easy, Medium, Hard
- Random number guessing
- Tracks attempts
- Colorful terminal output
🔧 Installation requirement:
We’ll use the colorama library to make the terminal output colorful.
Install it using:
pip install colorama
🐍 Python Code:
import random
from colorama import init, Fore, Style
# Initialize colorama
init(autoreset=True)
def welcome_message():
print(Fore.CYAN + "Welcome to the 'Guess the Number' Game!")
print("Try to guess the number I'm thinking of between 1 and 100.")
print(Fore.YELLOW + "Choose your difficulty:")
print("1 - Easy (10 attempts)")
print("2 - Medium (7 attempts)")
print("3 - Hard (5 attempts)\n")
def choose_difficulty():
while True:
choice = input(Fore.CYAN + "Select difficulty (1/2/3): ")
if choice == '1':
return 10
elif choice == '2':
return 7
elif choice == '3':
return 5
else:
print(Fore.RED + "Invalid choice! Please enter 1, 2, or 3.")
def play_game():
welcome_message()
attempts_left = choose_difficulty()
secret_number = random.randint(1, 100)
while attempts_left > 0:
print(Style.BRIGHT + f"\nAttempts left: {Fore.GREEN}{attempts_left}")
try:
guess = int(input(Fore.BLUE + "Enter your guess (1-100): "))
except ValueError:
print(Fore.RED + "Please enter a valid number!")
continue
if guess < 1 or guess > 100:
print(Fore.RED + "Number out of bounds! Choose between 1 and 100.")
continue
if guess == secret_number:
print(Fore.GREEN + Style.BRIGHT + f"\n🎉 Congratulations! You guessed the number {secret_number}!")
break
elif guess < secret_number:
print(Fore.YELLOW + "Too low! Try a higher number.")
else:
print(Fore.YELLOW + "Too high! Try a lower number.")
attempts_left -= 1
if attempts_left == 0:
print(Fore.RED + Style.BRIGHT + f"\n💥 You've run out of attempts! The number was {secret_number}.")
def main():
while True:
play_game()
replay = input(Fore.CYAN + "\nWould you like to play again? (y/n): ").lower()
if replay != 'y':
print(Fore.MAGENTA + "Thanks for playing! Goodbye! 👋")
break
if __name__ == "__main__":
main()
💡 How to Run:
- Save as
guess_the_number.py
- Open terminal and run:
python guess_the_number.py
Would you like a graphical version (using Pygame) or maybe a different kind of game (like hangman, tic-tac-toe, snake, etc.)? Let me know!