Microsoft Small Basic

Program Listing:
Embed this in your website
'Maze in 50 by Nik Coughlin -- Cleanup pushing it past 50 lines by codingCat aka Matthew L. Parets
gameLength = 20 'game starts with a length of 20 seconds
blocksize = 24 'Size of each section of the maze
viewsize = 18 'Number of sections visible on screen
score = 0 'a point for each star collected
GraphicsWindow.Height = blocksize * viewsize  'resize the window to exactly the size of the visible portion of the maze
GraphicsWindow.Width = blocksize * viewsize
playerX = 0 'initial player location
playerY = 0
maze[ playerX ][ playerY ] = "·" 'set the square of the maze at the player location to be empty so that it will always draw the first time through
GraphicsWindow.FontSize = blocksize 'increase/decrease this to make the squares bigger/smaller
GraphicsWindow.KeyUp = Move 'move when the player releases the key so that they have to press it once for each movement
Move() 'call move to draw the game for the first time otherwise screen will be blank until they press a key
Timer.Interval = 100 'Update the score with the timer every 1/10 of a second
Timer.Tick = updatescore
start = Clock.ElapsedMilliseconds 'Game last for 20 seconds with a half second extension for each point earned
While ((Clock.ElapsedMilliseconds - start) / 1000) <= (gameLength)
EndWhile
Timer.Interval = 1000000 'shutdown score update after game is over
GraphicsWindow.ShowMessage("Final Score: " + score,"Game over")
Program.End()

Sub Move
  newX = playerX 'make a copy of the player's current location
  newY = playerY
  If ( GraphicsWindow.LastKey = "Up" ) Then 'check if the player is moving and if so change the copy of the location accordingly
    newY = newY - 1
  ElseIf ( GraphicsWindow.LastKey = "Down" ) Then
    newY = newY + 1
  ElseIf ( GraphicsWindow.LastKey = "Left" ) Then
    newX = newX - 1
  ElseIf ( GraphicsWindow.LastKey = "Right" ) Then
    newX = newX + 1
  EndIf
  chkX = newX
  chkY = newY 'set location to the new position
  CheckMazeAtLocation() 'the square of maze at this location might not have been created yet
  If ( maze[ newX ][ newY ] <> "■" ) Then 'only update position and draw if they're not trying to move into a wall
    If( maze[ newX ][ newY ] = "☆" ) Then 'if the player walked onto a star...
      score = score + 1  'give them a point
      gameLength = gameLength + 0.5
      maze[ newX ][ newY ] = "·" 'remove the star and place an empty floor here instead
    EndIf
    playerX = newX 'update the player location
    playerY = newY
    For y = 0 To (viewsize-1) 'draw a section of the maze that is 18 x 18 tiles in size
      For x = 0 to (viewsize-1)
        chkX = ( x - 9 + playerX ) 'turn x and y into a location relative to the player
        chkY = ( y - 9 + playerY )
        CheckMazeAtLocation() 'the part of the maze at this location might not have been created yet
        GraphicsWindow.BrushColor = "MidnightBlue" 'clearing each square individually flickers less than a GraphicsWindow.Clear()
        GraphicsWindow.FillRectangle( x * GraphicsWindow.FontSize,  y * GraphicsWindow.FontSize, blocksize, GraphicsWindow.FontSize )
        GraphicsWindow.BrushColor = "PaleGreen" 'draw the maze square at this location
        GraphicsWindow.DrawText( x * GraphicsWindow.FontSize, y * GraphicsWindow.FontSize, maze[ chkX ][ chkY ] )
      EndFor
    EndFor
    GraphicsWindow.DrawText( 9 * GraphicsWindow.FontSize, 9 * GraphicsWindow.FontSize, "☻" ) 'draw the player
    updateScore() 'draw the score in the title bar
  EndIf
EndSub

Sub CheckMazeAtLocation 'check if the maze is empty at this location and if it is then randomly pick a tile to place here
  found = "False"
  If (maze[chkX][chkY] = "☆" Or maze[chkX][chkY] = "■" Or maze[chkX][chkY] = "·") then
    found = "True"
  EndIf
  If (found = "False") then
    If ( Math.GetRandomNumber( 100 ) = 1 ) Then
      maze[ chkX ][ chkY ] = "☆" 'star - increases your score
    ElseIf ( Math.GetRandomNumber( 4 ) = 1 ) Then
      maze[ chkX ][ chkY ] = "■" 'wall - a square you can't move through
    Else
      maze[ chkX ][ chkY ] = "·" 'floor - a square you can move over
    EndIf
  EndIf
EndSub

Sub updateScore 'Update the score and the timer
  status = "Score: " + score
  status = status + " -- Time Remaining: "
  elapsedSeconds = ((Clock.ElapsedMilliseconds - start) / 1000)
  timeLeft = gameLength - elapsedSeconds
  status = status + Math.Round(timeLeft * 10) / 10 'round to nearest 1/10 of a second
  GraphicsWindow.Title = status
EndSub

Copyright (c) Microsoft Corporation. All rights reserved.