Python 巨蟒石剪刀不懂打领带
我正在做一把剪刀,一旦一方获胜,紧接着就是平局 会说前一方赢了。例如:人类获胜,平局,但表示人类获胜,并在人类得分上加一。这里发生了什么Python 巨蟒石剪刀不懂打领带,python,tkinter,Python,Tkinter,我正在做一把剪刀,一旦一方获胜,紧接着就是平局 会说前一方赢了。例如:人类获胜,平局,但表示人类获胜,并在人类得分上加一。这里发生了什么 from tkinter import * import random import time root = Tk() root.title("Rock Paper Scissors") root.resizable(0,0) canvas = Canvas(root,width=500,height=300) background_color = ca
from tkinter import *
import random
import time
root = Tk()
root.title("Rock Paper Scissors")
root.resizable(0,0)
canvas = Canvas(root,width=500,height=300)
background_color = canvas.create_rectangle(0,0,500,300,fill="black")
divider = canvas.create_line(200,0,200,300,fill="white")
hst = canvas.create_text(75,100,text="Human Score: ",fill='red',font=('Courier', 14))
cst = canvas.create_text(90,200,text="Computer Score: ",fill='red',font=('Courier',14))
choice_display1 = canvas.create_text(250,100,text="Choice: ",fill='red',font=('Courier',14))
choice_display2 = canvas.create_text(250,200,text="Choice: ",fill='red',font=('Courier',14))
ROck = canvas.create_text(40,20,text="ROCK",fill="green",font=('Times',17))
PApeR = canvas.create_text(70,37,text="PAPER",fill="green",font=('Times',17))
SCisSOrs = canvas.create_text(60,54,text="SCISSORS",fill="green",font=('Times',17))
canvas.create_text(150,280,text="Winner: ",fill="White",font=('Times',15))
class game:
def __init__(self):
self.choices = ['Rock','Paper','Scissors']
rock_b = Button(canvas,text="Rock",bg='black',fg='green',command=self.rock)
rock_b.place(x=225,y=35)
paper_b = Button(canvas,text="Paper",bg='black',fg='green',command=self.paper)
paper_b.place(x=290,y=35)
scissors_b = Button(canvas,text="Scissors",bg="black",fg='green',command=self.scissors)
scissors_b.place(x=360,y=35)
game_continue = Button(canvas,text="Next round!",bg="black",fg="yellow",command=self.delete_all)
game_continue.place(x=275,y=65)
self.human_turn = False
self.computer_turn = True
self.human_choice = None
self.Winner = None
self.fhs = 0 #fhs means final human score
self.fcs = 0 #fch means final computer score
def turn(self):
start_x = 75
start_y = 100
start_x2 = 90
start_y2 =200
Turn = canvas.create_rectangle(start_x,start_y,50,50,fill='green')
not_turn = canvas.create_rectangle(start_x2,start_y2,50,50,fill='red')
def rock(self):
if self.human_turn == False:
#280,90,400,113
self.choice_display = canvas.create_text(310,100,text="Rock",fill="white",font=('Courier',13))
global human_turn #global required to change human_turn and commputer_turn
global computer_turn
global human_choice
self.human_choice = 'Rock'
self.human_turn = True
self.computer_turn = False
self.cc()
def paper(self):
if self.human_turn == False:
self.choice_display = canvas.create_text(315,100,text="Paper",fill="white",font=('Courier',13))
global human_turn
global computer_turn
global human_choice
self.human_choice = 'Paper'
self.human_turn = True
self.computer_turn = False
self.cc()
def scissors(self):
if self.human_turn == False:
self.choice_display = canvas.create_text(330,100,text="Scissors",fill="white",font=('Courier',13))
global human_turn
global computer_turn
global human_choice
self.human_choice = 'Scissors'
self.human_turn = True
self.computer_turn = False
self.cc()
def cc(self): #(computer choice)
self.computer_choice = random.choice(self.choices)
if self.computer_turn == False:
if self.computer_choice == 'Rock':
self.c_choice_display = canvas.create_text(310,200,text="Rock",fill="white",font=("Courier",13))
global human_turn
global computer_turn
self.human_turn = False
self.computer_turn = True
self.winner()
elif self.computer_choice == 'Paper':
self.c_choice_display = canvas.create_text(315,200,text="Paper",fill="white",font=("Courier",13))
global human_turn
global computer_turn
self.human_turn = False
self.computer_turn = True
self.winner()
elif self.computer_choice == 'Scissors':
self.c_choice_display = canvas.create_text(330,200,text="Scissors",fill="white",font=("Courier",13))
global human_turn
global computer_turn
self.human_turn = False
self.computer_turn = True
self.winner()
def winner(self):
global Winner
if self.human_choice == self.computer_choice:
self.Winner == 'tie'
self.points_and_winner_display()
elif self.human_choice == 'Rock' and self.computer_choice == 'Paper':
self.Winner = 'computer'
self.points_and_winner_display()
elif self.human_choice == 'Rock' and self.computer_choice == 'Scissors':
self.Winner = 'human'
self.points_and_winner_display()
elif self.human_choice == 'Paper' and self.computer_choice == 'Rock':
self.Winner = 'human'
self.points_and_winner_display()
elif self.human_choice == 'Paper' and self.computer_choice == 'Scissors':
self.Winner = 'computer'
self.points_and_winner_display()
elif self.human_choice == 'Scissors' and self.computer_choice == 'Rock':
self.Winner = 'computer'
self.points_and_winner_display()
elif self.human_choice == 'Scissors' and self.computer_choice == 'Paper':
self.Winner = 'human'
self.points_and_winner_display()
def points_and_winner_display(self):
global fhs
global fcs
self.hw = None
self.cw = None
if self.Winner == 'human':
global hw
self.hw = canvas.create_text(230,280,text="Human",fill="white",font=("Times",15))
self.fhs += 1
elif self.Winner == 'computer':
global cw
self.cw = canvas.create_text(240,280,text="Computer",fill="white",font=("Times",15))
self.fcs += 1
else:
global tie
self.tie = canvas.create_text(225,280,text="Tie",fill="white",font=("Times",15))
self.hs = canvas.create_text(145,100,text=self.fhs,fill="white",font=('Courier',14))
self.cs = canvas.create_text(180,200,text=self.fcs,fill="white",font=('Courier',14))
def delete_all(self):
global cs
global hs
global c_choice_display
global choice_dispaly
if self.Winner == 'computer':
global cw
canvas.delete(self.cw)
elif self.Winner == 'human':
global hw
canvas.delete(self.hw)
else:
global tie
canvas.delete(self.tie)
canvas.delete(self.choice_display)
canvas.delete(self.c_choice_display)
canvas.delete(self.hs)
canvas.delete(self.cs)
canvas.pack()
Game = game()
在新一轮开始时,您尚未清除上一个获胜者 因此,在
winner
中,您发现有一个平局,并将控制权转移到积分和\u winner\u显示
但是一旦到了那里,你首先要检查两名球员是否获胜——你最后要检查“平局”状态。因为
self.Winner
仍然持有上一轮的值。前一个获胜者再次被宣布为获胜者,并且
else:
global tie
永远也达不到
只有当两名球员都没有获胜时,才会宣布平局
您可以重新安排逻辑,首先检查是否存在平局。这将解决您眼前的问题,但会带来另一个类似的问题:
平局后,平局将被记住,此后任何球员都不能宣布为赢家 解决方案是在每轮开始时重置变量
self.Winner
。让我们在
\uuuu init\uuuu
def winner(self):
global Winner
self.Winner == None // clear previous winner the at start of each round.
if self.human_choice == self.computer_choice:
self.Winner == 'tie'
self.points_and_winner_display()
....
(如果将变量作为属性与
self.
一起保存,则无需使用global
)在某些可能导致其不工作的位置,如果是这种情况,则使用了错误的变量。您不应该在这里使用global。谢谢。这帮了大忙,太棒了!很高兴听到这个消息。