Search This Blog

Friday 28 February 2014

Space Invader

Introduction

This game is based very loosely on the classic early shootem-up game of Space Invaders. In this game there is only one alien, but if you want to make the game more complex you could duplicate the alien as many times as you wish.

List of techniques

  • Importing backgrounds
  • Painting Sprites
  • Keyboard input for moving sprites
  • Random movement of a Sprite
  • Duplicating Scripts
  • Importing and playing sounds
  • Sprite on Sprite collision detection
  • Changing Sprite costumes in response to a collision
  • Score variable
  • Forever loop
  • If loop
  • Green Flag Scripts



 Our first task is to provide a background for the game. We will import a readymade background - but we could draw our own.

Once Scratch has loaded, click on the Stage icon in the Sprites List Area.

Then click on the Backgrounds tab above the Script Area

And then click the Import button
In the pop-up Import Background window, double-click the Nature folder and then scroll down until you find the stars image file. Click this and then click OK

The Stage should now have a starry background.

Our next task is to create a sprite which will become the laser cannon which we can move and fire to shoot at the aliens.

Click the Paint new sprite button above the Sprite List Area.
To keep things simple we will draw a triangle for our laser cannon (you could draw any shape you like).

Click on the colour you want in the Colour Palette (I'm using a light green). Then click on the Line Draw tool on the Toolbar.

On the canvas draw three lines to form a triangle (or any shape you wish)


Click on the Fill bucket tool on the Toolbar.

Click somewhere in the centre of the triangle shape to fill it with colour. If the paint leaks out and fills the whole screen, click the Undo button and use the paintbrush tool or line tool to close any gaps in the shape before filling it again.

Then click the OK button
We'll now delete the Scratch the Cat sprite. We could have deleted it earlier (or later) or changed its shape.

Right-click (Ctrl-click on a Mac) on the Sprite itself or its icon in the Sprite List Area.

In the pop-up list click on delete.


If you feel your laser cannon is too large, you can shrink it by clicking on the Shrink Sprite button above the Stage Area and then clicking several times on the Sprite in the Stage Area.

Next to the Shrink Sprite button is the Grow Sprite button which you can use if you accidentally shrink your Sprite too much.
Now we have created a Sprite, we can begin to program it.

Click on the Control button above the Blocks Palette Area

From the Blocks Palette Area, drag the when space key pressed block into the Script Area



Click the drop down menu on the when space key pressed block which you have just dragged into the Script Area

From the drop down list, click on right arrow

This tells the Script to do something when the right arrow key is pressed on the keyboard
Click the Motion button above the Blocks Palette

From the Blocks Palette drag the point in direction 90 block into the Script Area and attach it to the when right arrow key is pressed block

Click the drop down menu on this block (ie on the 90) and from the list click on (90) right. It is not essential to do this as it is already set to point right.


From the Blocks Palette, drag the move 10 steps block into the Script Area and attach it below the point in direction 90 block in your script

You can now check how this script is working by pressing the right arrow on your keyboard. You can drag the Sprite back to the left if it reaches the edge of the screen.
textTo create a script for the left arrow key we could repeat the above steps. A quicker way is to duplicate the script we have just written and then change it.

Right-click (Ctrl-click on a Mac) on the when right arrow key pressed block and on the pop-up menu click duplicate


On the new script, change right arrow to left arrow in the when right arrow key pressed block

Change the point in direction 90 block to read point in direction (-90) left

You can now check these two scripts by pressing the left and right arrow keys to move your laser cannon across the bottom of the screen.


You will find that the cannon sprite flips upside down when travelling to the left. To prevent this click on the Only face left-right button above the Script Area text

Now we need to create a laser bullet for the cannon to fire. Click the paint new sprite button above the Sprite List Area (as in step 3).

Click on the colour you would like to use (I'm using yellow) in the Colour Palette and then using either the Line Tool or the Paintbrush Tool, draw a short vertical line on the canvas. Then click OK.
We now need to write a script to control this Sprite.

Click on the Control button above the Blocks Palette and from the Blocks Palette drag the when space key pressed block into the Script Area.

First of all we must tell the bullet Sprite to go to the same place on the Stage as the laser cannon Sprite (ie Sprite 2).

Click on the Motion button above the Blocks Palette and from the Blocks Palette drag the go to _  block into the Script Area and attach it below the when space key pressed block.

Click on the drop down menu on this block and then click on Sprite 2 in the menu


Now drag the point in direction 90 block from the Blocks Palette into the Script Area and attach it beneath the go to Sprite 2 block

Click on the drop down menu on this block (ie the 90) and from the menu select (0) up


From the Control Blocks Palette Area drag the repeat 10 loop block into the Script Area and attach it beneath the point in direction 0 block

Change the 10 to 50 on the repeat block in the script by clicking in the pane, deleting the 10 and typing 50
From the Motion Blocks Palette, drag the move 10 steps block into the Script Area and put it into the repeat 50 loop

This tells the bullet to keep moving up the screen 10 steps, fifty times (ie a total of 500 steps). This is far more than is needed but it makes sure the bullet reaches the top of the screen.

You can now test this Script by moving the cannon around with the arrow keys and firing the laser with the space bar.


It would be good if the laser cannon made a noise. So, click the Sounds tab above the Script Area and then click the Import button.

We could record our own sound effect but this time we will use one which comes with the Scratch program

Double-click the Electronic folder to open it and then click on the Laser1 sound file. Then click OK.

Click on the Scripts tab to show the Script for this Sprite

From the Sound Blocks Palette, drag the play sound Laser1 block into the Script Area and attach it beneath the point in direction 0 block.

It should insert itself if you drag the block to just below the point in direction 0 block. It doesn't matter if it is above the point in direction 0 block as long as it isn't inside the repeat 50 loop otherwise the laser sound will play 50 times as it travels up the screen.

Now when you press the space bar, your laser should make a sound when it is fired.
Now we need to make something for the laser to fire at. Press the paint a new sprite button (as in Step 3 above) and then use the paint tools to create something alien-looking. It doesn't matter if you make it too large, you can always shrink it.

Click the OK button when you are finished being creative.


Our alien Sprite needs two costumes: one its normal cuddly self and another showing it being blown-up after being hit by the laser bullet.

Click on the Costumes tab above the Script Area for this Sprite and then click the Copy button
You will now get a copy of the Sprite's original costume (called costume 2). Click the Edit button for this new costume.

Use the paint tools to show a blown-up alien.

Click OK when you are finished.
Now we need a Script for the Alien Sprite to move it around the screen and detect when it has been hit by the bullet.

Click on the Scripts tab above the Scripts Area and from the Control Blocks Palette drag the when Green Flag clicked block into the Script Area

Then drag the Forever loop block from the Blocks Palette into the Script Area and attach it beneath the when Green Flag clicked block.
From the Motion Blocks Palette, drag the go to x: _ y:_ block into the Script Area and insert it into the Forever loop.
Change the co-ordinates in the go to x: _ y:_ block so the block now says go to x: 0 y: 110

Press the Green Flag button which is above the Stage Area on the right.

If you try dragging the alien sprite around the stage it will keep shooting back to its (0,110) co-ordinate.

Press the Red button next to the Green Flag button to stop all scripts from running.





The game would be extremely easy if every time the alien was shot it reappeared in the same location. We will now make it appear in a random place on the screen.

From the Operators Block Palette, drag the pick random 1 to 10 block into the go to x: 0 Y: 110 block to replace the 0 after x:

Change the range of numbers in the pick random block so it reads pick random -200 to 200
We will leave the y: value at 110 so it always appears the same distance away from the cannon each time (you could put another random block in the y: co-ordinate pane if you wish).

From the Control Blocks Palette, drag the wait 1 secs block so it is inside the Forever loop below the go to x: pick random -200 to 200 y:110 block
We will now make it stay on the screen for a random amount of time, between 1 and 5 seconds, before moving to a new location.

From the Operators Block Palette, drag the random 1 to 10 block into the wait 1 secs block in the Script Area to replace the 1

Change the range so the block now reads wait [pick random 1 to 5] secs

You can now test this script by clicking on the Green Flag button. The alien should keep re-appearing at different locations after varying periods of time. Press the Red button when done.
The next stage is it detect when the bullet sprite  hits and change the costume of the alien sprite.

From the Control Blocks Palette, drag the If loop block into the Script Area.

For the moment there is no need to attach it to the script

From the Sensing Blocks Palette, drag the diamond-shaped touching _ ? block into the diamond shaped pane at the head of the If loop block in the Script Area.

Click the drop-down list on the touching _ ? block and from the list select Sprite 3 (ie the bullet Sprite)

This instruction check if the alien sprite is touching the bullet sprite.
From the Looks Blocks Palette drag the switch to costume block into the Script Area and insert it into the main script just below the when Green Flag clicked block.

If necessary, change the block so its says switch to costume [costume1]

Now drag the switch to costume block from the Blocks Palette and insert it into the If loop in the Script Area

If necessary, change the block so it reads switch to costume [costume2]

Now to add a sound effect.

Click the Sounds tab above the Script Area. Double-click the Electronic folder and then click on the Screech sound file.

Now click the OK button
Click the Scripts tab above the Script Area to return to the script

From the Sound Blocks Palette, drag the play sound Screech until done block into the Script Area and insert it into If loop after the switch to costume [costume2] block


From the Control Blocks Palette, drag the when Green Flag clicked block  into the Script Area

Now drag the Forever loop from the Blocks Palette and attach it to the when Green Flag clicked block

Drag the If loop into the Forever loop

You can test this new Script by pressing the Green Flag button above the Stage Area

You'll see that the problem is that when the alien re-appears at its next location it is still in its blown-up state (costume 2). After being hit we need the show the blown-up costume for a while (until the Screech sound ends) and then return the Alien Sprite to its original costume (costume 1)



From the Looks Blocks Palette, drag the switch to costume block into the Script Area and insert it into the If loop below the play sound Screech until done block

If necessary, change the block so it reads switch to costume [costume1]
We can now do a bit of tidying-up.

Drag the Forever loop block down from the switch to costume [costume1] block, and then remove the switch to costume [costume1] block by dragging it into the Blocks Palette Area

Now drag the switch to costume [costume1] block from the If loop and insert it into the Forever loop above the go to x: pick random -200 to 200 y: 110 block

Re-attach the Forever loop block to the when Green Flag clicked block


Last of all we need to add some sort of scoring system.

Click on the Variables button and then the Make a variable button

In the pop-up window, type Score as the variable name and then press OK
Drag the set score to 0 block into the Script Area and insert it into the lower of the two Scripts just below the when Green Flag clicked block

Now drag the change Score by 1 into the Script Area and insert it into the If loop (anywhere in the loop will do)


To give the player time to see the effect of their successful shot, drag the wait 1 secs block from the Control Blocks Palette Area to the Script Area and insert it into the If loop below the change Score by 1 block
To make the game a little more challenging, let's add a Script to enable the Alien Sprite to fire at the Laser Cannon Sprite.

Click on the Paint new sprite button and create an alien-looking laser bolt.

You could duplicate the existing laser bullet sprite (Sprite 3) to save time but you will then need to edit the script for the new laser bolt sprite.


By now you could probably create your own Script for the Alien laser bolt Sprite.

Here is a simple Script you could use.

Alternatively, you might want to add a random wait block at the start of the forever loop
You will also need to create a blown-up costume for the Laser Cannon Sprite and add to its Script to detect and respond when it is hit by the Alien's Laser Bolt Sprite - here is one example of how the script can be modified.




Extensions

  • You could add a variable to decrease the number of lives each time the Laser Cannon Sprite has been hit.
  • You could add a variable to show a High Score 
  • You could increase the number of aliens (simply duplicate the Alien Sprite)
  • You could improve the procedure for the Alien Sprite to make it move across and down the screen as in the original version of the game

No comments:

Post a Comment