簡體   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