Microsoft Small Basic

Program Listing: ZPJ862-0
' Snowman 0.2
' Copyright © 2012-2015 Nonki Takahashi. The MIT License.
'
' History:
' 0.2 2015-02-05 Renamed Shapes_Snowman() to Shapes_Init().
' 0.1 2012-11-27 Created. (ZPJ862)
' 0.0 2012-11-27 19:14:04 Generated by Shapes 1.5
'
GraphicsWindow.Title = "Snowman 0.2"
GraphicsWindow.BackgroundColor = "#939393"
' initialize shapes
Shapes_Init()
' add shapes
scale = 1
Shapes_Add()
eyesOpen = "True"
Timer.Interval = 2900
Timer.Tick = EyeBlink
angle = 0
GraphicsWindow.PenWidth = 2
GraphicsWindow.PenColor = "White"
level = 1
maxlevel = 2
arm = 6
ratio = 0.4
ymax = 73 + 256
imin = 1
fall = 20
For imax = 1 To 1000
xc[imax] = Math.GetRandomNumber(GraphicsWindow.Width)
yc[imax] = Math.GetRandomNumber(20)
AddSnow()
FallSnow()
EndFor
Sub EyeBlink
Timer.Pause()
If eyesOpen Then
Shapes.HideShape(shape[7]["obj"])
Shapes.HideShape(shape[8]["obj"])
Timer.Interval = 100
eyesOpen = "False"
Else
Shapes.ShowShape(shape[7]["obj"])
Shapes.ShowShape(shape[8]["obj"])
Timer.Interval = 2900
eyesOpen = "True"
EndIf
Timer.Resume()
EndSub
Sub FallSnow
For i = imin To imax
If yc[i] < ymax Then
vertical = Math.GetRandomNumber(20) - 10
xc[i] = xc[i] + vertical
yc[i] = yc[i] + fall
For k = 1 To 3
Shapes.Move(oBeam[i + "_" + k], xc[i], yc[i])
i1 = i + "_" + k + "1"
xc[i1] = xc[i1] + vertical
yc[i1] = yc[i1] + fall
i2 = i + "_" + k + "2"
xc[i2] = xc[i2] + vertical
yc[i2] = yc[i2] + fall
For l = 1 To 3
Shapes.Move(oBeam[i1 + "_" + l], xc[i1], yc[i1])
Shapes.Move(oBeam[i2 + "_" + l], xc[i2], yc[i2])
EndFor
EndFor
Else
imin = i + 1
EndIf
EndFor
EndSub
Sub AddSnow
' param xc[i], yc[i] - center
' param arm - arm length
' param ratio - arm length ratio
x1 = - arm
x2 = arm
y1 = 0
y2 = 0
oBeam[i + "_1"] = Shapes.AddLine(x1, y1, x2, y2)
Shapes.Move(oBeam[i + "_1"], xc[i], yc[i])
Stack.PushValue("local", level)
Stack.PushValue("local", arm)
Stack.PushValue("local", xc[i])
Stack.PushValue("local", yc[i])
level = level + 1
arm = arm * ratio
If level <= maxlevel Then
Stack.PushValue("local", xc[i])
Stack.PushValue("local", yc[i])
xc[i + "_11"] = xc[i] + x1
yc[i + "_11"] = yc[i] + y1
Stack.PushValue("local", x2)
Stack.PushValue("local", y2)
Stack.PushValue("local", i)
i = i + "_11"
AddSnow() ' recursive call
i = Stack.PopValue("local")
y2 = Stack.PopValue("local")
x2 = Stack.PopValue("local")
yc[i] = Stack.PopValue("local")
xc[i] = Stack.PopValue("local")
xc[i + "_12"] = xc[i] + x2
yc[i + "_12"] = yc[i] + y2
Stack.PushValue("local", i)
i = i + "_12"
AddSnow() ' recursive call
i = Stack.PopValue("local")
EndIf
yc[i] = Stack.PopValue("local")
xc[i] = Stack.PopValue("local")
arm = Stack.PopValue("local")
level = Stack.PopValue("local")
x1 = arm * Math.Cos(Math.Pi / 3)
x2 = -1 * arm * Math.Cos(Math.Pi / 3)
y1 = arm * Math.Sin(Math.Pi / 3)
y2 = -1 * arm * Math.Sin(Math.Pi / 3)
oBeam[i + "_2"] = Shapes.AddLine(x1, y1, x2, y2)
Shapes.Move(oBeam[i + "_2"], xc[i], yc[i])
Stack.PushValue("local", level)
Stack.PushValue("local", arm)
Stack.PushValue("local", xc[i])
Stack.PushValue("local", yc[i])
level = level + 1
arm = arm * ratio
If level <= maxlevel Then
Stack.PushValue("local", xc[i])
Stack.PushValue("local", yc[i])
xc[i + "_21"] = xc[i] + x1
yc[i + "_21"] = yc[i] + y1
Stack.PushValue("local", x2)
Stack.PushValue("local", y2)
Stack.PushValue("local", i)
i = i + "_21"
AddSnow() ' recursive call
i = Stack.PopValue("local")
y2 = Stack.PopValue("local")
x2 = Stack.PopValue("local")
yc[i] = Stack.PopValue("local")
xc[i] = Stack.PopValue("local")
xc[i + "_22"] = xc[i] + x2
yc[i + "_22"] = yc[i] + y2
Stack.PushValue("local", i)
i = i + "_22"
AddSnow() ' recursive call
i = Stack.PopValue("local")
EndIf
yc[i] = Stack.PopValue("local")
xc[i] = Stack.PopValue("local")
arm = Stack.PopValue("local")
level = Stack.PopValue("local")
x1 = -1 * arm * Math.Cos(Math.Pi / 3)
x2 = arm * Math.Cos(Math.Pi / 3)
y1 = arm * Math.Sin(Math.Pi / 3)
y2 = -1 * arm * Math.Sin(Math.Pi / 3)
oBeam[i + "_3"] = Shapes.AddLine(x1, y1, x2, y2)
Shapes.Move(oBeam[i + "_3"], xc[i], yc[i])
Stack.PushValue("local", level)
Stack.PushValue("local", arm)
Stack.PushValue("local", xc[i])
Stack.PushValue("local", yc[i])
level = level + 1
arm = arm * ratio
If level <= maxlevel Then
Stack.PushValue("local", xc[i])
Stack.PushValue("local", yc[i])
xc[i + "_31"] = xc[i] + x1
yc[i + "_31"] = yc[i] + y1
Stack.PushValue("local", x2)
Stack.PushValue("local", y2)
Stack.PushValue("local", i)
i = i + "_31"
AddSnow() ' recursive call
i = Stack.PopValue("local")
y2 = Stack.PopValue("local")
x2 = Stack.PopValue("local")
yc[i] = Stack.PopValue("local")
xc[i] = Stack.PopValue("local")
xc[i + "_32"] = xc[i] + x2
yc[i + "_32"] = yc[i] + y2
Stack.PushValue("local", i)
i = i + "_32"
AddSnow() ' recursive call
i = Stack.PopValue("local")
EndIf
yc[i] = Stack.PopValue("local")
xc[i] = Stack.PopValue("local")
arm = Stack.PopValue("local")
level = Stack.PopValue("local")
EndSub
Sub Shapes_Init
' Shapes | Initialize snowman's shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 0 ' x offset
shY = 73 ' y offset
shape = ""
shape[1] = "func=rect;x=383;y=197;width=38;height=15;angle=29;bc=#68432C;pw=0;"
shape[2] = "func=rect;x=0;y=257;width=624;height=113;bc=#FFFFFF;pw=0;"
shape[3] = "func=ell;x=415;y=136;width=84;height=73;bc=#FFFFFF;pw=0;"
shape[4] = "func=ell;x=399;y=190;width=115;height=97;bc=#FFFFFF;pw=0;"
shape[5] = "func=rect;x=433;y=167;width=16;height=6;bc=#000000;pw=0;"
shape[6] = "func=rect;x=465;y=167;width=16;height=6;bc=#000000;pw=0;"
shape[7] = "func=ell;x=432;y=160;width=18;height=19;bc=#000000;pw=0;"
shape[8] = "func=ell;x=464;y=160;width=18;height=19;bc=#000000;pw=0;"
shape[9] = "func=rect;x=446;y=187;width=21;height=5;bc=#000000;pw=0;"
EndSub
Sub Shapes_Add
' Shapes | Add shapes as shapes data
' param shape - array of shapes
' param scale - to zoom
' return nShapes - number of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
nShapes = Array.GetItemCount(shape)
s = scale
For i = 1 To nShapes
GraphicsWindow.PenWidth = shape[i]["pw"] * s
If shape[i]["pw"] > 0 Then
GraphicsWindow.PenColor = shape[i]["pc"]
EndIf
If shape[i]["func"] = "rect" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
shape[i]["obj"] = Shapes.AddRectangle(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "ell" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
shape[i]["obj"] = Shapes.AddEllipse(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "tri" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
shape[i]["obj"] = Shapes.AddTriangle(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s, shape[i]["x3"] * s, shape[i]["y3"] * s)
ElseIf shape[i]["func"] = "line" Then
shape[i]["obj"] = Shapes.AddLine(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s)
EndIf
Shapes.Move(shape[i]["obj"], shX + shape[i]["x"] * s, shY + shape[i]["y"] * s)
If Text.IsSubText("rect|ell|tri", shape[i]["func"]) And shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
shape[i]["rx"] = shape[i]["x"]
shape[i]["ry"] = shape[i]["y"]
EndFor
shAngle = 0
i = Stack.PopValue("local")
EndSub