#Vivian Li
#Hangman Game
import random
import tkinter
master = tkinter.Tk()
master.title("Hangman Game")
screen = tkinter.Canvas(master,width = 600, height = 600)
screen.pack()
puzzles = ""
pressable_char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
wrong_guess = 0
correct_characters = []
def draw_screen(puzzle):
"""Draws the hangman screen"""
x_cord = 300 -10*(len(puzzle)/2)
x_cord2 = 40
for x in puzzles:
if x == " ":
x_cord += 10
elif x == ",":
screen.create_line(x_cord+3,501,x_cord,503)
x_cord += 12
elif x == "'":
screen.create_line(x_cord+3,493,x_cord,495)
x_cord += 12
elif x == "-":
screen.create_line(x_cord,503,x_cord+5,503)
x_cord+=10
elif x.lower() in 'abcdefghijklmnopqrstuvwxyz':
screen.create_line(x_cord, 500, x_cord + 5, 500)
x_cord += 10
for x in 'abcdefghijklmnopqrstuvwxyz':
screen.create_text(x_cord2, 550, text=x.upper(), tag="letter")
x_cord2 += 20
screen.create_line(150,450,450,450)
screen.create_line(300, 450, 300, 100,400,100,400,150)
def read_phrase():
"""Chooses a random phrase to guess"""
#copy the hangman371 to the desktop
f = open('/Users/liv/Desktop/hangman371.txt')
selected_line = random.randrange(0,370)
for i in range(selected_line+1):
f.readline()
puzzle = f.readline()
f.close()
return puzzle
def play_again():
"""Resets the screen to play again"""
global pressable_char
global wrong_guess
global correct_characters
global puzzles
screen.delete("all")
pressable_char = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
wrong_guess = 0
correct_characters = []
puzzles = read_phrase()
draw_screen(puzzles)
def draw_man():
"""When the guess is wrong, draws a body part, and if the man is dead, tells the player what letters they missed"""
global screen
global wrong_guess
global puzzles
global pressable_char
x_cord = 300 -10*(len(puzzles)/2)
wrong_guess += 1
if wrong_guess == 1:
screen.create_oval(375,150,425,200,tag="man")
elif wrong_guess == 2:
screen.create_line(400,200,400,350, tag="man")
elif wrong_guess == 3:
screen.create_line(400,250,350,300,tag="man")
elif wrong_guess == 4:
screen.create_line(400,250,450,300,tag="man")
elif wrong_guess == 5:
screen.create_line(400,350, 450,400,tag="man")
elif wrong_guess == 6:
screen.create_line(400,350,350,400,tag="man")
elif wrong_guess == 7:
screen.create_line(400,300,475,350,tag='man')
for i in range(len(puzzles)):
for x in pressable_char:
if x == puzzles[i].lower():
screen.create_text(x_cord+3.5 + i*10,493, text=x, fill="red")
if tkinter.messagebox.askyesno("Game Over","Do you want to play again?") == True:
play_again()
def check_win():
"""Checks if the play won"""
global puzzles
global correct_characters
puzzlesi = len(puzzles) - puzzles.count(" ") - puzzles.count("'")-puzzles.count(",")
correct = 0
for x in puzzles.lower():
if x in correct_characters:
correct += 1
if correct >= puzzlesi -1:
if tkinter.messagebox.askyesno("You Won","Do you want to play again?") == True:
play_again()
def handle_keypress(event):
"""When a key is pressed, runs the key to see if it is in the word, and removes it from pressable characters"""
global pressable_char
global puzzles
global correct_characters
global screen
x_cord = 300 -10*(len(puzzles)/2)
x_cord2 = 40
if event.char in pressable_char:
for i in range(len(puzzles)):
if event.char == puzzles[i].lower():
screen.create_text(x_cord+3.5 + i*10,493, text=event.char)
correct_characters.append(event.char)
pressable_char.remove(event.char)
screen.delete("letter")
for x in pressable_char:
screen.create_text(x_cord2, 550, text=x.upper(),tag="letter")
x_cord2 += 20
if (event.char not in puzzles.lower()) and (event.char in 'abcdefjhijklmnopqrstuvwxyz'):
draw_man()
else:
message = tkinter.messagebox.showinfo("Invalid Character", "The character you have guessed either has been guessed before or isn't a valid character")
check_win()
puzzles = read_phrase()
screen.bind('<Key>', handle_keypress)
screen.focus_set()
draw_screen(puzzles)