Microsoft Small Basic

Program Listing:
Embed this in your website
'Soda Pop Showdown Krueg -- 2012
' Use mouse to aim
' Left click to fire
' Escape to quit

setupscreen()
loadimages()
splash()

restart:                                    'Go here for game restart
setupcontrols()
onmousemove()
newgame()
While alive > 0
  sodaflight()
  If plinklife > 0 Then
    showplink()
  EndIf
  If plunklife > 0 Then
    showplunk()
  EndIf
  If slomolife > Clock.ElapsedMilliseconds Then
    Program.Delay(20)
  EndIf
  Program.Delay(50-cancount*2)              'Simple speed control for dificulty during game
EndWhile
gameover()
Goto restart

Sub setupscreen
  GraphicsWindow.Hide()
  gw = Desktop.Width - 15
  gh = Desktop.Height - 64
  GraphicsWindow.Top = 4
  GraphicsWindow.Left = 4
  GraphicsWindow.Width = gw
  GraphicsWindow.Height = gh
  GraphicsWindow.CanResize = "False"
  GraphicsWindow.Title = "Soda Pop Showdown Krueg -- 2012"
  GraphicsWindow.BackgroundColor = "Black"
  GraphicsWindow.Show()
EndSub

Sub loadimages
  path = "http://www.krueg.com/sb/sodapop/"
  'path = "file://C:/Users/krueg/Documents/SmallBasic/MyProjects/SodaPop/"
  can1pic = ImageList.LoadImage(path + "can1.png")
  can2pic = ImageList.LoadImage(path + "can2.png")
  can3pic = ImageList.LoadImage(path + "can3.png")
  can4pic = ImageList.LoadImage(path + "can4.png")
  can5pic = ImageList.LoadImage(path + "can5.png")
  can6pic = ImageList.LoadImage(path + "can6.png")
  can7pic = ImageList.LoadImage(path + "can7.png")
  can8pic = ImageList.LoadImage(path + "can8.png")
  plinkpic = ImageList.LoadImage(path + "plink.png")  'Little spark when can is hit
  plunkpic = ImageList.LoadImage(path + "plunk.png")  'Big pop when can is destroyed
  crosshairpic = ImageList.LoadImage(path + "crosshairs.png")
  can[1] = Shapes.AddImage(can1pic)
  'Shapes.HideShape(can[1])
  can[2] = Shapes.AddImage(can2pic)
  'Shapes.HideShape(can[2])
  can[3] = Shapes.AddImage(can3pic)
  'Shapes.HideShape(can[3])
  can[4] = Shapes.AddImage(can4pic)
  'Shapes.HideShape(can[4])
  can[5] = Shapes.AddImage(can5pic)
  'Shapes.HideShape(can[5])
  can[6] = Shapes.AddImage(can6pic)
  'Shapes.HideShape(can[6])
  can[7] = Shapes.AddImage(can7pic)
  'Shapes.HideShape(can[7])
  can[8] = Shapes.AddImage(can8pic)
  'Shapes.HideShape(can[8])
  plink = Shapes.AddImage(plinkpic)
  Shapes.HideShape(plink)
  plunk = Shapes.AddImage(plunkpic)
  Shapes.HideShape(plunk)
  crosshair = Shapes.AddImage(crosshairpic)
  'Shapes.HideShape(crosshair)
' The following line could be harmful and has been automatically commented.
' hiscore = File.ReadContents(Program.Directory + "\HiScore.txt")
  If hiscore = "" Then
    hiscore = 0
  EndIf
EndSub

Sub splash
  GraphicsWindow.DrawResizedImage(path + "SodaBackground.jpg",0,0,1139*(gw/1139),768*(gh/768))
  Program.Delay(500)
  Shapes.Move(crosshair,gw*.6,gh*.5)
  Shapes.Move(can[1],gw*.45,gh*.04)
  Shapes.Move(can[2],gw*.4,gh*.55)
  Shapes.Move(can[3],gw*.6,gh*.55)
  Shapes.Move(can[4],gw*.8,gh*.55)
  Shapes.Move(can[5],gw*.75,gh*.04)
  Shapes.Move(can[6],gw*.04,gh*.3)
  Shapes.Move(can[7],gw*.04,gh*.45)
  Shapes.Move(can[8],gw*.04,gh*.6)
  GraphicsWindow.BrushColor = "Cyan"
  GraphicsWindow.FontSize = 64
  GraphicsWindow.DrawText(gw*.6-150,gh*.2,"SODA POP")
  GraphicsWindow.FontSize = 48
  GraphicsWindow.DrawText(gw*.6-140,gh*.35,"SHOWDOWN")
  GraphicsWindow.BrushColor = "Yellow"
  GraphicsWindow.FontSize = 16
  GraphicsWindow.DrawText(gw*.13,gh*.1,"USE MOUSE TO AIM")
  GraphicsWindow.DrawText(gw*.13,gh*.15,"LEFT CLICK TO SHOOT")
  GraphicsWindow.DrawText(gw*.13,gh*.2,"ESCAPE TO QUIT")
  GraphicsWindow.DrawText(gw*.15,gh*.35,"SLOW MOTION - 10 SEC.")
  GraphicsWindow.DrawText(gw*.15,gh*.5,"DOUBLE SCORES - 10 SEC.")
  GraphicsWindow.DrawText(gw*.15,gh*.65,"ADD 1 LIFE")
  GraphicsWindow.BrushColor = "Red"
  GraphicsWindow.DrawText(gw*.85,gh*.35,"HI SCORE IS " + hiscore)
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FontSize = 12
  ready = Controls.AddButton("Play",gw*.6-80,gh*.45)
  quit = Controls.AddButton("Quit",gw*.6+20,gh*.45)
  Controls.SetSize(ready,90,30)
  Controls.SetSize(quit,90,30)
  Controls.ButtonClicked = onbuttonpress
  While play < 1
    For i2 = 1 To 360
      For i = 1 to 8
        Shapes.Rotate(can[i],i2)
      EndFor
      Program.Delay(2)
    EndFor
  EndWhile
  play = 0
  For i = 1 To 8
    Shapes.HideShape(can[i])
  EndFor
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FillRectangle(0,0,gw,gh)
  Controls.Remove(ready)
  Controls.Remove(quit)
  GraphicsWindow.DrawResizedImage(path + "SodaBackground.jpg",0,0,1139*(gw/1139),768*(gh/768))
  Program.Delay(500)
