Microsoft Small Basic

Program Listing:
Embed this in your website
'Width and Height of window

gw = 800
gh = 600
GraphicsWindow.title="Noof Ball"
GraphicsWindow.CanResize = "False"
GraphicsWindow.Top = (Desktop.Height-gh)/2
GraphicsWindow.Left = (Desktop.Width-gw)/2
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "Lightblue"

GraphicsWindow.brushcolor="Black"
GraphicsWindow.DrawText(100,100,"Control the Red ball using the Arrow keys")
GraphicsWindow.DrawText(100,125,"See how many Blue balls you can collect")
GraphicsWindow.DrawText(100,150,"Without coming into contact with Yellow ones")
GraphicsWindow.brushcolor="red"
GraphicsWindow.FillEllipse(400,100,20,20)
GraphicsWindow.brushcolor="Black"
GraphicsWindow.drawEllipse(400,100,20,20)
GraphicsWindow.brushcolor="Blue"
GraphicsWindow.FillEllipse(400,125,20,20)
GraphicsWindow.brushcolor="Black"
GraphicsWindow.drawEllipse(400,125,20,20)
GraphicsWindow.brushcolor="yellow"
GraphicsWindow.FillEllipse(400,150,20,20)
GraphicsWindow.brushcolor="black"
GraphicsWindow.drawEllipse(400,150,20,20)


GraphicsWindow.KeyDown=Keypressed
Timer.Interval=35
Timer.tick=keypressed
Controls.ButtonClicked=ButtonClick
StartButton=Controls.AddButton("Start Game",360,260)
NewGame = 0


'Simple game varibles
xPos=400
yPos=300
foodxpos=Math.GetRandomNumber(70) * 10
foodypos=Math.GetRandomNumber(50) * 10
score=0

'Displaying score
'GraphicsWindow.BrushColor="Black"
'GraphicsWindow.DrawText(10,50,"Score: ")

NoOfBallsToAvoid = 1
TempBall = 1

'Main program



'Food()
'StartGame()

'ButtonSub
Sub ButtonClick
B=Controls.LastClickedButton
If (B=StartButton) Then
  GraphicsWindow.Clear()
  GraphicsWindow.BrushColor="Black"
  GraphicsWindow.DrawText(10,50,"Score: ")
  Food()
  StartGame()
elseif (B=StartButtonReset) Then
  GraphicsWindow.Clear()
  GraphicsWindow.BrushColor="Black"
  GraphicsWindow.DrawText(10,50,"Score: ")
  xPos=400
  yPos=300
  NewGame = 1
  food()
  StartGame()
  EndIf
endsub


'Start game sub
Sub StartGame
  GraphicsWindow.BrushColor="red"
  ball=shapes.AddEllipse(20,20)
  Shapes.Move(ball,xPos,yPos)
 ' GenerateBallToAvoid()
EndSub

'Keypress sub
Sub Keypressed

If NewGame = 1 then
  Timer.Resume()
  NewGame = 0
else

  Move=10
  If GraphicsWindow.lastkey="Left" And xPos > 0 Then
    xPos=xPos-move
    Shapes.Move(ball,xPos,yPos)
      elseif GraphicsWindow.lastkey="Right" And xPos < 780 then
       xPos=xPos+move
       Shapes.Move(ball,xPos,yPos)
      elseif graphicswindow.lastkey="Up" and yPos > 0 then
        yPos=yPos-move
       Shapes.Move(ball,xPos,yPos)
      elseif graphicswindow.lastkey="Down" and yPos < 580 then
       yPos=yPos+move
       Shapes.Move(ball,xPos,yPos)
     elseif GraphicsWindow.lastkey="Escape" then
       Program.End()
     endif
If (xPos = foodxpos - 15) and (yPos = foodypos - 15) Then
elseif (xPos = foodxpos) and (yPos = foodypos) then
   score=score+1
   GraphicsWindow.BrushColor="lightblue"
   GraphicsWindow.FillRectangle(50,50,100,100)
   GraphicsWindow.BrushColor="black"
   GraphicsWindow.DrawText(50,50,score)

   OK = "False"
   TempCounter = NoOfBallsToAvoid

   while OK = "False"

     foodxpos=math.GetRandomNumber(70)*10
     foodypos=math.GetRandomNumber(50)*10

     For counter = 1 to TempCounter
       If ((foodxpos > x[counter] - 20) and (foodxpos < x[counter] + 20)) or ((foodypos > y[counter] - 20) and (foodypos < y[counter] + 20)) then
         OK = "False"
         counter = TempCounter
       else
         OK = "True"
       EndIf

      If (OK = "True") and (counter = tempcounter) then
         Shapes.move(food,foodxpos,foodypos)
         NoOfBallsToAvoid = NoOfBallsToAvoid + 1
         GenerateBallToAvoid()
         MoveBall()
       EndIf
     endfor
   endwhile
 endif
 'If (xPos = x + 20) and (yPos = y + 20) Then
 ' elseif (xPos = x) and (yPos = y) then
 For z = 1 to NoOfBallsToAvoid
 If ((xPos > x[z] - 20) And (xPos < x[z] + 20)) And ((yPos > y[z] - 20) And (yPos < y[z] + 20)) then
      x = Math.GetRandomNumber(80)*10
      y = Math.GetRandomNumber(60)*10
      GraphicsWindow.Clear()
      'GraphicsWindow.BrushColor="Black"
      'GraphicsWindow.DrawText(350,250,"Game Over :(")
      'GraphicsWindow.DrawText(345,275,"Your score is "+score)
      For i = 1 To 7000
        xEnd=math.GetRandomNumber(800)
        yEnd=math.GetRandomNumber(600)
        'GraphicsWindow.brushcolor="yellow"
        GraphicsWindow.BrushColor="Yellow"
        GraphicsWindow.FillEllipse(xEnd,yEnd,20,20)
        GraphicsWindow.brushcolor="black"
        GraphicsWindow.drawEllipse(xEnd,yEnd,20,20)
      endfor
       Timer.Pause()
        GraphicsWindow.Clear()
              GraphicsWindow.BrushColor="Black"
      GraphicsWindow.DrawText(350,250,"Game Over :(")
      GraphicsWindow.DrawText(345,275,"Your score is "+score)
      StartButtonReset=Controls.AddButton("Try Again",360,300)
     ' Controls.ButtonClicked=ButtonClickReset
      endif
      endfor



      endif
      endsub

'Food sub
Sub Food
  GraphicsWindow.BrushColor="blue"
  food=shapes.addEllipse(20,20)
  Shapes.Animate(food,foodxpos,foodypos,0)
endsub

Sub GenerateBallToAvoid

  GraphicsWindow.brushcolor="yellow"
  balltoavoid[NoOfBallsToAvoid] = Shapes.AddEllipse(20,20)

EndSub

Sub MoveBall

 TempBall = TempBall + 1

  If TempBall <= NoOfBallsToAvoid Then
    'GraphicsWindow.DrawText(200,100, TempBall)
 ' While "true"
   'For Counter = 1 To NoOfBallsToAvoid
      x[TempBall] = Math.GetRandomNumber(78)*10
      y[TempBall] = Math.GetRandomNumber(58)*10
      Shapes.move(balltoavoid[TempBall], x[TempBall], y[TempBall])

   ' Program.Delay(3000)
 ' EndFor
'EndWhile
Else
  TempBall = 0
EndIf
EndSub

Sub ButtonClickReset

endsub
Copyright (c) Microsoft Corporation. All rights reserved.