# How to make hangman in Processing.py (Solved)

**URL:** <https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499>\
**Category:** Processing.py\
**Created:** [June 5, 2021, 2:20am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499 "2021-06-05T02:20:51Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 5, 2021, 2:20am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/1 "2021-06-05T02:20:51Z")

</div>

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)

```auto
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)

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 5, 2021, 10:06am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/2 "2021-06-05T10:06:15Z")

</div>

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

```python
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.

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 6, 2021, 12:55am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/3 "2021-06-06T00:55:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 6, 2021, 1:46am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/4 "2021-06-06T01:46:31Z")

</div>

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.

```python
...
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

```

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 5:23am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/5 "2021-06-07T05:23:12Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 5:27am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/6 "2021-06-07T05:27:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 5:35am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/7 "2021-06-07T05:35:36Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 7, 2021, 10:52am UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/8 "2021-06-07T10:52:56Z")

</div>

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 –

```python
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 –

```python
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'

```

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 5:47pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/9 "2021-06-07T17:47:07Z")

</div>

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:

```auto
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!'

```

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 7:19pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/10 "2021-06-07T19:19:23Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/ba0edba88d743d095ed9b0422083675e26037aaa.png)

Code as of now:

```auto
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!'

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 7, 2021, 9:13pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/11 "2021-06-07T21:13:32Z")

</div>

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:

```nohighlight
_ap_a __a__ r__a

```

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 9:23pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/12 "2021-06-07T21:23:17Z")

</div>

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4bc67d261a7514aa4b4e5088ffe4cdcfe387a8d7.png)

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 7, 2021, 9:32pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/13 "2021-06-07T21:32:19Z")

</div>

There’s a pattern here –

```auto
i ______ir_____ iro ____

```

is equal to

```auto
i ______+ ir_____ + iro ____

```

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

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 9:34pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/14 "2021-06-07T21:34:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 10:09pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/15 "2021-06-07T22:09:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [June 7, 2021, 10:24pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/16 "2021-06-07T22:24:48Z")

</div>

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

> [@tabreturn](#):
>
> ```auto
> def keyPressed():
> ...
> display = ''
> failed = 0
> 
> ```

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 7, 2021, 11:14pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/17 "2021-06-07T23:14:56Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![SilentStorm](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@SilentStorm](https://discourse.processing.org/u/SilentStorm)\
**Post date:** [June 8, 2021, 4:34pm UTC](https://discourse.processing.org/t/how-to-make-hangman-in-processing-py-solved/30499/18 "2021-06-08T16:34:59Z")

</div>

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

Final Code:

```auto
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!'

```
