I am pretty new to programming and am trying to get better. I have wrote this game in tkinter, but I can not figure out how can I add a while loop or something of that nature to my code so I do not have to reset the score to zero every time. I am also aware that my code is not the cleanest nor it is the best, but like I said I am pretty new to programming and was wondering if someone could help me figure out what should I do?
from tkinter import *
import random
m = Tk()
blank_space = " " * 65
m.title(blank_space "Rock Paper Scissors Game")
m.config(width=600, height=400, bg='grey')
# Frames
button_frame = Frame(m, width=300, height=200)
button_frame.grid_propagate(False)
button_frame.grid(row=0, column=0, padx=150)
text_frame = Frame(m, width=500, height=200)
text_frame.grid_propagate(False)
text_frame.grid(row=1, column=0, padx=50)
# Functions
def rock():
user_choice_label.config(text="your choice is : Rock")
computer_score = 0
user_score = 0
user_choice = "Rock"
available_text = ["Rock", "Paper", "Scissors"]
computer_choice = random.choice(available_text)
if computer_choice == user_choice:
computer_score = 0
user_score = 0
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
computer_score_label.config(text=f"computer score is : {computer_score}")
user_score_label.config(text=f"your score is : {user_score}")
if computer_choice == "Paper":
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
computer_score = 1
user_score = 0
computer_score_label.config(text=f"computer score is : {computer_score}")
user_score_label.config(text=f"your score is : {user_score}")
if computer_choice == "Scissors":
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
user_score = 1
computer_score = 0
user_score_label.config(text=f"your score is : {user_score}")
computer_score_label.config(text=f"computer score is : {computer_score}")
def paper():
user_choice_label.config(text="your choice is : Paper")
user_score = 0
computer_score = 0
user_choice = "Paper"
available_text = ["Rock", "Paper", "Scissors"]
computer_choice = random.choice(available_text)
if computer_choice == user_choice:
computer_score = 0
user_score = 0
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
computer_score_label.config(text=f"computer score is : {computer_score}")
user_score_label.config(text=f"your score is : {user_score}")
if computer_choice == "Scissors":
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
computer_score = 1
user_score = 0
computer_score_label.config(text=f"computer score is : {computer_score}")
user_score_label.config(text=f"your score is : {user_score}")
if computer_choice == "Rock":
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
user_score = 1
computer_score = 0
user_score_label.config(text=f"your score is : {user_score}")
computer_score_label.config(text=f"computer score is : {computer_score}")
def scissors():
user_choice_label.config(text="your choice is : Scissors")
user_score = 0
computer_score = 0
user_choice = "Scissors"
available_text = ["Rock", "Paper", "Scissors"]
computer_choice = random.choice(available_text)
if computer_choice == user_choice:
computer_score = 0
user_score = 0
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
computer_score_label.config(text=f"computer score is : {computer_score}")
user_score_label.config(text=f"your score is : {user_score}")
if computer_choice == "Rock":
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
computer_score = 1
user_score = 0
computer_score_label.config(text=f"computer score is : {computer_score}")
user_score_label.config(text=f"your score is : {user_score}")
if computer_choice == "Paper":
computer_choice_label.config(text=f"computer choice is : {computer_choice}")
user_score = 1
computer_score = 0
user_score_label.config(text=f"your score is : {user_score}")
computer_score_label.config(text=f"computer score is : {computer_score}")
# User Buttons
rock_button = Button(button_frame, text="Rock", activebackground='red', command=rock, width=15, bg='white')
rock_button.grid(row=0, column=0, padx=90, pady=15)
paper_button = Button(button_frame, text="Paper", command=paper, activebackground='red', width=15, bg='white')
paper_button.grid(row=1, column=0, padx=90, pady=15)
scissors_button = Button(button_frame, text="Scissors", command=scissors, activebackground='red', width=15, bg='white')
scissors_button.grid(row=2, column=0, padx=90, pady=15)
# Contents for the text frame
user_choice_label = Label(text_frame, text="your choice is : ")
user_choice_label.grid(row=1, column=0, padx=20, pady=10)
computer_choice_label = Label(text_frame, text="computer choice is : ")
computer_choice_label.grid(row=1, column=2, padx=120, pady=10)
computer_score_label = Label(text_frame, text="computer score is : ")
computer_score_label.grid(row=3, column=2)
user_score_label = Label(text_frame, text="your score is : ")
user_score_label.grid(row=3, column=0, padx=20)
# Exit button
exit_button = Button(text_frame, text="Exit", command=m.destroy, width =30, activebackground='red')
exit_button.grid(row=5, column=0, columnspan=3, pady= 20, padx=40)
m.mainloop()
CodePudding user response:
The idea to increment the scores is that you need to declare user_score
and computer_score
as a global
variable.
global user_score, computer_score
user_score = 0
computer_score = 0
m = Tk()
blank_space = " " * 65
.....
.....
Also, in each function, add global user_score, computer_score
and update the score assignment to increment (e.g. computer_score = 0
).
For example,
def rock():
global user_score, computer_score
....
....
if computer_choice == user_choice:
computer_score = 0
user_score = 0
....
....
if computer_choice == "Paper":
computer_score = 1
user_score = 0
....
....
if computer_choice == "Scissors":
user_score = 1
computer_score = 0
....
....
Expected result: