Microsoft Small Basic

Program Listing: ngn430
' Challenge of the month May 2013 // 30 second animation by NaochanON
' Moving ant // search a suger cube

makecubes()
Shapes_Init()
Shapes_Add()
CenterPosition()
t0=clock.ElapsedMilliseconds

While distance>90
Searchsugar() ' search angle to sugar
MovL= 50+Math.GetRandomNumber(30) ' random moving length
moveant() ' ant moves
XYC="X="+(Shapes.GetLeft(SHP[25]["obj"])-25)+";Y="+ (Shapes.Gettop(SHP[25]["obj"])+25) ' ant rotating center position
antdistance() ' distance from ant to sugar
Program.Delay(30)
endwhile
disappear()

Sub disappear
MovL=Math.SquareRoot(Math.Power((x[25]-X0),2)+Math.Power((y[25]-y0),2)) ' moves to sugar
moveant()
MovL=Math.SquareRoot(Math.Power((-200-X0),2)+Math.Power((y[25]-y0),2)) ' moves to disappear
moveant()
endsub

Sub antdistance
x[25]=Shapes.GetLeft(SHP[25]["obj"])+SHP[25]["width"]*s/2
y[25]=Shapes.Gettop(SHP[25]["obj"])+SHP[25]["height"]*s/2
distance=Math.SquareRoot(Math.Power((x[25]-X0),2)+Math.Power((y[25]-y0),2)) ' current distance
EndSub

Sub moveant
ddx=-MovL*Math.Cos(Math.GetRadians(targetangle))/50
ddy=-MovL*Math.Sin(Math.GetRadians(targetangle))/25
For k= 1 To 40
For i=1 To Array.GetItemCount(SHP)
Shapes.Move(SHP[i]["obj"],Shapes.GetLeft(SHP[i]["obj"])+ddx,shapes.GetTop(SHP[i]["obj"])+ddy)
EndFor
GraphicsWindow.DrawEllipse(x[25],y[25],5,5) ' marking
elapsedtime=Math.Floor((Clock.ElapsedMilliseconds-t0)/100)/10
Shapes.SetText(msg, "Ant search a sugar cube... "+elapsedtime+" sec") ' show elapsedtime
Program.Delay(40)
EndFor
EndSub

Sub Searchsugar
deg=deg0+(30- Math.GetRandomNumber(60)) ' random direction
if distance deg=math.GetDegrees(Math.ArcTan( (XYC["Y"]-Y0) / (XYC["X"]-X0) )) ' angle to sugar
EndIf
targetangle= math.Floor(deg*10)/10 ' temporary target angle
s1=s
For NN=NN0 To targetangle Step 2*(targetangle/Math.Abs(targetangle)) ' step .... -0.5 or +0.5
For i= 1 To Array.GetItemCount(SHP)
newX[i]=DR[i]*Math.Cos(Math.GetRadians(DS0[i]+NN))+XYC["X"] ' new position .. X //center of the shape
newY[i]=DR[i]*Math.sin(Math.GetRadians(DS0[i]+NN))+XYC["Y"] ' new position .. Y //center of the shape
Shapes.Move(SHP[i]["obj"],newx[i]-shp[i]["width"]*s1/2, newY[i]-shp[i]["height"]*s1/2) ' move the shape to Left:Top position
Shapes.Rotate(SHP[i]["obj"],NN+SHP[i]["angle"]) ' rotation
endfor
Program.Delay(5)
EndFor
NN0=targetangle ' start angle
EndSub

Sub CenterPosition
For i= 1 To array.GetItemCount(SHP)
X[i]=Shapes.GetLeft(SHP[i]["obj"])+SHP[i]["width"]*s/2 ' center of the shape ... X
Y[i]=Shapes.GetTop(SHP[i]["obj"])+SHP[i]["height"]*s/2 ' center of the shape ... Y
DS0[i]=math.GetDegrees(Math.ArcTan((Y[i]-XYC["Y"])/(X[i]-XYC["X"]))) ' degree // angle of the shape_Center from XYC position
DR[i]=Math.SquareRoot((X[i]-XYC["X"])*(X[i]-XYC["X"])+(Y[i]-XYC["Y"])*(Y[i]-XYC["Y"])) ' radius
endfor
distance=Math.SquareRoot(Math.Power((x[25]-X0),2)+Math.Power((y[25]-y0),2))
distance0=distance ' distance from ant to sugar cube
endsub

