Lucas Lucas - 13 days ago 2
Python Question

tkinter window hangman game

Upon creating a Graphics window based version of hangman, there is one problem I have come across that stops the program from working:

Traceback (most recent call last):
File "Z:\Documents\Hangman-master\hangman.py", line 325, in <module>
main(False, False)
File "Z:\Documents\Hangman-master\hangman.py", line 157, in main
win_butguess_label.setTextColor(color_rgg(0, 110, 0))
NameError: global name 'color_rgg' is not defined


I have looked round lots of times (it's a long code) and haven't found anything wrong with it. I will post the area it says it comes up in, and if anyone can find what's wrong, I'd be grateful.

def main(win, infile_name):
# If a word list file hasn't yet been sepcified, then we need to get one
if not(infile_name):
infile_name = getInfile()

# Choose a word at random from the acquired word list
word, word_len = chooseWord(infile_name)

# Build the grid of empty spaces, one space for each letter of the chosen word
grid = '__'
for i in range(word_len - 1):
grid = grid + ' __'

# Generate the graphics window for the game's GUI
win_width = 500
win_height = 460
if not(win):
# This will only be called on the first play-through of the game, so that
# a new window isn't generated each time a player chooses to "play again"
win = GraphWin("Hangman", win_width, win_height)
win.setCoords(0, 0, win_width, win_height)

# Draw the game message area, the ground of the Hangman graphic, and the empty grid
win_message = Text(Point(win_width / 2, 70), "Let's play! Guess a letter below to see if it's in the word.")
win_message.setStyle('bold')
win_message.draw(win)
win_ground = Line(Point(win_width / 10, 100), Point(win_width - win_width / 10, 100))
win_ground.draw(win)
win_grid = Text(Point(win_width / 2, win_height - 70), grid)
win_grid.draw(win)

# Draw the input box and accompanying text label
win_guesstxt = Text(Point(win_width / 2 - 35, 30), "Type a letter:")
win_guesstxt.draw(win)
win_guessinput = Entry(Point(win_width / 2 + 30, 28), 2)
win_guessinput.draw(win)
win_guessinput.setText('')

# Draw the "Guess it!" button and its label
win_butguess = Rectangle(Point(win_width / 2 + 55, 43), Point(win_width / 2 + 130, 13))
win_butguess.setFill(color_rgb(126, 236, 53))
win_butguess.setOutline(color_rgb(0, 110, 0))
win_butguess.draw(win)
win_butguess_label = Text(Point(win_width / 2 + 94, 28), 'Guess it!')
win_butguess_label.setTextColor(color_rgg(0, 110, 0))
win_butguess_label.draw(win)

# Now, we keep taking guesses of letters until either the player amounts 7 total strikes (wrong
# guesses), or until the player correctly guesses the entire word and the game is won
strikes = 0
guessed_letters = []
win_hangmanpic = []
game_won = False
while strikes < 7 and game_won == False:
# Log the player's mouse click location
p = win.getMouse()
# If the player clicked outside of the area of the "Guess it!" button, then we go back to the start
# of the loop and wait for another click
if p.getX() < win_width / 2 + 55 or p.getX() > win_width / 2 + 130 or p.getY() < 13 or p.getY() > 43:
continue

# Grab the guessed letter, and if it's invalid, go back to the top of the loop and wait for another guess
guess = win_guessinput.getText().lower()
win_guessinput.setText('')
if guess == ' ' or guess == '' or len(guess) != 1:
if len(guess) != 1:
win_message.setText("Please only guess a single letter at a time. Try again!")
continue

# If the guessed letter is in the word, and it hasn't been guessed yet by the player, then we update our
# grid by placing this letter in the appropriate empty spaces on the grid
if guess in word.lower() and not(guess in guessed_letters):
guessed_letters.append(guess)
grid = word
for letter in word:
if (not (letter.lower() in word) or not(letter.lower() in guessed_letters)) and letter != ' ':
grid = grid.replace(letter, ' __ ')

win_grid.setText(grid)

if grid == word:
game_won = True
else:
win_message.setText("Nice! {0} is in the word. Try another letter.".format(guess.upper()))
elif guess in guessed_letters:
# This letter has already been guessed, so alert the player about this and wait for another guess
win_message.setText("You've already guessed {0}. Try another letter.".format(guess.upper()))
else:
# Wrong guess! Add a strike and draw a new piece of the Hangman picture
guessed_letters.append(guess)
strikes = strikes + 1
win_message.setText('{0} is a wrong guess! Try another letter.'.format(guess.upper()))
drawPiece(strikes, win, win_width, win_height, win_hangmanpic)

# The game is over, so let's remove some of the objects at the bottom of the window
# to make room for the "Quit" and "Play Again?" buttons and the final message
win_guesstxt.undraw()
win_guessinput.undraw()
win_butguess.undraw()
win_butguess_label.undraw()
win_message.move(0, -10) # Move the message area down a bit to fill up some space

# Update the grid to display the full, actual word that the program chose
win_grid.setText(word.upper())
win_grid.setStyle('bold')
win_grid.setSize(16)

# Change the message at the bottom of the window to reflect a win or loss
if game_won == True:
win_message.setText("Congrats! You've guessed the word correctly.")
win_message.setTextColor(color_rgb(0, 120, 0))
win_grid.setTextColor(color_rgb(0, 120, 0))
# Remove the incomplete Hangman picture
for hm_obj in win_hangmanpic:
hm_obj.undraw()
win_hangmanpic = []
# And replace it with a picture that's representative of winning the game
circle1 = Circle(Point(win_width / 2, 240), 110)
circle1.setOutline(color_rgb(0, 120, 0))
circle1.setFill(color_rgb(126, 236, 53))
circle1.draw(win)
win_hangmanpic.append(circle1)
eye1 = Circle(Point(win_width / 2 - 40, 285), 20)
eye1.setOutline(color_rgb(0, 120, 0))
eye1.draw(win)
win_hangmanpic.append(eye1)
eye2 = eye1.clone()
eye2.move(80, 0)
eye2.draw(win)
win_hangmanpic.append(eye2)
eye1_inner = Circle(Point(win_width / 2 - 40, 285), 5)
eye1_inner.setFill(color_rgb(0, 120, 0))
eye1_inner.setOutline(color_rgb(0, 120, 0))
eye1_inner.draw(win)
win_hangmanpic.append(eye1_inner)
eye2_inner = eye1_inner.clone()
eye2_inner.move(80, 0)
eye2_inner.draw(win)
win_hangmanpic.append(eye2_inner)
mouth = Circle(Point(win_width / 2, 200), 50)
mouth.setFill(color_rgb(0, 120, 0))
mouth.setOutline(color_rgb(0, 120, 0))
mouth.draw(win)
win_hangmanpic.append(mouth)
mouth_cover = Rectangle(Point(win_width / 2 - 50, 200), Point(win_width / 2 + 50, 250))
mouth_cover.setFill(color_rgb(126, 236, 53))
mouth_cover.setOutline(color_rgb(126, 236, 53))
mouth_cover.draw(win)
win_hangmanpic.append(mouth_cover)
else:
win_message.setText("Sorry! You didn't completely guess the word.")
win_message.setTextColor(color_rgb(170, 0, 0))
win_grid.setTextColor(color_rgb(170, 0, 0))
# And make the lines drawn in the Hangman picture red
i = 0
for hm_obj in win_hangmanpic:
if i == 3:
# This is the guy's head, which is a circle, so it requires a different function
# to change the circle's color
hm_obj.setOutline(color_rgb(170, 0, 0))
else:
hm_obj.setFill(color_rgb(170, 0, 0))
# Increment the index counter
i = i + 1

# Draw a button to ask if the player wants to play again
win_butagain = Rectangle(Point(win_width - 90, 30), Point(win_width, 0))
win_butagain.setFill(color_rgb(126, 236, 53))
win_butagain.setOutline(color_rgb(0, 110, 0))
win_butagain.draw(win)
win_butagain_label = Text(Point(win_width - 45, 15), 'Play again?')
win_butagain_label.setTextColor(color_rgb(0, 110, 0))
win_butagain_label.draw(win)

# As well as a button for if the player want to quit the game
win_butquit = Rectangle(Point(4, 30), Point(54, 0))
win_butquit.setFill(color_rgb(255, 177, 177))
win_butquit.setOutline(color_rgb(170, 0, 0))
win_butquit.draw(win)
win_butquit_label = Text(Point(29, 15), 'Quit')
win_butquit_label.setTextColor(color_rgb(170, 0, 0))
win_butquit_label.draw(win)

Answer
    color_rgg

should be

    color_rgb

(as red, green, blue).

Comments