Actually Using Your Raspberry Pi Part 2A: Programming With Scratch

blog-raspberry-2a

04 May

In my last blog post, I spent some time in a trough of sadness. Over 2 or 3 hours of not making any progress and banging my head against the keyboard (metaphorically), the IP camera project ending in a dud. I took another two week break before turning my Pi back on. Here I was: 1 month in and no real progress to report.

Next I decided to try my luck at learning some Scratch. Chapter 4 of my user’s guide covered the language, so I opened up the Scratch program on my Raspberry Pi and started to follow along with the user’s guide. Take a look at this fun mascot/icon for scratch. His or Her name is apparently scratch cat.

scratch cat

Scratch is a free program developed by the Lifelong Kindergarten Group at the MIT Media lab. Scratch allows you to create a little interactive game or animation. Instead of writing code, scratch has you drag and drop little commands that represent a code principle or item, etc. This is great if you have young kids and want to introduce them to concepts. I read through and tried out the different types of commands in the user manual and started to get a grasp on things. The program has a simple look and feel, which you can see below. If you do not have a Raspberry Pi, you can still download scratch and play along on your computer.

image 2

There are 6 main parts that I have arrows pointing out above.

  1. Cursor Tool Menu: lets a user change what your cursor (mouse) does. It can make it copy, cut, delete, grow, or shrink things in your project.
  2. Application Menu: This is where you do your typical saving and menu type stuff.
  3. Script Area: Is where you assemble your code “blocks” that run the project. Most of your learning about coding principles is done here.
  4. Blocks Palette: This section lets you select different blocks that do different things.
  5. Sprites List: This area lists all of the sprites in your project. Sprite is the term for any object in your object that is not the background.
  6. Stage: This is where your sprites move and interact with each other. Essentially this is where the game or animation you are making lives.

There are many things that you can do with Scratch, and I will not start to cover them all here. I spent a few hours working through the basics and I found that a few blocks allowed me to do most of what I wanted.

Whenever you start a new project in Scratch the workspace in scratch is blank except for a sprite of Scratch Cat. You can see this in the picture above. Things start with the Scratch Cat selected, so any blocks you start to use will be set to effect that Scratch Cat sprite. Let’s go through some block types.

image 3

To make anything start in Scratch you will need an “Events” block. The main ones I used were:

  • “When Flag Clicked”
  • “When (space or arrow) key pressed.

When Flag Clicked will start all blocks after it(the block) when the green arrow is clicked.

The other block can be changed to the space bar or the up, down, left, and right arrows. Putting this block before others will cause the following blocks to start only after the selected key is pressed.

image 4

The next block type I got used to using where the motion blocks. These blocks need to follow an event block or they will never start.

The motion blocks do what you would expect them to do. They move your sprite. You can

  • Move forward (or backward with a negative value)
  • Turn left
  • Turn right
  • Point in a direction
  • Point towards something (like another sprite)
  • Go to a coordinate on the stage.
  • And more!

image 5

The next type of block that is the most common is the “Control” block. These blocks control the order in which other blocks are executed and are also used to stop your programming or make it wait.

  • Wait “1” secs: will make an operation wait for the selected number of seconds.
  • Repeat “10” times: will make anything in between the brackets repeat the selected amount of times.
  • Forever: will make anything inside the brackets repeat forever.
  • The “If then” and “if then else” blocks will look familiar to anyone who has some programing experience or uses excel formulas. These blocks start the things in between their brackets only if the condition is met, or in the case of “if then else” the block will start the blocks after the “else” if the first condition is not true.

After learning how to string together “Event”, “Motion”, and “Control” blocks and move my spirits across the workspace, I decided that it was time to try and make my own little game. My game would have a background, a sprite for the player, a sprite for the players weapon, and sprite for the “bad guy” which would come across my screen at the player. After experimenting with a lot of different sprite designs I ended up with a game that looks like this.

image 6

I call this game Knight Lighting! (exclamation point mandatory) Set on a background of a brick wall with a nicely mowed lawn.  The player is a knight with a sweet purple scarf coming out of its helmet. The knight has a sword in his hand, but his weapon is actually a lightning bolt that appears whenever the user presses the space button. The knights nemesis is a large dinosaur who moves from right to left across the screen.  If the knight hits the dinosaur with the lightning bolt then his score goes up by 1. But there is a delay after the lightning bolt is use, so if the knight misses he will get hit by the dinosaur and die. I also added the ability for the knight to jump with the up arrow. No big practical use for that.

If you want to take a look at how I built this game then go here for the details. I had a lot of fun making this game. A great part was the combination of learning something new, getting some programing “concept” experience, and actually making something! The scratch game took me about a week to get together. Working an hour every other day. There were a lot of false starts and rabbit holes. I looked into making the NES Double Dragon emulator, but decide that I wanted to move onto using a more “real” programing language. The next chapter in my users guide is on Python. Let’s take a look at that next.

Helpful Links:

Actually Using Your Raspberry Pi: Part 1 Getting a Pi

Actually Using Your Raspberry Pi: Part 2B Knight Lightning Game

James Demby
James Demby
jdemby@bandwidth.com

James studied Industrial Engineering at Georgia Tech, and loves going to GT Football Games. In his spare time he volunteers at Big Brother Big Sister in Raleigh, and spends time with his Fiancé exploring downtown. He also likes listening to podcasts, reading books (Michael Lewis rocks!), learning to code, and Raspberry Pi projects.

2 Comments

Post A Comment