Sunday, 7 September 2014

Final Game

Now that the Beta bug have been fixed we are left with a final product. You are able to play Gear Tumble now on the TouchDevelop Website using the URL below.

https://www.touchdevelop.com/szzca


The code for the final game is as shown below:

action main ()
 board :=  game → start
This game only has one life. This code turns off the life visibility as they are not required.
 game → set life visible(false)
This ensures that the characters don't fall off the top or bottom of the screen through the use of obstacles.
 board → create obstacle(0, 0,  board → width, 0, 1)
 board → create obstacle(0,  board → height,  board → width, 0, 1)
 Backgrounds
 MenuVar
The stop variable is used to determine whether the sprite is to stop running. 1 = Run 0 = Stop
 Stop := 1
 Crash → play
This ensures that the game doesn't start until the board has been touched
while not  board → touched do
 Menu
 board → update on wall
end while
 MGame
 MenuHide
This is where the rate of score increases it adds the value of scoreadd once every second
time → run every(1, perform)
where perform() is
 game → score :=  game → score +  scoreadd
end
end action
private action Animations ()
This action is called in Mgame
Defining the sprite parameters
 sprite :=  SpriteSheet → create sprite("1")
 sprite → set height(75)
 sprite → set y( board → height -  sprite → height / 1.5)
Here is where all the animations have been defined for use in the program. This has been done utilizing the sprite sheet.
This is where the sprite stands still
 SpriteSheet → add grid animation("Idle", 1, 1, 0, 100000, false)
Sprite runs right
 SpriteSheet → add grid animation("RRun", 9, 3, 0.5, 100, false)
Sprite runs left
 SpriteSheet → add grid animation("LRun", 5, 3, 0.5, 100, false)
sprite is hit
 SpriteSheet → add grid animation("Hit", 2, 3, 0.5, 100, false)
This sets the first animation to play "Idle"
 sprite → hide
 sprite → create animation → play frames("Idle")
 sprite → show
end action
private action BackgroundScroll ()
This action ensures that the two background continuously follow each other in a loop to ensure a constant scroll. This action is called in Gameloop.
if  Direction = 0 or  Direction = 3 then
This stops the background scrolling
 Back1 → set speed x(0)
 Back2 → set speed x(0)
else if  Direction = 1 then
Sets the Background scrolling Left
 Back1 → set speed x( scrollspeed)
 Back2 → set speed x( scrollspeed)
if  Back1 → x +  Back1 → width / 2 < 0 then
 Back1 → set x( Back2 → x +  Back2 → width)
else do nothing end if
if  Back2 → x +  Back2 → width / 2 < 0 then
 Back2 → set x( Back1 → x +  Back1 → width)
else do nothing end if
else
Set Background Scrolling Right
 Back1 → set speed x( -  scrollspeed)
 Back2 → set speed x( -  scrollspeed)
if  Back1 → x -  Back1 → width / 2 >  board → width then
 Back1 → set x( Back2 → x -  Back2 → width)
else do nothing end if
if  Back2 → x -  Back2 → width / 2 >  board → width then
 Back2 → set x( Back1 → x -  Back1 → width)
else do nothing end if
end if
end action
private action Backgrounds ()
This action is called in Main
This defines the width and images of Background 1 (Back1) and Background 2(Back2)
 Back1 :=  board → create picture( Background)
 Back1 → set width( board → width)
 Back2 :=  board → create picture( Background)
 Back2 → set width( board → width)
 Back2 → set x( Back1 → x +  Back1 → width)
Here dictates how fast the page will scroll as well as ensuring that Back2 follows Back1 to give the scrolling effect.
 scrollspeed := - 175
 Back1 → set speed x(0)
 Back2 → set speed x(0)
end action
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 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
else do nothing end if
if  sprite → overlaps with(sprite) then
This determines what occurs when the gears hit the sprite (character)
This stops the score from increasing
 scoreadd := 0
 sprite → create animation → stop
This links to Background Scroll and stops the background from scrolling
 Direction := 0
This plays the animation "hit"
 sprite → create animation → play frames("Hit")
This stops the sprite moving
 sprite → set speed x(0)
 Ow Cute2 → play
This prevents the sprite from continuing to run left or right
 Stop := 0
 gameOver
else
 Run
