Rock Paper Scissor Game using Python

Rock Paper Scissor Game using Python

Introduction

In this article, we’ll explore the process of building a Rock Paper Scissor game using Python and Tkinter. Tkinter is a versatile library for crafting graphical user interfaces (GUIs), while Python’s simplicity and flexibility make it an ideal choice for game development. By the end of this tutorial, you’ll have a fully functional Rock, Paper, Scissors game that you can play and personalize to your liking.

Setup

To begin, ensure that Python is installed on your system. Tkinter comes bundled with Python, eliminating the need for additional installations. You can utilize any text editor or Integrated Development Environment (IDE) of your preference to write the code.

Download Python

Implementation

  1. Importing Necessary Libraries: We kickstart by importing the required libraries. Tkinter will be employed for constructing the GUI components.
  2. Creating the RockPaperScissors Class: We define a class named RockPaperScissors, which will encapsulate all the functionalities of our game.
  3. Initializing the GUI: Inside the init method of the RockPaperScissors class, we create the main window, buttons for player choices (rock, paper, scissors), labels for displaying game results, and buttons for starting a game and resetting the scores.
  4. Starting the Game: When the “Start Game” button is clicked, the start_game method is invoked. This method initializes the game state and awaits player input.
  5. Handling Player Input: We bind the buttons for rock, paper, and scissors to their respective methods. When a button is clicked, the player’s choice is determined, and the game proceeds to evaluate the outcome.
  6. Game Logic: The game logic checks the player’s choice against the computer’s randomly generated choice (rock, paper, or scissors) to determine the winner. The result is displayed on the GUI.
  7. Updating Scores: The scores for the player and computer are updated based on the game outcome and displayed on the GUI.
  8. Starting a New Game: The “New Game” button enables players to reset the scores and start a new game.

Explanation

This project leverages Tkinter to create a straightforward GUI featuring buttons and labels. The game follows the classic Rock, Paper, Scissors gameplay, where the player selects one of three options (rock, paper, or scissors), competing against the computer’s randomly chosen selection.

The game state is managed through simple logic, comparing the player’s choice against the computer’s choice to determine the winner. The results are displayed on the GUI, updating the scores accordingly.

Player interaction is facilitated by clicking on the buttons corresponding to their chosen move. The game responds by evaluating the outcome and updating the scores on the GUI.

Conclusion

In this article, we’ve explored how to construct a basic Rock, Paper, Scissors game using Python and Tkinter. This project serves as an entertaining way to practice GUI programming and basic game development concepts. You’re encouraged to customize and enhance the game further by incorporating sound effects, refining the graphics, or introducing additional features. Happy coding and enjoy playing!

Read some more similar posts: Click Here

Source Code for Rock Paper Scissor Game using Python

import tkinter as tk
from tkinter import messagebox
import random

def determine_winner(user_choice, computer_choice):
    """Determine the winner of the game."""
    if user_choice == computer_choice:
        return "It's a tie!"
    elif (user_choice == 'rock' and computer_choice == 'scissors') or \
         (user_choice == 'paper' and computer_choice == 'rock') or \
         (user_choice == 'scissors' and computer_choice == 'paper'):
        return "You win!"
    else:
        return "Computer wins!"

class RockPaperScissors(tk.Tk):
    """Rock, Paper, Scissors game."""

    def __init__(self):
        super().__init__()
        self.title("Rock, Paper, Scissors")
        self.geometry("300x200")
        
        self.count_h = 0
        self.count_c = 0

        self.label_score = tk.Label(self, text=f"Human: {self.count_h} | Computer: {self.count_c}")
        self.label_score.pack(pady=10)

        self.button_frame = tk.Frame(self)
        self.button_frame.pack(pady=10)

        self.rock_button = tk.Button(self.button_frame, text="Rock", width=10, command=lambda: self.play_game('rock'))
        self.rock_button.pack(side=tk.LEFT, padx=10)

        self.paper_button = tk.Button(self.button_frame, text="Paper", width=10, command=lambda: self.play_game('paper'))
        self.paper_button.pack(side=tk.LEFT, padx=10)

        self.scissors_button = tk.Button(self.button_frame, text="Scissors", width=10, command=lambda: self.play_game('scissors'))
        self.scissors_button.pack(side=tk.LEFT, padx=10)

        self.new_game_button = tk.Button(self, text="New Game", width=10, command=self.new_game)
        self.new_game_button.pack(pady=10)

    def play_game(self, user_choice):
        """Play a single game of Rock, Paper, Scissors."""
        computer_choice = random.choice(['rock', 'paper', 'scissors'])
        result = determine_winner(user_choice, computer_choice)
        messagebox.showinfo("Result", f"The computer chose {computer_choice}.\n{result}")
        if result == "You win!":
            self.count_h += 1
        elif result == "Computer wins!":
            self.count_c += 1
        self.update_scores()

    def update_scores(self):
        """Update the score labels."""
        self.label_score.config(text=f"Human: {self.count_h} | Computer: {self.count_c}")

    def new_game(self):
        """Reset the scores."""
        self.count_h = 0
        self.count_c = 0
        self.update_scores()

if __name__ == "__main__":
    app = RockPaperScissors()
    app.mainloop()

Output

Rock Paper Scissor Game using Python

Leave a Comment