- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
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()
我需要它来记分和打球。 Easy 只需要启用剪刀石头布,而 Hard 则需要全部启用。然后在文本框中显示结果。我一直在一遍又一遍地尝试让它工作,但我是 Python 的新手,需要一些指导。谁能帮帮我?
最佳答案
使用 self.playerChoice = StringVar()
使其成为一个属性。
self.results_txt("Well done you won!")
是一个字符串,因此它不可调用。
使用 self.results_txt ="干得好,你赢了!"
需要将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()
关于python - gui python 中的岩石、布、剪刀、蜥蜴、spock 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311261/
显然是在学习java。我能够让游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的东西,那么它默认为 Rock。我不需要循环。如果我放石头的话,游戏就完美了。如果我放置 RPS 以外的任
我刚刚开始使用 python,需要一些帮助!我当时在做剪刀石头布游戏,我想在人或计算机赢得 3 场胜利后添加一个重启选项。 我已经四处寻找一些答案,但从我看到的所有其他代码来看,似乎超出了我的范围,或
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我的类(class)被分配了一个石头剪刀布游戏。以下是作业说明: 学习目标: 练习枚举的使用 创建一个包含构造函数、字段和方法的枚举 设计并实现您自己的 GUI 创建一个可运行的 jar 描述:编写一
我正在用Python创建一个石头剪刀布游戏。我的方法如下。然而,我不想打印玩家 1 和玩家 2,而是想让它们成为真实的名字。因此,可以说 John Wins 或 Joe Wins。如何实现每次打印玩家
我正在尝试完成类里面的石头、剪刀、布作业。 我收到“UnboundLocalError:赋值前引用的局部变量“绑定(bind)”” 错误。 有人可以告诉我为什么会收到此错误吗? import rand
我在 C++ 上学习石头剪刀布游戏时有一个游戏实例类。我想创建整数常量,它表示游戏中允许的符号数。对于经典的 rsp 游戏,它是 3(石头、剪刀和布),但是有一些有趣的 rcs 游戏扩展带有额外的符号
我正在上 Codecademy 类(class)(找到 here ),但一直告诉我“当输入是纸和石头时,您的代码返回‘石头获胜’而不是‘纸获胜’”,为什么?应该是正确的。既然它在谈论“石头获胜”,那么
我正在可汗学院编写一个剪刀石头布游戏,这样我就可以看到视觉效果,但是 var Compare = function(choice1, choice2) 无法正常工作。 html 它工作得很好。插入了我
对于我的类(class)项目,我们要制作一个“石头剪刀布”游戏,使用一个函数来显示菜单和验证输入,以及一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我同时使用这两个函数
我目前受困于我的 RPS 程序,因为它无法正确存储用户丢失的次数或与计算机的关联。例如,当我运行程序并输入“q”退出时,我得到以下输出: Enter R, P, S, or Q (for quit)
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 7 年前。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里
我一直在尝试解决石头剪刀布项目,但我不知道如何做出 if/else 语句。我这样做了很多次,最后我认为我已经接近解决问题了,但问题是,每次运行程序时我都会得到错误的输出。 例如,我使用“Paper”,
以下是我在 BlueJ 中编写的石头剪刀布游戏的代码。当我编译并且用户输入输入时,计算机立即从playerWins()打印大量输出。当用户输入“退出”时游戏结束。有人可以帮助我,这样我的屏幕就不会被淹
如果之前曾问过此问题的某个版本,我们深表歉意。我查了一下,但找不到真正能解决我的问题的东西。 我今天开始学习Python,并尝试构建一个简单的石头剪刀布游戏。 我有以下代码,它运行得很好: impor
我今天一直在开发这个应用程序,但我似乎无法弄清楚代码有什么问题。 当我检查时,没有语法错误。但是当我尝试运行它并调试它时,它说 "Source not found" on "ActivityThrea
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我有 JS 中的基本“石头、剪刀、布”游戏的代码。它可以与提示一起使用,但我希望能够通过按钮做出选择。我想使用“getElementById”和“addEventListener(“click”)”。
有没有一种方法可以用大于号和小于号来做剪刀石头布游戏? 我知道执行 RPS 有不同的方法,但我想具体了解大于和小于。 这是我的代码: "Rock" > "Scissors" and 'Rock' "
我是一名优秀的程序员,十分优秀!