EndSub

Sub gameover
  Shapes.HideShape(can[sn])
  disablecontrols()
  GraphicsWindow.BrushColor = "Red"
  GraphicsWindow.FontSize = 128
  GraphicsWindow.DrawText(gw*.5-200,gh*.08,"GAME")
  GraphicsWindow.DrawText(gw*.5-190,gh*.25,"OVER")
  If score > hiscore Then
    hiscore = score
' The following line could be harmful and has been automatically commented.
' File.WriteContents(Program.Directory + "\HiScore.txt",hiscore)
    GraphicsWindow.FontSize = 16
    GraphicsWindow.BrushColor = "Yellow"
    GraphicsWindow.DrawText(gw*.5-145,gh*.5,"YOU HAVE THE NEW HI SCORE " + hiscore)
  EndIf
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FontSize = 12
  ready = Controls.AddButton("Play",gw*.5-100,gh*.6)
  quit = Controls.AddButton("Quit",gw*.5,gh*.6)
  Controls.SetSize(ready,90,30)
  Controls.SetSize(quit,90,30)
  Controls.ButtonClicked = onbuttonpress
  While play < 1
  EndWhile
  play = 0
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FillRectangle(0,0,gw,gh)
  Controls.Remove(ready)
  Controls.Remove(quit)
  GraphicsWindow.DrawResizedImage(path + "SodaBackground.jpg",0,0,1139*(gw/1139),768*(gh/768))
  Program.Delay(500)
EndSub

Sub setupcontrols
  Mouse.HideCursor()
  Mouse.MouseX = gw/2
  Mouse.MouseY = gh/2
  GraphicsWindow.MouseMove = onmousemove
  GraphicsWindow.MouseDown = onmousedown
  GraphicsWindow.KeyDown = onkeyboard
EndSub

Sub disablecontrols
  Mouse.ShowCursor()
  GraphicsWindow.MouseMove = disable
  GraphicsWindow.MouseDown = disable
EndSub

Sub disable
  'Disable an event handler
EndSub

Sub onbuttonpress
  If Controls.LastClickedButton = ready Then
    play = 1
  EndIf
  If Controls.LastClickedButton = quit Then
    Program.End()
  EndIf
EndSub

Sub onmousemove
  cx = GraphicsWindow.MouseX-15
  cy = GraphicsWindow.MouseY-15
  Shapes.Move(crosshair,cx,cy)
EndSub

Sub onmousedown
  If Mouse.IsLeftButtonDown Then            'Mouse fire
    If cx > sx-50 And cx < sx+50 And cy > sy And cy < sy+100 Then
      sodahit()
    EndIf
  EndIf
EndSub

Sub onkeyboard
  If GraphicsWindow.LastKey = "Escape" Then 'Someone pressed ESCAPE
    Program.End()
  EndIf
EndSub

Sub newgame
  alive = 3
  score = 0
  canhit = 0
  cancount = 0
  plinkalive = 0                            'The little spark when a soda is hit
  plunkalive = 0                            'The pop when you destroy a soda
  play = 0
  quit = 0
  showscore()
  newsoda()
EndSub

Sub newsoda
  tryagain:
  sn = Math.GetRandomNumber(8)              'Soda number
  If sn = sl Then                           'Don't show the same soda again
    Goto tryagain
  EndIf
  sxs = Math.GetRandomNumber(20)-10         'Soda speed X
  sys = -35 + Math.GetRandomNumber(10)      'Soda speed Y
  sx = Math.GetRandomNumber(gw-120) + 60    'Soda start position X
  sy = gh - 30                              'Soda start position Y
  sa = 0                                    'Soda angle
  sas = Math.GetRandomNumber(130) - 65      'Soda angle speed
  canhit = 0                                'Soda can hits
  Shapes.ShowShape(can[sn])                 'Show the new soda
  sl = sn                                   'Soda from last time
