Microsoft Small Basic

Program Listing: NKC523
' Thunder 0.1
' Copyright (c) 2012 Nonki Takahashi. All rights reserved.
'
' History:
' 0.1 2012/11/28 Created.
' 0.0 2012/09/23 0:53:03 TB1 Generated by Shapes 1.31
'
GraphicsWindow.Title = "Thunder 0.1"
GraphicsWindow.BackgroundColor = "Black"
' initialize shapes
Shapes_Init()
' add shapes
scale = 0.4
Shapes_Add()
angle = 90
Shapes_Rotate()
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "White"
flash = Shapes.AddRectangle(gw, gh)
Shapes.SetOpacity(flash, 0)
times = 0
While "True"
times = times + 1
For o = 0 To 100 Step 10
Shapes.SetOpacity(flash, o)
Program.Delay(10)
EndFor
xLast = Math.GetRandomNumber(gw)
yLast = 0
GraphicsWindow.PenWidth = 4
GraphicsWindow.PenColor = "White"
For y = 100 To gh Step 20
n = n + 1
x = xLast + 50 - Math.GetRandomNumber(100)
thunder[n] = Shapes.AddLine(xLast, yLast, x, y)
xLast = x
yLast = y
EndFor
For o = 100 To 0 Step -10
Shapes.SetOpacity(flash, o)
Program.Delay(10)
EndFor
For o = 100 To 0 Step -10
For i = 1 To n
Shapes.SetOpacity(thunder[i], o)
EndFor
Program.Delay(10)
EndFor
For i = 1 To n
Shapes.Remove(thunder[i])
EndFor
If Math.Remainder(times, 5) = 0 Then
y = 70
For x = -100 To 700
Shapes_Move()
Program.Delay(5)
EndFor
Else
Program.Delay(5000)
EndIf
EndWhile

Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = -100 ' x offset
shY = 70 ' y offset
shape = ""
shape[1] = "func=tri;x=47;y=0;x1=26;y1=0;x2=0;y2=105;x3=52;y3=105;bc=#858585;pc=#000000;pw=2;"
shape[2] = "func=rect;x=0;y=169;width=23;height=108;angle=30;bc=#858585;pc=#000000;pw=2;"
shape[3] = "func=tri;x=32;y=126;x1=17;y1=0;x2=0;y2=60;x3=34;y3=60;angle=19;bc=#858585;pc=#000000;pw=2;"
shape[4] = "func=rect;x=54;y=66;width=37;height=14;bc=#1C1C1C;pc=#000000;pw=2;"
shape[5] = "func=tri;x=53;y=1;x1=20;y1=0;x2=0;y2=73;x3=40;y3=73;bc=#BD1414;pc=#000000;pw=2;"
shape[6] = "func=rect;x=120;y=168;width=23;height=108;angle=332;bc=#858585;pc=#000000;pw=2;"
shape[7] = "func=tri;x=84;y=131;x1=15;y1=0;x2=0;y2=55;x3=31;y3=55;angle=341;bc=#858585;pc=#000000;pw=2;"
shape[8] = "func=rect;x=40;y=244;width=68;height=22;bc=#1C1C1C;pc=#000000;pw=2;"
shape[9] = "func=tri;x=31;y=220;x1=42;y1=0;x2=0;y2=32;x3=84;y3=32;bc=#858585;pc=#000000;pw=2;"
shape[10] = "func=rect;x=47;y=106;width=53;height=137;bc=#858585;pc=#000000;pw=2;"
shape[11] = "func=rect;x=51;y=297;width=47;height=39;bc=#1C1C1C;pc=#000000;pw=2;"
shape[12] = "func=rect;x=58;y=306;width=33;height=51;bc=#858585;pc=#000000;pw=2;"
shape[13] = "func=rect;x=30;y=256;width=88;height=47;bc=#5AA7CD;pc=#000000;pw=2;"
shape[14] = "func=tri;x=3;y=314;x1=21;y1=0;x2=0;y2=32;x3=43;y3=32;bc=#858585;pc=#000000;pw=2;"
shape[15] = "func=tri;x=104;y=315;x1=21;y1=0;x2=0;y2=32;x3=43;y3=32;bc=#858585;pc=#000000;pw=2;"
shape[16] = "func=rect;x=23;y=306;width=33;height=51;bc=#858585;pc=#000000;pw=2;"
shape[17] = "func=rect;x=93;y=306;width=32;height=51;bc=#858585;pc=#000000;pw=2;"
shape[18] = "func=tri;x=44;y=311;x1=30;y1=0;x2=0;y2=33;x3=60;y3=33;bc=#858585;pc=#000000;pw=2;"
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)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Shapes_CalcWidthAndHeight()
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"]
x = shape[i]["x"]
y = shape[i]["y"]
width = shape[i]["width"]
height = shape[i]["height"]
shape[i]["obj"] = Shapes.AddRectangle(width * s, height * s)
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
If shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
ElseIf shape[i]["func"] = "ell" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
x = shape[i]["x"]
y = shape[i]["y"]
width = shape[i]["width"]
height = shape[i]["height"]
shape[i]["obj"] = Shapes.AddEllipse(width * s, height * s)
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
If shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
ElseIf shape[i]["func"] = "tri" Then
GraphicsWindow.BrushColor = shape[i]["bc"]
x = shape[i]["x"]
y = shape[i]["y"]
x1 = shape[i]["x1"]
y1 = shape[i]["y1"]
x2 = shape[i]["x2"]
y2 = shape[i]["y2"]
x3 = shape[i]["x3"]
y3 = shape[i]["y3"]
shape[i]["obj"] = Shapes.AddTriangle(x1 * s, y1 * s, x2 * s, y2 * s, x3 * s, y3 * s)
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
If shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
ElseIf shape[i]["func"] = "line" Then
x = shape[i]["x"]
y = shape[i]["y"]
x1 = shape[i]["x1"]
y1 = shape[i]["y1"]
x2 = shape[i]["x2"]
y2 = shape[i]["y2"]
shape[i]["obj"] = Shapes.AddLine(x1 * s, y1 * s, x2 * s, y2 * s)
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
EndIf
shape[i]["rx"] = x
shape[i]["ry"] = y
EndFor
shAngle = 0
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' return nShapes - number of shapes
' return shWidth, shHeight - total size of shapes
nShapes = Array.GetItemCount(shape)
For i = 1 To nShapes
If shape[i]["func"] = "tri" Or shape[i]["func"] = "line" Then
xmin = shape[i]["x1"]
xmax = shape[i]["x1"]
ymin = shape[i]["y1"]
ymax = shape[i]["y1"]
If shape[i]["x2"] < xmin Then
xmin = shape[i]["x2"]
EndIf
If xmax < shape[i]["x2"] Then
xmax = shape[i]["x2"]
EndIf
If shape[i]["y2"] < ymin Then
ymin = shape[i]["y2"]
EndIf
If ymax < shape[i]["y2"] Then
ymax = shape[i]["y2"]
EndIf
If shape[i]["func"] = "tri" Then
If shape[i]["x3"] < xmin Then
xmin = shape[i]["x3"]
EndIf
If xmax < shape[i]["x3"] Then
xmax = shape[i]["x3"]
EndIf
If shape[i]["y3"] < ymin Then
ymin = shape[i]["y3"]
EndIf
If ymax < shape[i]["y3"] Then
ymax = shape[i]["y3"]
EndIf
EndIf
shape[i]["width"] = xmax - xmin
shape[i]["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shape[i]["x"] + shape[i]["width"]
shHeight = shape[i]["y"] + shape[i]["height"]
Else
If shWidth < shape[i]["x"] + shape[i]["width"] Then
shWidth = shape[i]["x"] + shape[i]["width"]
EndIf
If shHeight < shape[i]["y"] + shape[i]["height"] Then
shHeight = shape[i]["y"] + shape[i]["height"]
EndIf
EndIf
EndFor
EndSub

Sub Shapes_CalcRotatePos
' Shapes | Calculate position for rotated shape
' param["x"], param["y"] - position of a shape
' param["width"], param["height"] - size of a shape
' param ["cx"], param["cy"] - center of shapes
' param ["angle"] - rotate angle
' return x, y - rotated position of a shape
_cx = param["x"] + param["width"] / 2
_cy = param["y"] + param["height"] / 2
x = _cx - param["cx"]
y = _cy - param["cy"]
Math_CartesianToPolar()
a = a + param["angle"]
x = r * Math.Cos(a * Math.Pi / 180)
y = r * Math.Sin(a * Math.Pi / 180)
_cx = x + param["cx"]
_cy = y + param["cy"]
x = _cx - param["width"] / 2
y = _cy - param["height"] / 2
EndSub

Sub Shapes_Move
' Shapes | Move shapes
' param shape - array of shapes
' param scale - to zoom
' param x, y - position to move
' return shX, shY - new position of shapes
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
dx = x - shX
dy = y - shY
shX = x
shY = y
For i = 1 To nShapes
_x = shape[i]["rx"] + dx
_y = shape[i]["ry"] + dy
Shapes.Move(shape[i]["obj"], shX + _x * s, shY + _y * s)
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub Shapes_Remove
' Shapes | Remove shapes
' param shape - array of shapes
Stack.PushValue("local", i)
For i = 1 To nShapes
Shapes.Remove(shape[i]["obj"])
EndFor
i = Stack.PopValue("local")
EndSub

Sub Shapes_Rotate
' Shapes | Rotate shapes
' param shape - array of shapes
' param scale - to zoom
' param angle - to rotate
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
s = scale
param["angle"] = angle
param["cx"] = shWidth / 2
param["cy"] = shHeight / 2
For i = 1 To nShapes
param["x"] = shape[i]["x"]
param["y"] = shape[i]["y"]
param["width"] = shape[i]["width"]
param["height"] = shape[i]["height"]
Shapes_CalcRotatePos()
shape[i]["rx"] = x
shape[i]["ry"] = y
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shape[i]["obj"], angle + shape[i]["angle"])
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub Math_CartesianToPolar
' Math | convert cartesian coodinate to polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x > 0 And y < 0 Then
a = a + 360
EndIf
EndSub