0% found this document useful (0 votes)
6 views6 pages

Rock Paper Scissors - 3 Rounds

The document provides a detailed breakdown of a Rock Paper Scissors game implemented in Python using the Tkinter library. It explains the code structure, including importing modules, creating the game class, initializing the GUI, handling player moves, determining winners, and resetting the game. The game allows a player to compete against the computer over three rounds, displaying results and scores throughout the process.

Uploaded by

Tanmay Warthe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Rock Paper Scissors - 3 Rounds

The document provides a detailed breakdown of a Rock Paper Scissors game implemented in Python using the Tkinter library. It explains the code structure, including importing modules, creating the game class, initializing the GUI, handling player moves, determining winners, and resetting the game. The game allows a player to compete against the computer over three rounds, displaying results and scores throughout the process.

Uploaded by

Tanmay Warthe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Let's break down the Rock Paper Scissors - 3 Rounds game step by step, explaining each part of the

code in detail.

1. Importing Required Modules

import random

import tkinter as tk

from tkinter import messagebox

 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).

2. Creating the RockPaperScissors Class

class RockPaperScissors:

 This class represents the Rock-Paper-Scissors game.

 It handles the GUI, game logic, and final results.

3. Initializing the Game Window

def __init__(self):

self.window = tk.Tk()

self.window.title("Rock Paper Scissors - 3 Rounds")

self.window.geometry("300x300")

 self.window = tk.Tk(): Creates the main application window.

 self.window.title(): Sets the title of the window.

 self.window.geometry("300x300"): Sets the window size to 300x300 pixels.

4. Initializing Game Variables

self.player_score = 0

self.computer_score = 0

self.round = 1

 self.player_score: Stores the player's score.


 self.computer_score: Stores the computer's score.

 self.round: Tracks the current round number (starts at 1).

5. Creating the Score Label

self.score_label = tk.Label(self.window, text="Round 1/3", font=('Arial', 12))

self.score_label.pack(pady=10)

 tk.Label(): Creates a label to display the current round.

 text="Round 1/3": Shows the current round out of 3.

 .pack(pady=10): Adds some vertical spacing (10 pixels).

6. Creating Buttons for Player Choices

for choice in ['Rock', 'Paper', 'Scissors']:

tk.Button(self.window, text=choice, width=10,

command=lambda x=choice: self.play(x)).pack(pady=5)

 This loop creates 3 buttons for "Rock", "Paper", and "Scissors".

 command=lambda x=choice: self.play(x): Calls the play() function when a button is clicked
and passes the player's choice (x).

 .pack(pady=5): Adds vertical spacing (5 pixels).

7. Creating a Label to Show Round Results

self.result_label = tk.Label(self.window, text="", font=('Arial', 11))

self.result_label.pack(pady=20)

 This label displays:

o The player's choice.

o The computer's choice.

o The round result (Win, Lose, or Tie).

 It starts as an empty string (text="").

 .pack(pady=20): Adds vertical spacing (20 pixels).

8. Handling Player’s Move (play Method)

def play(self, player_choice):


if self.round > 3:

return

 play() is triggered when the player selects Rock, Paper, or Scissors.

 if self.round > 3: Stops the game if the 3 rounds are already completed.

(A) Computer’s Random Choice

computer = random.choice(['Rock', 'Paper', 'Scissors'])

 The computer randomly selects one of the three choices.

(B) Determining the Round Winner

if player_choice == computer:

result = "Tie!"

 If both player and computer choose the same, it's a tie.

elif ((player_choice == 'Rock' and computer == 'Scissors') or

(player_choice == 'Paper' and computer == 'Rock') or

(player_choice == 'Scissors' and computer == 'Paper')):

result = "You win this round!"

self.player_score += 1

 The player wins if:

o Rock beats Scissors

o Paper beats Rock

o Scissors beat Paper

 The player's score (self.player_score) increases by 1.

else:

result = "Computer wins this round!"

self.computer_score += 1

 The computer wins in all other cases, so its score (self.computer_score) increases by 1.

(C) Updating the Result Label

self.result_label.config(text=f"Your choice: {player_choice}\n"

f"Computer's choice: {computer}\n{result}")

 This updates the label to show:

o Player’s choice
o Computer’s choice

o Who won the round?

(D) Moving to the Next Round or Ending the Game

if self.round == 3:

self.show_winner()

else:

self.round += 1

self.score_label.config(text=f"Round {self.round}/3")

 If 3 rounds are completed, call show_winner() to display the final result.

 Otherwise, move to the next round and update the round label.

9. Displaying the Final Winner (show_winner Method)

def show_winner(self):

final_result = "Game Over!\n"

if self.player_score > self.computer_score:

final_result += "You win the game!"

elif self.computer_score > self.player_score:

final_result += "Computer wins the game!"

else:

final_result += "It's a tie!"

 It first prints "Game Over!".

 Then, it checks:

o Player has a higher score → "You win the game!"

o Computer has a higher score → "Computer wins the game!"

o Same scores → "It's a tie!"

(A) Showing Final Result in a Message Box

messagebox.showinfo("Game Over", final_result)

 A pop-up window appears displaying the final result.

(B) Resetting the Game

self.reset_game()
 Calls reset_game() to restart the game.

10. Resetting the Game (reset_game Method)

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="")

 Resets player and computer scores to 0.

 Sets the round back to 1.

 Updates the labels to show that the game has restarted.

11. Running the Game (run Method)

def run(self):

self.window.mainloop()

 Calls self.window.mainloop(), which keeps the GUI running until closed.

12. Creating and Running the Game

game = RockPaperScissors()

game.run()

 Creates an instance of RockPaperScissors.

 Calls run() to start the game.

💡 Summary of Game Flow

1. Game starts → GUI opens.

2. Player chooses Rock, Paper, or Scissors.

3. Computer chooses randomly.

4. Winner is decided → Score is updated.

5. Repeat for 3 rounds.

6. After 3 rounds → Show the final winner.


7. Game resets after showing the result.

This is a simple yet effective Rock-Paper-Scissors game in Python using Tkinter! 🎮

You might also like