Saturday, 6 September 2014

Gears

Small Gear
Large Gear












The gears that have been created are shown above. they are a neutral/bronze colour this is to fit with the steam punk style and so they contrast well with the background.

The Gears need to fly in from the right hand side of the screen, bouncing off the top and bottom of the screen until they fly off the left hand side

To do this several sections of code are required. We need code to spawn the gears, code to determine whether they are large or small and code to ensure that they return after they leave the screen,
to help code this section a Touch develop tutorial was used. This tutorial was for a scrolling shooter game and can be found here.

The first section of code is to create the gears and spawn them. The Large gear and the small gear have almost identical sections of code however the sprites and the sizes are slightly different. Both gears have been set to fly from the right hand side of the screen at a random angle, position and speed.

Large Gear

private action SpawnLGear ()
This declares the Large Gear as well as determining it's trajectory each time it is spawned
 Lsprite :=  board → create picture( Large Gear)
 Lsprite → set width(100)
 Lsprite → set pos( board → width + 2, math → random( board → height -  Lsprite → height / 2))
 Lsprite → set angular speed(600)
 Lsprite → set speed( - 100 - math → random(50), - 200 - math → random(150))
 bounce sound → play
 Gears → add( Lsprite)
end action


Small Gear

private action SpawnSGear ()
This declares the Small gear and determines it's trajectory when it's spawned
 Ssprite :=  board → create picture( SmallGear)
 Ssprite → set width(50)
 Ssprite → set pos( board → width + 2, math → random( board → height -  Ssprite → height / 2))
 Ssprite → set angular speed(600)
 Ssprite → set speed( - 100 - math → random(50), - 50 - math → random(100))
 bounce sound → play
 Gears → add( Ssprite)
end action

This code was called in main and tested and worked as expected. The obstacles created earlier ensured that the gears would not fly out of the screen and the gears would rebound off of them. The next bit of code is to determine just how many gears will spawn at any one time. currently only two gears spawn, one large and one small. to do this a sprite set collection was created. this collection holds the desired amount of gears and will ensure that only that many will spawn.

 Gears :=  board → create sprite set

The above code was run in main along with the code below. The code below spawns a gear and through the use of a random value determines whether the gear is Large or Small.

private action SpawnGears (
GearsToSpawn : Number)
do
This sets the original gears that fly across. it also determines whether they will be large or small
for each sprite in  Gears
where true
do
sprite → delete
end for each
 Gears → clear
for 0 ≤ i < GearsToSpawn do
LorS is whether the gears will be Large or Small. if LorS = 1 the gear will be Large if LorS is 2 the gear will be Small
 LorS := math → random(2) + 1
if  LorS = 1 then
 SpawnLGear
else
 SpawnSGear
end if
end for
end action
Now three gears fly across the screen and are a random combination of small and large gears however they only fly across once. to change this another action is added and this action runs continously in Gameloop to ensure that the gears are constantly re-spawning after they leave.

private action CheckGears ()
This action is called in Gameloop
for each sprite in  Gears
where true
do
if sprite → x + sprite → width < 0 then
This ensures that when a gear(sprite) leaves the screen it will return. This also dictates whether the Gear will be Large or Small
 Gears → remove(sprite)
sprite → delete
 LorS := math → random(2) + 1
if  LorS = 1 then
 SpawnLGear
else
 SpawnSGear
end if
end for each
end action

Now the gears fly across the screen as it scrolls and will randomly re-spawn a gear of any size once a gear has left the screen.

1 comment:

  1. Good thinking with regards to where you were to call the gears in the main as well testing the elements so that they worked

    ReplyDelete