EndSub

Sub sodaflight
  If sas > 0 Then                           'Is soda spinning to the right
    sa = sa - sas                           'Subtract the speed from the spin angle
    If sa < 0 Then                          'Is spin angle below 0
      sa = sa + 360
    EndIf
    sas = sas - .5                          'Slow spin speed
    If sas < 10 Then                        'Don't let soda stop spining
      sas = 10
    EndIf
  Else                                      'Must be spinning left
    sa = sa - sas                           'Subtract speed from spin angle
    If sa > 360 Then                        'Is angle above 360
      sa = sa - 360
    EndIf
    sas = sas + .5                          'Slow spin speed
    If sas > -10 Then                       'Don't let soda stop spinning
      sas = -10
    EndIf
  EndIf
  sys = sys + .75                           'Gravity to bring the can down
  sx = sx + sxs                             'Add to the soda position
  sy = sy + sys
  If sx < 0 Or sx > gw - 30 Then            'Did soda hit the sides
    sxs = -sxs
  EndIf
  If sy < 0 Then                            'Did soda hit the top
    sy = 0                                  'Keep soda on screen
    sys = -sys*.25                          'Send soda down at 1/4 speed
  EndIf
  If sy > gh Then                           'Did soda hit bottom
    Shapes.HideShape(can[sn])               'Hide the soda
    alive = alive - 1                       'Count as a miss
    showscore()
    newsoda()                               'Setup a new soda
  EndIf
  Shapes.Rotate(can[sn],sa)                 'Rotate soda can
  Shapes.Move(can[sn],sx,sy)                'Move soda can
EndSub

Sub sodahit
  If canhit = 4 Then                        'Was soda hit 5 times?
    Sound.PlayChimes()
    canhit = 0                              'Reset can hit count
    If doublescore > Clock.ElapsedMilliseconds Then
      score = score + 100*(cancount+1)
    Else
      score = score + 50*(cancount+1)       'Score big for hitting soda
    EndIf
    plunklife = 10                          'Give big plunk life
    Shapes.Move(plunk,sx+25,sy+50)
    Shapes.Rotate(plunk,Math.GetRandomNumber(360))
    Shapes.ShowShape(plunk)                 'Show plunk
    Shapes.HideShape(can[sn])
    If sn = 6 Then                          'Add 10 seconds to the slomolife
      slomolife = Clock.ElapsedMilliseconds + 10000
    EndIf
    If sn = 7 Then                          'Add 10 seconds to the doublescore life
      doublescore = Clock.ElapsedMilliseconds + 10000
    EndIf
    If sn = 8 Then
      addlife = Clock.ElapsedMilliseconds + 1000
      alive = alive + 1                     'Add 1 life
    EndIf
    cancount = cancount + 1                 'Add to cans destroyed count
    newsoda()
    showscore()
  Else
    Sound.PlayClick()
    sxs = Math.GetRandomNumber(20)-10       'Create new soda speed X
    sys = Math.GetRandomNumber(10)-25       'Create new soda speed Y
    sas = Math.GetRandomNumber(25)+40       'Create new soda spin speed
    If Math.GetRandomNumber(10) < 5 Then    'Create direction to spin
      sas = -sas
    EndIf
    canhit = canhit + 1                     'Can hit count
    If doublescore > Clock.ElapsedMilliseconds Then
      score = score + 20*(cancount+1)       'Add the double score for a hit
    Else
      score = score + 10*(cancount+1)       'Add to the regular score for the hit
    EndIf
    plinklife = 5                           'Give little plink life
    Shapes.Move(plink,cx,cy)
    Shapes.ShowShape(plink)                 'Show plink
    showscore()
  EndIf
EndSub

Sub showplink
  plinklife = plinklife - 1                 'Let plink show long enough to see
  If plinklife < 1 Then
    Shapes.HideShape(plink)
  EndIf
EndSub

Sub showplunk
  plunklife = plunklife - 1                 'Show plunk long enough to see it
  If plunklife < 1 Then
    Shapes.HideShape(plunk)
  EndIf
EndSub

Sub showscore
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FillRectangle(gw*.2,10,gw-gw*.38,25)
  If slomolife > Clock.ElapsedMilliseconds Then
    GraphicsWindow.BrushColor = "Red"
  ElseIf doublescore > Clock.ElapsedMilliseconds then
    GraphicsWindow.BrushColor = "Orange"
  Else
    GraphicsWindow.BrushColor = "Blue"
  EndIf
  GraphicsWindow.FontSize = 20
  GraphicsWindow.DrawText(gw*.22,10,"SCORE " + score)
  GraphicsWindow.DrawText(gw*.4,10,"CANS DESTROYED " + cancount)
  If addlife > Clock.ElapsedMilliseconds Then
    GraphicsWindow.BrushColor = "SpringGreen"
  EndIf
  GraphicsWindow.DrawText(gw*.7,10,"LIVES " + alive)
EndSub

Copyright (c) Microsoft Corporation. All rights reserved.