Python根据条件逐个显示图片

时间:2016-10-18 16:21:51

标签: python-3.x user-interface

对于每个正确的猜测,播放器让我想要显示一幅图像。该图片为human

所以首先正确猜测=面部,第二个正确的猜测=面部和左手等。如果有人能指出我在检查/读取的内容的正确路径或者要查看的链接,我的代码在下面大。我在想tkinter,然后是PIL,然后是pygame,但现在我很不确定。

我发现了一些关于逐件揭示图片的旧帖子,但我认为它们与我的需求无关。

Reveal a picture piece by piece

我正在使用MacBook Pro 2015年初运行El Capitan并使用PyCharm社区版和Python 3.5.2。

我在网上找到了一些代码来制作一个刽子手游戏,我想修改它以显示图像的一部分作为我的“刽子手”,用于每个用户做出的正确猜测。

有没有办法使用tkinter并运行以下命令?

或许我接近它完全错了?

我在考虑添加

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

1 个答案:

答案 0 :(得分:0)

我最初想到的方法是逐片剪切图像。经过一番思考后,绘制x个黑色方块并将它们放在图像上会更容易。然后你可以导入你想要的任何文件而不必砍掉它。我使用pygame进行blitting对象,但tkinter会对你有用。 (与python 3+不兼容)

以下是前一个答案的链接,该答案显示了如何使用填充绘制多个正方形。我只会使用if语句来绘制正方形。

how to draw more than one squares in tkinter python 3

这是一种将项目叠加在一起的方法。

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

希望这有帮助!