DEV Community

Cover image for 🎮 Crafting Fun with Code: My Journey Building the Hangman Game 🕹️
Anik Chand
Anik Chand

Posted on

🎮 Crafting Fun with Code: My Journey Building the Hangman Game 🕹️

Repo link : https://github.com/anikchand461/Hangman-Game

What motivated me to start blogging ?
This is my first blog, and the motivation to start blogging came from my friend, Abhiraj Adhikary. He encouraged me to explore blogging as a way to connect with like-minded people, share knowledge, and engage with a broader community. His insights made me realize that blogging isn’t just about documenting ideas but also a way to clarify and refine my own concepts, particularly in projects. Writing about my work helps me dive deeper into the details, reflect on my learning, and present it in a way that others can benefit from. Inspired by his advice, I decided to start this journey to share my experiences and build meaningful connections with others in the tech and learning community.

What is hangman Game ?
Hangman is a classic word-guessing game with a simple yet engaging objective: players guess a hidden word by suggesting letters within a limited number of attempts. With each incorrect guess, a part of a stick-figure “hangman” is drawn, increasing the tension. The game tests vocabulary, problem-solving skills, and strategy, making it a popular choice for both casual play and learning activities.

What inspired me to create this game ?
I was learning Python through Jenny’s Lecture CS IT YouTube channel, where the teacher introduced the Hangman game as a project. However, I decided to take a different approach—I wanted to challenge myself to create the game independently, without watching the tutorial. To start, I played a few Hangman games from apps downloaded from the Play Store, which helped me understand the gameplay and logic. These experiences inspired me to incorporate small changes and enhancements into my version of the game, aiming for a more polished and engaging result.

Objective of the game
The main goal of creating my Hangman game was twofold. It was a fun project that allowed players to enjoy a word-guessing game with categories like sports, food, and animals. For me, it was also a valuable learning experience, reinforcing my understanding of Python concepts like conditionals, loops, and the random module for dynamic word selection. Building the game enhanced my logic-building skills, as I designed the gameplay flow, added ASCII art, and implemented features like replay options. This project was a perfect mix of fun and learning, marking an important step in my Python journey.

Features of the Game

Diverse Word Categories
The game includes multiple word categories, such as sports, food, and animals, offering players a varied and engaging experience.

Dynamic Word Selection
With the use of Python’s random module, words are dynamically selected from the chosen category, ensuring unpredictability and replayability.

Engaging ASCII Art
The game features creative ASCII art for the Hangman and the game header, adding a visual element that enhances the player’s experience.

Image description

Replay Option
Players have the option to replay the game after a round ends, making it easy to enjoy multiple sessions without restarting the program.

Image description

User-Friendly Enhancements
To improve the gameplay experience, I incorporated user-friendly features such as-

Error-Handling
Ensuring the program runs smoothly even if the player makes invalid inputs.

Score-keeping
Tracking player performance to add a competitive edge and encourage improvement.

  print(f'''
    GAME SUMMARY--
    Player name : {name}
    Difficulty level : {difficulty_level}
    lives used : {6 - life}
    Total attampts : {correct_attampts + wrong_attampts}
    Correct attampts : {correct_attampts}
    Wrong attampts : {wrong_attampts}
    ''')
Enter fullscreen mode Exit fullscreen mode

Technologies Used
Programming Language: Python
The game is built entirely using Python, a versatile and beginner-friendly programming language known for its simplicity and readability.

Libraries used

import random as r
import os
Enter fullscreen mode Exit fullscreen mode

random :
This library is utilized to dynamically select words from predefined categories, ensuring that each game offers a unique experience.

os :
The os module is used for tasks like clearing the screen between guesses, enhancing the overall gameplay presentation.

Why Python?
Python was chosen for this project because I was actively learning Python at the time. I thought building the Hangman game would be a great way to apply and master the concepts I was learning. The simplicity and versatility of Python made it the perfect choice for creating a fun and engaging project while improving my skills.

Code Walkthrough

First Implementation

I started by creating a flowchart to map out the game’s logic:
• I chose a predefined word and created a dashed list, with each dash representing a letter of the word.
• When the player guessed a letter, it was compared to the word. If a match was found, the dash was replaced with the correct letter. This continued until all letters were revealed or the player ran out of attempts.

Secondary Improvement

After implementing the basic logic, I enhanced the game by:
• Allowing players to replace all instances of the guessed letter in the dashed list (e.g., “away” becomes [‘a’, ‘’, ‘a’, ’’] when guessing ‘a’).

    for i in range(letter_numbers):
        list_word.append('_')

• Introducing a lives system, where players start with 6 lives, losing 1 life with each incorrect guess. The game ends when the word is guessed correctly or lives run out.

Further Improvement

I improved the game’s word selection by:
• Creating a list of 1000 words using ChatGPT and using the random module to select a word.
• Adding categories like sports, food, and animals, allowing players to choose based on their interests, making the game more engaging.

Additional Development

To make the game more engaging, I added difficulty levels and visual enhancements:

     if difficulty_level == 'easy':
        select_word = select_word1(key)
    elif difficulty_level == 'moderate':
        select_word = select_word2(key)
    elif difficulty_level == 'hard':
        select_word = select_word3(key)
    else:
        print('please enter valid input')

Difficulty Levels:
• Introduced three levels: easy (1–5 letters), medium (5–7 letters), and hard (8+ letters).
• Players could choose a level, and word selection adjusted accordingly for added challenge.
Visual Enhancements:
• Included ASCII art hangman figures to show progress, with more detail as lives were lost.

    r"""
      +---+
      |   |  
      O   | 
     /|\  | 
     /    | 
          |
    """,
    r"""
      +---+
      |   |  
      O   | 
     /|\  | 
     / \  | 
          | ☠️
    """

• Added win and loss ASCII art for a dramatic and satisfying game ending.
These features made the game more dynamic, fun, and visually appealing.

Final Touches

To give the game a polished feel, I made key improvements:
• Added an introductory message with hangman ASCII art.
• Included player name tracking, lives used, and final results showing the word.
• Wrapped the game in a while(True) loop for infinite rounds, with an option to replay after each round.
These enhancements made the game more engaging, visually appealing, and fun.

Lessons Learned

1. Problem-Solving Skills

Building the Hangman game was a great way to enhance my problem-solving skills. Debugging errors and optimizing gameplay required critical analysis and logical thinking. Handling edge cases like duplicate guesses, invalid inputs, and word categories taught me to design robust conditions. Each challenge refined my structured problem-solving approach: identifying issues, breaking them down, and systematically testing solutions. This iterative process improved my code and strengthened my ability to handle complex problems in future projects.

2. Time Management

Balancing learning Python while working on this project tested my time management skills. I scheduled time for learning concepts, implementing features, and refining the game. Initially, juggling syntax, debugging, and creative elements like ASCII art felt overwhelming. However, a step-by-step plan and task prioritization kept me organized. I focused on essential features, like basic game logic, before adding enhancements like visuals and error-handling. This disciplined approach improved my efficiency and became a valuable skill in my learning journey.

Enjoy The Game

Image description
Feel free to explore the project on GitHub (github repo link : https://github.com/anikchand461/Hangman-Game ), try the game, or share your feedback and ideas. I’d love to hear your thoughts and connect with others who are passionate about coding and learning. Let’s build something amazing together!

Top comments (0)