How to make hangman in Processing.py (Solved)

Hey all, so I’m trying to make a Hangman game in Processing.py and have written code for both how I want it to look, as well as separate code to run the game. The problem is combining the two in Processing. Is there a way to make a Hangman game with Processing.py? Or am I just wasting my time? Thanks in advance to all that help.

Code for Hangman game (non-processing)

import time
import random
name = input("What is your name? ")
print ("Hello, " + name, "Time to play hangman!")
print ('')
time.sleep(2)
print ("Start guessing...")
bank = ["ironman", "hulk", "captain america", "black widow", "thor", "hawkeye"]
r = (random.randint(0,5))
word = bank[r]
guesses = ''
turns = 10
while turns > 0:         
    failed = 0             
    for char in word:      
        if char in guesses:    
            print (char, )
        else:
            print ("_", )   
            failed += 1
    if failed == 0:
        print  ("You won")  
        break              
    print
    guess = input("guess a character:") 
    guesses += guess                    
    if guess not in word:  
        turns -= 1        
        print ("Wrong")   
        print ("You have", + turns, 'more guesses' )
        if turns == 0:           
            print ("You Lose")

Code for Hangman visuals (Processing)

def setup():
    size(250,350)
    background(102,204,255)
    fill(17,214,11)
    rect(0,201,250,350)
   
def draw():
    strokeWeight(2)
    line(100,100,100,200)
    line(100,100,150,100)
    line(150,100,150,125)
    line(75,200,125,200)
    line(85,200,100,175)
    line(115,200,100,175)

It’s totally doable. Restructure your code to work with Processing’s setup(), draw(), and keyPressed() functions –

bank = ['ironman', 'hulk', 'captain america']
word = bank[int(random(0, len(bank)))]
guesses = ''

def setup():
    size(250, 350)
   
def draw():
    background(102, 204, 255)
    fill(17, 214, 11)
    rect(-10, 200, 270, 350)
    line(100, 100, 100, 200)
    line(100, 100, 150, 100)
    line(150, 100, 150, 125)
    fill(0)
    text('Start guessing...', 20, 250)
    text(guesses, 20, 300)
    
def keyPressed():
    global guesses
    guesses += key
    ...

You can add your guesses logic (to check for correct letters, etc.) to the keyPressed() block.

2 Likes

Thank you for your help so far! Can you by chance show me what you mean by add my guesses logic to the keyPressed() block? I keep trying, but get an error each time. Sorry if this is really a simple task. This is my still new with Python, and very new to Processing.

You’ll need to consider how you draw each part of the man governed by how many turns remain. In addition, you don’t want to rely on the console to display messages to the player – so you might use a message variable to pass that text to the draw() function.

...
turns = 10
message = ''
...
   
def draw():
    ...
    text(message, 20, 300)

    if turns <= 9:  # draw head
        circle(150, 130, 15)
    if turns <= 8:  # draw torso
        ...
    
def keyPressed():
    global guesses, message, turns
    ...
    if key in word:
        message = key + ' is correct'
    else:
        message = key + ' is incorrect'
        turns -= 1
1 Like

Once again thank you for the help @tabreturn! The code is working good so far, but when I try to add the message variable it give me an error saying its undefined. Here is what I have so far:

import sys
bank = ['ironman', 'hulk', 'captain america']
word = bank[int(random(0, len(bank)))]
turns = 7
guesses = ''

def setup():
    size(250, 350)
   
def draw():
    background(102, 204, 255)
    fill(17, 214, 11)
    rect(-10, 200, 270, 350)
    line(100, 100, 100, 200)
    line(100, 100, 150, 100)
    line(150, 100, 150, 125)
    fill(0)
    text('Start guessing...', 20, 250)
    text(message, 20, 300)
    if turns <= 6:
        circle(150, 130, 15)
    if turns <= 5:
        line(150, 125, 150, 165) 
    if turns <= 4:
        line(150, 145, 140, 155)
    if turns <= 3:
        line(150, 145, 160, 155)
    if turns <= 2:
        line(150, 165, 140, 175)
    if turns <= 1:
        line(150, 165, 160, 175)
        text('You lose', 20, 268)
        text('Press F to exit', 20, 285)
    if turns <= 0:
        sys.exit()
    
