繁体   English   中英

尝试使用 tkinter 编写“剪刀石头布”游戏。 但是我的编码没有给出我想要的,并且没有发生错误

[英]Trying to code 'rock paper scissors' game with tkinter. But my coding doesn't give what i want, and no error has occured

这就是我刚刚所做的。

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

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

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

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

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

您可以进行的其他更改

您还可以通过其他方式改进您的代码,使其更易读懂。 可以说最重要的是将您的代码移动到 Object 面向编程,我觉得这个指南相当不错!

您可以进行的另一个更改是更改创建按钮的for loop ,以使用 Python 的枚举function 和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)

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

希望这可以帮助,

詹姆士

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM