Challenge Description

Download the starter file here.

Let's Build Rock Paper Scissors!

In this challenge, you'll combine all your prior learning in the previous lesson to build a Rock Paper Scissors game. We'll break it down into small, fun exercises that are easy to understand. Don't worry if you get stuck - that's part of learning!

Exercise 1: Creating Game Variables

  • Your first task is to initialize the variables for this game

# 1. TODO Create a list called 'choices' that contains "Rock", "Paper", and "Scissors"
        # Ex. name_list = ["John", "Jill", "Jack"]
choices =

# 2. TODO Create 'player_choice' variable set to None
player_choice =

# 3. TODO Create 'computer_choice' variable set to None
computer_choice =

# 4. TODO Create 'wins' variable that starts at 0
wins =

# 5. TODO Create 'draws' variable that starts at 0
draws =

# 6. TODO Create 'losses' variable that starts at 0
losses =

# 7. TODO Create'game_running' that equals True
game_running =

# 8. TODO Create 'result_message' that starts as an empty text message
result_message =

# (Optional) Print your variables to see if they work!
print("Game choices:", choices)
print("Player score:", player_score)
print("Computer score:", computer_score)
print("Is game running?", game_running)
print("Result message:", result_message)

Exercise 2: Creating Buttons

  • Next, you need to create the buttons that the user will interact with to select Rock, Paper, or Scissors. We recommend you use the pygame.Rect() built-in function which takes 4 parameters: x-coordinate, y-coordinate, width, and height.

# Ex. click_me_button = pygame.Rect(50, 300, 300, 200)

# 1. TODO Create 'rock_button'
rock_button =

# 2. TODO Create 'paper_button'
paper_button =

# 3. TODO Create 'scissors_button'
scissors_button =

# 4. TODO Create 'reset_button'
reset_button =

Exercise 3: Making Game Decisions

  • Complete and test the determine_winner() function which decides who wins a round or if the player and computer tie. The function has 2 parameters, player_choice and computer_choice which we will compare using a conditional statement.

def determine_winner(player_choice, computer_choice):
    # We need to use 'global' to change variables from outside the function
    global wins, draws, losses
    
    # 1. TODO First, check if it's a tie (both players chose the same thing):
        # increment 'draws' by 1
        return "Draw!"

    # 2. TODO Next, check if the player wins!
        # increment 'wins' by 1
        return "You Win!"

    # 3. TODO If it's not a tie and the player didn't win, then the computer must have won!
        # increment 'losses' by 1
        return "You Lose!"

# 4. Let's test your function! Uncomment the following code when your done testing:
print(determine_winner("Rock", "Scissors")) # This should print: You Win!
print(determine_winner("Paper", "Paper"))   # This should print: Draw!
print(determine_winner("Scissors", "Rock")) # This should print: You Lose!

Inside Main Loop (while game_running:)

Inside the while game_running loop:

  • Draw the buttons,

  • Render the text

  • Handle events (button clicks)

Exercise 4: Drawing Buttons

  • To draw the buttons, we recommend using the pygame.draw.rect() pygame built-in function which has 3 parameters: pygame display, RGB color, and rectangle properties. Pygame display and RGB color have already been defined in the starter file, and we defined our rectangles in Exercise 2.

# Ex. pygame.draw.rect(screen, BLACK, click_me_button)

# 1. TODO Draw 'rock_button'
pygame.draw.rect()

# 2. TODO Draw 'paper_button'
pygame.draw.rect()

# 3. TODO Draw 'scissors_button'
pygame.draw.rect()

# 4. TODO Draw 'reset_button'
pygame.draw.rect()

Exercise 5: Render Text

  • In this exercise, you'll focus on rendering text on the screen. You'll need to display important information like the player's choice, the computer's choice, game results, and the button text.

# Ex. screen.blit(font.render("Click Me", True, WHITE), (50, 300))

# 1. TODO: Render the "rock" text
screen.blit(font.render()

# 2. TODO: Render the "paper" text
screen.blit(font.render()

# 3. TODO: Render the "scissors" text
screen.blit(font.render()

# 4. TODO: Render the "reset" text
screen.blit(font.render()

# 5. TODO: Render Player's Choice
screen.blit(font.render()

# 6. TODO: Render Computer's Choice
screen.blit(font.render()

# 7. TODO: Render Game Results (Wins, Draws, Losses)
screen.blit(font.render()

# 8. TODO: Render Result Message
screen.blit(font.render()

# Hint - You can print variables within a string as follows: "Your number is: {magic_number}"

Exercise 6: Button Selection

  • In this exercise, you will handle the player's selection of the game choices (Rock, Paper, Scissors) using buttons, and reset the game with the "Reset" button. You'll also manage the computer's choice and game results.

# Ex. if click_me_button.collidepoint(event.pos):

# 1. TODO: Handle player selection for "rock_button"
        
# 2. TODO: Handle player selection for "paper_button"
            
# 3. TODO: Handle player selection for "scissors_button"

# 4. TODO: Handle "reset_button" click (reset player_choice and scores)

# 5. TODO: If player_choice is not None, use random.choice to set computer_choice

    # 6. TODO: Set computer_choice to "Rock", "Paper", or "Scissors" (Hint - use random.choice(choices))

    # 7. TODO: Call the determine_winner function to update result_message

# 8. TODO: If player_choice is None:
    computer_choice = None
    result = None

Final: Test, Debug, and Play Your Game!

  • To test and debug your game, it’s helpful to use print() statements throughout your code. You can print out the values of variables (such as player_choice, computer_choice, wins, draws, etc.) to the terminal to ensure everything is functioning correctly

  • You can view these outputs in the terminal inside VSCode. This helps you trace any logic issues and ensures your program behaves as expected.

  • Once you are finished, play and enjoy your game!

Want More Challenges?

Once you've finished the main exercises, try these fun optional additions:

  1. Calculate how often the player win rates (as a percentage)

  2. Let players choose if they want to play best out of 3

  3. Import images for the Rock, Paper, and Scissors buttons

Remember: It's okay to make mistakes - that's how we learn! Try things out, see what happens, and have fun building your game!