Microsoft Small Basic

Program Listing: mdw482
' make a pyramid by NaochanON
' Challenge of the month July 2012

Init()
makecubes()
input()

Controls.ButtonClicked=movecubes

Sub movecubes
If MM>0 Then
reposition() ' hide (remove) cubes
EndIf
div=30 ' like animation dividing
MM= Controls.GetTextBoxText(box) ' step number
If MM>11 Then
MM=10
ElseIf MM<2 then
MM=2
EndIf
'-------------------------------------------------- stacking ---------------------------------------------
st=1
for N=MM To 1 Step -1
for i=st To st+N*N-1
NewX=X1+(MM-N)*R0/4+R0*(Math.Remainder((i-st),N))-R0*(Math.Floor((i-st)/N))/2
NewY=Y1-(MM-N)*3*R0/4+R0*(Math.Floor((i-st)/N))/2
Stacking()
Sound.PlayClick()
EndFor
st=st+N*N
endfor
EndSub

Sub stacking
dx=(NewX-X0)/div ' moving length
dy=(NewY-Y0)/div
For j=1 To div
X=X0+dx*j
Y=Y0+dy*j
Shapes.Move(a[i],x,y)
Shapes.Move(b[i],x,y)
Shapes.Move(c[i],x,y)
Shapes.Move(d[i],x+R0,y)
Shapes.Move(e[i],x+R0,y)
Shapes.Move(f[i],x+dxy,y-dxy)
Shapes.Move(g[i],x+R0,y)
Shapes.Move(h[i],x+R0+dxy,y-dxy)
Program.Delay(10)
EndFor
EndSub

Sub Init
GraphicsWindow.Hide()
GraphicsWindow.BackgroundColor="darkgreen"
GraphicsWindow.Width=1000
GraphicsWindow.Height=700
GraphicsWindow.Left=50
GraphicsWindow.Top=20
'---------------------------------- image photo ---------------------------------------------------
url="http://farm5.static.flickr.com/4012/4325427343_3154b886ca.jpg"
'url="http://farm6.static.flickr.com/5181/5631039815_142b9082b7.jpg"
image= ImageList.LoadImage(url)
GraphicsWindow.DrawResizedImage(image,0,0,1000,700)
EndSub

Sub makecubes
'---------------------------------- Make Cubes ---------------------------------------------------
R0=50 ' cube size
dxy=25 ' dx dy
X0= 500 ' initial position X Y
y0= -100
sum=385 ' 385= 1+2^2+3^2+....... 10^2
For i=1 To sum
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"
f[i]= Shapes.AddLine(0,0,R0,0) ' --
Shapes.Move(f[i],X0+dxy,Y0-dxy)
g[i]= Shapes.AddLine(0,0,dxy,-dxy) ' /
Shapes.Move(g[i],X0+R0,y0)
h[i]= Shapes.AddLine(0,0,0,R0) ' |
Shapes.Move(h[i],X0+R0+dxy,y0-dxy)
EndFor
GraphicsWindow.Show()
X1=400 ' pyramid starting position X , Y
Y1=400
endsub

Sub input
'---------------------------------- Step Start button-------------------------------
GraphicsWindow.FontSize=18
box= Controls.AddTextBox(330,20)
Controls.SetSize(box,40,30)
GraphicsWindow.BrushColor="Yellow"
msg=shapes.AddText(" How many steps? 2-10 ")
Shapes.Move(msg,50,20)
start= Controls.AddButton("Start",400,20)
endsub

Sub reposition
newY=-100
div=1
sum=0
For i=1 To MM
sum=sum+i*i
EndFor
For i=1 To sum
stacking()
EndFor
EndSub