def keyPressed():
    global guesses, message, turns
    guesses += key
    if key in word:
        message = key + ' is correct'
    else:
        message = key + ' is incorrect'
        turns -= 1

Ah never mind. I have fixed the problem. Sorry for the ping

Ok @tabreturn I have the game running great thanks to you! My last ask is if there is a way to add a win condition that shows the correct word as it is written. I was able to add a loss condition and a message and even a way to end the program, but I need a way to let the player know they won. Here is the code currently:

import sys
bank = ['ironman', 'hulk', 'captain america']
word = bank[int(random(0, len(bank)))]
guesses = ''
turns = 7
message = ''

def setup():
    size(250, 350)
   
def draw():
    background(102, 204, 255)
    fill(17, 214, 11)
    rect(-10, 200, 270, 350)
    line(100, 100, 100, 200)
    line(100, 100, 150, 100)
    line(150, 100, 150, 125)
    fill(0)
    text('Start guessing...', 20, 250)
    text(message, 20, 300)
    if turns <= 6:
        circle(150, 130, 15)
    if turns <= 5:
        line(150, 125, 150, 165) 
    if turns <= 4:
        line(150, 145, 140, 155)
    if turns <= 3:
        line(150, 145, 160, 155)
    if turns <= 2:
        line(150, 165, 140, 175)
    if turns <= 1:
        line(150, 165, 160, 175)
        text('You lose', 20, 268)
        text('Press F to exit', 20, 285)
    if turns <= 0:
        sys.exit()
    
def keyPressed():
    global guesses, message, turns
    guesses += key
    if key in word:
        message = key + ' is correct'
    else:
        message = key + ' is incorrect'
        turns -= 1

You’ve already provided the solution for your last question in your initial post. You need to adapt the “non-Processing” code to work with the Processing structure. So this –

while turns > 0:         
    failed = 0             
    for char in word:      
        if char in guesses:    
            print (char, )
        else:
            print ("_", )   
            failed += 1
    if failed == 0:
        print  ("You won") 

Becomes something like –

def keyPressed():
    ...
    display = ''
    failed = 0
    for char in word:      
        if char in guesses:    
            display += char
        else:
            display += '_'
            failed += 1
    if failed == 0:
        display = 'You won'

Ok so it gave it a win condition (thanks for that), but when you type either a correct or incorrect letter it adds a LOT of dashes and eventually goes off screen. How can I fix this as nothing I have done so far seems to help.

Code as of now:

import sys
bank = ['ironman', 'hulk', 'captain america']
word = bank[int(random(0, len(bank)))]
guesses = ''
turns = 7
message = ''
display = ''

def setup():
    size(250, 350)
   
def draw():
    background(102, 204, 255)
    fill(17, 214, 11)
    rect(-10, 200, 270, 350)
    line(100, 100, 100, 200)
    line(100, 100, 150, 100)
    line(150, 100, 150, 125)
    fill(0)
    text('Start guessing...', 20, 250)
    text(message, 20, 300)
    text(display, 20, 225)
    if turns <= 6:
        circle(150, 130, 15)
    if turns <= 5:
        line(150, 125, 150, 165) 
    if turns <= 4:
        line(150, 145, 140, 155)
    if turns <= 3:
        line(150, 145, 160, 155)
    if turns <= 2:
        line(150, 165, 140, 175)
    if turns <= 1:
        line(150, 165, 160, 175)
        text('You lose', 20, 268)
        text('Press F to exit', 20, 285)
    if turns <= 0:
        sys.exit()
    
def keyPressed():
    global guesses, message, turns, display
    guesses += key
    if key in word:
        message = key + ' is correct'
    else:
        message = key + ' is incorrect'
        turns -= 1
    failed = 0
    for char in word:      
        if char in guesses:    
            display += char
        else:
            display += '_'
            failed += 1
    if failed == 0:
        display = 'You won!'

