2014-09-12 81 views
0

这个hang子手游戏是我所做过的第一场比赛之一,我最近一直在回顾我的旧程序,并将它们添加了一点。在这一个我添加了肉桂卷:D。但是,当代码运行时,它最终会延迟一段时间,然后退出,而不是肉桂卷。我尝试过使用Google搜索我的问题,以及我正在尝试做什么,但是我知道没有人会熟悉tkinter。问题发生在底部。这里是代码:Hang子手(肉桂卷)[tkinter]

import random 
import time 
from tkinter import * 
tk = Tk() 
tk.title("Hangman!") 
canvas = Canvas(tk, width=700, height=700) 
canvas.pack() 
cinnamonroll = PhotoImage(file='C:\\Python34\\cinnamonroll.gif') 
hangmanpic0 = PhotoImage(file='C:\\Python34\\hangmanpic0.gif') 
hangmanpic1 = PhotoImage(file='C:\\Python34\\hangmanpic1.gif') 
hangmanpic2 = PhotoImage(file='C:\\Python34\\hangmanpic2.gif') 
hangmanpic3 = PhotoImage(file='C:\\Python34\\hangmanpic3.gif') 
hangmanpic4 = PhotoImage(file='C:\\Python34\\hangmanpic4.gif') 
hangmanpic5 = PhotoImage(file='C:\\Python34\\hangmanpic5.gif') 
hangmanpic6 = PhotoImage(file='C:\\Python34\\hangmanpic6.gif') 
hangmanpics = (hangmanpic0, hangmanpic1, hangmanpic2, hangmanpic3, \ 
    hangmanpic4, hangmanpic5, hangmanpic6) 
words = ("taffy", "guitar", "talking", "retire", "fanning", "news", \ 
    "moon", "sunny", "driving", "prevent", "stick", "images", \ 
    "trees", "reading", "books", "character", "doctor", "telephone", \ 
    "license", "braces", "giraffe", "spotted", "exiting", \ 
    "animation", "flower", "assassination", "mother", "tomato", \ 
    "fruity", "school") 
word = random.choice(words) 
progress = "-" * len(word) 
wrong = 0 
lettersguessed = [] 
print("Are you ready to play hangman!?") 
time.sleep(1) 
while wrong < 6 and progress != word: 
    canvas.create_image(0, 0, anchor=NW, image=hangmanpics[wrong]) 
    print("You have used %s out of 6 guesses" % wrong) 
    print("You have guessed these letters: %s" % lettersguessed) 
    print(progress) 
    guess = input("Guess a letter. ") 
    guess = guess.lower() 
    while guess in lettersguessed: 
     guess = input("You already guessed %s. Guess another letter!" % guess) 
     guess = guess.lower() 
    lettersguessed.append(guess) 
    if guess in word: 
     print("Correct, good job!") 
     new = "" 
     for i in range(0, len(word)): 
      if guess == word[i]: 
       new += guess 
      else: 
       new += progress[i] 
       progress = new 
      else: 
       print("NOPE! Sad face.") 
       wrong += 1 
if wrong == 6: 
    print(hangmanpics[wrong]) 
    print("You got hanged!!! *triple snap*") 
else: 
    print("You win!") 
    canvas.create_image(0, 0, anchor=NW, image=cinnamonroll) 
    time.sleep(2) 
print("The word was %s! Thanks for playing!" % word) 

我觉得很蠢。

+0

看起来你需要'tk.mainloop()',如果没有其他的话。 – 2014-09-12 22:37:28

+0

请注意,您必须将代码封装在函数/方法中,并通过'after'方法运行。 – TidB 2014-09-12 22:41:47

+0

该代码有一个语法错误,无法运行(if后的其他两个)。那第二个应该是缩进? – tdelaney 2014-09-12 23:08:00

回答

0

您正在以一种奇怪的方式使用tkinter(无应用程序或主循环)。 tkinter事件不会触发,更新也不会及时发生。事实证明,你的游戏只能靠运气。无论您何时拨打input(),tkinter都会启动其处理循环并显示事物。我不知道这种魔法是什么,但是例如,您的原始图像只会在input()之后显示(您可以在之前添加睡眠以检查自己)。你可以让你的最终显示的工作,加入

... 
canvas.create_image(0, 0, anchor=NW, image=cinnamonroll) 
tk.update_idletasks() 
time.sleep(2) 

后做所有的传统知识更新的相同,睡一会超过2秒 - 这不是那么快!