【问题标题】:Tkinter GUI for hangman game用于刽子手游戏的 Tkinter GUI
【发布时间】:2016-12-10 23:53:52
【问题描述】:

我正在尝试使用 tkinter 使用 gui 在 python 中编写一个简单的刽子手游戏。我用用户条目设置了 gui 框架。问题是当我输入一个字母然后按播放按钮运行代码时,它会在代码中运行字母,直到代码用完(10次)。我怎样才能改进我的代码,使一个字母只运行一次,然后可以输入一个新字母再次猜测? 谢谢

from numpy import*
from Tkinter import*

#set up gui frame

win=Tk() #create window assigned to variable win
welc=Label(win,text="Welcome to Hangman!").grid(row=0)
inst=Message(win,text="instructions: To play start by guessing a letter in the secret word. if the letter is in the word it will fill in a blank if not you lose a life. Continue guessing untill you have guessed the secret word.").grid(row=1)
guess_text=Label(win,text="Enter a letter or Word").grid(row=2)
blank_text=Label(win,text="Secret Word").grid(row=3)
lives_text=Label(win,text="Lives Remaining").grid(row=4)
e=Entry(win)
e.grid(row=2,column=1)

#library of secret words
lib=['hanakah','christmas','holly','thanksgiving','reigndeer','family','presents','santa','kwanza', 'chocolate', 'cheesecake']
n=len(lib)

#randomly pick secret word
inx=random.randint(1,n)
secret=lib[inx]

#set up game 
lives_remaining=10
guessed_letters=''


#define function to play game
def play():
    word=secret
    while True:
        guess=get_guess(word)
        print guess
        print type(guess)
        if process_guess(guess,word):
            Label(win,text="You Win!").grid(row=6)
            break
        if lives_remaining==0:
            Label(win,text="You Lose!").grid(row=6)
            Label(win,text="The secret word was: "+word).grid(row=7)
            break
Button(win,text=("Play"),command=play).grid(row=5,column=0)

def get_guess(word):
    blanks(word)
    guess=e.get()
    return guess

#diplay guessed letter in postion of word
def blanks(word):
    Label(win,text=lives_remaining).grid(row=4,column=1)
    display_word=''
    for letter in word:
        if guessed_letters.find(letter)> -1:
                  #LETTER found
            display_word=display_word+letter
        else:
            #letter not found
            display_word=display_word+'-'
    Label(win,text=display_word).grid(row=3,column=1)

def process_guess(guess,word):
    if len(guess)>1 and len(guess)==len(word):
        return whole_word_guess(guess, word)
    else:
        return single_letter_guess(guess, word)

def whole_word_guess(guess, word):

    if guess.lower() == word.lower():
        return True
    else:
        lives_remaining=lives_remaining+(-1)
        return False                  


def single_letter_guess(guess, word):
    global guessed_letters
    global lives_remaining
    if word.find(guess) == -1:
        # letter guess was incorrect
        lives_remaining = lives_remaining+(-1)
        guessed_letters = guessed_letters + guess.lower()
    if all_letters_guessed(word):
        return True
    return False

def all_letters_guessed(word):
    for letter in word:
        if guessed_letters.find(letter.lower()) == -1:
            return False
    return True
mainloop()

【问题讨论】:

  • 我看不出有什么问题。看起来它对我有用。您能否在While True 循环中添加打印输出,说明还剩多少生命以及当前的猜测是什么?

标签: python-2.7 tkinter


【解决方案1】:

我想我现在明白了。 get_guess 函数总是提取 e Entry 中的任何信息,而不是等待 e 的值发生变化。

不创建侦听器的简单解决方法是创建一个名为 last_guess 的新全局变量,该变量可以初始化为 '',然后作为第二个参数传递给 get_guess 以检查猜测是否已更改:

def get_guess(word, last_guess):
    blanks(word)
    guess=e.get()

    #If the guess has not changed ret false
    if guess == last_guess:
        return False

    #If the guess has changed update the last_guess
    else:
        last_guess = guess
        return guess

然后在While主循环中:

while True:
        guess=get_guess(word)
        print guess
        print type(guess)

        if not guess: #<-- If the get_guess returned False
            continue #<-- Just go on to on to the next iteration of the loop w/out updating lives etc

        if process_guess(guess,word):
            Label(win,text="You Win!").grid(row=6)
            break
        if lives_remaining==0:
            Label(win,text="You Lose!").grid(row=6)
            Label(win,text="The secret word was: "+word).grid(row=7)
            break

我没有运行这段代码,所以它可能有错误,但我认为一般的想法会起作用

【讨论】:

    猜你喜欢
    • 2015-05-29
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2014-10-19
    • 1970-01-01
    相关资源
    最近更新 更多