尝试使用 tkinter 编写'rock paper scissors'游戏代码。但是我的编码没有给出我想要的,也没有发生错误



这就是我刚刚所做的。

from tkinter import *
import random
window = Tk()
button_list=['Rock', 'Paper', 'Scissors']
RockImage=PhotoImage(file="rock.png")
PaperImage=PhotoImage(file="paper.png")
ScissorImage=PhotoImage(file="scissors.png")
def update_image(num,Img):
if num==1:
inputLabel_1=Label(window, image=Img)
inputLabel_1.grid(row=0, column=0)
elif num==2:
inputLabel_2=Label(window, image=Img)
inputLabel_2.grid(row=0, column=2)

Mid_ResultLabel=Label(window, text=' ', fg='green')
ResultLabel=Label(window, text=' ',fg='green')
Mid_ResultLabel.grid(row=0, column=1)
ResultLabel.grid(row=1, column=1)

def game(choice):
opponent = random.randint(1,3)
if opponent == 1:
update_image(2,RockImage)
elif opponent == 2:
update_image(2,PaperImage)
elif opponent ==3:
update_image(2,ScissorImage)
if choice == 'Rock':
update_image(1,RockImage)
if opponent == 1:
Mid_ResultLabel = Label(window, width=10, text='======')
ResultLabel = Label(window, width=10, text='DRAW!',fg='green')
elif opponent == 2:
Mid_ResultLabel = Label(window, width=10, text='<<<<<<')
ResultLabel = Label(window, width=10, text='LOSE...')
elif opponent ==3:
Mid_ResultLabel = Label(window, width=10, text='>>>>>>')
ResultLabel = Label(window, width=10, text='YOU WON!')
elif choice == 'Paper':
update_image(1,PaperImage)
if opponent == 1:
Mid_ResultLabel = Label(window, width=10, text='>>>>>>')
ResultLabel = Label(window, width=10, text='YOU WON!')
elif opponent == 2:
Mid_ResultLabel = Label(window, width=10, text='======')
ResultLabel = Label(window, width=10, text='DRAW!')
elif opponent == 3:
Mid_ResultLabel = Label(window, width=10, text='<<<<<<')
ResultLabel = Label(window, width=10, text='LOSE...')
elif choice == 'Scissors':
update_image(1,ScissorImage)
if opponent == 1:
Mid_ResultLabel = Label(window, width=10, text='<<<<<<')
ResultLabel = Label(window, width=10, text='LOSE...')
elif opponent == 2:
Mid_ResultLabel = Label(window, width=10, text='>>>>>>')
ResultLabel = Label(window, width=10, text='YOU WON!')
elif opponent == 3 :
Mid_ResultLabel = Label(window, width=10, text='======')
ResultLabel = Label(window, width=10, text='DRAW!')
Mid_ResultLabel.grid(row=0, column=1)
ResultLabel.grid(row=1, column=1)

i=0
for button_text in button_list:
def click(t=i):
game(t)
Button(window, text=button_text, width=30, command = click).grid(row=3, column = i)
i+=1

window.mainloop()

我不能在这件事中使用画布......只是允许使用标签。 运行此程序时不会出现错误。所以我无法弄清楚出了什么问题。

我应该在这里编辑什么?我哪里犯了错误?

  • 摇滚图像
  • 纸质图像
  • 剪刀形象

首先,你向game函数传递一个整数而不是一个字符串,所以当你检查它是石头、纸还是剪刀时,它永远不会返回一个值。这意味着您应该将代码更改为:

if choice == 'Rock': -> if choice == 0:
elif choice == 'Paper': -> elif choice == 1:
elif choice == 'Scissors': -> elif choice == 2:

此外,您可以使用label.configure简单地更改文本,而不是重新构建标签:

