[英]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!
但是,这些更改将使您的代码正常工作!
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()
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.