2017-11-11 7 views
-1

私と私のパートナーはハングマンゲームを作ろうとしていますが、正しい文字が押されているかどうかを検出することが問題です。それは大きな問題です。私たちの考えは、ボタンを押したときにxを別の値にして、それが正しかったかどうかをチェックして、もちろんハングマンを再生することでしたが、ボタンを押しても何もしません。アイデア?一般的なルールとしてHang Man Python

from tkinter import * 
import random as r 

#Hang Man 

class menu: 
    def __init__(self): 
     global game 
     global play 
     global canvas 
     game = Tk() 
     game.geometry('600x600+50+50') 
     game.title("Hang Man") 

     canvas = Canvas(game,height=400,width=800,bg='light goldenrod yellow')  
     canvas.pack() 
     play = Button(game,text="Play",command=self.playbt) 
     play.pack(side=BOTTOM) 

def playbt(self): 
    self.difs() 

def difs(self): 
    global easy 
    global medium 
    global hard 
    play.destroy() 

    easy = Button(game,text="Easy",command=self.easy) 
    medium = Button(game,text="Medium",command=self.medium) 
    hard = Button(game,text="Hard",command=self.hard) 

    easy.pack(side=BOTTOM) 
    medium.pack(side=BOTTOM) 
    hard.pack(side=BOTTOM) 
def letters(self): 

    A = Button(game, text="A", command=self.A) 
    B = Button(game, text="B", command=self.B) 
    C = Button(game, text="C", command=self.C) 
    D = Button(game, text="D", command=self.D) 
    E = Button(game, text="E", command=self.E) 
    F = Button(game, text="F", command=self.F) 
    G = Button(game, text="G", command=self.G) 
    H = Button(game, text="H", command=self.H) 
    I = Button(game, text="I", command=self.I) 
    J = Button(game, text="J", command=self.J) 
    K = Button(game, text="K", command=self.K) 
    L = Button(game, text="L", command=self.L) 
    M = Button(game, text="M", command=self.M) 
    N = Button(game, text="N", command=self.N) 
    O = Button(game, text="O", command=self.O) 
    P = Button(game, text="P", command=self.P) 
    Q = Button(game, text="Q", command=self.Q) 
    R = Button(game, text="R", command=self.R) 
    S = Button(game, text="S", command=self.S) 
    T = Button(game, text="T", command=self.T) 
    U = Button(game, text="U", command=self.U) 
    V = Button(game, text="V", command=self.V) 
    W = Button(game, text="W", command=self.W) 
    X = Button(game, text="X", command=self.X) 
    Y = Button(game, text="Y", command=self.Y) 
    Z = Button(game, text="Z", command=self.Z) 

    A.place(x = 80, y = 550) 
    B.place(x = 98, y = 550) 
    C.place(x = 116, y = 550) 
    D.place(x = 134, y = 550) 
    E.place(x = 152, y = 550) 
    F.place(x = 170, y = 550) 
    G.place(x = 188, y = 550) 
    H.place(x = 206, y = 550) 
    I.place(x = 222, y = 550) 
    J.place(x = 240, y = 550) 
    K.place(x = 256, y = 550) 
    L.place(x = 278, y = 550) 
    M.place(x = 296, y = 550) 
    N.place(x = 314, y = 550) 
    O.place(x = 332, y = 550) 
    P.place(x = 350, y = 550) 
    Q.place(x = 368, y = 550) 
    R.place(x = 386, y = 550) 
    S.place(x = 404, y = 550) 
    T.place(x = 422, y = 550) 
    U.place(x = 440, y = 550) 
    V.place(x = 458, y = 550) 
    W.place(x = 476, y = 550) 
    X.place(x = 494, y = 550) 
    Y.place(x = 512, y = 550) 
    Z.place(x = 530, y = 550) 


def A(self): 
    global x 
    x = 'A' 
def B(self): 
    x = 'B' 
def C(self): 
    x = 'C' 
def D(self): 
    x = 'D' 
def E(self): 
    x = 'E' 
def F(self): 
    x = 'F' 
def G(self): 
    x = 'G' 
def H(self): 
    x = 'H' 
def I(self): 
    x = 'I' 
