from tkinter import *
import random
class Application(Frame):
"""A Gui Application for a game."""
def __init__(self,master):
"""Initialize the frame."""
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
self.wins = 0
self.losses = 0
self.ties = 0
def computerRandom(self):
options = ["Rock", "Paper", "Scissors", "Lizard", "Spock"]
randomChoice = random.randint(0,4)
def comparison(self, selfComputer):
if self.playerChoice.get()== selfComputer:
return "Draw"
elif self.playerChoice.get() == "Rock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Spock":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Rock" and selfComputer == "Spock":
return "Computer Wins"
else:
return "Human Wins"
def play(self):
selfComputer = self.computerRandom()
result = self.comparison(selfComputer)
if result == "Draw":
self.results_txt ="Its a draw"
elif result == "Computer Wins":
self.results_txt = "Unlucky you lost!"
else:
self.results_txt ="Well done you won!"
def create_widgets(self):
"""Create labels and buttons."""
#create welcome label
Label(self,text = "Welcome to the Rock, Paper, Scissors, Spock and Lizard Game\n"
"You chose one of the weapons to fight with and let's see if the computer\n"
"beats you or not!! Enjoy!!").grid(row = 0, column = 0, columnspan=3, sticky = W)
#create difficulty label
Label(self,text = "Which difficulty level would you like to play?:").grid(row = 1, column = 0, columnspan = 3, sticky = W)
#create variables
self.difficulty = StringVar()
self.difficulty.set(None)
self.playerChoice = StringVar()
self.playerChoice.set(None)
self.results = StringVar()
self.computer_lbl = BooleanVar()
self.computer_lbl.set(None)
#radio buttons for difficulty levels
Radiobutton(self,text = "Easy",variable = self.difficulty,value = "easy").grid(row = 2, column = 0, sticky = W)
Radiobutton(self,text = "Hard",variable = self.difficulty,value = "hard").grid(row = 2, column = 3, sticky = E)
Label(self,text = " ").grid(row = 3, column = 0, sticky = W)
#computer
self.computer_lbl = Label(self, text = "Computer")
self.computer_lbl.grid(row = 4, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Rock").grid(row = 5, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Paper").grid(row = 6, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Scissors").grid(row = 7, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Lizard").grid(row = 8, column = 0, sticky = W)
self.computer_lbl = Label(self,text = "[]Spock").grid(row = 9, column = 0, sticky = W)
#player
self.player_lbl = Label(self, text = "Player")
self.player_lbl.grid(row = 4, column = 3, sticky = W)
Radiobutton(self, text ="Rock",variable = self.playerChoice, value = "Rock").grid(row=5, column=3, sticky=W)
Radiobutton(self, text ="Paper",variable = self.playerChoice, value = "Paper").grid(row=6, column=3, sticky=W)
Radiobutton(self, text ="Scissors",variable = self.playerChoice, value = "Scissors").grid(row=7, column=3, sticky=W)
Radiobutton(self, text ="Lizard",variable = self.playerChoice, value = "Lizard").grid(row=8, column=3, sticky=W)
Radiobutton(self, text ="Spock",variable = self.playerChoice, value = "Spock").grid(row=9, column=3, sticky=W)
#buttons
Label(self,text = " ").grid(row = 10, column = 0, sticky = W)
self.bttn2 = Button(self, text = "Fight!", command = self.play)
self.bttn2.grid(row=11, column=3, sticky=W)
self.play_bttn = Button(self,text = "Play!")
self.play_bttn.grid(row=12, column=3, sticky=W)
self.bttn3 = Button(self, text = "Exit", command = root.destroy)
self.bttn3.grid(row=13,column=3, sticky=W)
#Wins,Losses,Ties, and Results
Label(self, text = "Results:").grid(row=5, column=1, sticky=W)
self.results_txt = Text(self, width=30, height=5, wrap=WORD)
self.results_txt.grid(row=5, rowspan=5, column=1, columnspan=1)
self.wins_lbl = Label(self, text = "Wins:")
self.wins_lbl.grid(row=11, column=0, sticky=W)
self.losses_lbl = Label(self, text = "Losses:")
self.losses_lbl.grid(row=12, column=0, sticky=W)
self.ties_lbl = Label(self, text = "Ties:")
self.ties_lbl.grid(row=13, column=0, sticky=W)
#main
root = Tk()
root.title("Rock, Paper, Scissors, Lizard, and Spock ")
root.geometry("465x410")
app = Application(root)
root.mainloop()
我需要它来保持得分和比赛时间。很容易只需要启用摇滚,纸张和剪刀,而所有这些都很难。然后在文本框中显示结果。我一直在尝试让它工作,但我是Python的新手,需要一些指导。任何人都可以帮助我吗?
答案 0 :(得分:2)
使用self.playerChoice = StringVar()
将其设为属性。
self.results_txt("Well done you won!")
是一个字符串,因此无法调用。
使用self.results_txt ="Well done you won!"
您需要将computerRandom()
称为self.computerRandom()
,这是该类的方法
self.computer.set(options[randomChoice])
无效,因为您班级中没有computer
属性。
使用comparison(self, selfComputer)
会更好。
同样result = comparison(self, selfComputer)
将无效。使用self.comparison
,它也是一种方法。
此代码应该更接近您的需要,我不确定self.computer.set(options[randomChoice])
应该做什么,必须修复。
from tkinter import *
import random
class Application(Frame):
"""A Gui Application for a game."""
def __init__(self,master):
"""Initialize the frame."""
super().__init__()
self.grid()
self.create_widgets()
self.wins = 0
self.losses = 0
self.ties = 0
def computerRandom(self):
options = ["Rock", "Paper", "Scissors", "Lizard", "Spock"]
randomChoice = random.randint(0,4)
self.computer.set(options[randomChoice])
return options[randomChoice]
def comparison(self, selfComputer): # use self as the first parameter, it refers to each instance
# use if/elif, eilf's will only be evaluated if the previous statement is False
if self.playerChoice.get()== selfComputer:
return "Draw"
elif self.playerChoice.get() == "Rock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Rock":
return "Computer Wins"
elif self.playerChoice.get()== "Lizard" and selfComputer == "Scissors":
return "Computer Wins"
elif self.playerChoice.get()== "Paper" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Scissors" and selfComputer == "Spock":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Lizard":
return "Computer Wins"
elif self.playerChoice.get()== "Spock" and selfComputer == "Paper":
return "Computer Wins"
elif self.playerChoice.get()== "Rock" and selfComputer == "Spock":
return "Computer Wins"
else:
return "Human Wins"
def play(self):
selfComputer = self.computerRandom() # again we need to call a method using self
result = self.comparison(selfComputer) # comparing self to `selfComputer`
if result == "Draw":
self.results_txt ="Its a draw"
elif result == "Computer Wins":
self.results_txt = "Unlucky you lost!"
else:
self.results_txt ="Well done you won!"
def create_widgets(self):
"""Create labels and buttons."""
#create welcome label
Label(self,text = "Welcome to the Rock, Paper, Scissors, Spock and Lizard Game\n"
"You chose one of the weapons to fight with and let's see if the computer\n"
"beats you or not!! Enjoy!!").grid(row = 0, column = 0, columnspan=3, sticky = W)
#create difficulty label
Label(self,text = "Which difficulty level would you like to play?:").grid(row = 1, column = 0, columnspan = 3, sticky = W)
#create variables
self.playerChoice = StringVar()
self.playerChoice.set(None)
self.results = StringVar()