简体   繁体   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

So here's what I just did.这就是我刚刚所做的。

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

I can't use canvas in this thing.. just allowed to use Labels.我不能在这件事上使用 canvas .. 只允许使用标签。 Error does not appear when I run this.运行此程序时不会出现错误。 So I can't figure it out what is wrong.所以我无法弄清楚出了什么问题。

What should I edit on here?我应该在这里编辑什么? Where did I made mistakes?我在哪里犯了错误?

For a start, you pass to the game function an integer not a string, so when you check to see if it is rock, paper or scissors it never returns a value.首先,你传递给game function 一个 integer 不是一个字符串,所以当你检查它是石头、纸还是剪刀时,它永远不会返回一个值。 This means you should change your code to read:这意味着您应该将代码更改为:

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

Furthermore, instead of rebuilding the labels you can use label.configure to change the text simply:此外,您可以使用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!')

There are more improvements you can also make to your code;您还可以对代码进行更多改进; however, these changes will make your code working!但是,这些更改将使您的代码正常工作!

Overall the code changes made总体而言,所做的代码更改

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

Other Changes you can make您可以进行的其他更改

You can also improve your code to be more ledgible in some other ways.您还可以通过其他方式改进您的代码,使其更易读懂。 Arguably the most important would be to move your code to Object Orientated Programming, I find this guide to be rather good!可以说最重要的是将您的代码移动到 Object 面向编程,我觉得这个指南相当不错!

Another change you can make is to change your for loop , that creates the buttons, to use Python's enumerate function and lambda :您可以进行的另一个更改是更改创建按钮的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)

As you can see, that one code change can improve the look of your code!如您所见,一次代码更改可以改善代码的外观!

Hope this helps,希望这可以帮助,

James詹姆士

暂无
暂无

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

相关问题 我正在使用 Tkinter 在 Python 中制作剪刀石头布游戏。 我的分数工作不正常 - I'm Making a Rock Paper Scissors game in Python with Tkinter. My Score isn't Working Properly 我能做些什么来修复我的石头剪刀布代码以保持分数并结束游戏 - What can I do to fix my Rock, Paper, Scissors code to keep score and to end the game 我的部分代码在 vscode 中无法访问,我正在尝试构建基本的剪刀石头布游戏 - part of my code is unreachable in vscode, Iam trying to build basic rock paper scissors game 我的石头剪刀布游戏中的错误,通过语句后循环无法正常工作 - Bug in my rock paper scissors game , loop doesn't work correct after pass statement 我该怎么办这个石头剪刀布游戏我只是不知道从这里开始? - what do i do with this rock paper scissors game i just don't where to go with it from here? 如何用更少的代码行制作我的基本的 Rock/Paper/Scissors 游戏? - How do I make my basic Rock/Paper/Scissors game with fewer lines of code? 如何使我的Rock,Paper和剪刀游戏的代码不那么多余? - How can I make my code for a game of Rock, Paper, and scissors less redundant? 我的石头剪刀布游戏坏了循环 - broken loop for my rock paper scissors game 这是我在 Python 中的剪刀石头布游戏 - This is for my Rock, Paper Scissors game in Python 基本的石头剪刀布对我的计算器不起作用 - Basic Rock, Paper, Scissors Doesn’t Work for my Calculator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM