簡體   English   中英

我正在使用 Tkinter 在 Python 中制作剪刀石頭布游戲。 我的分數工作不正常

[英]I'm Making a Rock Paper Scissors game in Python with Tkinter. My Score isn't Working Properly

所以在這個剪刀石頭布游戲中,我得分,但由於某種原因,在多走 1 步后,得分重置。 例如,我贏了,那么我的分數會增加到 1。如果我再次輸了,雖然它總是會回到 0 而不是對手得到一分。 這是我的代碼:

import random
from tkinter import *
from tkinter.font import Font
from PIL import ImageTk, Image

OppoScore = 0
PlayerScore = 0
OpponentMove = ""
PlayerMove = ""
#Make Opponent Move
def Opponent(RockLabel, Rock, Paper, Scissors):
    global OpponentMove
    Choice = random.choice(range(3))
    if Choice == 0:
        RockLabel.config(image=Rock)
        OpponentMove = "rock"
    if Choice == 1:
        RockLabel.config(image=Paper)
        OpponentMove = "paper"
    if Choice == 2:
        RockLabel.config(image=Scissors)
        OpponentMove = "scissors"

#Make 'Rock' Button React
def RockFun(Label2, Label, Rock, Paper, Scissors, FlippedRock):
    global PlayerMove, OpponentMove, PlayerScore, OppoScore
    Opponent(Label2, Rock, Paper, Scissors)
    Label.config(image=FlippedRock)
    PlayerMove = "rock"
    Score(PlayerMove, OpponentMove, PlayerScore, OppoScore)

#Make 'Paper' Button React
def PaperFun(Label2, Label, Rock, Paper, Scissors, FlippedPaper):
    global PlayerMove, OpponentMove, PlayerScore, OppoScore
    Opponent(Label2, Rock, Paper, Scissors)
    Label.config(image=FlippedPaper)
    PlayerMove = "paper"
    Score(PlayerMove, OpponentMove, PlayerScore, OppoScore)

#Make 'Scissors' Button React
def ScissorsFun(Label2, Label, Rock, Scissors, Paper, FlippedScissors):
    global PlayerMove, OpponentMove, PlayerScore, OppoScore, OppoScoreTxt, PlayScoreTxt
    Opponent(Label2, Rock, Paper, Scissors)
    Label.config(image=FlippedScissors)
    PlayerMove = "scissors"
    Score(PlayerMove, OpponentMove, PlayerScore, OppoScore)

def Score(Player, Opponent, PlayScore, OppoScore):
    global OppoVar, PlayVar
    if Player == "rock" and Opponent == "rock":
        PlayScore += 0
        OppoScore += 0
    if Player == "rock" and Opponent == "paper":
        OppoScore += 1
    if Player == "rock" and Opponent == "scissors":
        PlayScore += 1
    if Player == "paper" and Opponent == "rock":
        PlayScore += 1
    if Player == "paper" and Opponent == "paper":
        PlayScore += 0
        OppoScore += 0
    if Player == "paper" and Opponent == "scissors":
        OppoScore += 1
    if Player == "scissors" and Opponent == "rock":
        PlayScore += 0
        OppoScore += 0
    if Player == "scissors" and Opponent == "paper":
        OppoScore += 1
    if Player == "scissors" and Opponent == "scissors":
        PlayScore += 1
    OppoVar.set(str(OppoScore))
    PlayVar.set(str(PlayScore))

#Window Settings
root = Tk()
root.geometry("600x600")
root.title("Rock, Paper, Scissors!")
root.iconbitmap("icon.ico")
root.resizable(False, False)
root.config(bg="#ff7575")

#Buttons
Rock = Button(root,text="Rock", height=5, width=20, bd=0, command= lambda: RockFun(RockLabel, RockFlippedLabel, Rock, Paper, Scissors, RockFlipped))
Rock.place(x=50, y=400)
Paper = Button(root,text="Paper", height=5, width=20, bd=0, command= lambda: PaperFun(RockLabel, RockFlippedLabel, Rock, Paper, Scissors, PaperFlipped))
Paper.place(x=225, y=400)
Scissors = Button(root,text="Scissors", height=5, width=20, bd=0, command= lambda: ScissorsFun(RockLabel, RockFlippedLabel, Rock, Scissors, Paper, ScissorsFlipped))
Scissors.place(x=400, y=400)

#Score Text
OppoVar = StringVar()
PlayVar = StringVar()
Font1 = Font(family="comicsansms", size=50)
OppoScoreTxt = Label(root, textvariable=OppoVar, font=Font1, bg="#ff7575")
OppoScoreTxt.place(x=100, y=500)
PlayScoreTxt = Label(root, textvariable=PlayVar, font=Font1, bg="#ff7575")
PlayScoreTxt.place(x=450, y=500)
OppoVar.set(str(OppoScore))
PlayVar.set(str(PlayerScore))

#Opponent Hand (Set as rock)
RockImage = Image.open("Rock.png")
RockImage1 = RockImage.resize((300, 300), Image.ANTIALIAS)
Rock = ImageTk.PhotoImage(RockImage1)
RockLabel = Label(root, image=Rock, bg="white")
RockLabel.place(x= -2, y=75)

#Player Hand (Set as rock). Flipped images in this
RockImageFlipped = Image.open("RockFlipped.png")
RockImageFlipped1 = RockImageFlipped.resize((300, 300), Image.ANTIALIAS)
RockFlipped = ImageTk.PhotoImage(RockImageFlipped1)
RockFlippedLabel = Label(root, image=RockFlipped, bg="white")
RockFlippedLabel.place(x=298, y=75)

#Scissors
ScissorsImage = Image.open("Scissors.png")
ScissorsImage1 = ScissorsImage.resize((300, 300), Image.ANTIALIAS)
Scissors = ImageTk.PhotoImage(ScissorsImage1)

#Paper
PaperImage = Image.open("Paper.png")
PaperImage1 = PaperImage.resize((300, 300), Image.ANTIALIAS)
Paper = ImageTk.PhotoImage(PaperImage1)

#Flipped Rock
ScissorsFlippedImage = Image.open("ScissorsFlipped.png")
ScissorsFlippedImage1 = ScissorsFlippedImage.resize((300, 300), Image.ANTIALIAS)
ScissorsFlipped = ImageTk.PhotoImage(ScissorsFlippedImage1)

#Flipped Paper
PaperFlippedImage = Image.open("PaperFlipped.png")
PaperFlippedImage1 = PaperFlippedImage.resize((300, 300), Image.ANTIALIAS)
PaperFlipped = ImageTk.PhotoImage(PaperFlippedImage1)

#Loop the window
root.mainloop()

請幫忙。 我花了 2 個小時尋找出了什么問題,但我找不到。 我是 Tkinter 的中間人,所以如果是一個簡單的解決方案,抱歉。

您必須將分數設置為另一個變量才能存儲。 當您使用與數據相同的變量時會發生什么情況,它會被重置。

例如,您可以執行以下操作:

ScorePlayer += playerScore

而對於對手:

ScoreOpp += 對手得分

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM