Programming games

Programming with Scratch Jr - Lessons 5 & 6 

Objectives

  • Design and program a simple game with multiple sprites
  • Use messaging to control sprites in a game

Lesson Resources

Introduction

The content in this lesson is likely to span two lessons but it’s hard to divide it perfectly in half and say you should be exactly at a particular point after lesson 5, so it has been written as one combined process, simply break off wherever you need to at the end of your first lesson.

 

Who plays computer games? What sort of games do you play? You can discuss these things as a class and it will provide you with a great opportunity to discuss a bit of online safety, how appropriate the games they play are and why there are age ratings on games.

 

Age ratings

In Europe there are two main age ratings systems; Apple have their own which covers all games and apps in their iOS and Mac app stores. Their categories and descriptors are as follows:

 

 

Most other game platforms (Google Play, Windows Mobile, Playstation, Xbox, Nintendo) use the PEGI age ratings system. You don’t need to go through these in huge detail with the students, but pick out a few key phrases from different sections to highlight the differences.

 

 

 

The content of games given this rating is considered suitable for all age groups. Some violence in a comical context (typically Bugs Bunny or Tom & Jerry cartoon-like forms of violence) is acceptable. The child should not be able to associate the character on the screen with real life characters, they should be totally fantasy. The game should not contain any sounds or pictures that are likely to scare or frighten young children. No bad language should be heard.

 

 

Any game that would normally be rated at 3 but contains some possibly frightening scenes or sounds may be considered suitable in this category.

 

Video games that show violence of a slightly more graphic nature towards fantasy character and/or non-graphic violence towards human-looking characters or recognisable animals, as well as video games that show nudity of a slightly more graphic nature would fall in this age category. Any bad language in this category must be mild and fall short of sexual expletives.

 

This rating is applied once the depiction of violence (or sexual activity) reaches a stage that looks the same as would be expected in real life. More extreme bad language, the concept of the use of tobacco and drugs and the depiction of criminal activities can be content of games that are rated 16.

 

The adult classification is applied when the level of violence reaches a stage where it becomes a depiction of gross violence and/or includes elements of specific types of violence. Gross violence is the most difficult to define since it can be very subjective in many cases, but in general terms, it can be classed as the depictions of violence that would make the viewer feel a sense of revulsion.

 

Game ratings make it illegal for shopkeepers to sell a game to an underage person, but are mainly there to protect children from content that is not suitable for them. Emphasise to the children: Some of the things in these games could really scare, shock or upset you.  Do not play games if you are younger than the age rating, do not ask your family to buy you these games, leave the room if other people in your family are playing these games, tell someone if you are worried about a friend playing these games.

But, when we play the right games they are really fun! What makes a good game? It’s not always fancy graphics that make the best games, it’s often down to the challenge of the game, it shouldn’t be too hard or too easy as we might give up on the game or get bored quickly.

Over the next two lessons we’re going to have a go at making a simple game with ScratchJr.

Have a look at this side-scrolling space game. Can you work out how it was created?

 

 

Create the background

Start a new project and begin by deleting Scratch the cat so you have no sprites and no background. Go to the backgrounds button.

 

 

Instead of choosing one of the picture backgrounds, select the paintbrush icon and then the fill tool.

 

 

Fill the background black and tap the tick to confirm this.

 

Go to the sprite library and choose the rocket sprite (right at the bottom of the sprite library).

As the rocket appears rather large, our first job is to shrink it down when the game starts. Ask the class how they'd do this and then demonstrate using the when green flag pressed block to start the code, then the shrink block with a value of about 7. We also need the rocket to face to the right as it's going to be a side-scrolling game. The numbers in the turning blocks work like a clock (12 does a full turn, 6 a half turn etc), so what number should we use to make the rocket face to the right? 3 will do this. Ask the students to build and test the code.

 

Next, we have to be able to control our rocket to play the game. But Scratch Jr doesn’t come with any easy way to control a character in a game, so our next job is to create some directional control buttons for this purpose.

Go to choose a new sprite, but press the paintbrush at the top of the screen to create your own. Use the triangle tool to draw a triangle that fits inside 2 x 2 grid squares and faces up. Then use the fill tool to colour it yellow.

 

   

 

When it’s done, press the tick and move it to the top right of the project screen. This will be our move up button.

 

