Microsoft Small Basic

Program Listing: XNB002
DesktopHeight = Desktop.Height - 30
DesktopWidth= Desktop.Width - 30

GraphicsWindow.Width = DesktopWidth
GraphicsWindow.Height = DesktopHeight
GraphicsWindow.Left = 0
GraphicsWindow.Top = 0

ClLF = Text.GetCharacter(10)

Local_Busy = 0
DrawUI_MinBoxes = 30 'Square this Number
DrawUI_BoxSize = 30
DrawUI_BoxMinSize = 20

GenerateMinIllegals = 150
GenerateMinPoints = 40

BoxSizeChanged = 0

AccessFunction = "Calculate_Access"
args = ""
return = ""

MinRequirements()
GenerateLevel()

Sub MinRequirements
While (DesktopHeight < DrawUI_MinBoxes * DrawUI_BoxSize) Or (DesktopWidth < DrawUI_MinBoxes * DrawUI_BoxSize) And ( DrawUI_BoxSize >= DrawUI_BoxMinSize)
BoxSizeChanged = 1
DrawUI_BoxSize = DrawUI_BoxSize - 1
EndWhile
EndSub

'_________________________________________________________________________________________________________________________________
' Generate Level
'_________________________________________________________________________________________________________________________________

Sub GenerateLevel
Games = Games + 1
GameBoard = ""
GameBoard[2][2] = 0
Score = 0
endsquare = 0
BoxesToGo = GenerateMinPoints

GenerateBorders()
GenerateIllegals()
GeneratePoints()
DrawUI()
If Games = 1 Then
ShowHelp()
EndIf
SpawnPlayer()
EndSub

Sub GenerateBorders
For GenerateBordersXVar = 0 To 30
For GenerateBordersYVar = 0 To 30
If GenerateBordersXVar = 1 Or GenerateBordersXVar = DrawUI_MinBoxes Then
GameBoard[GenerateBordersXVar][GenerateBordersYVar] = 1
ElseIf GenerateBordersYVar = 1 or GenerateBordersYVar = DrawUI_MinBoxes Then
GameBoard[GenerateBordersXVar][GenerateBordersYVar] = 1
EndIf
EndFor
EndFor

EndSub

Sub GeneratePoints '
For GeneratedPointsVar = 1 To GenerateMinPoints
RandomX = Math.GetRandomNumber(DrawUI_MinBoxes)
RandomY = Math.GetRandomNumber(DrawUI_MinBoxes)
If GameBoard[RandomX][RandomY] = "" Or GameBoard[RandomX][RandomY] = 0 Then

If GameBoard[RandomX][RandomY] <> 2 And GameBoard[RandomX][RandomY] <> 3 And GameBoard[RandomX][RandomY] <> 4 And GameBoard[RandomX][RandomY] <> 1 Then
If LDCall.Function2(AccessFunction,RandomX,RandomY) > 1 then
GameBoard[RandomX][RandomY] = 3
Elseif LDCall.Function2(AccessFunction,RandomX,RandomY) = 1 and endsquare = 0 Then
endsquare = 1
GameBoard[RandomX][RandomY] = 4
EndIf
EndIf

Else
GeneratedPointsVar = GeneratedPointsVar - 1
EndIf
EndFor
EndSub

Sub GenerateIllegals
For GeneratedIllegalsVar = 1 To GenerateMinIllegals
RandomX = Math.GetRandomNumber(DrawUI_MinBoxes)
RandomY = Math.GetRandomNumber(DrawUI_MinBoxes)

If GameBoard[RandomX][RandomY] = "" Or GameBoard[RandomX][RandomY] = 0 Then
If LDCall.Function2(AccessFunction,RandomX,RandomY) >= 1 And GameBoard[RandomX][RandomY] <> 1 And GameBoard[RandomX][RandomY] <> 2 And GameBoard[RandomX][RandomY] <> 3 And GameBoard[RandomX][RandomY] <> 4 then
GameBoard[RandomX][RandomY] = 1
EndIf
Else
GeneratedIllegalsVar = GeneratedIllegalsVar - 1
EndIf
EndFor
EndSub

