Microsoft Small Basic

Program Listing:
Embed this in your website
GraphicsWindow.BackgroundColor = "SkyBlue"
gameWidth = "450"
gameHeight = "350"
GraphicsWindow.Width = gameWidth
GraphicsWindow.Height = gameHeight
GraphicsWindow.Left = Desktop.Width / 2 - GraphicsWindow.Width / 2
GraphicsWindow.Top = Desktop.Height / 2 - GraphicsWindow.Height / 2

beec = 0
heading = 1
getgoing = 0
dirdeg = 0
dirreq = "s"
dirarr = 0

shoot = 0

GraphicsWindow.Show()
GraphicsWindow.KeyDown = getcommand



sx = 100 'ship x starting position
sy = 100 'ship y starting position
wx = 1 ' begin wind x
wy = 1 ' begin wind y
windhead = 0
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "Brown"
GraphicsWindow.BrushColor = "Brown"
myship = Shapes.AddEllipse(5, 11)
Shapes.Move(myship, sx, sy)

  saveshead = ""
  savewhead = ""
  savesarr = 0
  savewarr = 0


  '************************************************
  '** dr[] controls how fast the ship moves into or
  '** with the wind, depending on its heading.
  '************************************************
  dr[0] = .2 '.2
  dr[1] = .3 '.4
  dr[2] = .5 '.8
  dr[3] = .4 '.6
  dr[4] = .4
  dr[5] = .5
  dr[6] = .3
  dr[7] = .2


run()



Sub run
  running = 1

  While running = 1
    'find the ship heading
    dirdeg = heading
    calcdir()
    saveshead = dirreq
    savesarr = dirarr

    'find the wind heading
    dirdeg = windhead
    calcdir()
    savewhead = dirreq
    savewarr = dirarr

    GraphicsWindow.Title = "shiph: " + saveshead +", windh: "+ savewhead + ", " + modspeed
    Program.Delay(20)
    If getgoing = 1 Then
      getwind()
      calcmove()
      moveship()
    endif

    If shoot = 1 Then
      doshoot()
    EndIf
  endwhile

endsub



'************************************************
'** Moves the ship. The variable 'beec' is the main
'** variable that changes speed.
'************************************************
Sub moveship
  sx = Math.Remainder(sx + (Math.Cos(Math.GetRadians(heading - 90)) * beec) + gameWidth, gameWidth)
  sy = Math.Remainder(sy + (Math.Cos(Math.GetRadians(heading)) * beec) + gameHeight, gameHeight)
  Shapes.Move(myship, sx, sy)
endsub



'************************************************
'** Generates wind direction and controls when
'** the wind changes direction
'************************************************
wdelay = 0
Sub getwind
  wdelay = wdelay + 1
  If wdelay = 1000 then
    windhead = Math.Remainder(Math.GetRandomNumber(360), 360)
    wdelay = 1
  endif

endsub




'************************************************
'** Modifies the speed of the ship by calculating
'** the wind speed and direction
'************************************************
Sub calcmove
  modspeed = math.abs(savesarr - savewarr)
  beec = ((beec * dr[modspeed]) + .11)
endsub





'************************************************
'** narrows the 360 possible points of the
'** compas down to eight. This is used in run()
'** where run queries both the wind heading
'** and the ship heading.
'************************************************
Sub calcdir
  If dirdeg < 45 then
    dirreq = "sse"
    dirarr = 4

  endif

  If dirdeg > 315 then
    dirreq = "ssw"
    dirarr = 5
  endif

  If dirdeg < 90 and dirdeg > 45 then
    dirreq = "se"
    dirarr = 3
  endif

  If dirdeg < 135 and dirdeg > 90 then
    dirreq = "ne"
    dirarr = 2
  endif

  If dirdeg < 180 and dirdeg > 135 then
    dirreq = "nne"
    dirarr = 1
  endif

  If dirdeg < 225 and dirdeg > 180 then
    dirreq = "nnw"
    dirarr = 8
  endif

  If dirdeg < 270 and dirdeg > 225 then
    dirreq = "nw"
    dirarr = 7
  endif

  If dirdeg < 315 and dirdeg > 270 then
    dirreq = "sw"
    dirarr = 6
  endif
endsub




'************************************************
'** Listen for keystrokes
'************************************************
Sub getcommand
  If(GraphicsWindow.LastKey = "Up") Then
    getgoing = 1
  EndIf

  If(GraphicsWindow.LastKey = "Left") Then
    heading = math.Abs(Math.Remainder(heading + .9, 360))
    vhead = heading * -1
    Shapes.Rotate(myship, vhead)
  endif

  If(GraphicsWindow.LastKey = "Right") Then
    If heading = 0 Then
      heading = 360
    EndIf
    heading = Math.Remainder(heading - .9, 360)
    vhead = heading * -1
    Shapes.Rotate(myship, vhead)
  endif

  If(GraphicsWindow.LastKey = "Down") Then
    beec = 0
    getgoing = 0
  EndIf

  If(GraphicsWindow.LastKey = "Space") Then
    shoot = 1
  EndIf
EndSub

Sub doshoot

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