Microsoft Small Basic

Program Listing: KQG813
'Setup the Graphics Window
gw_size = 100
GraphicsWindow.Height = gw_size
GraphicsWindow.Width = gw_size
GraphicsWindow.CanResize = "No"
GraphicsWindow.BackgroundColor = "Black"

'Game Text
gameinstructions = "Use the mouse to do awesome things!"
GraphicsWindow.Title = ""

scoreshown = 0

'Grid Rules
'rows = 18
'columns = 18
rows = 20
columns = 20
cellsize = 15

''Game Variables

'------------------------------------------------
'Setup the Grid

'------------------------------------------------
'Setup the Game
Sub createplayer
playerx = 1
playery = 1
playersize = 9
GraphicsWindow.BrushColor = "red"
GraphicsWindow.PenColor = "red"
player = Shapes.AddEllipse(playersize, playersize)
Shapes.Move(player, playerlocx, playerlocy)
playerlocx = Shapes.GetLeft(player)
playerlocy = Shapes.GetTop(player)
endsub
'------------------------------------------------

sub createtarget
GraphicsWindow.BrushColor = "white"
GraphicsWindow.PenColor = "white"
target = Shapes.AddEllipse(cellsize, cellsize)
targetx = Math.GetRandomNumber(100)
targety = Math.GetRandomNumber(100)
Shapes.Move(target, targetx, targety)
endsub

'------------------------------------------------
'Get the Game Loop going

createtarget()
createplayer()
game()

'------------------------------------------------
'The Main Game Loop
Sub game
GraphicsWindow.KeyDown = OnKeyDown
continue = "True"
While (continue)
'Listen for keys
If (keyIsUpdated = "True") Then
If(key = "Space") then
key = ""
restartgame()
elseif(key = "Escape") Then
continue = "False"
keyIsUpdated = "False"
Program.End()
elseif (key = "Left") then
playerx = playerx - 1
movecells()
elseif (key = "Right") then
playerx = playerx + 1
movecells()
elseif (key = "Up") then
playery = playery - 1
movecells()
elseif (key = "Down") then
playery = playery + 1
movecells()
EndIf
gamerules()
keyIsUpdated = "False"

EndIf
gamerules()
'Game Loop Ends Here
EndWhile
endsub

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


'Restart the game
Sub RestartGame
GraphicsWindow.Clear()
game()
EndSub

'------------------------------------------------

Sub movecells
shapes.Move(player,playerx,playery)
EndSub

sub gamerules
If targetx+3 = playerx and targety+3 = playery and scoreshown <> 1 Then
scoreshown = 1
GraphicsWindow.ShowMessage("You win!", "You win!")
EndIf
EndSub