0

For every correct guess the player makes I want a piece of an image to show. The image is a human.

so first correct guess = face, second correct guess = face and left hand, etc. My code is below if anyone can point me on the right path of what to check out / read or a link to look into that would be great. I was thinking tkinter, then PIL, then pygame but now I am so unsure.

I found some older posts about revealing pictures piece by piece but I do not think they are relevant to my needs.

Reveal a picture piece by piece

I am using a MacBook Pro Early-2015 running El Capitan and using PyCharm Community version and Python 3.5.2.

I found some code online to make a hangman game and I wanted to modify it to reveal part of an image as my "hangman" for every correct guess the user makes.

Is there any way to use tkinter and run the following?

Or maybe I am approaching it completely wrong?

I was thinking of adding

import Image
Image.show # But this would not call back only a piece of an image, 
#unless I could use image.slicer to cut the image up before hand and 
#save the image as multiple pieces and add it. But this seems inefficient


import random

words = ['GOOGLE', 'FACEBOOK', 'NETFLIX', 'AMAZON']

while True:
    start = input("Press enter/return to start")
    if start == "" \
                        "":
        break


secret_word = random.choice(words)
bad_guesses = []
good_guesses = []

while len(bad_guesses) < 10 and len(good_guesses) != len(list(secret_word)):
    # show guessed letter, spaces and strikes
    for letter in secret_word:
        if letter in good_guesses:
            print(letter, end='')
#For every letter in good_guesses we should reveal part of the image
        else:
            print('_ ', end='')

    print('')
    print('Strikes: {}/10'.format(len(bad_guesses)))
    print('')

    #user guesses letter

    guess = input("Guess a letter! : ").upper()

    if len(guess) != 1:
        print ("You have to guess one letter at a time!")
        continue
    elif guess in bad_guesses or guess in good_guesses:
        print("You already guessed this letter, try another letter!")
        continue
    elif not guess.isalpha():
        print("You can only guess letters nothing else!")
        continue

    if guess in secret_word:
        good_guesses.append(guess)

        # for every good guess we want to add a picture to appear
        if len(good_guesses) == len(list(secret_word)):
            print("You win! The word was {}".format(secret_word))
            break
    else:
        bad_guesses.append(guess)
else:
    print("Sorry! the word was {}".format(secret_word))
Community
  • 1
  • 1

1 Answers1

0

The way I first thought of was to cut up the image piece by piece. After a little bit of thinking, it would be MUCH easier to draw x amount of black squares and position them over the image. Then you can import whatever file you want without having to chop it up. I use pygame for blitting objects but tkinter will work for you. (not compatible with python 3+ yet)

Here's a link to a previous answer that shows how to draw multiple squares with a fill. I would just use if statements to not draw squares.

how to draw more than one squares in tkinter python 3

Here is a way to stack items on top of each other.

Tkinter, overlay foreground image on top of a background image with transparency

Hope this helps!

Community
  • 1
  • 1
Zak Vogt
  • 83
  • 6