'_________________________________________________________________________________________________________________________________
' Draw Level
'_________________________________________________________________________________________________________________________________

Sub DrawUI ' Draws Basic UI
DrawNumbers()
For UIx = 1 To DrawUI_MinBoxes
For UIy = 1 To DrawUI_MinBoxes
CurrentCell = GameBoard[UIx][UIy]
If CurrentCell = 1 Then
GraphicsWindow.BrushColor = "Black"
ElseIf CurrentCell = 2 then

ElseIf CurrentCell = 3 Then
GraphicsWindow.BrushColor = "Yellow"
Elseif CurrentCell = 4 Then
GraphicsWindow.BrushColor = "Red"
Else
GraphicsWindow.BrushColor = "White"
EndIf

GraphicsWindow.DrawRectangle( UIx * DrawUI_BoxSize , UIy * DrawUI_BoxSize , DrawUI_BoxSize , DrawUI_BoxSize )
GraphicsWindow.FillRectangle( UIx * DrawUI_BoxSize , UIy * DrawUI_BoxSize , DrawUI_BoxSize , DrawUI_BoxSize )
EndFor
EndFor
EndSub

Sub DrawNumbers
For UIx = 1 To DrawUI_BoxSize * 30 Step DrawUI_BoxSize
GraphicsWindow.DrawText(UIx , 10, Math.Round( UIx/ DrawUI_BoxSize) )
EndFor
For UIy = 1 To DrawUI_BoxSize * 30 Step DrawUI_BoxSize
GraphicsWindow.DrawText(10 , UIy, Math.Round( UIy/ DrawUI_BoxSize) )
EndFor
EndSub


'_________________________________________________________________________________________________________________________________
' Misc
'_________________________________________________________________________________________________________________________________

Sub Calculate_Access
CAx = args[1]
CAy = args[2]
Access = 4
If GameBoard[CAx][CAy+1] = 1 Or GameBoard[CAx][CAy+1] = 2 Then 'Up
Access = Access - 1
EndIf

If GameBoard[CAx][CAy-1] = 1 Or GameBoard[CAx][CAy-1] = 2 Then 'Down
Access = Access - 1
EndIf

If GameBoard[CAx-1][CAy] = 1 Or GameBoard[CAx-1][CAy] = 2 Then 'Left
Access = Access - 1
EndIf

If GameBoard[CAx+1][CAy] = 1 Or GameBoard[CAx+1][CAy] = 2 Then 'Righ
Access = Access - 1
EndIf
return = Access
EndSub

Sub StartInterval
Timer.Interval = 1000
Timer.Tick = Tick
EndSub

Sub Tick
GraphicsWindow.Title = "Turtle " + Math.Floor( (( Clock.ElapsedMilliseconds - StartTime ) /1000 ) ) + "(s) Score: " + Score
EndSub

Sub ShowHelp
MSG = " Hello!"+ClLF+"Welcome to Turtle. Turtle is a game developed in Small Basic." + ClLF + "Here are the controls : " +ClLF
MSG = MSG + ClLF + "W - Up " + ClLF + "A - Left " + ClLF + "S - Down " + ClLF + "D - Right " + ClLF + "F1 - Show this screen" + ClLF + "F5 - Generate a new level " +ClLF
GraphicsWindow.ShowMessage(MSG , "Help")
EndSub

'_________________________________________________________________________________________________________________________________
' Player Controls
'_________________________________________________________________________________________________________________________________

Sub SpawnPlayer
Turtle.Show()
Turtle.X = (3 * DrawUI_BoxSize) - (DrawUI_BoxSize/2)
Turtle.Y = (3 * DrawUI_BoxSize) - (DrawUI_BoxSize/2)
Turtle.Speed = 10
Turtle.PenUp()
RandomBrush = GraphicsWindow.GetRandomColor()

