How to Code Rock Paper Scissors Lizard Spock on Raspberry Pi

Source: Tom's Hardware added 10th Jan 2021

  • how-to-code-rock-paper-scissors-lizard-spock-on-raspberry-pi

Icons made by Smashicons from www.flaticon.com
(Image credit: Tom’s Hardware)

Before computers, smartphones, and the internet, kids entertained themselves for hours playing Rock Paper Scissors. Adults have made big decisions such as, “Who goes first?” or “Who gets the last piece of cake?” based on a game of Rock Paper Scissors. The magic of Rock Paper Scissors is both the simplicity (you don’t need any gadgets to play, just your hands), and the combination of luck and strategy against your opponent.  

What is Rock Paper Scissors Lizard Spock? 

If you’re already familiar with the classic game of Rock Paper Scissors, now we add two more choices, Lizard and Spock, in our game of chance and strategy. The game of Rock Paper Scissors Lizard Spock (according to this wiki page), was invented by Sam Kass with Karen Bryla and was made popular on a TV sitcom, “Big Bang Theory.” 

All of the original rules for Rock Paper Scissors apply:

  • Scissors cuts paper
  • Paper covers rock
  • Rock crushes scissors

With Lizard and Spock, we had these variations:

  • Lizard poisons Spock
  • Lizard eats paper
  • Scissors cuts lizard
  • Rock crushes lizard
  • Spock smashes scissors
  • Spock vaporizes Rock
  • Paper disproves Spock
  • Lizard poisons Spock

This tutorial is a “sneaky” way to learn guizero, a Python 3 library that can easily create graphical user interfaces (GUI) on your Raspberry Pi. Combining guizero with simple Python code allows us to create this fun game. A more common way to create games on Raspberry Pi utilizes Pygame or Pygame Zero which we may cover in a future tutorial. Let us know in your comments if you would like to see a tutorial with Pygame or Pygame Zero. 

 What You’ll Need 

  • Any Raspberry Pi including Raspberry Pi 3B+, or the new Raspberry Pi 400
  • 8 GB (or larger) microSD card (see best Raspberry Pi microSD cards) with a fresh install of Raspberry Pi OS
  • Power supply/Keyboard/Mouse/Monitor/HDMI Cable (for your Raspberry Pi)

Setup guizero 

1. Boot your Raspberry Pi. If you don’t already have a microSD card see our article on how to set up a Raspberry Pi for the first time or how to do a headless Raspberry Pi install.

2. Install the guizero library via your Pi Terminal. Press Ctrl-T to open a terminal, then enter the command to install guizero. 

pip3 install guizero

(Image credit: Tom’s Hardware)

3. Clone my repository for the source code:

git clone https://github.com/carolinedunn/RockPaperScissorsLizardSpock

4.  Open your file explorer and navigate to your new folder: /home/pi/rockpaperscissorslizardspock 

(Image credit: Tom’s Hardware)

5. Double-click 0-helloworld.py to open it in Thonny. (Feel free to use your preferred Python editing software for this project.)

6. Click “Run” in your Python editor. You should be presented with a gray box with “Hello World” in the title bar and, “Welcome to the app” in the box.

7. Click the “x” in the upper right corner of your app window to close it. As you move through this tutorial, you’ll realize that the next set of code won’t run until you close the previous app. 

(Image credit: Tom’s Hardware)

Code Notes on 0-helloworld.py:

As stated earlier, we are employing the guizero library which makes it easy to create graphical interfaces. There are 2 main widgets within the guizero library that we are utilizing for this first step:

  • App – The App widget is the first and most foundational widget we will call to start our GUI. In order to display our GUI, we will call app.display() near the end of our code.
  • Text – To display text, we will call the Text widget. In this case, the text reads, “Welcome to the app.”

Not very exciting; is it? Let’s move on to our Rock Paper Scissors game.

Rock Paper Scissors

1. Within your file explorer /home/pi/rockpaperscissorslizardspock folder, double click on 1-RPS-text.py to open it in a code editor.

2. Click the “Run” button in your Python emulator. You should be presented with 3 buttons, labeled: Rock, Paper, Scissors respectively. Feel free to play the game and click Rock, Paper, and/or Scissors.

(Image credit: Tom’s Hardware)

Code Notes on 1-RPS-text.py:

We add two more widgets from the guizero library:

  •  Box – We specify the grid layout to create a place for our three buttons. 
  •  PushButtonPushButton creates buttons that can call a function when pressed by the user. We specify the position within the grid by specifying x,y coordinates starting at [0,0] – 1st row, 1st column. We specify the function that will be called when the button is pressed with the parameter, “command =” 

When the user presses a button, we call the get_winner function to determine if the computer won or the user won. Within the get_winner function: 

  • The code will randomly select from the 3 choices, rock, paper, or scissors as the computer’s pick.
  • If/then/else statements determine if the user won or the computer won.
  • The winner is selected and message.value is sent back to the main program to let the user know who won the game.

(Image credit: Tom’s Hardware)

 1-RPS-text.py is a good start, but there are a few things lacking. Next, we’ll add images and a scoreboard, which also creates the need to reset the scoreboard. 

3. Go back to your file explorer and open 2-RPS-images.py.

4. Run 2-RPS-images.py. You should be presented with 3 images (rock, paper, scissors) and a scoreboard. Click on the buttons to play the game. 

Icons made by Smashicons from www.flaticon.com (Image credit: Tom’s Hardware)

Code Notes on 2-RPS-images.py:

  • Within each PushButton, we replace the text with images. By default, guizero can display .gif and .png files. If both image and text parameters are specified within a PushButton, only the image is displayed.
  • A new PushButton is added to enable the user to reset the scoreboard at any time. When the “Reset Scoreboard” button is pressed, all scores are reset to zero.
  • With each turn, the score is updated, except when both players select the same apparatus.

Icons made by Smashicons from www.flaticon.com (Image credit: Tom’s Hardware)

Now it’s time to add Lizard and Spock!

Rock Paper Scissors Lizard Spock

1. Go back to your file explorer and open 3-Lizard-Spock.py.

2. Run 3-Lizard-Spock.py. You should be presented with 5 images (rock, paper, scissors, lizard, Spock) and a scoreboard. Click on the buttons to play the game.

Icons made by Smashicons from www.flaticon.com (Image credit: Tom’s Hardware)

Code Notes on 3-Lizard-Spock.py: 

  • This code is simply an expansion of 2-RPS-images.py with the addition of lizard and Spock push buttons to the grid.
  • Additional if/then/else logic is added to determine the winner with 5 possibilities instead of three.
  • Scoring and scoreboard functionality remain the same.

We hope you enjoyed playing Rock Paper Scissors Lizard Spock against your computer. How did you do?

Read the full article at Tom's Hardware

brands: Best  CODE  fresh  Keyboard  Logic  New  Python  Raspberry Pi  ROCK  the game  Tom  Value  
media: Tom's Hardware  
keywords: App  Games  Internet  Software  TV  

Related posts


Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88

Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 88

Related Products



Notice: Undefined variable: all_related in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91

Warning: Invalid argument supplied for foreach() in /var/www/vhosts/rondea.com/httpdocs/wp-content/themes/rondea-2-0/single-article.php on line 91