gpt4 book ai didi

python - gui python 中的岩石、布、剪刀、蜥蜴、spock 游戏

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:11 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com