Now we need to create a down arrow button. We can save ourselves some work and ensure our buttons are the same by duplicating this button. Go to make a new sprite and you’ll see thumbnails of the sprites you’ve already made at the top of the screen. Tap the arrow we just made and then the paintbrush icon and it will copy your first sprite and take you to the editor

 

 

Use the fill tool to change the colour to green, then tap the rotate button (turning arrows) and then use your finger to drag the triangle round so it faces the opposite way (down) in this example, don’t worry about where it is on the grid screen, as long as it’s straight.

 

 

Once you press the tick, move it into position in the bottom left of the screen. You should now have up and down-facing arrow buttons.

 

Adding controls

Now to the programming. We’re going to start with the directional arrows we made. How could I turn these into buttons that we can press to control our rocket sprite? How could it know when we press them? If they need a clue, open the palette of yellow blocks.

The start-on-tap block lets us activate code by tapping a sprite in the project window.

 

Start with your yellow up arrow sprite and drag a start on tap block in. But when we tap the arrows, it isn’t the arrow that we want to do something, it’s the rocket. So we’ll send a yellow message to the rocket when we tap it, and then end the code.

 

 

When we tap the up arrow what do we want the rocket to do? Move up the screen! So on the rocket sprite we add a receive yellow message block, a move up 1 arrow, and an end block. Test this out.

 

 

Tap the down arrow, why isn’t this one working? Because we haven’t programmed it yet! What do you think we should do next? We need to program the down arrow to make the rocket move down. Ask the students to work on their tables to try and make this work. 

First, the green down arrow should send a green message when tapped.

 

Then, the message goes to the rocket sprite but this time tells it to move down the screen by 1 step.

 

 

The rocket should now have three separate bits of code: one to set it's size and direction, one to make it move up, one to make it move down.

 

The stars come out

Next, it's time to add some cosmic items to avoid in our game. Go to the sprite library and choose one of the stars or planets.

 

 

We will make these items move across the screen which will actually give the appearance of the rocket flying through space, while always keeping it on our screen. We've set this one to fast, made it move 25 steps to the left, then set it to repeat that code forever. As it leaves the screen on the left it will reappear on the right, giving the impression another star is flying past our rocket as we speed through space.

 

 

Try it out.

Repeat this process with another star or planet, but to make the game a little harder, we've added a shorter left movement but also included a down movement.

 

 

Add more stars or planets as required, all with similar code that makes them move to the left, perhaps at different speeds, and with up or down movement as well. You can also resize things using the shrink block (good for some of the larger items like the planets).

 

 

Collision detection

But our rocket will fly right past all of these items unless we add some code for collision detection.

Ask the class - How could we program the rocket to know when it crashes into an object? They may remember the bump trigger block from previous lessons, which is what we’re going to use.

 

What should happen when our rocket crashes? We're going to make a game over screen which will be displayed after a crash.

Add a new scene to the project with the + sign on the right of the screen.

 

 

Delete the cat sprite from the new scene and go to the background button.

 

 

Choose the empty black background that we created earlier from the top of the screen.

Next, select the text tool and add a 'Game over' message. Alter the size and colour of the text as desired with the options buttons.

 

   

 

Then position it as wanted with your finger and add a few of the space-themed sprites to decorate the screen around the text.

 

The final code

To get everything working, return to scene 1 and the rocket sprite. Now we have a second scene, the option to switch to that scene appears in the red section. Add this to the 'when bump a sprite' block

 

 

No any collision between the rocket and the other sprites on the screen will take us to the game over screen on scene 2.

Finally, go back to scene 2 and choose any of the sprites you've added. Ask the class, what will this final code do?

 

 

It will wait 3 seconds after the scene starts and then take us back to scene 1, where the game will restart.

Here's that video of our example game again.

 

 

 

Plenary

Ask the students to join up with a partner and play each others games. After they have had time to play the game ask; does everything in the game work? If not how could they fix it? Is it too easy or too hard? How could they change it to get the difficulty level right?

They can then go back and make any adjustments they want to to position, speed and movement of the objects in the game.

Can you think of anything else we could add to this game? What other sorts of games could we make?

Still want more?

Don’t forget to check out these great resources from ScratchJr.org

< Previous Lesson