Microsoft Small Basic

Program Listing:
Embed this in your website
' Challenge of the Month - August 2015 Draw a Shark by NaochanON
'===========================================================================
'Permission to Apply Gradient Background | Jibba Jabba | http://smallbasic.com/program/?NBL543

defaultPenWidth = GraphicsWindow.PenWidth 'need for gradient b/ground

GUI()

DrawBackground() 'JJ

While "true"
  start = Clock.ElapsedMilliseconds
  NN=NN+1
  If Math.Remainder(NN,15)=0 then
    ddy=3- Math.GetRandomNumber(6)
  endif

  If Shapes.GetTop(SHP[1]) >= GraphicsWindow.Height-110 Then 'JJ - ensure for browser
    ddy = -6
  EndIf
  '-------------------------------------------------------------------------------------------------------------------------
  MaxLeft=0
  For j= 1 To NMB
    Shapes.Move( Sardine[j],Shapes.GetLeft(Sardine[j])-5,Shapes.GetTop(Sardine[j])+ddy)
    If Shapes.GetTop(Sardine[j])<80 then
      Shapes.Move( Sardine[j],Shapes.GetLeft(Sardine[j])-5,80+math.GetRandomNumber(30))
    endif
  endfor
  '-------------------------------------------------------------------------------------------------------------------------
  For i = Array.GetItemCount(Shark) To 1 Step -1 'JJ reversed
    If Shapes.GetTop(SHP[1])<80 then
      ddy=0
    endif
    Shapes.Move(SHP[i],Shapes.GetLeft(SHP[i])-5,Shapes.GetTop(SHP[i])+ddy+1)
  EndFor
  If ddy>0 then
    For ii=10 to 13
      Shapes.Rotate(SHP[ii],15)
    endfor
  Else
    For ii=10 to 13
      Shapes.Rotate(SHP[ii],-5)
    endfor
  endif
  If Shapes.GetLeft(SHP[1])<-200 Then
    Sardine_Right()
    shark_Right()
  EndIf
  '-------------------------------------------------------------------------------------------------------------------------
  bbl_Move()

  delay = 25 - (Clock.ElapsedMilliseconds - start) 'JJ added 10ms
  If delay > 0 Then
    Program.Delay(delay)
  EndIf
endwhile

