2016-10-18 125 views
0

正確猜測玩家讓我想要一張圖片顯示。該圖像是一個humanPython根據條件一張一張地顯示圖片

第一個正確的猜測=臉部,第二個正確的猜測=臉部和左手等我的代碼如果任何人都可以指向我檢查/很棒。我正在考慮tkinter,然後是PIL,然後pygame,但現在我很不確定。

我發現了一些關於逐張顯示圖片的舊帖子,但我不認爲它們與我的需求相關。

Reveal a picture piece by piece

我使用的是MacBook Pro的早期2015年運行埃爾卡皮坦和使用PyCharm社區版和Python 3.5.2。

我在網上發現了一些代碼來製作hang子手遊戲,並且我想修改它以揭示圖像的一部分,作爲我的「hang子手」,以供用戶進行每次正確的猜測。

有什麼辦法可以使用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)) 

回答

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

希望這會有所幫助!

相關問題