Hangman Game - Python Code

This is the Python code for a Hangman game.


import random
import os

# Word categories
categories = {
    "Animals": ["elephant", "giraffe", "dolphin", "penguin", "kangaroo"],
    "Movies": ["titanic", "avatar", "inception", "gladiator", "jaws"],
    "Countries": ["canada", "brazil", "germany", "japan", "india"]
}

# Hangman stages
HANGMAN_PICS = [
    """
       +---+
           |
           |
           |
          ===""",
    """
       +---+
       O   |
           |
           |
          ===""",
    """
       +---+
       O   |
       |   |
           |
          ===""",
    """
       +---+
       O   |
      /|   |
           |
          ===""",
    """
       +---+
       O   |
      /|\\  |
           |
          ===""",
    """
       +---+
       O   |
      /|\\  |
      /    |
          ===""",
    """
       +---+
       O   |
      /|\\  |
      / \\  |
          ==="""
]

# Colors for terminal output
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
RESET = "\033[0m"

def clear_screen():
    """Clears the terminal screen."""
    os.system("cls" if os.name == "nt" else "clear")

def choose_word():
    """Chooses a random word from a category."""
    category = random.choice(list(categories.keys()))
    word = random.choice(categories[category]).upper()
    return category, word

def display_status(word, guessed_letters, wrong_attempts, category):
    """Displays the current game status."""
    clear_screen()
    print(CYAN + "šŸŽ© HANGMAN GAME šŸŽ©" + RESET)
    print(HANGMAN_PICS[wrong_attempts])  # Show hangman drawing
    print(YELLOW + f"Hint: {category}" + RESET)
    
    # Show word progress
    display_word = " ".join([letter if letter in guessed_letters else "_" for letter in word])
    print(GREEN + f"\nWord: {display_word}" + RESET)
    
    # Show used letters
    print(RED + f"\nUsed letters: {', '.join(sorted(guessed_letters))}" + RESET)

def hangman():
    """Main function to play the game."""
    category, word = choose_word()
    guessed_letters = set()
    wrong_attempts = 0
    max_attempts = len(HANGMAN_PICS) - 1

    while wrong_attempts < max_attempts:
        display_status(word, guessed_letters, wrong_attempts, category)
        guess = input("\nEnter a letter: ").upper()

        if not guess.isalpha() or len(guess) != 1:
            print(RED + "Invalid input! Please enter a single letter." + RESET)
            continue

        if guess in guessed_letters:
            print(YELLOW + "You already guessed that letter!" + RESET)
            continue

        guessed_letters.add(guess)

        if guess in word:
            print(GREEN + "āœ… Correct!" + RESET)
            if set(word) <= guessed_letters:
                display_status(word, guessed_letters, wrong_attempts, category)
                print(GREEN + f"\nšŸŽ‰ Congratulations! You guessed the word: {word} šŸŽ‰" + RESET)
                return
        else:
            wrong_attempts += 1
            print(RED + "āŒ Incorrect guess!" + RESET)

    # Game over
    display_status(word, guessed_letters, wrong_attempts, category)
    print(RED + f"\nšŸ’€ You lost! The word was: {word} šŸ’€" + RESET)

if __name__ == "__main__":
    hangman()