好的,所以我和我的伙伴正试图制作悬挂式游戏,但我们遇到的问题是检测是否按下了正确的字母,我们还有一些我们需要修复的小细节,但那是重大问题。我们的想法是在按下按钮时使x等于不同的值,然后检查这是否正确并且当然是扮演吊人,但现在按下按钮时它没有做任何事情。想法?
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()
答案 0 :(得分:2)
作为一般规则,每当您有大量重复代码时,请使用循环或重复调用函数。这是一个替代方法,使用循环创建按钮,使用命令调用函数,并使用partial发送按下的函数,因此您只使用一个函数。
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()