Tag Archives: Scratch

It’s Not All Fun and Games!

By Robert Walsh

What can creating a video game teach students about math? Quite a lot, actually. Today’s video games have become so realistic that, at a glance, one may think they are live action television. Development environments like Unity and GameMaker Studio handle much of the heavy lifting when it comes to the mathematical calculations necessary to move the in-game objects, to do the intense graphical rendering (also based on math), and to apply physical laws like gravity and inertia.

However, with a simpler environment like Scratch, students must do this work for themselves.  Even relatively simple games like Bricks Buster reinforce concepts identified in the Common Core State Standards Initiative like inequalities, fractions and decimals, geometric transformations, and the x-y coordinate plane.

Bricks Buster stage with coordinate plane overlay.

Bricks Buster also uses recursion to draw the rows of bricks, thus allowing it to connect with a lesson on the Fibonacci sequence, for example.

Other projects in our Learn Programming by Creating Video Games unit require students to use even more complex concepts like working with vectors and applying trigonometric relationships in order to update a sprite’s position in each iteration of the game loop.

So, while it may look like students are simply playing games, they are actually learning and applying important mathematical concepts. In addition, they get to see these concepts without the artificial separation often imposed by traditional curricula.

If this looks like a fun way to learn math, we can help! We have programs for schools and for individuals spanning a wide range of subject areas including game development. Send us a note to let us know you are interested!

Scratch as a Programming Environment

By Robert Walsh

I have been programming for a long time. I started writing commercial applications in the middle 1990s, but my first exposure to computers was in middle school. Whether it be on the TRS-80 Color Computer or a TI99-4/a, I could

10 PRINT "HELLO"
20 GOTO 10

RUN

with the best on them. In high school, I learned a bit of Pascal using the Apple IIe, graduating to Turbo Pascal 5 on an IBM-compatible 80286 in college. From there, a whole host of other languages, including C/C++, C#, Java, JavaScript, and so on. One thing they all had in common was that they were text-based. Even though Visual Basic and rest of the languages available in Microsoft’s Visual Studio supported a visual editor for the GUI, the code to do things in response to user interaction was still written in text.

Programming with Puzzle Pieces

So, when I first learned about Scratch, I was not particularly impressed. “What can a programmer do just by dragging puzzle pieces around,” I thought. I tried to interest my daughter in Scratch, but we didn’t get much farther than moving the cat around the screen and switching its costumes.

Now that I have looked more closely at Scratch and how it can be used to introduce programming to those that have never done it before, I have gained newfound respect for Scratch as a programming environment. Yes, it is colorful and cartoony, but that does not mean it should be limited only to kids under a certain age.

Scratch as a Teaching Tool

To me, Scratch does two things that make it a great tool for teaching programming.

First, Scratch frees the programmer from having to remember what are often obscure syntax rules and language constructs. For example, even though I can write essentially the same program in Java, C#, or Python, the punctuation I use, the standard conventions for naming things, and even the physical structure of the code will be different. As a result, the programmer can quickly become overwhelmed trying to learn both concepts and semantics. By giving programmers an organized toolbox of available commands and operations, Scratch lets them focus on what they want to accomplish rather than the language-specific details for how to do it.

Second, Scratch allows the programmer to see the structure of the program being created in ways that are not necessarily obvious in text-based environments. Conditionals and loops are two examples. In C-like languages, the curly braces do denote blocks of code, as do indents in Python; however, these can become challenging to match when there is more than one level of nesting (and infinitely more challenging without proper and consistent indenting, something Python at least enforces).

With Scratch, the color coded shapes clearly show the pieces of code that belong inside because they are literally surrounded. Some might consider the fact that the if/else construct in Scratch does not support multiple else clauses to be a limitation. In my view, though, this emphasizes the fact that an if/else construct is really just an either/or. The “if” condition is either true or it isn’t. In the case that it isn’t, any other tasks that need to be done are part of the “else” clause of the initial check. The if/else if/else construct is really just shorthand for what should be considered a nested set of binary choices.

Give Scratch a Try!

If you are wanting to learn to program but you aren’t sure where to start, give Scratch a try. There are many self-guided tutorials that will walk you through creating all kinds of programs. Or, contact us! Even if you are an experienced programmer, take a look at Scratch and see if it changes anything about the way you think about your programs.

Lesson: Exploring the Gyroscope using the Raspberry Pi, the Sense HAT module, and Scratch

By: Robert Walsh

Engage

