Build a Classic Rock, Paper, Scissors Game with Python
The “Rock, Paper, Scissors” project aims to develop an interactive Python game in which players compete with the machine. The project attempts to teach basic programming abilities like managing user input, applying conditional logic to decide game results, and using random functions.
Prerequisites for Python Rock, Paper, Scissors Game
A fundamental understanding of is required before beginning the “Rock, Paper, Scissors” project. This includes familiarity with variables, loops, conditional statements, and functions.
The machine should install Python, and coding should be done using an IDE or text editor such as or . Having a rudimentary understanding of running Python programs from the terminal or command line is also helpful. These requirements are necessary to follow along and finish the project.
Need of Python Rock, Paper and Scissors Game
A Python-based rock-paper-scissors game with a GUI permits an interactive, visually appealing manner of playing the classical game. It provides an easy way for users to make their choices and view the outcome, making the game more engaging.
The above-mentioned GUI empowers other features of the game, such as keeping scores, adjusting game settings, and replaying the game to further improve the user’s experience.
Project File Structure
- Importing all necessary libraries and modules
- The player’s selection is requested and acknowledged as input. Once the player and computer have made their respective decisions, the outcomes are compared. Based on the decisions made, the code calculates and prints whether the player wins, loses, or draws.
- Make a window on the display for user input.
Step by Step Code Implementation
Importing Necessary Libraries:
import tkinter as tk
import random
- import tkinter as tk: This library serves to create the GUI.
- import random: This library is used to generate random choices for the computer player
options = ["Rock", "Paper", "Scissors"]
- Options = [“Rock”, “Paper”, “Scissors”]: This is the list of choices to choose from while playing the game.
def decide_winner(q, w):
if q == w:
return "It's a tie!"
elif (q == "Rock" and w == "Scissors") or \
(q == "Paper" and w == "Rock") or \
(q == "Scissors" and w == "Paper"):
return "You win!"
else:
return "You lose!"
def decide_winner(q, w): This function determines the winner based on the choices made by the player and the computer:
- It’s a draw if both choices are the same.
- If the player’s choice is more powerful than that of the computer, that is, Rock beats Scissors, Paper beats Rock, and Scissors beats Paper, the player is declared the winner.
- Otherwise, the computer wins.
Creating the Main Window:
app = tk.Tk()
app.title("Rock, Paper, Scissors Game")
app.geometry("400x300")
- app = tk.Tk(): Initializes the main application window.
- app.title(“Rock, Paper, Scissors Game”): Sets the title of the window.
- app.geometry(“400x300”): Sets the initial size of the window.
Handling Button Clicks:
def on_button_click(choice):
w = random.choice(options)
outcome = decide_winner(choice, w)
display_text.set(f"Your choice: {choice}\nComputer's choice: {w}\n{outcome}")
def on_button_click(choice): Called when a button is clicked.
- w = random.choice(options): Randomly selects the choice by the computer.
- outcome = decide_winner(choice, w): The function decides the winner involving the choice of a player and the computer.
- display_text.set(f”Your choice: {choice} Computer’s choice: {w} {outcome}”): Updates the display text with the player’s choice, computer’s choice, and the outcome.
button_rock = tk.Button(app, text="Rock", command=lambda: on_button_click("Rock"))
button_rock.pack(pady=10)
button_paper = tk.Button(app, text="Paper", command=lambda: on_button_click("Paper"))
button_paper.pack(pady=10)
button_scissors = tk.Button(app, text="Scissors", command=lambda: on_button_click("Scissors"))
button_scissors.pack(pady=10)
- button_rock = tk.Button(app, text=”Rock”, command=lambda: on_button_click(“Rock”)) This displays a “Rock” option to the user in the form of a button.
- button_paper = tk.Button(app, text=”Paper”, command=lambda: on_button_click(“Paper”)) : Button widget for “Paper” choice.
- It creates a “Scissors” choice button: button_scissors = tk.Button(app, text=”Scissors”, command=lambda: on_button_click(“Scissors”): ).
- .pack(pady=10): It will pack buttons vertically with some padding between them.
display_text = tk.StringVar()
result_display = tk.Label(app, textvariable=display_text)
result_display.pack(pady=20)
- display_text = tk.StringVar(): Creates a StringVar to hold the result display text.
- result_display = tk.Label(app, textvariable=display_text): This displays the result in a label.
- .pack(pady=20): Places the result display label with some padding below the buttons.
Running the Application:
app.mainloop()
Python Rock, Paper, Scissors Game Output
Conclusion
Options are initialized at the start of the game. After the player inputs a valid option, the computer selects an option randomly, and the game’s logic decides whether the outcome is a tie, a player win, or a loss.