Microsoft Small Basic

Program Listing: NKB128
'Setup the Game Window
GraphicsWindow.Height = 200
GraphicsWindow.Width = 800
GraphicsWindow.Title = "The Brendan Martin Project, Game a Week 2, Score = 0" + score

'Create Player/Setup Player
playersize = 20
player = Shapes.AddRectangle(playersize,playersize)
playerx = 70
playery = 150
Shapes.Move(player, playerx, playery)

'Create Instructions
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawBoundText(500, 120, 280, "Press space to jump the square over the moving circle. A negative point is awared if they touch, and a point is granted if it is jumped successfully. A penalty is also awarded if no attempt is made.")

'Create The Line!
GraphicsWindow.DrawLine(1, 100, 800, 100)

'Create Red Ball/Setup ball
Sub createball
ballx = 800
bally = 93
ballsize = Math.GetRandomNumber(10) + 10
Shapes.ShowShape(ball)
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
ball = Shapes.AddEllipse(ballsize,ballsize)
Shapes.Move(ball, 810, 93)
ballspeed = 5
hit = 3
endsub

ballisalive = 0
scorelock = 1

'Setup the Game Control
Sub gamecontrol
GraphicsWindow.KeyDown = OnKeyDown
continue = "True"
While (continue)
If (keyIsUpdated = "True") Then
If(key = "Space") then
playerjump = 1
elseif(key = "Escape") Then
continue = "False"
Program.End()
EndIf
keyIsUpdated = "False"
EndIf
If playerjump = 1 Then
boxmove()
EndIf
If ballisalive = 0 then
createball()
EndIf
animateball()
gamerules()
'debug()
EndWhile
endsub

Sub OnKeyDown
key = GraphicsWindow.LastKey
keyIsUpdated = "True"
EndSub

jumpstate = 0
Sub boxmove
If playery <= 150 And jumpstate = 0 then
playery = playery - 2
if playery = 10 And jumpstate = 0 then
jumpstate = 1
Program.Delay(1)
endif
elseif playery >= 10 And jumpstate = 1 then
playery = playery + 2
If playery = 150 And jumpstate = 1 then
Program.Delay(1)
jumpstate = 0
playerjump = 0
scorelock = 0
endif
endif
Shapes.Move(player,playerx,playery)
EndSub

'Animate the Ball!

Sub animateball
ballisalive = 1
Program.Delay(ballspeed)
Shapes.move(ball, ballx, bally)
ballx = ballx - 1
If ballx <= 0 Then
killtheball()
endif
EndSub

'Start the Game here!
gamecontrol()

'When the timer is up, kill the ball
Sub killtheball
Shapes.Remove(ball)
ballisalive = 0
updatescore()
EndSub

Sub gamerules
If math.Abs(playerx - ballx) <= ballsize/2 And playery <= bally + ballsize/2 and playerjump = 1 Then
hit = 1
elseIf math.Abs(playerx - ballx) >= ballsize/2 And playery >= bally + ballsize/2 and playerjump = 1 Then
hit = 2
endif
endsub

sub updatescore
If hit = 1 or hit = 3 then
score = score - 1
elseif hit = 2 then
score = score + 1
endif
GraphicsWindow.Title = "The Brendan Martin Project, Game a Week 2, Score = " + score
endsub


Sub debug
ball_locx = Shapes.GetLeft(ball)
GraphicsWindow.Title = ballx - playerx
EndSub