Sub GUI
  'GraphicsWindow.BackgroundColor="Lightcyan"
  GraphicsWindow.width=1200
  GraphicsWindow.height=700
  GraphicsWindow.Top=0
  GraphicsWindow.Left=0
  '-------------------------------------------------------------------------------------------------------------------------
  GraphicsWindow.BrushColor="Blue"
  GraphicsWindow.PenColor="blue"
  GraphicsWindow.PenWidth=2

  Goto down
  For i=5 To 1 Step -1
    sblue[i]= Shapes.AddRectangle(1400,42*i)          ' stripe
    y=y+i
    Shapes.Move(sblue[i],0,720-42*y)
    Shapes.SetOpacity(sblue[i],15*i)
  EndFor
  down:
  '-------------------------------------------------------------------------------------------------------------------------
  GraphicsWindow.BrushColor="White"
  GraphicsWindow.PenColor="White"
  GraphicsWindow.PenWidth=3
  For k=1 To 5
    For L=1 To 6
      By[k][L]=800
      sbbl[k][L]=Shapes.AddEllipse(L+4,L+4)               ' bubbles
      bx[k][L]=250*(K-1)+30*Math.GetRandomNumber(L)
      Shapes.Move(sbbl[k][L],bx[k][L],By[k][L])
    EndFor
  EndFor
  '-------------------------------------------------------------------------------------------------------------------------
  GraphicsWindow.BrushColor="Blue"
  GraphicsWindow.PenColor="Blue"
  NMB= 50  '80
  For j=1 To NMB
    w= 15+math.GetRandomNumber(10)
    Sardine[j]=Shapes.AddEllipse(w,w*0.2)         ' Sardines
  EndFor
  Sardine_Right()
  '-------------------------------------------------------------------------------------------------------------------------
  Shark[1] = "fn=rc;x=353;y=87;w=43;h=34;angle=25;bc=#2792C3;pc#2792C3;pw=2;"    ' fin-1
  Shark[2] = "fn=rc;x=273;y=192;w=43;h=34;angle=-20;bc=#2792C3;pc#2792C3;pw=2;"  ' fin -2
  Shark[3] = "fn=rc;x=353;y=172;w=43;h=34;angle=-30;bc=#2792C3;pc#2792C3;pw=2;"  ' fin-3
  Shark[4] = "fn=el;x=64;y=80;w=352;h=130;bc=#A0D8EF;pc#2792C3;pw=2;"            ' body white
  Shark[5] =  "fn=el;x=70;y=83;w=342;h=100;bc=#2792C3;pc#2792C3;pw=2;"           ' body navy
  Shark[6] = "fn=tr;x=0;y=82;x1=0;y1=60;x2=95;y2=25;x3=80;y3=90;angle=0;bc=#2792C3;pc=#2792C3;pw=2;"  ' Head
  Shark[7] = "fn=el;x=33;y=130;w=16;h=12;bc=#FFEA00;pc#2792C3;pw=2;"              ' eye
  Shark[8] = "fn=ln;x=36;y=128;x1=0;y1=0;x2=8;y2=14;pc=#222C3A;bc=#FFFFF3;pw=2;"  ' eye
  Shark[9] = "fn=tr;x=170;y=0;x1=68;y1=0;x2=0;y2=89;x3=85;y3=89;angle=0;bc=#2792C3;pc=#2792C3;pw=2;"      ' Dorsal fin
  Shark[10] = "fn=tr;x=400;y=70;x1=140;y1=0;x2=0;y2=78;x3=58;y3=78;angle=0;bc=#2792C3;pc=#2792C3;pw=2;"   ' upperCaudal fin
  Shark[11] = "fn=tr;x=440;y=70;x1=100;y1=0;x2=0;y2=78;x3=28;y3=78;angle=0;bc=#A0D8EF;pc#A0D8EF;pw=2;"    ' Caudal fin white
  Shark[12] = "fn=tr;x=400;y=148;x1=0;y1=0;x2=70;y2=0;x3=95;y3=50;angle=0;bc=#A0D8EF;pc#2792C3;pw=2;"     ' underCaudal fin
  Shark[13] = "fn=tr;x=135;y=160;x1=0;y1=20;x2=50;y2=0;x3=66;y3=122;angle=0;bc=#2992C3;pc#2792C3;pw=2;"   ' Pelvic fin
  Shark[14] = "fn=tr;x=30;y=148;x1=0;y1=5;x2=40;y2=0;x3=10;y3=10;angle=0;bc=#A0D8EF;pc#2792C3;pw=2;"      ' mouth
  Shark[15] = "fn=tr;x=356;y=94;x1=0;y1=0;x2=80;y2=46;x3=0;y3=75;angle=0;bc=#2792C3;pc=#2792C3;pw=2;"     ' tail
  s=0.5 ' 0.4+ (2-math.GetRandomNumber(4))*0.1
  For i = 1 To Array.GetItemCount(Shark)
    GraphicsWindow.Penwidth = Shark[i]["pw"]*s
    GraphicsWindow.PenColor = Shark[i]["pc"]
    GraphicsWindow.BrushColor = Shark[i]["bc"]
    If Shark[i]["fn"] = "rc" Then
      SHP[i] = shapes.Addrectangle(Shark[i]["w"]*s, Shark[i]["h"]*s)
    ElseIf Shark[i]["fn"] = "el" Then
      SHP[i] = shapes.Addellipse(Shark[i]["w"]*s, Shark[i]["h"]*s)
    ElseIf Shark[i]["fn"] = "tr" Then
      SHP[i]= Shapes.Addtriangle(Shark[i]["x1"]*s, Shark[i]["y1"]*s, Shark[i]["x2"]*s, Shark[i]["y2"]*s, Shark[i]["x3"]*s, Shark[i]["y3"]*s)
    ElseIf Shark[i]["fn"] = "ln" Then
      SHP[i]= Shapes.AddLine(Shark[i]["x1"]*s, Shark[i]["y1"]*s, Shark[i]["x2"]*s, Shark[i]["y2"]*s)
    EndIf
    shapes.Move(SHP[i], Shark[i]["x"]*s, Shark[i]["y"]*s)
    If Text.IsSubText("rc:el:tr", Shark[i]["fn"]) And Shark[i]["angle"] <> 0 Then
      Shapes.Rotate(SHP[i], Shark[i]["angle"])
    EndIf
    pos[i]["X"]=Shapes.GetLeft(SHP[i])
    pos[i]["Y"]=Shapes.Gettop(SHP[i])
  EndFor
  shark_Right()
