[Repost] Target Practice

[This was originally posted last year, on an old blog to promote computational thinking and programming in schools. I’ll be reposting these over the next month as I think they’re still useful to schools embarking on the new Computer Science course]

Far-Side-buck-birthmark

This week I’ve started a bit of games development using Scratch. It’s really what it was made for, so once you get used to the basics, simple games can take just a few hours. Even less if you have all the graphics prepared beforehand.

This example is really a starting point rather than a finished game. It’s a simple Duck Hunt style game whereby your cursor is a crosshair, you shoot moving objects.

In terms of programming, you need very little ‘code’. This example introduces the use of ‘random’, sprite costume switches, and sound.

I suggest you play the game first, then read on.

 
jar

So here’s how I built it:

Step 1: Prepare your graphics. As mentioned in a previous post, being able to prepare transparent gifs is essential if you’re going to bring your own ideas into scratch. In this example, I found a crosshair, and a few images of star wars characters, chopped out the backgrounds, shrunk them down and imported them into scratch as gifs. I then ‘duplicated’ the gifs within Scratch, and edited the character duplicates just to add red crosses on their eyes for the ‘dead’ sprite costume. Image editing within Scratch is incredibly limiting and frustrating, hence why I used Photoshop first.

Step 2: Attaching the crosshair to the cursor. Just like in the Homer Simpson example, with almost identical code, I made the crosshair follow the cursor. The only difference here was that I added a sound for when the mouse button is pressed (a Star Wars blaster). All the sounds were just googled. One or two needed a little editing in Audacity before importing, as there’s no sound editing tools in Scratch.

The code for the cursor is below:

code

Step 3: Import one of your sprites. As you’ll want to test as you go, don’t crowd the screen with sprites that you aren’t ready to use yet. Just import one, try out the code until it’s doing what you want, then import the others and duplicate the code. Otherwise it’s needlessly annoying.

As mentioned, each sprite needs an ‘alive’ costume and a ‘dead’ costume:

code 2

Now it’s time to make them do something. The code for each sprite is in two parts. The first part is activated from when the program starts. It essentially sends the sprite on a random journey around the page. The sprite will only stop when its costume changes (ie its been ‘shot’). Although you should note that it won’t stop until it has finished running through the first part of the ‘if’ statement. In other words, if you ‘shoot’ it just as it starts a new movement, it will complete that movement before stopping.

code 3

The second part of the code will only run when you click on the sprite (‘shoot’ it). When that happens, the sprite ‘changes costume’ into the dead version, and its sound file is activated. Then it disappears for a little while, and ‘respawns’ 4 seconds later.

As I said before, it’s barely a game. There’s no scoring system, introduction, game over screen etc, but it’s a starting point for a more complicated program. the code is simple, and if you prepare all the images / sounds beforehand, you could complete this in one lesson.

Note: It obviously doesn’t have to be Star Wars, you could do pretty much anything. It doesn’t even have to be shooting. There’s a fun fly swatting game here for example. Or as an Easter game, the cursor could be a spoon which breaks eggs to find chickens inside or something. I only chose Star Wars because I wanted to shoot Jar Jar Binks.

jar2

You can download the whole program here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s