#Shreyas Vivek
#Hangman game
from tkinter import *
import random
characters = 'abcdefghijklmnopqrstuvwxyz'
used_characters = ""
wrong = 0
done = False
def phrase():
'''This function generates the phrase for the game'''
f = open('hangman.txt')
selected_line = random.randrange(0,370)
for i in range(selected_line-1):
f.readline()
puzzle = f.readline()
f.close()
return puzzle.lower()
def gallows():
'''This function draws the gallows'''
c.create_line(385,360, 385,135,fill="white",width=2)
c.create_line(440,135, 440,160,fill="white",width=2)
c.create_line(385,135, 440,135,fill="white",width=2)
c.create_line(345,360, 425,360,fill="white",width=2)
def head():
'''Draws the head'''
c.create_oval(420,160, 460,200,fill="white",width=2)
def body():
'''Draws the body'''
c.create_line(440,200, 440,285,fill="white",width=2)
def right_arm():
'''Draws the right arm'''
c.create_line(440,222, 470,260,fill="white",width=2)
def left_arm():
'''Draws the left arm'''
c.create_line(440,222, 410, 260,fill="white",width=2)
def right_leg():
'''Draws the right leg'''
c.create_line(440,285, 460, 325,fill="white",width=2)
def left_leg():
'''Draws the left leg'''
c.create_line(440,285, 420, 325,fill="white",width=2)
def handle_keypress(event):
'''This function writes down the charcter in the space if its presnt in the phrase,
recognizes if it isnt present and draws a body part, and notifys the user if it is an illegal character '''
global used_characters
global wrong
global done
p = set(puzzle)
pu = set()
if done == False:
if event.char in puzzle:
if event.char in characters and event.char not in used_characters:
for i in range(len(puzzle)):
if event.char == puzzle[i]:
c.create_text(10+i*20, 395, text=event.char, fill="white",width=2)
used_characters += str(event.char)
pu = set(used_characters)
elif event.char in characters and event.char in used_characters:
print("You have already used this character! Try again!")
elif event.char not in puzzle:
if event.char in characters:
wrong = wrong + 1
print("That is your " + str(wrong) +" character wrong")
man()
elif event.char not in characters:
print("You cannot use this character becuase it is illegal! Try again!")
if (p & set(characters)) - pu == set():
c.create_text(300,450, fill="white", text = "Winner!",width=2)
done = True
if done == True:
c.create_text(300,500, fill="white", text = "Are you done or ready for more? (click y to play again or n to leave the game)")
if event.char == "y":
setup()
elif event.char == "n":
c.create_text(300,550, fill="white", text = "See you later!",width=2)
def lines():
'''Draws the lines for the characters'''
x = 5
tries = 0
while tries < len(puzzle):
if puzzle[tries] in characters:
c.create_line(x,400, (x+10), 400, fill="white",width=2)
x = x + 20
tries = tries + 1
elif puzzle[tries] == "'":
c.create_text(x,380, text = puzzle[tries])
x = x + 20
tries = tries + 1
else:
c.create_text(x,400,text = puzzle[tries])
x = x + 20
tries = tries + 1
def man():
'''This draws the hangman whenever the user inputs a wrong character'''
global wrong
global done
if done == False:
if wrong == 1:
head()
elif wrong == 2:
body()
elif wrong == 3:
left_arm()
elif wrong == 4:
right_arm()
elif wrong == 5:
left_leg()
elif wrong == 6:
right_leg()
print("Loser! The correct phrase was:" +str(puzzle))
done = True
elif done == True:
c.create_text(300,500, text = "Are you done or ready for more? (click y to play again or n to leave the game)",width=2)
if event.char == "y":
setup()
elif event.char == "n":
c.create_text(300,550, text = "See you later!",width=2)
def setup():
'''This function sets up the game'''
global puzzle
global done
global used_characters
global wrong
c.create_rectangle(0,0, 1000,1000, fill="black")
puzzle = phrase()
lines()
gallows()
done = False
used_characters = ""
wrong = 0
main_window = Tk()
main_window.title("Hangman Game-Shreyas Vivek")
c = Canvas(main_window, width=1000, height=1000)
c.pack()
c.bind('<Key>', handle_keypress)
c.focus_set()
setup()
mainloop()