Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Smart Robotics with LEGO MINDSTORMS Robot Inventor

You're reading from   Smart Robotics with LEGO MINDSTORMS Robot Inventor Learn to play with the LEGO MINDSTORMS Robot Inventor kit and build creative robots

Arrow left icon
Product type Paperback
Published in May 2021
Publisher Packt
ISBN-13 9781800568402
Length 438 pages
Edition 1st Edition
Concepts
Arrow right icon
Author (1):
Arrow left icon
Aaron Maurer Aaron Maurer
Author Profile Icon Aaron Maurer
Aaron Maurer
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Chapter 1: History of Mindstorms 2. Chapter 2: Getting Started with the Robot Inventor Kit FREE CHAPTER 3. Chapter 3: Building an Industrial Robot Claw 4. Chapter 4: Building a LEGO Guitar 5. Chapter 5: Building a Scorpion 6. Chapter 6: Building a Solid Sumobot 7. Chapter 7: Building a Dragster 8. Chapter 8: Building an Egg and Ornament Decorator 9. Chapter 9: Creating Plankton from SpongeBob SquarePants – Part 1 10. Chapter 10: Creating Plankton from SpongeBob SquarePants – Part 2 11. Other Books You May Enjoy

Coding a rock, paper, scissors wrist game

You are now going to code this wrist game to be able to randomly choose rock, paper, or scissors. You can play it against yourself or use it against another human. If you have a friend that has a kit, then you can both build one and compete this way:

  1. Using the default yellow when program starts block, add a purple Light block named write Hello. You will need to change the word from Hello to Left:
    Figure 2.37 – "Left" being programmed to screen

    Figure 2.37 – "Left" being programmed to screen

  2. Add two yellow Events blocks named when Left Button pressed. Make one that activates when the setting is set for the left button and the second block set for the right button.
  3. Go to the pink My Block sections of the coding blocks and make two new blocks. Name one start game and the other playagain. Add these new blocks to the yellow blocks. Add the start game block for the left button and playagain for the right button:
    Figure 2.38 – Decision making with MyBlocks

    Figure 2.38 – Decision making with MyBlocks

  4. In the previous step when you created these blocks, you should have seen two new pink blocks show up in your coding canvas named define playagain and define Start Game. Locate define playagain:
    Figure 2.39 – playagain My Block

    Figure 2.39 – playagain My Block

  5. Under the define playagain block, add a sound effect by adding a purple play sound block named play sound Cat Meow until done. Change the audio file to one of your choice. Under this block, add a Light block named turn on Smiley Face for 2 seconds. Click the face and change the graphic to your choice. In this example, an X was made. Add another light block underneath this one, turning off all the lights. Finally, add a final purple Light block named write Hello and change Hello to Left.
  6. You will follow a similar process for the other define Start Game block. Locate this pink block. Add a purple play sound block named play sound Cat Meow until done. Change the audio file to your choice. Add a purple Light block named turn on Smiley Face for 2 seconds. Change this block to the number 3 in the graphical interface and change it from 2 seconds to 1 second. Right-click the purple play sound block you just added and choose duplicate. Duplicate these two blocks two times so you don't have to keep dragging blocks. Change the 3 to a 2 and the other to a 1 to create a countdown timer. Last, create another pink My Block and name it Rps. Add this block to the end of this code:
Figure 2.40 – start game My Block

Figure 2.40 – start game My Block

The final step is to program the Rps block:

  1. Add a purple Light block named turn on and change the smiley face to the four squares in the corners.
  2. Add an orange Control block named wait until. Add a blue Sensor block of Hub is shaken to the diamond space.
  3. Go to the orange Variable block and make a variable named Rps_Random. Add the set Rps_Random block to the code. Use a green Operator block named Pick Random and select numbers 1-3.
  4. Add an orange Control block named If. Drag in a green Operator block that compares with the = sign. On one side of the equals sign, add your Rps_Random block, and on the other side, insert the number 1.
  5. Within that If block, add a purple Light block named turn on and turn on all the lights to symbolize paper.
  6. Add one more purple Light block named set Center Button light to and choose a color.
  7. Right-click this If block you just created and duplicate it two more times. For these two copies, change the numbers to 2 and 1. Change the design from paper (all lights) to a smaller square for rock for one, and for the other, design a pair of scissors.
  8. Add an orange Control block named Wait and choose 5 seconds so that you have time to see your game choice.
  9. Add a purple Light block named write Hello and change it to Right:
Figure 2.41 – Rps My Block

Figure 2.41 – Rps My Block

In the end, your code should look like this:

Figure 2.42 – Complete view of the code

Figure 2.42 – Complete view of the code

How it works is the game will tell you Left on the screen to indicate to press the left button. This will trigger the start game My Block, which will do a countdown timer to show your choice. Once it counts down, it will then move to Rps, where the code will wait for you to shake the Intelligent Hub before displaying a choice of rock, paper, or scissors. After it displays the choice for 5 seconds, it will then tell you Right, so when you press the right button, it will reset the screen and wait for you to press left again to play the game.

You have been reading a chapter from
Smart Robotics with LEGO MINDSTORMS Robot Inventor
Published in: May 2021
Publisher: Packt
ISBN-13: 9781800568402
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image