TurtleFx = Math.Round( Turtle.X / DrawUI_BoxSize )
TurtleFY = Math.Round( Turtle.Y / DrawUI_BoxSize )
CurrentCell = GameBoard[TurtleFx][TurtleFY]
IsValidLocation()

StartTime = Clock.ElapsedMilliseconds
StartInterval()
GraphicsWindow.KeyDown = PlayerControlsHandler
EndSub

Sub PlayerControlsHandler
If Local_Busy = 0 Then
Local_Busy = 1
KeyDown = GraphicsWindow.LastKey
PlayerControls()
Local_Busy = 0
EndIf
EndSub

Sub PlayerControls
TurtleFx = Math.Round( Turtle.X / DrawUI_BoxSize )
TurtleFY = Math.Round( Turtle.Y / DrawUI_BoxSize )

TurtleX = TurtleFx
TurtleY = TurtleFY
validkey = 0

If KeyDown = "W" Then
TurtleY = TurtleFY + 1
Turtle.Angle = 0
validkey = 1
ElseIf KeyDown = "A" then
TurtleX = TurtleY - 1
Turtle.Angle = 270
validkey = 1
ElseIf KeyDown = "S" then
TurtleY = TurtleY - 1
Turtle.Angle = 180
validkey = 1
ElseIf KeyDown = "D" then
TurtleX = TurtleX + 1
Turtle.Angle = 90
validkey = 1
ElseIf KeyDown = "F1" then
ShowHelp()
Elseif KeyDown = "F5" then
GraphicsWindow.Clear()
GameBoard = ""
GenerateLevel()
EndIf

If validkey = 1 Then
Turtle.Move( DrawUI_BoxSize )
TurtleFx = Math.Floor( Turtle.X / DrawUI_BoxSize )
TurtleFY = Math.Floor( Turtle.Y / DrawUI_BoxSize )
CurrentCell = GameBoard[ TurtleFx][TurtleFY]

'TextWindow.WriteLine("(" + TurtleFx + "," + TurtleFY +") " + CurrentCell)

IsValidLocation()
If ValidMove = 1 Then
GameOverCheck()
EndIf
EndIf
EndSub

Sub IsValidLocation
ValidMove = 0
If CurrentCell = 1 Or CurrentCell = 2 Then 'Barriers
Turtle.Move( "-"+DrawUI_BoxSize )
ElseIf CurrentCell = 3 or CurrentCell = 4 Then 'Point Squares
BoxesToGo = BoxesToGo - 1
Score = Score + 100
GameBoard[TurtleFx][TurtleFY] = 2

GraphicsWindow.BrushColor = RandomBrush
GraphicsWindow.FillRectangle( TurtleFx * DrawUI_BoxSize , TurtleFY * DrawUI_BoxSize , DrawUI_BoxSize , DrawUI_BoxSize )
GraphicsWindow.BrushColor = "Black"
ValidMove = 1
Else
GameBoard[TurtleFx][TurtleFY] = 2

GraphicsWindow.BrushColor = RandomBrush
GraphicsWindow.FillRectangle( TurtleFx * DrawUI_BoxSize , TurtleFY * DrawUI_BoxSize , DrawUI_BoxSize , DrawUI_BoxSize )
GraphicsWindow.BrushColor = "Black"
ValidMove = 1
EndIf

EndSub


Sub GameOverCheck
If BoxesToGo = 0 then
EndTime = Clock.ElapsedMilliseconds
Tick()
Timer.Pause()
GraphicsWindow.ShowMessage("You have won!", " Game Over :)")
ElseIf LDCall.Function2(AccessFunction,TurtleFx,TurtleFY) = 0 Then
EndTime = Clock.ElapsedMilliseconds
Tick()
Timer.Pause()

GraphicsWindow.ShowMessage("You are Trapped", " Game Over :(")
EndIf
EndSub