-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from Aditijainnn/Hangman-game
Added hangman game
- Loading branch information
Showing
14 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
# Hangman Game 🎮 | ||
|
||
## 🎯 Objective | ||
|
||
Here’s a more elaborate game called "Hangman Game" using Python's Tkinter library. In this classic Hangman game, your goal is to guess the hidden word one letter at a time. You have a limited number of incorrect guesses before the game is over, so choose wisely! | ||
--- | ||
|
||
**Prerequisite**:Install `pthon` | ||
|
||
Before running the code, you need to install the `python` | ||
|
||
## 🚀 How to Play: | ||
|
||
- The game selects a random word. | ||
- You have to guess the word one letter at a time. | ||
- You can make a limited number of wrong guesses before the | ||
stickman is "hanged". | ||
- Each correct guess reveals the letter(s) in the word. | ||
- The game ends when you either guess the word or run out of | ||
guesses. | ||
|
||
--- | ||
|
||
## 🔧 How to Run | ||
|
||
1. Clone the repository and navigate to the project folder: | ||
```bash | ||
git clone <repository-url> | ||
cd <project-folder> | ||
``` | ||
|
||
2. Install dependencies (if any) and then run the program: | ||
```bash | ||
python hangman.py | ||
``` | ||
|
||
3. Enjoy playing the Snake game! | ||
|
||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from tkinter import * | ||
from tkinter import messagebox | ||
from string import ascii_uppercase | ||
import random | ||
|
||
|
||
window = Tk() | ||
window.title('Hangman-GUESS CITIES NAME') | ||
word_list= ['MUMBAI','DELHI','BANGLORE','HYDRABAD','AHMEDABAD','CHENNAI','KOLKATA','SURAT','PUNE','JAIPUR','AMRITSAR','ALLAHABAD','RANCHI', | ||
'LUCKNOW','KANPUR','NAGPUR','INDORE','THANE','BHOPAL','PATNA','GHAZIABAD','AGRA','FARIDABAD','MEERUT','RAJKOT','VARANASI','SRINAGAR', | ||
'RAIPUR','KOTA','JHANSI'] | ||
|
||
photos = [PhotoImage(file="images/hang0.png"), PhotoImage(file="images/hang1.png"), PhotoImage(file="images/hang2.png"), | ||
PhotoImage(file="images/hang3.png"), PhotoImage(file="images/hang4.png"), PhotoImage(file="images/hang5.png"), | ||
PhotoImage(file="images/hang6.png"), PhotoImage(file="images/hang7.png"), PhotoImage(file="images/hang8.png"), | ||
PhotoImage(file="images/hang9.png"), PhotoImage(file="images/hang10.png"), PhotoImage(file="images/hang11.png")] | ||
|
||
|
||
|
||
|
||
|
||
|
||
def newGame(): | ||
global the_word_withSpaces | ||
global numberOfGuesses | ||
numberOfGuesses =0 | ||
|
||
the_word=random.choice(word_list) | ||
the_word_withSpaces = " ".join(the_word) | ||
lblWord.set(' '.join("_"*len(the_word))) | ||
|
||
def guess(letter): | ||
global numberOfGuesses | ||
if numberOfGuesses<11: | ||
txt = list(the_word_withSpaces) | ||
guessed = list(lblWord.get()) | ||
if the_word_withSpaces.count(letter)>0: | ||
for c in range(len(txt)): | ||
if txt[c]==letter: | ||
guessed[c]=letter | ||
lblWord.set("".join(guessed)) | ||
if lblWord.get()==the_word_withSpaces: | ||
messagebox.showinfo("Hangman","You guessed it!") | ||
else: | ||
numberOfGuesses += 1 | ||
imgLabel.config(image=photos[numberOfGuesses]) | ||
if numberOfGuesses==11: | ||
messagebox.showwarning("Hangman","Game Over") | ||
|
||
|
||
imgLabel=Label(window) | ||
imgLabel.grid(row=0, column=0, columnspan=3, padx=10, pady=40) | ||
|
||
|
||
|
||
lblWord = StringVar() | ||
Label(window, textvariable =lblWord,font=('consolas 24 bold')).grid(row=0, column=3 ,columnspan=6,padx=10) | ||
|
||
n=0 | ||
for c in ascii_uppercase: | ||
Button(window, text=c, command=lambda c=c: guess(c), font=('Helvetica 18'), width=4).grid(row=1+n//9,column=n%9) | ||
n+=1 | ||
|
||
Button(window, text="New\nGame", command=lambda:newGame(), font=("Helvetica 10 bold")).grid(row=3, column=8) | ||
|
||
newGame() | ||
window.mainloop() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.