Sub Shapes_Add
GraphicsWindow.Left=30
GraphicsWindow.Top=50
GraphicsWindow.Width=1250
GraphicsWindow.Height=650
GraphicsWindow.BackgroundColor="Lightcyan"
GraphicsWindow.FontSize=25
msg= Shapes.AddText(" Ant search a sugar cube ")
Shapes.Move(msg,300,20)
For i = 1 To Array.GetItemCount(SHP)
GraphicsWindow.PenWidth = SHP[i]["pw"]*s
GraphicsWindow.PenColor = SHP[i]["pc"]
GraphicsWindow.BrushColor = SHP[i]["bc"]
If SHP[i]["func"] = "rect" Then
SHP[i]["obj"] = Shapes.AddRectangle(SHP[i]["width"]*s, SHP[i]["height"]*s)
ElseIf SHP[i]["func"] = "ell" Then
SHP[i]["obj"] = Shapes.AddEllipse(SHP[i]["width"]*s, SHP[i]["height"]*s)
EndIf
Shapes.Move(SHP[i]["obj"], shX + SHP[i]["x"]*s, shY + SHP[i]["y"]*s)
Shapes.Rotate(SHP[i]["obj"], SHP[i]["angle"])
EndFor
'-------------------------------------------------------------------------------------------------------------------------
XYC="X="+(Shapes.GetLeft(SHP[25]["obj"])-10)+";Y="+ (Shapes.Gettop(SHP[25]["obj"])+10)
deg0= math.GetDegrees(Math.ArcTan( (XYC["Y"]-Y0) / (XYC["X"]-X0) ))
'-------------------------------------------------------------------------------------------------------------------------
GraphicsWindow.PenColor="Red"
GraphicsWindow.PenWidth=3
EndSub

Sub makecubes
dxy=10 ' dx dy
R0=2*dxy ' cube size
X0= 200 ' initial position X Y
y0= 450
GraphicsWindow.penColor="blue"
GraphicsWindow.BrushColor="lightcyan"
GraphicsWindow.PenWidth=1
a[i]= Shapes.AddRectangle(R0,R0) ' rectangle
Shapes.Move(a[i],x0,y0)
GraphicsWindow.penColor="Lightgray"
GraphicsWindow.BrushColor="lightgray"
b[i]= Shapes.AddTriangle(0,0,R0,0,dxy,-dxy) ' top
Shapes.Move(b[i],x0,y0)
c[i]= Shapes.AddTriangle(dxy,-dxy,R0+dxy,-dxy,R0,0) ' top
Shapes.Move(c[i],x0,y0)
GraphicsWindow.penColor="gray"
GraphicsWindow.BrushColor="gray"
d[i]= Shapes.AddTriangle(0,0,dxy,-dxy,0,R0) ' side
Shapes.Move(d[i],x0+R0,y0)
e[i]= Shapes.AddTriangle(dxy,-dxy,dxy,R0-dxy,0,R0) ' side
Shapes.Move(e[i],x0+R0,y0)
GraphicsWindow.penColor="Blue"
endsub