Have you ever played a video game where all you had to do was tilt the controller to direct the on-screen action? The Nintendo Wii was one of the first gaming systems to employ this technology when it was released in 20061, and smartphone and tablet games like Temple Run and Subway Surfers use it too. Did you stop playing the game long enough to wonder how this works?

In this lesson, we will use a Raspberry Pi with the Sense HAT module to create a simple scene where you just need to tilt the Raspberry Pi to control a rocket ship. To complete this activity, you will need:

Explore

The video below will walk you through creating a Scratch program that allows you to control a rocket ship in space using a Raspberry Pi with a Sense HAT module. This is the same technology used in smartphones and video game controllers.

If you need help getting your program working, try these downloads:

Note: The complete project download is a ZIP file and will have to be expanded to get to the Scratch program. Scratch programs have an .SB3 file extension.

Explain

The Sense HAT contains an inertial monitoring unit (IMU) that is able to detect motion2. One component of the IMU is a gyroscope that detects rotation. When the Sense HAT is tilted forward or backward, a value called roll changes. When it is tilted left or right, the pitch changes. The Sense HAT can also detect a twisting rotation (like if you left the Raspberry Pi flat on a surface and rotated it to the left or right without tilting it). This is called yaw. Our program did not use the yaw value.

Airplanes, spacecraft, and other flying objects use pitch, roll, and yaw to describe their motion3. Pitch generally indicates whether the object is climbing or diving, while roll typically the tilt to the left or the right. (The Sense HAT extension for Scratch, though, uses forward and backward for roll and left or right for pitch.). Yaw shows where the nose is pointed.

This project also taught us about the XY coordinate plane. We used XY coordinates to position our sprite and to indicate which of the Sense HAT’s LEDs we wanted lit. For the sprite, the origin (the center or the point whose X and Y coordinates are both zero) was located in the center of the stage. X values greater than zero were to the right of center, while those with values less than zero were to the left. Y values greater than zero were above the center, and those less than zero were below. To move the rocket to the right, we increased the X coordinate, and to move to the left, we decreased the X. Likewise, to go up, we increased the Y, and to go down, we decreased it.

The Sense HAT LED matrix uses XY coordinates, too, but the origin is not in the center. It is in the upper left corner. As we move to the right, the X coordinate gets larger. However, as we move down, the Y coordinate gets larger. This is the opposite of the coordinate system for the sprite where larger Y values were higher on the screen. Another important point about the coordinates for the LEDs on the Sense HAT is that the numbering starts at zero, not one. For example, an LED in the first column has an X coordinate of 0 and one in the last column has an X coordinate of 7 even though there are 8 columns. The same is true for the Y coordinates, but instead of columns, the Y indicates the row.

Extend

In addition to game controllers and flying crafts, gyroscopes and IMUs are used in other devices, too. Segways, innovative personal transportation devices sometimes used by police officers and security guards in places where other vehicles are impractical, also use internal gyroscopes to help keep the Segway upright and to detect which way the rider is leaning4.

There are many ways that we could extend the rocket ship program. For example, we could make the ship move faster in a given direction based on how long the Sense HAT is tilted. This would make the movement more realistic. We could also use the yaw value to turn the ship rather than the left or right tilt. This might be a more appropriate control system game with a car or even a person. A game viewed in the first-person where the player sees what the character sees could use both tilt and yaw. One could turn the character, while the other could just turn the head to look in a different direction.

Another change could involve adding obstacles that the player must avoid. Or the player could be expected to collect the other objects on the screen rather than to avoid them.

What other adaptations could you think to make?

Evaluate

What we learned in this lesson:

  • How to interact with the Sense HAT module using Scratch 3 on a Raspberry Pi
  • How the gyroscope component of the IMU detects motion and reports pitch and roll
  • How to locate an object on an XY coordinate plane

How did you do with the lesson?

  • What parts were easy and what parts were confusing?
  • Were any parts a review of things you already knew?
  • What would you like to know more about?

References

1Romero, J. (2006, December 18). How do motion-sensing video game controllers work? ScienceLine. https://scienceline.org/2006/12/motioncontrollers/

2Raspberry Pi Foundation. (n.d.). Getting started with the Sense HAT. https://projects.raspberrypi.org/en/projects/getting-started-with-the-sense-hat/8

3SoftwarePole. (2015, March 25). Airplane control – roll, pitch, yaw [YouTube video]. https://www.youtube.com/watch?v=pQ24NtnaLl8

4Harris, T. (n.d.). How Segways work. How Stuff Works. https://science.howstuffworks.com/transport/engines-equipment/ginger.htm