While (gamePlay = true)
start = Clock.ElapsedMilliseconds
ObstacleUpdate()
If (exploding = false) Then
playerUpdate()
Else
Explode()
PlayerDie()
EndIf
delay = 20 - (Clock.ElapsedMilliseconds - start)
If (delay > 0) Then
Program.Delay(delay)
EndIf
EndWhile
' End Game
Shapes.Move(gameOverLabel, 160, gh/2-50)
Shapes.ShowShape(gameOverLabel)
For op = 1 to 200 Step 0.5
ObstacleUpdate()
Shapes.SetOpacity(gameOverLabel, op)
Program.Delay(20)
EndFor
For i = 1 To numObstacles
obstacleIndex = i
ObstacleReset()
EndFor
Timer.Resume()
EndSub
Sub ObstacleReset
' Takes in 'obstacleIndex' as a parameter to determine the obstacle to act on
' obstacleIndex' must be set to the current obstacle prior to calling this finction
localX = Math.GetRandomNumber(gw-30) + 15
localY = -(Math.GetRandomNumber(30) + 50)
obstacleX[obstacleIndex] = localX
obstacleY[obstacleIndex] = localY
obstacleSpeed[obstacleIndex] = Math.GetRandomNumber(6) + 1
Shapes.Move(obstacles[obstacleIndex], localX-obstacleRadius[obstacleIndex], localY-obstacleRadius[obstacleIndex])
EndSub
Sub ObstacleUpdate
For i = 1 To activeObstacles
obstacleY[i] = obstacleY[i] + obstacleSpeed[i]
obstacleSpeed[i] = obstacleSpeed[i] + gravity
Shapes.Move(obstacles[i], obstacleX[i]-obstacleRadius[i], obstacleY[i]-obstacleRadius[i])
If (exploding = false) Then
If (obstacleY[i] > gh + 36) Then
obstacleIndex = i
ObstacleReset()
ScoreUpdate()
EndIf
EndIf
EndFor
EndSub
Sub ScoreUpdate
score = score + 1
Shapes.SetText(scoreLabel, score)
EndSub
Sub playerUpdate
If (keyLeft) Then
playerX = playerX - playerSpeed
ElseIf (keyRight) Then
playerX = playerX + playerSpeed
EndIf
Sub PlayerDie
deathElapsed = (Clock.ElapsedMilliseconds - explosionStart)
If (playerZoom >= 0.1) Then
playerZoom = 1.0 - ((deathElapsed / 500) * 1.0)
Shapes.Zoom(player, playerZoom, playerZoom)
Else
Shapes.HideShape(player)
EndIf
EndSub
Sub CheckCollisions
For i = 1 To activeObstacles
closestX = Math.Min(Math.Max(obstacleX[i], playerLeft), playerRight)
closestY = Math.Min(Math.Max(obstacleY[i], playerTop), playerBottom)
Sub Explode
elapsed = Clock.ElapsedMilliseconds - explosionStart
If opacity >= 0 Then
opacity = 100 - ((elapsed/explosionTime) * 100)
explosionZoom = explosionZoom + ((elapsed/explosionTime) * 0.5)
For o = 1 to numParticles
Shapes.SetOpacity(particles[o], opacity)
Shapes.Zoom(particles[o], explosionZoom,explosionZoom)
endfor
EndIf
If opacity < 0 Then
Shapes.HideShape(particles[o])
gamePlay = false
EndIf
EndSub
'++++++++++++++++++++++++
' Event Handlers
'++++++++++++++++++++++++
Sub OnKeyDown
lastKeyDown = GraphicsWindow.LastKey
If (lastKeyDown = "Escape") Then
Program.End()
ElseIf (lastKeyDown = "Left") Then
keyLeft = true
ElseIf (lastKeyDown = "Right") Then
keyRight = true
EndIf
EndSub
Sub OnKeyUp
lastKeyUp = GraphicsWindow.LastKey
If (lastKeyUp = "Left") Then
keyLeft = false
ElseIf (lastKeyUp = "Right") Then
keyRight = false
EndIf
EndSub
Sub AddObstacle
' Timer event to increase the number of falling obstacles
If (activeObstacles < numObstacles) Then
activeObstacles = activeObstacles + 1
EndIf
'initally add obstacles quickly, then slow down after a few seconds
If (Timer.Interval < 3000) Then
Timer.Interval = Timer.Interval + 500
Endif
If activeObstacles > 9 Then
Timer.Interval = 30000
EndIf
EndSub