Sub Shapes_Init
s=0.25
shX = 1200 ' x offset
shY = 55 ' y offset
'------------------------------------------------------------------------------------------------------------------------------------
SHP[1] = "func=ell;x=11;y=103;width=30;height=7;angle=352;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 1 1 feet
SHP[2] = "func=ell;x=30;y=85;width=44;height=8;angle=308;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 1 2 shin
SHP[3] = "func=ell;x=64;y=72;width=48;height=7;angle=5;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 1 3 thighs
'------------------------------------------------------------------------------------------------------------------------------------
SHP[4] = "func=ell;x=112;y=61;width=43;height=9;angle=330;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 2 1
SHP[5] = "func=ell;x=137;y=72;width=44;height=8;angle=63;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 2 2
SHP[6] = "func=ell;x=167;y=98;width=31;height=6;angle=22;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 2 3
'------------------------------------------------------------------------------------------------------------------------------------
SHP[7] = "func=ell;x=142;y=63;width=41;height=11;angle=330;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 3 1
SHP[8] = "func=ell;x=169;y=77;width=60;height=8;angle=46;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 3 2
SHP[9] = "func=ell;x=219;y=102;width=31;height=6;angle=7;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 3 3
'------------------------------------------------------------------------------------------------------------------------------------
SHP[10] = "func=ell;x=65;y=56;width=59;height=24;angle=8;bc=#000000;pc=#969B8A;pw=2;" ' body 1
SHP[11] = "func=ell;x=119;y=65;width=50;height=22;angle=4;bc=#000000;pc=#969B8A;pw=2;" ' body 2
SHP[12] = "func=ell;x=166;y=58;width=76;height=31;angle=357;bc=#000000;pc=#969B8A;pw=2;" ' body 3
'------------------------------------------------------------------------------------------------------------------------------------
SHP[13] = "func=ell;x=0;y=116;width=40;height=7;angle=352;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 4 1
SHP[14] = "func=ell;x=16;y=83;width=56;height=10;angle=284;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 4 2
SHP[15] = "func=ell;x=50;y=64;width=69;height=9;angle=14;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 4 3
'------------------------------------------------------------------------------------------------------------------------------------
SHP[16] = "func=ell;x=135;y=72;width=61;height=10;angle=351;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 5 1
SHP[17] = "func=ell;x=169;y=95;width=64;height=11;angle=79;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 5 2
SHP[18] = "func=ell;x=205;y=133;width=44;height=8;angle=22;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 5 3
'------------------------------------------------------------------------------------------------------------------------------------
SHP[19] = "func=ell;x=119;y=63;width=33;height=9;angle=330;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 6 1
SHP[20] = "func=ell;x=124;y=86;width=64;height=11;angle=83;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 6 2
SHP[21] = "func=ell;x=158;y=122;width=31;height=6;angle=17;bc=#969B8A;pc=#9A7869;pw=2;" ' leg 6 3
'------------------------------------------------------------------------------------------------------------------------------------
SHP[22] = "func=rect;x=55;y=49;width=19;height=7;angle=36;bc=#000000;pc=#9A7869;pw=2;" ' neck
SHP[23] = "func=ell;x=20;y=36;width=45;height=23;angle=352;bc=#000000;pc=#969B8A;pw=2;" ' head
SHP[24] = "func=ell;x=35;y=42;width=9;height=9;bc=#969B8A;pc=#9A7869;pw=2;" ' eye
SHP[25] = "func=ell;x=9;y=50;width=17;height=7;angle=352;bc=#000000;pc=#969B8A;pw=2;" ' mouth
'------------------------------------------------------------------------------------------------------------------------------------
SHP[26] = "func=rect;x=21;y=29;width=19;height=7;angle=32;bc=#000000;pc=#9A7869;pw=2;" ' Tentacles 1
SHP[27] = "func=rect;x=11;y=17;width=19;height=7;angle=75;bc=#000000;pc=#9A7869;pw=2;"
SHP[28] = "func=rect;x=2;y=12;width=19;height=5;angle=337;bc=#000000;pc=#9A7869;pw=2;"
'------------------------------------------------------------------------------------------------------------------------------------
SHP[29] = "func=rect;x=32;y=26;width=19;height=7;angle=65;bc=#000000;pc=#9A7869;pw=2;" ' Tentacles 2
SHP[30] = "func=rect;x=23;y=8;width=25;height=7;angle=75;bc=#000000;pc=#9A7869;pw=2;"
SHP[31] = "func=rect;x=19;y=0;width=16;height=6;angle=337;bc=#000000;pc=#9A7869;pw=2;"
EndSub