# Name: Mike Abrahamson
# Game of Hangman
# uses file dictionaryWords.txt
from random import randrange
import string
from graphics import *
from button import Button
def makeWin(): #makes the window
win = GraphWin("Mike's Hangman", 600, 600)
win.setCoords(0,0,600,600) #resets the cords to 0,0
win.setBackground('slategrey') #set background color to yellow
return win
def makeButtons(win): #this makes the letter buttons
y = 550
val = ord('A')
letter_buttons = [] #sets the buttons to an emply list to be appended and to make
#a table of letter buttons.
for k in range(7):
x = 400
for j in range(4):
pt = Point(x,y)
letter = Button(win, pt, 45, 45, chr(val), 'green2')
letter.activate()
letter_buttons.append(letter)
x = x + 45
val = val + 1
y = y - 45
for j in range(1,3):
letter_buttons[-j].setLabel("") # this sets the lables of each button with the letters in alpahbetical order
return letter_buttons
def getData():
infile = open('dictionaryWords.txt', 'r') # opens the file to be read
words = infile.readlines() #reads the file lines
wordList = []
for j in words:
wordList.append(j[:-1]) # appens the blank list with a word from the file
return wordList, len(wordList)
def getWord(wList, cnt):
x = randrange(1, (cnt+1))
theWord = string.upper(wList[x])
return theWord
def makeStop(win):
stop = Button(win, Point(560,35), 60, 30, "Quit", 'red')
#stop.activate()
return stop
def makeAgain(win):
again = Button(win, Point(485,35), 80, 30, "Play Again", 'green')
#again.activate()
return again
def makeGallow(win):
window = Rectangle(Point(10,150),Point(350,590))
window.setFill('white')
window.draw(win)
win1 = Rectangle(Point(17,10),Point(370,100))
win1.setFill('white')
win1.draw(win)
win1txt = Text(Point(180,80),'Use the keypad to guess a leter\n the word is:')
win1txt.draw(win)
line1 = Line(Point(15,165), Point(250,165))
line1.setWidth(5)
line1.draw(win)
line2 = Line(Point(25,165), Point(25,580))
line2.setWidth(5)
line2.draw(win)
line3 = Line(Point(23,580), Point(200,580))
line3.setWidth(5)
line3.draw(win)
line4 = Line(Point(200,583), Point(200,500))
line4.setWidth(5)
line4.draw(win)
def bodyList(win):
bodyParts = []
head = Circle(Point(200,470), 30)
head.setWidth(5)
bodyParts.append(head)
torso = Line(Point(200,440), Point(200,340))
torso.setWidth(5)
bodyParts.append(torso)
rArm = Line(Point(200,410), Point(175,360))
rArm.setWidth(5)
bodyParts.append(rArm)
lArm = Line(Point(200,410), Point(225,360))
lArm.setWidth(5)
bodyParts.append(lArm)
rLeg = Line(Point(200,340), Point(175,280))
rLeg.setWidth(5)
bodyParts.append(rLeg)
lLeg = Line(Point(200,340), Point(225,280))
lLeg.setWidth(5)
bodyParts.append(lLeg)
return bodyParts
def guesses(win):
guesses = Text(Point(450,175), "Guesses Remaining:")
guesses.setSize(14)
guesses.draw(win)
remain = Text(Point(550,175), "6")
remain.setSize(20)
remain.setFill('blue')
remain.draw(win)
return remain
def makeBlanks(win, word):
blankList = []
for j in range (len(word)):
blankList.append("_")
return blankList
def spellWord(win):
blanks = Text(Point(170,35), "")
blanks.draw(win)
blanks.setSize(16)
blanks.setFill('black')
return blanks
def setText(bList, text):
bStr = ""
for k in bList:
bStr = bStr + " " + k
text.setText(bStr)
def getGuess(win, letters):
selected = False
while not selected:
pt = win.getMouse()
for j in letters:
if j.clicked(pt):
choice = j.label.getText()
j.deactivate()
selected = True
def compare(bList, word, guess):
found = False
for k in range(len(word)):
if guess == word[k]:
bList[k] = guess
found = True
return found
def lessTries(remain, win, body):
tries = eval(remain.getText()) - 1
remain.setText(str(tries))
index = 6 - (tries+1)
body[index].draw(win)
return tries
def youWin(win):
winner = Text(Point(500,120), "Congratulations!")
winner.setFill('green')
winner.setSize(16)
winner.draw(win)
def youLost(win, word):
loser = Text(Point(470,120),"YOU LOST!\nThe Word Was\n "+word)
loser.setFill('red')
loser.setSize(16)
loser.draw(win)
def playGame(win, words, count):
letters = makeButtons(win)
g_word = getWord(words, count)
p_a = makeAgain(win)
stop = makeStop(win)
makeGallow(win)
bodyParts = bodyList(win)
remain = guesses(win)
blankList = makeBlanks(win, g_word)
text = spellWord(win)
found = False
while not found:
setText(blankList, text)
guess = getGuess(win, letters)
find = compare(blankList, g_word, guess)
if find == False:
tries = lessTries(remain, win, bodyParts)
else:
tries = eval(remain.getText())
cnt = string.count(blankList, "_")
if cnt == 0:
setText(blankList, text)
winner = youWin(win)
found = True
elif tries == 0:
lost = youLost(win, g_word)
found = True
return p_a, stop, remain, text
def playAgain(win, again, stop, num, under):
again.activate()
stop.activate()
clicked = False
while not clicked:
p = win.getMouse()
if again.clicked(p):
clicked = True
again.erase('slategrey')
stop.erase('slategrey')
num.undraw()
under.undraw()
keep_playing = True
elif stop.clicked(p):
clicked = True
keep_playing = False
return keep_playing
def main():
win = makeWin()
words, count = getData()
playing = True
while playing:
again, stop, num, under = playGame(win, words, count)
playing = playAgain(win, again, stop, num, under)
win.close()
main()
Python hangman game helpneed help figuring out what i am missing in my code
Page 1 of 1
0 Replies - 1994 Views - Last Post: 10 April 2009 - 05:25 AM
#1
Python hangman game help
Posted 10 April 2009 - 05:25 AM
ok, well I have everything running just fine, the only problem i'm having is when the play again button is clicked, i cant get my youWin and youLost text to reset back to blank, by you win and you lose i mean my , ''congratulations'', and ''you lost the word was..''
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote


|