#Brooke Christiansen
#HANGMAN program
from tkinter import *
from random import *
from time import sleep
legal_chars = 'abcdefghijklmnopqrstuvwxyz'
used_char = ''
wrong_answerz = 0
right_answerz = 0
len_of_puzzle = 0
done = False
def intro():
c.create_text(400,100,text='ready to die?', font = ('LCDMono2',30))
def draw_gallows():
'''draws the gallows for the man'''
c.create_line(90,60,90,20,200,20,200,400, width="10", fill = 'red')
c.create_line(60,400,340,400, width="10",fill = 'red')
def read_phrase():
'''picks a random phrase'''
f = open('hangman.txt')
selected_line = randrange(0,370) #changed this to a random value between 0 and 370
for i in range(selected_line-1):
f.readline() #skip over the lines till we get to the selected line
puzzle = f.readline()
f.close()
strpuzzle = str(puzzle)
l_puzzle= strpuzzle.lower()
return l_puzzle
def draw_word_lines(puzzle):
'''draws the areas for characters
puzzle - the given phrase'''
global len_of_puzzle
for i in range(len(puzzle)):
if puzzle[i] in legal_chars:
c.create_line(15+i*25,490,(15+(i)*25)+8,490)
len_of_puzzle += 1
else:
c.create_text(20+i*25,480,text=puzzle[i])
def handle_keypress(event):
'''creates the spacing for correct letters
event - if keyboard is used'''
global wrong_answerz,right_answerz, len_of_puzzle, used_char, done
if done:
main_window.destroy()
elif event.char not in used_char:
used_char += event.char
if event.char in legal_chars:
for i in range(len(puzzle)):
if event.char == puzzle[i]:
c.create_text(20+i*25,480,text=event.char, font = ('LCDMono2',30))
right_answerz = right_answerz + 1
if wrong_answerz < 7 and right_answerz == len_of_puzzle:
c.create_text(500,300,text="you miraculously didn't die.(press any character to quit)", font = ('LCDMono2',30))
done = True
if event.char not in puzzle:
wrong_answerz = wrong_answerz + 1
wrong(wrong_answerz)
c.create_text(500+wrong_answerz*25,200,text=event.char, font = ('LCDMono2',30))
def wrong(wrong_answerz):
'''draws the hangman and tells you if you lost
wrong_answerz - the amount of incorrect guesses'''
global done
if wrong_answerz == 1:
c.create_oval(50,60,130,140)
elif wrong_answerz == 2:
c.create_line(90,140,90,250)
elif wrong_answerz == 3:
c.create_line(90,250,50,320)
elif wrong_answerz == 4:
c.create_line(90,250,130,320)
elif wrong_answerz == 5:
c.create_line(90,180,130,180)
elif wrong_answerz == 6:
c.create_line(90,180,50,180)
c.create_text(500,300,text='you died. rip.(press any character to quit)', font = ('LCDMono2',30))
done = True
main_window = Tk()
main_window.title("Brooke's hangman bonanza")
puzzle = read_phrase()
c = Canvas(main_window,width=1000, height=500)
c.pack()
c.bind('<Key>',handle_keypress)
c.focus_set()
intro()
draw_word_lines(puzzle)
draw_gallows()
mainloop()