if choice == 0:
update_image(1,RockImage)
if opponent == 1:
Mid_ResultLabel.configure(text='======')
ResultLabel.configure(text='DRAW!')
elif opponent == 2:
Mid_ResultLabel.configure(text='<<<<<<')
ResultLabel.configure(text='LOSE...')
elif opponent ==3:
Mid_ResultLabel.configure(text=">>>>>>")
ResultLabel.configure(text='YOU WON!')
elif choice == 1:
update_image(1,PaperImage)
if opponent == 1:
Mid_ResultLabel.configure(text=">>>>>>")
ResultLabel.configure(text='YOU WON!')
elif opponent == 2:
Mid_ResultLabel.configure(text='======')
ResultLabel.configure(text='DRAW!')
elif opponent == 3:
Mid_ResultLabel.configure(text='<<<<<<')
ResultLabel.configure(text='LOSE...')
elif choice == 2:
update_image(1,ScissorImage)
if opponent == 1:
Mid_ResultLabel.configure(text='<<<<<<')
ResultLabel.configure(text='LOSE...')
elif opponent == 2:
Mid_ResultLabel.configure(text=">>>>>>")
ResultLabel.configure(text='YOU WON!')
elif opponent == 3 :
Mid_ResultLabel.configure(text='======')
ResultLabel.configure(text='DRAW!')

您还可以对代码进行更多改进;但是,这些更改将使代码正常工作!

总体上进行了代码更改

from tkinter import *
import random
window = Tk()
button_list = ['Rock', 'Paper', 'Scissors']
RockImage = PhotoImage(file="rock.png")
PaperImage = PhotoImage(file="paper.png")
ScissorImage = PhotoImage(file="scissors.png")

def update_image(num, Img):
if num == 1:
inputLabel_1 = Label(window, image=Img)
inputLabel_1.grid(row=0, column=0)
elif num==2:
inputLabel_2=Label(window, image=Img)
inputLabel_2.grid(row=0, column=2)

Mid_ResultLabel=Label(window, text=' ', fg='green')
ResultLabel=Label(window, text=' ',fg='green')
Mid_ResultLabel.grid(row=0, column=1)
ResultLabel.grid(row=1, column=1)

def game(choice):
print("GAME FUNCTION")
opponent = random.randint(1,3)
if opponent == 1:
update_image(2,RockImage)
elif opponent == 2:
update_image(2,PaperImage)
elif opponent ==3:
update_image(2,ScissorImage)
if choice == 0:
update_image(1,RockImage)
if opponent == 1:
Mid_ResultLabel.configure(text='======')
ResultLabel.configure(text='DRAW!')
elif opponent == 2:
Mid_ResultLabel.configure(text='<<<<<<')
ResultLabel.configure(text='LOSE...')
elif opponent ==3:
Mid_ResultLabel.configure(text=">>>>>>")
ResultLabel.configure(text='YOU WON!')
elif choice == 1:
update_image(1,PaperImage)
if opponent == 1:
Mid_ResultLabel.configure(text=">>>>>>")
ResultLabel.configure(text='YOU WON!')
elif opponent == 2:
Mid_ResultLabel.configure(text='======')
ResultLabel.configure(text='DRAW!')
elif opponent == 3:
Mid_ResultLabel.configure(text='<<<<<<')
ResultLabel.configure(text='LOSE...')
elif choice == 2:
update_image(1,ScissorImage)
if opponent == 1:
Mid_ResultLabel.configure(text='<<<<<<')
ResultLabel.configure(text='LOSE...')
elif opponent == 2:
Mid_ResultLabel.configure(text=">>>>>>")
ResultLabel.configure(text='YOU WON!')
elif opponent == 3 :
Mid_ResultLabel.configure(text='======')
ResultLabel.configure(text='DRAW!')

i=0
for button_text in button_list:
def click(t=i):
game(t)
Button(window, text=button_text, width=30, command = click).grid(row=3, column = i)
i+=1

window.mainloop()

您可以进行的其他更改

您还可以通过其他方式改进代码,使其更具可访问性。可以说,最重要的是将您的代码移动到面向对象编程,我发现本指南相当不错!

您可以进行的另一个更改是更改创建按钮的for loop,以使用 Python 的枚举函数和 lambda:

for i, button_text in enumerate(button_list):
Button(window, text=button_text, width=30, command = lambda t=i: game(t)).grid(row=3, column = i)

如您所见,一次代码更改可以改善代码的外观!

希望这有帮助,

詹姆斯