2017-11-11 1 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

글자를 얻기 위해 엔트리를 사용한 다음 : word_to_guess에 letter_entered가있는 경우 : –

+0

죄송 합니다만이 코드는 행맨 게임처럼 "단순한"무언가에 너무 무겁습니다. 귀하의 코드를 스캔하고 사전에 대해 읽어 보도록 권합니다. 데이터를 키 + 값으로 저장하는 가장 효율적인 방법이며 더 가벼운 코드를 작성하는 데 매우 유용합니다. 팁 : 코드를 다시 작성하고 코드를 다시 작성하십시오. –

답변

2

일반적으로 반복되는 코드가 많을 때마다 루프를 사용하거나 반복적으로 함수를 호출하십시오. 함수를 호출하는 명령을 사용하여 단추를 작성하는 루프를 사용하고 함수에 눌린 문자를 보내기 위해 부분을 사용하는 대신 하나의 함수 만 사용할 수있는 대안이 있습니다.

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