社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

我正在用Python和Tkinter制作一个石头剪刀游戏。我的分数不正常

Aarushan Shankar • 3 年前 • 1175 次点击  

所以在这个石头剪刀布游戏中,我得分了,但由于某种原因,在再移动一步后,得分会重置。例如,我赢了,然后我的分数增加到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()

请帮忙。我花了两个小时寻找哪里出了问题,但我找不到。我是Tkinter的中间人,所以如果这是一个简单的解决方案,对不起。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133129
 
1175 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Tom
Reply   •   1 楼
Tom    3 年前

您必须将分数设置为另一个变量,以便存储它。当使用与数据相同的变量时会发生什么?它会被重置。

例如,你可以这样做:

ScorePlayer+=球员核心

对于对手:

ScoreOpp+=opponentScore