Microsoft Small Basic

Program Listing:
Embed this in your website
'Invaders -- By Krueg -- 4-14-2012
dw = 800                            'Desktop Width
dh = 600                            'Desktop Height
GraphicsWindow.Hide()
GraphicsWindow.Width=dw
GraphicsWindow.Height=dh            'Set up the graphics window
GraphicsWindow.Top=5
GraphicsWindow.Left=0
GraphicsWindow.CanResize="False"
GraphicsWindow.BackgroundColor="Black"
'path = "file://C:/Users/krueg/Documents/SmallBasic/MyProjects/Invaders/"
path = "http://www.krueg.com/sb/"
back0 = ImageList.LoadImage(path + "space.png")
back = Shapes.AddImage(back0)
Shapes.SetOpacity(back,50)
ufo10 = ImageList.LoadImage(path + "ufo1.png")
ufo20 = ImageList.LoadImage(path + "ufo2.png")
ufo30 = ImageList.LoadImage(path + "ufo3.png")
explode20 = ImageList.LoadImage(path + "explode2.png")
bullet0 = ImageList.LoadImage(path + "bullet.png")
rocket0 = ImageList.LoadImage(path + "rocket.png")
rocket = Shapes.AddImage(rocket0)
score = 0
level = 0
deadline = 400
GraphicsWindow.FontSize = 60
GraphicsWindow.PenColor = "Red"
GraphicsWindow.BrushColor = "Red"
GraphicsWindow.DrawLine(0,deadline,800,deadline)
Mouse.MouseX = 400
Mouse.MouseY = 600
Mouse.HideCursor()
GraphicsWindow.Show()
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseDown = Fire
ed = 0                              'Enemy start speed
en = 0                              'Number of enemies
el = 5                              'How much enemies drop when they hit the sides

MainLoop:                           'Here is the main routine
If en = 0 Then                      'if enemy count = 0
  LevelUp()                         'Increase level and create enemies
endif
GraphicsWindow.Title = "Invaders -- Score " + score + " -- Level " + level + " Krueg, 2012"
MoveEnemies()
If ba = 1 Then                      'Is bullet alive
  bullet()
endif
If hl > 0 Then                      'Keeps explosion on screen long enough to see it
  HitLife()
endif
Program.Delay(2-en/18)               'Compensate delay for enemy count
Goto MainLoop

Sub OnMouseMove                     'Moves players rocket
  x = GraphicsWindow.MouseX
  y = GraphicsWindow.MouseY
  If y < 450 Then                   'Don't let rocket rocket move up to high
    y = 450
  EndIf
  Shapes.Move(rocket,x-15,y-40)     'Show players rocket
EndSub

Sub Fire                            'Launch a bullet from your rocket
  Shapes.Remove(missle)             'Removes old bullet if it was allready flying
  missle = Shapes.AddImage(bullet0) 'Load bullet image into missle
  ba = 1                            'Set bullet alive
  bs = 2                            'Set bullet Speed
  bx = x-5                          'Bullet x-position in relation to rocket
  by = y-40                         'Bullet y-position
EndSub

Sub bullet                          'Moves your bullet up the screen
  Shapes.Move(missle,bx,by)         'Puts bullet into position
  by = by - bs                      'How much to move bullet in y-position
  If by < 0 Then                    'Remove bullet when it reaches the top
    ba = 0
    Shapes.Remove(missle)
  EndIf
EndSub

