蟒蛇岩石剪刀不理解领带

时间:2017-07-22 17:47:46

标签: 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 = 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()

1 个答案:

答案 0 :(得分:1)

在新一轮开始时,你还没有清除之前的赢家。

因此,在winner中,您发现存在平局,并将控制转移到points_and_winner_display

但是一旦到了,你首先要检查两位球员的胜利 - 你正在检查"领带"状态最后。
由于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()
    ....