Alex Majkic

Hangman Game Code

This is my hangman program.

  • December 19, 2016 at 6:03 AM
  • Visible to public
#Alex Majkic, Mods 3-4
#This program allows the user to play hangman

from tkinter import *
import random

legal_chars = 'abcdefghijklmnopqrstuvwxyz'
used_letters = ""
wrong = 0
done = False

def draw_gallows():
    '''This function draws the gallows'''
    c.create_line(335,350, 415,350)
    c.create_line(375,350, 375,125)
    c.create_line(375,125, 430,125)
    c.create_line(430,125, 430,150)

def draw_head():
    '''This function draws the head'''
    c.create_oval(410,150, 450,190)

def draw_body():
    '''This function draws the body'''
    c.create_line(430,190, 430,275)

def draw_l_arm():
    '''This function draws the left arm'''
    c.create_line(430,212, 400, 250)

def draw_r_arm():
    '''This function draws the right arm'''
    c.create_line(430,212, 460,250)

def draw_l_leg():
    '''This function draws the left leg'''
    c.create_line(430,275, 410, 315)

def draw_r_leg():
    '''This function draws the right leg'''
    c.create_line(430,275, 450, 315)

def read_phrase():
    '''This function generates the phrase for the game'''
    f = open('hangman371.txt')
    selected_line = random.randrange(0, 371)
    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.lower()

def handle_keypress(event):
    '''This function makes the letters appear on the screen
    and notes whether or not the player has chosen a correct character
    event = when the player types a character'''
    global used_letters
    global wrong
    global done
    ps = set(puzzle)
    uls = 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)
                        uls = set(used_letters)
        elif event.char in legal_chars and event.char in used_letters:
                print("This character has already been used, please try again")            
        elif event.char not in puzzle:
            if event.char in legal_chars:
               wrong = wrong + 1
               print("You have gotten " + str(wrong) +" characters wrong")
               man()
            elif event.char not in legal_chars:
                print("This character is an illegal character, please try again")
        if (ps & set(legal_chars)) - uls == set():
            print("You win!")
            done = True
    if 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 draw_lines():
    '''This function draws the lines for the characters'''
    x = 5
    counter = 0
    while counter < len(puzzle):
        if puzzle[counter] in legal_chars:
            c.create_line(x,400, (x+10), 400)
            x = x + 20
            counter = counter + 1
        elif puzzle[counter] == "'":
            c.create_text(x,380, text = puzzle[counter])
            x = x + 20
            counter = counter + 1
        else:
            c.create_text(x,400,text = puzzle[counter])
            x = x + 20
            counter = counter + 1

def man():
    '''This program forms the hangman based on how many characters the character has gotten wrong'''
    global wrong
    global done
    if done == False:
       if wrong == 1:
            draw_head()
       elif wrong == 2:
            draw_body()
       elif wrong == 3:
            draw_l_arm()
       elif wrong == 4:
            draw_r_arm()
       elif wrong == 5:
            draw_l_leg()
       elif wrong == 6:
            draw_r_leg()
            print("You lose, the puzzle was:" +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 game_setup():
    '''This function sets up the game'''
    global puzzle
    global done
    global used_letters
    global wrong
    c.create_rectangle(0,0, 800,750, fill="white")
    puzzle = read_phrase()
    draw_lines()
    draw_gallows()
    done = False
    used_letters = ""
    wrong = 0
    

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

game_setup()

mainloop()