 |
| At school we have a policy of non
violence, but one of the things that makes games lots of fun is the
skill of shooting at things. So to help us remember that it is
just a game and avoid any 'violence', you are asked not to use
images or sprites that look like guns or people. Keep your
shooting to things like hamburgers at teddy bears! |
|
In this exercise we are going to have a monster that
shoots an icy-pole bullet.
 |
Create a new game with a
bullet and monster sprite and a
bullet and monster
object. Place the monster object in a room.
|
 |
Open the properties window for the Monster object. Add a
keypress event for when the space bar is pressed. Take
care not to use a Keyboard Event!Choose the Main1 tab, and choose the 'Create an Instance of
an Object' 
Select the following options:
We are creating a bullet at the same x coordinate as the
monster, but 2 pixels above the monster. Ticking relative
means the coordinates are added to the coordinates of the
monster. (This will make more sense later) |

|
Now we need to make the bullet move. Open the properties
window for the objBullet.
Add a create event.
From the movement tab, choose Set the Vertical Speed

Set the speed to be -6 (Making the vertical speed -6
means the bullet will move upwards) |
 |
 |
| There is one more thing we need to do. After the
bullet disappears from the play area, even though we can't see
it, it is still in the game moving upwards forever. The
computer keeps track of all the bullets, so if we don't destroy
them after they leave the play area, our game will get slower
and slower as the computer tries to keep track of all the
bullets.
|
|
We can fix this by adding an event to destroy the bullet
after it leaves the play area.
Open the properties for objBullet.
Add an 'Other' Event > Choose Outside Room
From the Main1 tab, choose Destroy the Instance
 |
| |
Now try your game. Press spacebar to shoot. There is
still a long way to go before our game is much good to play, but
we are on the way! |
 |
Activity 1
Make a bullet move across the screen
until it is outside the play area then destroy
it.
Hint: You will need to set the horizontal speed, not
the vertical speed, for the bullet.
Save it as moveacross Activity 2
Try setting both the horizontal and vertical speeds.
What do you think will happen? Activity 3
Make the bullet shoot in 8 different directions, depending on
which key is pressed. For example, pressing the A key will
shoot left, S key will shoot left and upwards, D key will shoot
straight upwards etc. Activity 4
How could you make it shoot in even more directions? |
|
 |
 |
Making an explosion from a bullet
When a bullet hits something you may want to
make an explosion. This is how you do it.
 |
Make a new game. Add bullet and animated explosion sprites and objects to your game |
Place the bullet at the bottom of the room
and make it move up the screen by giving it a vertical speed of -8:
 |
 |
We want the bullet to explode when it is 50
pixels from the top of the screen. Every Step test whether y<50 (ie the bullet is within 50 pixels of the top of the screen). If it is, then create an explosion and destroy the bullet.
Instead of making the bullet explode when it is near the top of the screen, you could use a collision event for an explosion when the bullet hits something.

To make the explosion disappear, use the animation end event for the Explosion.

Save your game sample as explosion |
|
|
 |
Blocks of Code
You will notice in the code above the actions for starting and
ending a block of code

This means that if the question at the top is true, the entire
block of code (all the actions) will be executed, otherwise if the
question is false, none of it will be executed. |
|
 |
Activity 5
Make an object move across the screen in the
top half of the play area.
Have another object at the bottom of the
screen (say a plane). When the spacebar is pressed the plane
should shoot at the moving object. If it hits it, then the
plane and bullet should explode and points should be scored.
If the bullet misses, it should be destroyed when it leaves the
play area.
Extension: Add events to move the
plane sideways when the left and right keys are pressed.
Save it as shooting |
|
 |
Changing Direction of Sprites
Sometimes you have a sprite that is facing in a particular direction, for
example this plane appears to be flying upwards:

When you change directions to fly left, it does not look right, so you need a
sprite that looks like this: 
To do this you must first create the sprites to face in each of the
directions. This video will show you how:
 |
| When using multiple
sprites for the same object you should turn off Precise
Collision Checking and the Bounding box should be set to
manual. The bounding box should be identical for all
sprites. The bounding box does not have to surround
all pixels of the sprite, it is just the area that is checked
for collisions. Otherwise when the sprite is changed, the
object may become stuck. |
|
Once you have created the sprites, you need to set up events to change the
sprite for the object whenever the left, right, up or down keys are pressed.
This video will show you how:
 |
| Often you want to stop your object to
stop moving when you are no longer pressing a key and you
would like the object to change to a 'stopped' sprite. This
video will show you how:
Add a stopped sprite into the activity above. |
|
 |
Shooting in the Direction You are Facing
It can be quite challenging to make your character shoot in
the direction it is facing.
Here is an example you can download that will show you how
it is done. Read the game information to see how to set it
up. |
|
|
|