Simon Ren

Hangman Game Code

Hangman Assignment Code

  • January 13, 2017 at 9:47 PM
  • Visible to public
#Simon Ren
#This program plays the Hangman Game
import random
from tkinter import *

def read_phrase():
    ''' Generates a phrase to be used in the puzzle'''
    f = open('hangman.txt')
    selected_line = random.randrange(0,371) #change 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()
    return puzzle

def draw_gallows():
    ''' Draws the gallows for the Hangman game '''
    c.create_line(0, 600, 80, 600)
    c.create_line(40, 600, 40, 300)
    c.create_line(40, 300, 120, 300)
    c.create_line(120, 300, 120, 350)

def draw_head():
    ''' Draws the head of the Hangman'''
    c.create_oval(80, 350, 160, 430)

def draw_body():
    ''' Draws the body of the Hangman '''
    c.create_line(120, 430, 120, 520)

def draw_arm1():
    ''' Draws the left arm of the Hangman '''
    c.create_line(100, 490, 120, 460)

def draw_arm2():
    ''' Draws the right arm of the Hangman '''
    c.create_line(140, 490, 120, 460)

def draw_leg1():
    ''' Draws the left leg of the Hangman '''
    c.create_line(100, 540, 120, 520)

def draw_leg2():
    ''' Draws the right leg of the Hangman '''
    c.create_line(120, 520, 140, 540)

def mistake():
    ''' Draws the corresponding body part for each mistake the user makes
        If the body is completely drawn, displays a Lose message '''
    if incorrect < 7:
        if incorrect == 1:
            draw_head()
        elif incorrect == 2:
            draw_body()
        elif incorrect == 3:
            draw_leg1()
        elif incorrect == 4:
            draw_leg2()
        elif incorrect == 5:
            draw_arm1()
        elif incorrect == 6:
            draw_arm2()
            c.create_text(350, 450, text="Lost :(")
            c.create_text(350, 350, text=secret_phrase)
    
     
def handle_keypress(event):
    ''' Allows the program to respond to keystrokes
        Creates the inputed character in the correct location
        If the phrase is completed, displays a Win message '''
    global correct
    global incorrect
    if event.char in legal_chars:
        for i in range(len(secret_phrase)):
            if event.char == secret_phrase[i]:
                c.create_text(16 + i*20, 35, text=event.char)
                correct += 1
        if correct == letternum:
            c.create_text(350, 450, text="Winner!  :)")
    if event.char in legal_chars:
        if event.char not in secret_phrase:
            incorrect += 1
            mistake()
            c.create_text(100 + incorrect*20, 150, text=event.char)

def blanks():
    ''' Draws the blanks for the phrase that the useer must complete '''
    global letternum
    for i in range(len(secret_phrase)):
        if secret_phrase[i] in legal_chars:
            c.create_line(15+i*20, 45, 25+i*20, 45)
            letternum += 1
        else:
            c.create_text(15 + i*20, 35, text=secret_phrase[i])

        




read_phrase()
letternum = 0
correct = 0
incorrect = 0
legal_chars = 'abcdefghijklmnopqrstuvwxyz'
secret_phrase = read_phrase()
secret_phrase = secret_phrase.lower()

main_window = Tk()
main_window.title("Hangman")
    
c = Canvas(main_window, width=800, height=800)
c.pack()
c.bind('<Key>', handle_keypress)
c.focus_set()
draw_gallows()
blanks()


mainloop()