end if
end for each
end action
private action EndGame ()
This ensures that there is a 5 second wait before the score is posted and the game ends
time → run after(5, perform)
where perform() is
wall → clear
bazaar → post leaderboard score(math → round( game → score))
bazaar → post leaderboard to wall
time → sleep(3)
do nothing
This sets the game to start again if the player wishes
if wall → ask boolean("Play Again?"""then
 Restart
else
time → stop
end if
end
end action
private action MGame ()
This is where the game information that runs in main is kept
 Gears :=  board → create sprite set
The 3 is the amount of gears that will spawn at any given time
 SpawnGears(3)
This sets the sprite sheet and it's grid
 SpriteSheet :=  board → create sprite sheet( CharSheet6)
 SpriteSheet → set frame grid(3, 4, 0, 0, 0, 0, 0)
 Animations
This sets the score increase to 1
 scoreadd := 1
This sets the direction of the background (0 and 3 = No scrolling, 1 =Scroll Left, 2= Scroll Right
 Direction := 3
This sets the default score as well as set the position of the score display.
 game → set score(0)
 ScoreDisplay :=  board → create text(100, 20, 40, "Score: " ∥  game → score)
 ScoreDisplay → set pos( board → width / 2, 0)
end action
private action Menu ()
This sets the positions and visibility of the menu. This is called in Main
 Title2 → set pos( board → width / 2, 50)
 Title2 → set width(500)
 Title2 → show
 InstructionsTitle → set pos( board → width / 2 - 20, 100)
 InstructionsTitle → show
 Instructions → set pos(125, 170)
 Instructions → show
end action
private action MenuHide ()
This hides the menu during the game
 Title2 → create animation → fade out(0.5, "cubic")
 InstructionsTitle → create animation → fade out(0.5, "cubic")
 Instructions → create animation → fade out(0.5, "cubic")
 board → update on wall
end action
private action MenuVar ()
This sets the variables for the Menu
 Title2 :=  board → create picture( Title3)
 Title2 → hide
 InstructionsTitle :=  board → create text(100, 20, 40, "Instructions")
 InstructionsTitle → hide
 Instructions :=  board → create text(50, 50, 20, "Something has gone very wrong with the professors latest invention \nand Widget is caught in the middle.\nHelp Widget to avoid the flying gears by tapping the screen. \nTap on the left side of the screen to move him left and tap on\nthe right side of the screen to move him right. \nGood Luck\n\n(Tap Screen to Start)\n")
 Instructions → hide
end action
private action Restart ()
This resets the game to be played again and as such all the required variables and animations have been reset and the game over text hidden from view
wall → clear
 Stop := 1
 game → set score(0)
 scoreadd := 1
 SpawnGears(3)
 GameOver → hide
 sprite → create animation → play frames("Idle")
 sprite → set x( board → width / 2)
The re-posting of the board to the wall resets the page without rerunning main
 board → post to wall
end action
private action Run ()
This determines that if the screen is tapped left the character will move left and if the screen was tapped right the character would run right.
 board → on tap(tapped)
where tapped(x : Number , y : Number) is
if  Stop = 1 then
if x <  board → width / 2 then
 sprite → create animation → stop
 sprite → show
 sprite → create animation → play frames("LRun")
 sprite → set speed x( - 100)
 Direction := 2
else
 sprite → create animation → stop
 sprite → show
 sprite → create animation → play frames("RRun")
 sprite → set speed x(100)
 Direction := 1
end if
else do nothing end if
do nothing
end
end action
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 := math → random(2) + 1
if  LorS = 1 then
 SpawnLGear
else
 SpawnSGear
end if
end for
end action
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(400)
 Lsprite → set speed( - 50 - math → random(50), - 150 - math → random(50))
 bounce sound → play
 Gears → add( Lsprite)
end action
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
private action SpriteLimits ()
This limits the sprite the the confines of the screen. If the sprite leaves the screen the game finishes.
if  sprite → x >  board → width or  sprite → x < 0 then
 scoreadd := 0
 Awww2 → play
 sprite → hide
 sprite → set x( board → width / 2)
 sprite → set speed x(0)
 Stop := 0
 gameOver
else do nothing end if
end action
private action gameOver ()
This declares and sets the position of the game over text as well as calling the End Game
 GameOver :=  board → create text(100, 20, 40, "Game Over")
 GameOver → show
 GameOver → set pos( board → width / 2 - 40,  board → height / 2 -  GameOver → height)
for each sprite in  Gears
where true
do
sprite → create animation → fade out(6, "cubic")
end for each
 Gears → clear
 EndGame
end action
event gameloop ()
 board → evolve
 board → update on wall
 ScoreDisplay → set text("Score: " ∥ math → round( game → score))
 BackgroundScroll
 CheckGears
 SpriteLimits
end event
data Back1 : Sprite
data Back2 : Sprite
data Direction : Number
data GameOver : Sprite
data Gears : Sprite Set
data Instructions : Sprite
data InstructionsTitle : Sprite
data LorS : Number
data Lsprite : Sprite
data Menu2 : Number
data Play2 : Boolean
data PlayAgainScore : Number
data ScoreDisplay : Sprite
data SpriteSheet : Sprite Sheet
data Ssprite : Sprite
data StartTime : DateTime
data Stop : Number
data Title2 : Sprite
data board : Board
data scoreadd : Number
data scrollspeed : Number
data sprite : Sprite
art Awww2 : Sound
with url: "https://az31353.vo.msecnd.net/pub/aaodwaed"
art Background : Picture
with url: "https://az31353.vo.msecnd.net/pub/bvnigdzt"
art CharSheet6 : Picture
with url: "https://az31353.vo.msecnd.net/pub/xqsqrdeb"
art Crash : Sound
with url: "https://az31353.vo.msecnd.net/pub/kzsxzztw"
art Large Gear : Picture
with url: "https://az31353.vo.msecnd.net/pub/urgirsdv"
art Ow Cute2 : Sound
with url: "https://az31353.vo.msecnd.net/pub/ctnqwypc"
art SmallGear : Picture
with url: "https://az31353.vo.msecnd.net/pub/gqfftmbc"
art Title3 : Picture
with url: "https://az31353.vo.msecnd.net/pub/yjntteqs"
art bounce sound : Sound
with url: "https://az31353.vo.msecnd.net/pub/lpwyguiw"
art transparent : Picture
with url: "https://az31353.vo.msecnd.net/pub/stuvptro"
import game
published fzqla
import gamepad
published clfwa
uses game bound to game with