EndSub

Sub Sardine_Right
  Rposy=100+Math.GetRandomNumber(200)
  For j=1 To NMB
    R=20+math.GetRandomNumber(150)
    X=1250+R*math.Cos(math.GetRadians(j*3.6))
    Y=R*math.sin(math.GetRadians(j*3.6))*0.2
    Shapes.Move( Sardine[j],X,Y+Rposy)
  EndFor
EndSub

Sub shark_Right
  dx= 100+Math.GetRandomNumber(500)
  dy= 50+Math.GetRandomNumber(200)
  For i = 1 To Array.GetItemCount(Shark)
    Shapes.Move(SHP[i],1300+dx+pos[i]["X"],100+dy+pos[i]["Y"])
  EndFor
EndSub

Sub bbl_Move
  For k=1 To 5
    For L=1 To 6
      bdy[k][L]=Math.GetRandomNumber(5)
      By[k][L]=By[k][L]-bdy[k][L]
      Shapes.Move(sbbl[k][L],bx[k][L],By[k][L])      ' bubble go up
      If By[k][L] < GraphicsWindow.Height * (1-oceanDepth_pc)+3 Then 'JJ
        By[k][L]=720
        Shapes.Move(sbbl[k][L],bx[k][L],By[k][L])   ' new place
      EndIf
    EndFor
  EndFor
EndSub

'=============================================================================
'Add On - Background
'=============================================================================
Sub DrawBackground
  'GraphicsWindow.CanResize = "False"
  GraphicsWindow.Title = "SWIMMING SHARK with Sardines. [NaochanON]"

  'Browser and local
  If Text.StartsWith(Program.Directory "http") Then
    GraphicsWindow.Width = 640/480 * GraphicsWindow.Height
    GraphicsWindow.Height = 480/640 * GraphicsWindow.Width
  EndIf

  GraphicsWindow.PenWidth = defaultPenWidth

  'Maintain Smooth Gradient - default browser height = 480
  GraphicsWindow.PenWidth = GraphicsWindow.PenWidth * GraphicsWindow.Height / 480

  colorDepth = 220  '255 = white
  oceanDepth_pc = 89 / 100 '% to GW.height
  gradientHeight = (GraphicsWindow.Height * oceanDepth_pc) / colorDepth

  For i = 1 To colorDepth
    GraphicsWindow.PenColor = GraphicsWindow.GetColorFromRGB(i i 255)
    GraphicsWindow.DrawRectangle(0, GraphicsWindow.Height - gradientStep, GraphicsWindow.Width, gradientHeight)  '
    gradientStep = gradientStep + gradientHeight
  EndFor

  GraphicsWindow.PenColor = "black"
  GraphicsWindow.PenWidth = 5
  wLength = 10
  For i = 0 To GraphicsWindow.Width / wLength + 1
    Y = 3 * Math.Sin(i) + GraphicsWindow.Height - gradientStep
    GraphicsWindow.DrawLine((i-1) * wLength, lastY, i * wLength, Y)
    lastY = Y
  EndFor

  GraphicsWindow.BrushColor = "black"  'is sky
  GraphicsWindow.FillRectangle(0, 0, GraphicsWindow.Width, (GraphicsWindow.Height * (1-oceanDepth_pc)))

  GraphicsWindow.BrushColor = "white"  'are stars
  For i = 1 To 10
    X = Math.GetRandomNumber(GraphicsWindow.Width - 25)
    Y = Math.GetRandomNumber(GraphicsWindow.Height * (1-oceanDepth_pc) - 25)

    star[i] = Shapes.AddText("*")  'using Shapes for browser
    Shapes.Move(star[i] X Y)
    scale = 0.5 + Math.GetRandomNumber(10)/10
    Shapes.Zoom(star[i] scale scale)
  EndFor
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.