Welcome to PyMakers! In this blog post, we’re going to explore a classic and entertaining game, “Rock, Paper, Scissors,” implemented using the Pygame library. We will take you through the code step by step, explaining how it works, and discuss how you can use this as a foundation for building your own interactive Pygame games.

Pygame: A Versatile Game Development Library

Pygame is a Python library specifically designed for creating 2D games and multimedia applications. It provides essential tools for handling graphics, user input, audio, and more, making it a fantastic choice for game development, interactive simulations, and educational projects.

Building “Rock, Paper, Scissors” with Pygame

Rock paper scissors GIF

The code you provided demonstrates a simplified version of the “Rock, Paper, Scissors” game using Pygame. It allows a player to choose from three options (Rock, Paper, or Scissors) and pits their choice against the computer’s randomly generated choice. Let’s break down the code to understand how this game works.

Setting Up the Environment

We start by importing the Pygame library and initializing it to set up the game environment.

import pygame
import random

# Initialize Pygame
pygame.init()

Constants and Window Setup

Next, we define several constants, such as the window dimensions, colors, and button dimensions for the game’s interface.

# Constants
WIDTH, HEIGHT = 480, 400
WHITE = (255, 255, 255)
ROCK, PAPER, SCISSORS = 0, 1, 2
OPTIONS = ["Rock", "Paper", "Scissors"]
BUTTON_WIDTH, BUTTON_HEIGHT = 130, 50
ROCK_BUTTON_RECT = pygame.Rect(50, 300, BUTTON_WIDTH, BUTTON_HEIGHT)
PAPER_BUTTON_RECT = pygame.Rect(175, 300, BUTTON_WIDTH, BUTTON_HEIGHT)
SCISSORS_BUTTON_RECT = pygame.Rect(300, 300, BUTTON_WIDTH, BUTTON_HEIGHT)
FONT = pygame.font.Font(None, 36)

These constants help maintain a consistent appearance and layout for the “Rock, Paper, Scissors” game.

Create the Game Window

We create the Pygame window, set its dimensions, and give it a title.

# Create a window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rock, Paper, Scissors")

This code initializes the game window where the “Rock, Paper, Scissors” game will be played.

Game Logic

We define functions and variables for the game’s logic. These functions determine the winner based on the player’s choice and the computer’s random choice.

def get_computer_choice():
    return random.randint(0, 2)

def determine_winner(player_choice, computer_choice):
    if player_choice == computer_choice:
        return "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):
        return "You win!"
    else:
        return "Computer wins!"

These functions handle the comparison of choices and return the result of the game.

Creating Buttons

We create buttons for the three game options: Rock, Paper, and Scissors.

ROCK_BUTTON_RECT = pygame.Rect(50, 300, BUTTON_WIDTH, BUTTON_HEIGHT)
PAPER_BUTTON_RECT = pygame.Rect(175, 300, BUTTON_WIDTH, BUTTON_HEIGHT)
SCISSORS_BUTTON_RECT = pygame.Rect(300, 300, BUTTON_WIDTH, BUTTON_HEIGHT)

These buttons are essential for player interaction with the game.

Drawing Buttons

A function named draw_buttons() is defined to draw the buttons and their corresponding text labels on the screen.

def draw_buttons():
    pygame.draw.rect(screen, (0, 128, 255), ROCK_BUTTON_RECT)
    pygame.draw.rect(screen, (0, 128, 255), PAPER_BUTTON_RECT)
    pygame.draw.rect(screen, (0, 128, 255), SCISSORS_BUTTON_RECT)
    rock_text = FONT.render("Rock", True, WHITE)
    paper_text = FONT.render("Paper", True, WHITE)
    scissors_text = FONT.render("Scissors", True, WHITE)
    screen.blit(rock_text, (ROCK_BUTTON_RECT.centerx - 30, ROCK_BUTTON_RECT.centery - 10))
    screen.blit(paper_text, (PAPER_BUTTON_RECT.centerx - 30, PAPER_BUTTON_RECT.centery - 10))
    screen.blit(scissors_text, (SCISSORS_BUTTON_RECT.centerx - 40, SCISSORS_BUTTON_RECT.centery - 10))

This function ensures that the buttons and their labels are displayed correctly on the screen.

Main Game Loop

The core of the “Rock, Paper, Scissors” game is the main game loop, where we handle user input and update the screen.

def main():
    running = True
    player_choice = None
    computer_choice = None

    while running:
        for event in pygame.event.get():
            # Handle events

In this section, we start the main loop, initialize variables for the player’s choice and the computer’s choice, and prepare to handle events.

Event Handling

The code listens for user input events, such as mouse clicks, to allow the player to choose their option.

if event.type == pygame.QUIT:
    running = False

if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    if ROCK_BUTTON_RECT.collidepoint(event.pos):
        player_choice = ROCK
        computer_choice = get_computer_choice()
    elif PAPER_BUTTON_RECT.collidepoint(event.pos):
        player_choice = PAPER
        computer_choice = get_computer_choice()
    elif SCISSORS_BUTTON_RECT.collidepoint(event.pos):
        player_choice = SCISSORS
        computer_choice = get_computer_choice()

The code responds to the following events:

  • When the player closes the window (pygame.QUIT), the game loop exits.
  • When the player clicks on one of the buttons, the player’s choice is recorded, and the computer’s choice is randomly generated.

Displaying the Interface

The code draws the user interface, which includes the buttons for Rock, Paper, and Scissors.

screen.fill(WHITE)

draw_buttons()

This part clears the screen and displays the buttons.

Displaying the Game Outcome

If the player has made a choice, the code displays the result of the game (Win, Lose, or Tie) and the choices made by the player and the computer.

if player_choice is not None:
    result = determine_winner(player_choice, computer_choice)
    player_choice_text = FONT.render("You chose " + OPTIONS[player_choice], True, (0, 128, 255))
    computer_choice_text = FONT.render("Computer chose " + OPTIONS[computer_choice], True, (255, 0, 0))
    result_text = FONT.render(result, True, (0, 128, 0))
    screen.blit(player_choice_text, (10, 10))
    screen.blit(computer_choice_text, (10, 50))
    screen.blit(result_text, (10, 90))

This section displays the player’s choice, the computer’s choice, and the game result on the screen.

Updating the Display

We update the display at the end of each loop iteration to show any changes made to the

interface or the game outcome.

pygame.display.flip()

Closing the Game

The main loop continues to run until the player closes the window, at which point we clean up and exit Pygame.

pygame.quit()

Conclusion and Future Ideas

The “Rock, Paper, Scissors” game in Pygame is a fantastic example of a simple yet fun interactive application. You can use this project as a foundation for building more complex games and applications. Here are some ideas on how to expand upon this project:

  1. Graphics and Animations: Enhance the visual aspects of the game with more elaborate graphics and animations.

  2. User Interface Improvements: Consider adding features like a score tracker, play again button, or menu screen.

  3. Multiplayer Mode: Implement a multiplayer mode, allowing two players to compete against each other.

  4. Additional Game Modes: Create variations of the game, such as “Rock, Paper, Scissors, Lizard, Spock.”

  5. Sound Effects: Add sound effects to make the game more engaging and immersive.

  6. Machine Learning: Experiment with machine learning models to predict and adapt to the player’s choices.

We hope this blog post has inspired you to explore Pygame and create your own interactive projects. Game development is not only educational but also a lot of fun. Happy coding, and have a great time playing “Rock, Paper, Scissors” with your new Pygame implementation!