def J(self): 
    x = 'J' 
def K(self): 
    x = 'K' 
def L(self): 
    x = 'L' 
def M(self): 
    x = 'M' 
def N(self): 
    x = 'N' 
def O(self): 
    x = 'O' 
def P(self): 
    x = 'P' 
def Q(self): 
    x = 'Q' 
def R(self): 
    x = 'R' 
def S(self): 
    x = 'S' 
def T(self): 
    x = 'T' 
def U(self): 
    x = 'U' 
def V(self): 
    x = 'V' 
def W(self): 
    x = 'W' 
def X(self): 
    x = 'X' 
def Y(self): 
    x = 'Y' 
def Z(self): 
    x = 'Z' 

def easy(self): 
    ewords = r.choice(["TABLE","CHAIR","DESK","PHONE","LIGHT","MAN"]) 
    self.play(ewords) 
def medium(self): 
    mwords = r.choice(["PYTHON","LAPTOP","JACKET","VIDEO","MODULE","LIBRARY"]) 
    self.play(mwords) 
def hard(self): 
    hwords = r.choice(["PROGRAM","TOLEDO","UNIVERSITY","ENGINEER","FOOTBALL","LANGUAGE"]) 
    self.play(hwords) 

def play(self,words): 
    easy.destroy() 
    medium.destroy() 
    hard.destroy() 
    canvas.create_line(100,50,100,500,tags="Line") #Vertical 
    canvas.create_line(20,500,80,500,tags="Line") #Lower Horizontal 
    canvas.create_line(100,50,150,50,tags="Line") #Horizontal Line 
    self.letters() 

    lwords = [] 
    for j in range(len(words)): 
     lwords.append(" _") 

    canvas.create_text(300,300,text=("".join(repr(e) for e in lwords)),font="Times 16",tags="text") 

    hm = 0 
    x = 0 

    for i in range(len(words)): 
     if x == 0: 
      continue 
     else: 
      if words[i] == x: 
       words.replace(lwords[i],words[i]) 
      else: 
       hm += 1 
       if hm == 1: 
        canvas.create_oval(125,50,175,100) #HEad 
        canvas.update() 
       elif hm == 2: 
        canvas.create_line(150,100,150,150) #Body 
        canvas.update() 
       elif hm == 3: 
        canvas.create_line(150,125,125,100) #Left Arm 
        canvas.update() 
       elif hm == 4: 
        canvas.create_line(150, 125, 175, 100) #Right Arm 
        canvas.update() 
       elif hm == 5: 
        canvas.create_line(125,150,100,125) #Left Leg 
        canvas.update() 
       elif hm == 6: 
        canvas.create_line(125,150,100,175) 
        canvas.update() 


menu() 
+1

「self.A」とは何ですか?あなたはそれをどこで定義しますか? –

+0

文字を取得するためにエントリを使用してください:if word_to_guessのletter_entered: –

+0

申し訳ありませんが、このコードはHangmanのゲームのように単純なものでは重すぎます。私はあなたのコードをスキャンし、辞書について読むことをお勧めします。データをキー+値として保存する最も効率的な方法であり、より軽いコードを書くのに非常に便利です。私のヒント: それを見て、あなたのコードを書き直してください。 –

答えて

2

、あなたが繰り返しのコードの多くを持っている時はいつでも、ループを使用するか、または繰り返し関数を呼び出します。ここでは、関数を呼び出すコマンドを使用してボタンを作成するループを使用し、関数に押された文字を送信するためにpartialを使用する代わりに、関数を1つだけ使用します。

import sys 
if sys.version_info[0] < 3: 
    import Tkinter as tk  ## Python 2.x 
else: 
    import tkinter as tk  ## Python 3.x 

from functools import partial 
import string 

def button_pressed(ltr): 
    print(ltr) 

game=tk.Tk() 
game.geometry("1000x600") 
this_x=80 
for b_ltr in string.ascii_uppercase: 
    tk.Button(game, text=b_ltr, bg="lightblue", 
       command=partial(button_pressed, b_ltr)).place(x=this_x, y=550) 
    this_x += 33 
game.mainloop() 
関連する問題