Skip to content

Commit c7143fc

Browse files
Merge pull request #2332 from andoriyaprashant/branch2
Hangman Gui Script Added
2 parents 20bd563 + ea611fc commit c7143fc

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Hangman_Gui/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Hangman Game (GUI-Based)
2+
3+
This is a simple implementation of the classic Hangman game in Python with a graphical user interface (GUI) using Tkinter. Players guess a secret word letter by letter, with a limited number of attempts.
4+
5+
## How to Play
6+
7+
1. Run the `hangman_gui.py` script in your Python environment.
8+
2. A graphical window will pop up with the Hangman game interface.
9+
3. The game will choose a random word from a predefined list.
10+
4. You need to guess the word by entering one letter at a time in the input field provided.
11+
5. Click the "Guess" button or press Enter to submit your guess.
12+
6. The game will display your progress, the letters you've guessed correctly, and the attempts left.
13+
7. If you guess all the letters correctly before running out of attempts, you win!
14+
8. If you run out of attempts before guessing the word, you lose.
15+
16+
Enjoy the game and have fun!
17+
18+
## Requirements
19+
20+
- Python 3.x
21+
22+
## How to Run
23+
24+
To play the Hangman game, simply execute the `hangman_gui.py` file using your Python environment.
25+

Hangman_Gui/hangman_gui.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import random
2+
import tkinter as tk
3+
from tkinter import messagebox
4+
5+
def choose_word():
6+
words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape"]
7+
return random.choice(words)
8+
9+
class HangmanGame:
10+
def __init__(self, master):
11+
self.master = master
12+
self.word = choose_word()
13+
self.guessed_letters = []
14+
self.attempts = 0
15+
16+
self.label_word = tk.Label(master, text=self.display_word())
17+
self.label_word.pack()
18+
19+
self.label_attempts = tk.Label(master, text="Attempts left: 6")
20+
self.label_attempts.pack()
21+
22+
self.entry_guess = tk.Entry(master)
23+
self.entry_guess.pack()
24+
25+
self.button_guess = tk.Button(master, text="Guess", command=self.make_guess)
26+
self.button_guess.pack()
27+
28+
def display_word(self):
29+
displayed_word = ""
30+
for letter in self.word:
31+
if letter in self.guessed_letters:
32+
displayed_word += letter
33+
else:
34+
displayed_word += "_ "
35+
return displayed_word
36+
37+
def make_guess(self):
38+
guess = self.entry_guess.get().lower()
39+
40+
if len(guess) != 1 or not guess.isalpha():
41+
messagebox.showwarning("Invalid Input", "Please enter a single letter.")
42+
return
43+
44+
if guess in self.guessed_letters:
45+
messagebox.showinfo("Already Guessed", "You've already guessed this letter.")
46+
return
47+
48+
self.guessed_letters.append(guess)
49+
50+
if guess not in self.word:
51+
self.attempts += 1
52+
self.label_attempts.config(text=f"Attempts left: {6 - self.attempts}")
53+
54+
self.label_word.config(text=self.display_word())
55+
56+
if "_" not in self.display_word():
57+
messagebox.showinfo("Congratulations!", f"You guessed the word: {self.word}")
58+
self.master.destroy()
59+
60+
if self.attempts >= 6:
61+
messagebox.showinfo("Game Over", f"Out of attempts! The word was: {self.word}")
62+
self.master.destroy()
63+
64+
def main():
65+
root = tk.Tk()
66+
root.title("Hangman Game")
67+
game = HangmanGame(root)
68+
root.mainloop()
69+
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)