Rock Paper Scissors - 3 Rounds
Rock Paper Scissors - 3 Rounds
code in detail.
import random
import tkinter as tk
random: Used to make the computer choose randomly between "Rock", "Paper", or
"Scissors".
tkinter: A built-in Python library for creating graphical user interfaces (GUIs).
messagebox: A module inside tkinter to display pop-up messages (used for showing the final
game result).
class RockPaperScissors:
def __init__(self):
self.window = tk.Tk()
self.window.geometry("300x300")
self.player_score = 0
self.computer_score = 0
self.round = 1
self.score_label.pack(pady=10)
command=lambda x=choice: self.play(x): Calls the play() function when a button is clicked
and passes the player's choice (x).
self.result_label.pack(pady=20)
return
if self.round > 3: Stops the game if the 3 rounds are already completed.
if player_choice == computer:
result = "Tie!"
self.player_score += 1
else:
self.computer_score += 1
The computer wins in all other cases, so its score (self.computer_score) increases by 1.
o Player’s choice
o Computer’s choice
if self.round == 3:
self.show_winner()
else:
self.round += 1
self.score_label.config(text=f"Round {self.round}/3")
Otherwise, move to the next round and update the round label.
def show_winner(self):
else:
Then, it checks:
self.reset_game()
Calls reset_game() to restart the game.
def reset_game(self):
self.player_score = 0
self.computer_score = 0
self.round = 1
self.score_label.config(text="Round 1/3")
self.result_label.config(text="")
def run(self):
self.window.mainloop()
game = RockPaperScissors()
game.run()