Ethan Bowman

Hangman Game Code

My python hangman game program.

  • January 2, 2017 at 3:47 PM
  • Last updated almost 9 years ago
  • Visible to public
#Ethan Bowman
#Hangman Game
from tkinter import*
import random
import sys
import os


legal_char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
punctuation = " ' " 

def read_phrase():#Choses one random phrase
    global puzzle
    f = open('hangman.txt')
    selected_line = random.randrange(0, 370)
    for i in range(selected_line-1):
        f.readline()
    puzzle = f.readline()
    f.close()
read_phrase()

secret_phrase = puzzle.lower()

def num_characters(): #Counts the number of characters in the phrase
    global total
    for o in range(len(secret_phrase)):
        if secret_phrase[o] in legal_char:
            total = total + 1
    return total

def draw_game(): #Draws gallows and letter underlining
    a = 0
    for i in range (len(secret_phrase)):
        if secret_phrase[i] in legal_char:
            c.create_line (250-(len(secret_phrase)*2.5)+(15*a), 550,  260 - (len(secret_phrase)*2.5) + (15*a), 550)
        if secret_phrase[i] in punctuation:
            c.create_text(253 - (len(secret_phrase)*2.5) +i*15.1, 540, text=secret_phrase[i])
        a = a + 1
    c.create_line(250, 450,  550, 450,)
    c.create_line(400, 450,  400, 100,  325, 100,  325, 120)

def handle_keypress(event): #Processes user input
    global b
    global t
    global tot_chars
    if event.char in legal_char:
        if event.char not in tot_chars:
            for i in range(len(secret_phrase)):
                if event.char == secret_phrase[i]:
                    c.create_text(253 - (len(secret_phrase)*2.5) +i*15.1, 540, text=event.char)
                    t = t + 1
                    if t == total:
                        c.create_text(500, 250, text="You Win!")
                        end_of_game()
            if event.char not in secret_phrase:
                c.create_text(253 + b, 600, text=event.char)
                draw_body()
                b = b + 12
            tot_chars = tot_chars + event.char
    if event.char == " ":
        python = sys.executable
        os.execl(python, python, * sys.argv)
    
def draw_body(): #Draws new body part each time called 
    global n
    if n == 1:
        #Draw head
        c.create_oval(295, 120,  355, 180 )
        n = n + 1
        return
    if n == 2:
        #Draw body
        c.create_line(325, 180,  325, 300)
        n = n + 1
        return
    if n == 3:
        #Draw left leg
        c.create_line(325, 300,  275, 370)
        n = n + 1
        return
    if n == 4:
        #Draw right leg
        c.create_line(325, 300,  375, 370)
        n = n + 1
        return
    if n == 5:
        #Draw right arm
        c.create_line(325, 200, 375, 250)
        n = n + 1
        return
    if n == 6:
        #Draw left arm
        c.create_line(325, 200, 275, 250)
        c.create_text(500, 250, text="You Loose!")
        end_of_game()

def end_of_game(): #Completes puzzle and finishes game
    for i in range(len(secret_phrase)):
        if secret_phrase[i] not in tot_chars:
            c.create_text(253 - (len(secret_phrase)*2.5) +i*15.1, 540, text=secret_phrase[i])
    c.create_text(550, 320, text="Press space bar to continue")

      
n = 1
b = 0
t = 0
tot_chars = " "
total = 0

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_game()
num_characters()

mainloop()