2017-07-22 14 views
-1

我正在做岩石剪刀,一旦一方獲勝,那麼一個領帶之後 會說前一方獲勝。例如:人類勝利,但是說人類勝利,並增加人類得分。這裏發生了什麼?Python岩石剪刀不理解領帶

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() 
+2

(無需使用'如果你持有變量與'self.'屬性global') – PRMoureu

+0

在某些點會導致它不工作 –

+1

如果是這樣的話,那麼你正在使用錯誤的變量。你根本不應該在這裏使用全球。 –

回答

1

在新一輪的開始,你還沒有清除以前的贏家。

因此在winner中,您發現有一條平行線,並將控制權轉移至points_and_winner_display

但是一旦出現,你首先檢查兩個球員的勝利 - 你最後檢查「tie」狀態。
由於self.Winner仍然保持上一輪的價值。
以前的獲勝者可以得到再次宣佈獲勝者,並

else: 
    global tie 

從未達到。

只有兩個球員都沒有贏,是否宣佈平局。

您可以重新安排邏輯以首先檢查是否存在聯繫。
這將解決你的直接問題,但引入另一個類似的問題:
平局之後,領帶將被記住,並且之後沒有任何球員可以被宣佈爲獲勝者。

解決的辦法是在每輪開始時重置變量self.Winner
讓我們把它恢復到初始化遊戲時給定的值,期間__init__

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() 
    .... 
+0

謝謝。這幫了很多 –

+0

非常棒!很高興聽到。 – SherylHohman