Alright I was able to make the dashes go away by removing the code "display += ‘_’ ", but now the letters that get written out are all screwed up. Here is what it looks like:
Capture

Code as of now:

import sys
bank = ['ironman', 'hulk', 'captain america']
word = bank[int(random(0, len(bank)))]
guesses = ''
turns = 7
message = ''
display = ''

def setup():
    size(250, 350)
   
def draw():
    background(102, 204, 255)
    fill(17, 214, 11)
    rect(-10, 200, 270, 350)
    line(100, 100, 100, 200)
    line(100, 100, 150, 100)
    line(150, 100, 150, 125)
    fill(0)
    text('Start guessing...', 20, 250)
    text(message, 20, 300)
    text(display, 20, 225)
    if turns <= 6:
        circle(150, 130, 15)
    if turns <= 5:
        line(150, 125, 150, 165) 
    if turns <= 4:
        line(150, 145, 140, 155)
    if turns <= 3:
        line(150, 145, 160, 155)
    if turns <= 2:
        line(150, 165, 140, 175)
    if turns <= 1:
        line(150, 165, 160, 175)
        text('You lose', 20, 268)
        text('Press F to exit', 20, 285)
    if turns <= 0:
        sys.exit()
    
def keyPressed():
    global guesses, message, turns, display
    guesses += key
    if key in word:
        message = key + ' is correct'
    else:
        message = key + ' is incorrect'
        turns -= 1
    failed = 0
    for char in word:      
        if char in guesses:    
            display += char
        else:
            failed += 1
    if failed == 0:
        display = 'You won!'

If you take away underscores/dashes, how does the player know which letters are missing? For example, if the solution is captain america and the player has typed a, p, r and space, the game should display:

_ap_a__ a__r__a

Ah alright I didn’t realize that, but there is still the problem of it going off screen. Is there a way for it to display the dashed lines at the start and then replace them with the letters as they are guessed? Here is what it looks like going off screen.
Capture 2

There’s a pattern here –

i______ir_____iro____

is equal to

i______ + ir_____ + iro____

So you’re adding to the display variable each time (a key is pressed) when you should clear it first.

Got it. Ill try to figure it out and message again if I cant. Thank you!

Alright I’m lost. I cant figure out how to clear the display. It keeps adding onto it. Even tried google and nothing came up.

Read through your code carefully to understand how it works. Maybe you’ve missed something.

@tabreturn I am still lost. I added "display = ’ ’ " above “failed = 0” and now it only shows a dash and the 4 letter of each word.

Finally Got everything working! Thank you so much @tabreturn for all your help!

Final Code:

import sys
bank = ['ironman', 'hulk', 'captain america', 'blackwidow', 'hawkeye']
word = bank[int(random(0, len(bank)))]
guesses = ''
turns = 7
message = ''
display = ''

def setup():
    size(250, 350)
   
def draw():
    background(102, 204, 255)
    fill(17, 214, 11)
    rect(-10, 200, 270, 350)
    line(100, 100, 100, 200)
    line(100, 100, 150, 100)
    line(150, 100, 150, 125)
    fill(0)
    text('Start guessing...', 20, 250)
    text(message, 20, 300)
    text(display, 20, 225)
    if turns <= 6:
        circle(150, 130, 15)
    if turns <= 5:
        line(150, 125, 150, 165) 
    if turns <= 4:
        line(150, 145, 140, 155)
    if turns <= 3:
        line(150, 145, 160, 155)
    if turns <= 2:
        line(150, 165, 140, 175)
    if turns <= 1:
        line(150, 165, 160, 175)
        text('You lose', 20, 268)
        text('Press F to exit', 20, 285)
    if turns <= 0:
        sys.exit()
    
def keyPressed():
    global guesses, message, turns, display
    guesses += key
    if key in word:
        message = key + ' is correct'
    else:
        message = key + ' is incorrect'
        turns -= 1
    display = ''
    failed = 0
    for char in word:      
        if char in guesses:    
            display += char
        else:
            display += '_'
            failed += 1
    if failed == 0:
        display = 'You won!'
1 Like