Aarushan Shankar
Aarushan Shankar

Reputation: 11

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

So in this rock paper scissors game, I make the score, but for some reason after 1 more move, the score resets. For example, I win, then my score gets increased to 1. If I lose again, though it always goes back to 0 instead of the opponent getting a point. This is my code:

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

Please help. I've spent 2 hours looking for what was wrong but I can't find it. I'm an intermediate at Tkinter so if was an easy fix sorry😅.

Upvotes: 0

Views: 213

Answers (1)

Tom
Tom

Reputation: 11

You have to set the score to another variable to it will be stored. What happens when you use the same variable as the data goes it gets reset.

For example you could do something like:

ScorePlayer += playerScore

And for the opponent:

ScoreOpp += opponentScore

Upvotes: 1

Related Questions