Sub MoveEnemies
  If ex1[1] < 50 Or ex1[1] > 200 Then 'When enemies hit the side
    ed = -ed                        'Change enemy direction
    For i = 1 To 6                  'Move enemies down
      ey1[i] = ey1[i] + el
      ey2[i] = ey2[i] + el
      ey3[i] = ey3[i] + el
      ey4[i] = ey4[i] + el
      ey5[i] = ey5[i] + el
      ey6[i] = ey6[i] + el
    EndFor
  EndIf
  For i = 1 To 6                    'Move enemies sideways
    ex1[i] = ex1[i] + ed
    ex2[i] = ex2[i] + ed
    ex3[i] = ex3[i] + ed
    ex4[i] = ex4[i] + ed
    ex5[i] = ex5[i] + ed
    ex6[i] = ex6[i] + ed
    If ea1[i] = 1 Then              'Check if the enemy is alive
      Shapes.Move(e1[i],ex1[i],ey1[i])      'Move enemy
      If ey1[i] + 30 > deadline Then    'Check if enemy makes is to the deadline
        GameOver()
      EndIf                         'Check bullet hit below
      If bx > ex1[i] And bx < ex1[i]+50 And by > ey1[i] And by < ey1[i]+25 Then
      en = en - 1                   'Subtract from enemy count
      ea1[i] = 0                    'Enemy alive is false
      ba = 0                        'Turn bullet alive off
      bx = 0
      by = 0
      hl = 10                       'Set explosion duration
      score = score + 30 * level    'Add to the score
      Shapes.Remove(missle)         'Remove the Bullet
      Shapes.Remove(e1[i])          'Remove enemy ship
      explode2 = Shapes.AddImage(explode20) 'Show explosion
      Shapes.Move(explode2,ex1[i],ey1[i])
      EndIf
    EndIf
    If ea2[i] = 1 Then
      Shapes.Move(e2[i],ex2[i],ey2[i])
      If ey2[i] + 30 > deadline Then
        GameOver()
      EndIf
      If bx > ex2[i] And bx < ex2[i]+50 And by > ey2[i] And by < ey2[i]+25 Then
      en = en - 1
      ea2[i] = 0
      ba = 0
      bx = 0
      by = 0
      hl = 10
      score = score + 30 * level
      Shapes.Remove(missle)
      Shapes.Remove(e2[i])
      explode2 = Shapes.AddImage(explode20)
      Shapes.Move(explode2,ex2[i],ey2[i])
      EndIf
    EndIf
    If ea3[i] = 1 Then
      Shapes.Move(e3[i],ex3[i],ey3[i])
      If ey3[i] + 24 > deadline Then
        GameOver()
      EndIf
      If bx > ex3[i] And bx < ex3[i]+50 And by > ey3[i] And by < ey3[i]+25 Then
      en = en - 1
      ea3[i] = 0
      ba = 0
      bx = 0
      by = 0
      hl = 10
      score = score + 20 * level
      Shapes.Remove(missle)
      Shapes.Remove(e3[i])
      explode2 = Shapes.AddImage(explode20)
      Shapes.Move(explode2,ex3[i],ey3[i])
      EndIf
    EndIf
    If ea4[i] = 1 Then
      Shapes.Move(e4[i],ex4[i],ey4[i])
      If ey4[i] + 24 > deadline Then
        GameOver()
      EndIf
      If bx > ex4[i] And bx < ex4[i]+50 And by > ey4[i] And by < ey4[i]+25 Then
      en = en - 1
      ea4[i] = 0
      ba = 0
      bx = 0
      by = 0
      hl = 10
      score = score + 20 * level
      Shapes.Remove(missle)
      Shapes.Remove(e4[i])
      explode2 = Shapes.AddImage(explode20)
      Shapes.Move(explode2,ex4[i],ey4[i])
      EndIf
    EndIf
    If ea5[i] = 1 Then
      Shapes.Move(e5[i],ex5[i],ey5[i])
      If ey5[i] + 24 > deadline Then
        GameOver()
      EndIf
      If bx > ex5[i] And bx < ex5[i]+50 And by > ey5[i] And by < ey5[i]+25 Then
      en = en - 1
      ea5[i] = 0
      ba = 0
      bx = 0
      by = 0
      hl = 10
      score = score + 10 * level
      Shapes.Remove(missle)
      Shapes.Remove(e5[i])
      explode2 = Shapes.AddImage(explode20)
      Shapes.Move(explode2,ex5[i],ey5[i])
      EndIf
    EndIf
    If ea6[i] = 1 Then
      Shapes.Move(e6[i],ex6[i],ey6[i])
      If ey6[i] + 24 > deadline Then
        GameOver()
      EndIf
      If bx > ex6[i] And bx < ex6[i]+50 And by > ey6[i] And by < ey6[i]+25 Then
      en = en - 1                   'Subtract from enemy count
      ea6[i] = 0                    'Enemy alive is false
      ba = 0                        'Turn bullet alive off
      bx = 0
      by = 0
      hl = 10                       'Set explosion duration
      score = score + 10 * level    'Add to the score
      Shapes.Remove(missle)         'Remove the Bullet
      Shapes.Remove(e6[i])          'Remove enemy ship
      explode2 = Shapes.AddImage(explode20) 'Show explosion
      Shapes.Move(explode2,ex6[i],ey6[i])
      EndIf
    EndIf
  EndFor
EndSub

Sub HitLife                         'Explode the enemy ship
  hl = hl -1                        'Shorten elplosion duration
  If hl = 0 Then                    'Remove explotion
    Shapes.Remove(explode2)
  EndIf
EndSub

Sub LevelUp                         'Initialize enemy array
  level = level + 1                 'Next level
  ed = level * 0.2                  'Set enemy speed
  For i=1 To 6                      'Create one column of enemies at a time
    en = en + 6                     'Add to enemy count
    e1[i] = Shapes.AddImage(ufo10)  'EnemyArray image
    ea1[i] = 1                      'Enemy alive 1-yes 0-no
    ex1[i] = i * 100                'Enemy x-position
    ey1[i] = 50                     'Enemy y-position
    Shapes.Move(e1[i],ex1[i],ey1[i])'Set enemy into position
    e2[i] = Shapes.AddImage(ufo10)
    ea2[i] = 1
    ex2[i] = i * 100
    ey2[i] = 100
    Shapes.Move(e2[i],ex2[i],ey2[i])
    e3[i] = Shapes.AddImage(ufo20)
    ea3[i] = 1
    ex3[i] = i * 100
    ey3[i] = 150
    Shapes.Move(e3[i],ex3[i],ey3[i])
    e4[i] = Shapes.AddImage(ufo20)
    ea4[i] = 1
    ex4[i] = i * 100
    ey4[i] =200
    Shapes.Move(e4[i],ex4[i],ey4[i])
    e5[i] = Shapes.AddImage(ufo30)
    ea5[i] = 1
    ex5[i] = i * 100
    ey5[i] = 250
    Shapes.Move(e5[i],ex5[i],ey5[i])
    e6[i] = Shapes.AddImage(ufo30)
    ea6[i] = 1
    ex6[i] = i * 100
    ey6[i] = 300
    Shapes.Move(e6[i],ex6[i],ey6[i])
  EndFor
EndSub

  Sub GameOver
    GraphicsWindow.BrushColor = "Yellow"
    GraphicsWindow.DrawText(200,500,"GAME OVER")
    Program.Delay(10000)
    Program.End()
  EndSub

Copyright (c) Microsoft Corporation. All rights reserved.