#Shaunak Lavande, Mods 15-16
#This program allows the user to play hangman
from tkinter import *
import random
legal_chars = 'abcdefghijklmnopqrstuvwxyz'
used = ""
wrong = 0
done = False
def read_phrase():
'''This function generates the phrase for the game'''
f = open('hangman.txt')
selected_line = random.randrange(0, 371)
for i in range(selected_line-1):
f.readline()
puzzle = f.readline()
f.close()
return puzzle.lower()
def draw_gallows():
'''This function draws the gallows'''
c.create_line(300,315,380,315,fill='red',width=4)
c.create_line(340,315,340,90, fill='red',width=4)
c.create_line(340,90,395,90,fill='red',width=4)
c.create_line(395,90,395,115,fill='red',width=4)
def head():
'''This function draws the head'''
c.create_oval(375,115,415,155)
def body():
'''This function draws the body'''
c.create_line(395,155,395,240)
def left_arm():
'''This function draws the left arm'''
c.create_line(395,177,365,215)
def right_arm():
'''This function draws the right arm'''
c.create_line(395,177,425,215)
def left_leg():
'''This function draws the left leg'''
c.create_line(395,240,375,280)
def right_leg():
'''This function draws the right leg'''
c.create_line(395,240,415,280)
def handle_keypress(event):
'''This function is used to mark the characters the player picks.
Also, it asks if the player wants to continue playing or not.'''
global used_letters
global wrong
global done
yee = set(puzzle)
wee = set()
if done == False:
if event.char in puzzle:
if event.char in legal_chars and event.char not in used_letters:
for i in range(len(puzzle)):
if event.char == puzzle[i]:
c.create_text(10+i*20, 395, text=event.char)
used_letters += str(event.char)
wee = set(used_letters)
elif event.char in legal_chars and event.char in used_letters:
print("Character is used. Try another one")
elif event.char not in puzzle:
if event.char in legal_chars:
wrong = wrong + 1
print("You got " + str(wrong) +" characters wrong")
man()
elif event.char not in legal_chars:
print("This character is illegal, please try again")
if (yee & set(legal_chars)) - wee == set():
print("You won!")
done = True
if done == True:
c.create_text(300,600, text = "Would you like to play again?(Please press 'y' for yes and 'n' for no)")
if event.char == "y":
game_setup()
elif event.char == "n":
c.create_text(300,450, text = "Thanks for playing")
def draw_lines():
'''This function draws the lines for the characters'''
x = 5
number_of_tries = 0
while number_of_tries < len(puzzle):
if puzzle[number_of_tries] in legal_chars:
c.create_line(x,400, (x+10), 400)
x = x + 20
number_of_tries = number_of_tries + 1
elif puzzle[number_of_tries] == "'":
c.create_text(x,380, text = puzzle[number_of_tries])
x = x + 20
number_of_tries = number_of_tries + 1
else:
c.create_text(x,400,text = puzzle[number_of_tries])
x = x + 20
number_of_tries = number_of_tries + 1
def man():
'''This program makes the dead hangman depending on how many characters the player got wrong'''
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("You lost, the phrase is:" +str(puzzle))
done = True
elif done == True:
c.create_text(300,500, text = "Would you like to play again?(Please press 'y' for yes and 'n' for no)")
if event.char == "y":
game_setup()
elif event.char == "n":
c.create_text(300,550, text = "Thank you for playing")
def setup():
'''This function sets the game'''
global puzzle
global done
global used_letters
global wrong
c.create_rectangle(0,0, 900,900, fill="green")
puzzle = read_phrase()
draw_lines()
draw_gallows()
done = False
used_letters = ""
wrong = 0
main_window = Tk()
main_window.title("Hangman Game by Shaunak Lavande")
c = Canvas(main_window, width=900, height=900)
c.pack()
c.bind('<Key>', handle_keypress)
c.focus_set()
setup()
mainloop()