A Rock, Paper, Scissors game using tkinter in Python

Here’s how to create a simple Rock, Paper, Scissors game using tkinter in Python.

In this game, the player chooses Rock, Paper, or Scissors, and the computer randomly chooses one of the three options.

The program then displays the choices and determines the winner based on standard Rock-Paper-Scissors rules.

Table of Contents

Toggle

Code

import tkinter as tk
import random

# Set up main application window
root = tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("400x400")

# Define choices and outcomes
choices = ["Rock", "Paper", "Scissors"]

# Function to handle the game logic
def play_game(player_choice):
    # Random choice for the computer
    computer_choice = random.choice(choices)
    
    # Determine the result
    if player_choice == computer_choice:
        result = "It's a tie!"
    elif (player_choice == "Rock" and computer_choice == "Scissors") or \
         (player_choice == "Paper" and computer_choice == "Rock") or \
         (player_choice == "Scissors" and computer_choice == "Paper"):
        result = "You win!"
    else:
        result = "Computer wins!"
    
    # Update labels to show choices and result
    player_choice_label.config(text="You chose: " + player_choice)
    computer_choice_label.config(text="Computer chose: " + computer_choice)
    result_label.config(text="Result: " + result)

# Labels for instructions and displaying choices and results
instructions_label = tk.Label(root, text="Choose Rock, Paper, or Scissors", font=("Helvetica", 16))
instructions_label.pack(pady=20)

player_choice_label = tk.Label(root, text="You chose: ", font=("Helvetica", 14))
player_choice_label.pack(pady=10)

computer_choice_label = tk.Label(root, text="Computer chose: ", font=("Helvetica", 14))
computer_choice_label.pack(pady=10)

result_label = tk.Label(root, text="Result: ", font=("Helvetica", 16))
result_label.pack(pady=20)

# Buttons for Rock, Paper, and Scissors
rock_button = tk.Button(root, text="Rock", command=lambda: play_game("Rock"), width=15)
rock_button.pack(pady=5)

paper_button = tk.Button(root, text="Paper", command=lambda: play_game("Paper"), width=15)
paper_button.pack(pady=5)

scissors_button = tk.Button(root, text="Scissors", command=lambda: play_game("Scissors"), width=15)
scissors_button.pack(pady=5)

# Run the main event loop
root.mainloop()

Explanation

  1. Import Libraries:
    • tkinter is imported as tk to create the GUI components.
    • random is used to let the computer choose randomly between Rock, Paper, and Scissors.
  2. Set Up the Main Application Window:
    • root = tk.Tk() creates the main application window.
    • root.title(“Rock Paper Scissors”) sets the title of the window.
    • root.geometry(“400×400”) sets the size of the window to 400×400 pixels.
  3. Define Choices and Outcomes:
    • choices is a list containing the three possible choices for the game: “Rock”, “Paper”, and “Scissors”.
  4. Function to Handle Game Logic (play_game):
    • play_game(player_choice) is a function that takes the player’s choice as an argument.
    • computer_choice = random.choice(choices) selects a random choice from the choices list for the computer.
    • The if and elif statements then determine the result:
      • If player_choice == computer_choice, it’s a tie.
      • Otherwise, the game logic checks who wins based on the rules:
        • Rock beats Scissors
        • Scissors beat Paper
        • Paper beats Rock
    • Updating the Result:
      • player_choice_label.config(text=”You chose: ” + player_choice) updates the label with the player’s choice.
      • computer_choice_label.config(text=”Computer chose: ” + computer_choice) updates the label with the computer’s choice.
      • result_label.config(text=”Result: ” + result) updates the result label with the game outcome.
  5. Labels for Displaying Instructions, Choices, and Results:
    • instructions_label displays the game instructions.
    • player_choice_label shows what the player chose.
    • computer_choice_label shows what the computer chose.
    • result_label shows the outcome of the game (Win, Lose, or Tie).
  6. Buttons for Player Choices:
    • Each button (Rock, Paper, Scissors) calls play_game() with the respective choice as an argument using lambda.
    • rock_button, paper_button, and scissors_button are buttons that execute play_game(“Rock”), play_game(“Paper”), and play_game(“Scissors”) respectively when clicked.
  7. Run the Main Event Loop:
    • root.mainloop() starts the main event loop, keeping the window open and responsive to user interactions.

Summary

  • The Rock-Paper-Scissors game simulates a basic game where the player competes against the computer.
  • The game uses random.choice() to let the computer choose randomly.
  • Labels and buttons in tkinter provide the user interface for displaying instructions, choices, and results.
  • The game logic is implemented in the play_game function, which handles input from both the player and computer and updates the game status accordingly.

This program is a simple yet interactive way to create a GUI-based game using tkinter.

Related posts

BMI and BMR calculator using Tkinter

A Python program that generates a random password

a Python GUI-based random quote generator