Microsoft Small Basic

Program Listing: GRT732-2
' Bowling Game
' Version 0.4a
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-03-10
' Program ID GRT732-2

GraphicsWindow.Title = "Bowling Game 0.4a - Hit arrow keys."
Init()
GraphicsWindow.KeyDown = OnKeyDown
While "True"
InitBall()
Position() ' for the first throwing
ThrowBall()
SelectPins() ' to blow
BlowPins()
InitBall()
Position() ' for the second throwing
ThrowBall()
SelectPins() ' to blow
BlowPins()
InitPins()
EndWhile

Sub BlowPins
For t = 0 To 1000 Step delay
For i = 1 To 10
If fell[i] Then
x[i] = x[i] + dx[i]
y[i] = y[i] - 5
a[i] = a[i] + da[i]
Shapes.Move(pin[i], x[i], y[i])
Shapes.Rotate(pin[i], a[i])
EndIf
EndFor
If ye <= yb Then
MoveBall()
yb = yb - 2
EndIf
Program.Delay(delay)
EndFor
Shapes.HideShape(ball)
For i = 1 To 10
If fell[i] Then
Shapes.HideShape(pin[i])
EndIf
EndFor
EndSub

Sub DrawBall
' param xb, yb - ball position
' param ab - ball angle
scale = yb / 212
If shadow = "" Then
GraphicsWindow.BrushColor = "Black"
shadow = Shapes.AddEllipse(78, 20)
Shapes.SetOpacity(shadow, 50)
EndIf
Shapes.Move(shadow, xb - 39, yb - 10)
Shapes.Zoom(shadow, scale, scale)
If ball = "" Then
path = "http://www.nonkit.com/smallbasic.files/Ball.png"
ball = Shapes.AddImage(path)
EndIf
Shapes.Move(ball, xb - 39, yb - 33 * scale - 39)
Shapes.Zoom(ball, scale, scale)
Shapes.Rotate(ball, ab)
EndSub

Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
da = "1=-10;2=-8;3=8;4=-6;5=-6;6=6;7=-4;8=-4;9=4;10=4;"
dx = "1=-3;2=-4;3=4;4=-5;5=-3;6=5;7=-5;8=-4;9=4;10=5;"
GraphicsWindow.BackgroundColor = "LightGray"
' draw gutter
GraphicsWindow.BrushColor = "SaddleBrown"
color = GraphicsWindow.BrushColor
rate = 0.5
Colors_Init()
Color_Blacken()
GraphicsWindow.BrushColor = color
GraphicsWindow.FillTriangle(gw / 2, 0, -70, gh, gw + 70, gh)
' draw lane
ye = 170
GraphicsWindow.BrushColor = "SaddleBrown"
GraphicsWindow.FillTriangle(gw / 2, 0, 0, gh, gw, gh)
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, ye)
GraphicsWindow.BrushColor = "Black"
path = "http://www.nonkit.com/smallbasic.files/Pin.png"
i = 6
d = 3
For row = 4 To 1 Step -1
y = 100 - row * 10
For col = 1 To row
i = i + 1
pin[i] = Shapes.AddImage(path)
x = 249 + col * 60 - row * 30
Shapes.Move(pin[i], x, y)
EndFor
i = i - row - d
d = d - 1
shadow = Shapes.AddRectangle(366, 160)
Shapes.SetOpacity(shadow, 15)
Shapes.Move(shadow, 116, 52)
EndFor
shadow = ""
' draw wall
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "GreenYellow"
wall = Shapes.AddRectangle(gw, 52)
Shapes.Move(wall, 0, 0)
wall = Shapes.AddRectangle(116,160)
Shapes.Move(wall, 0, 52)
wall = Shapes.AddRectangle(149, 160)
Shapes.Move(wall, gw - 116, 52)
delay = 10 ' [ms]
EndSub

Sub InitBall
xb = gw / 2 ' ball position
yb = gh + 120
ab = 120 ' ball angle
DrawBall()
EndSub

Sub InitPins
For i = 1 To 10
Shapes.Move(pin[i], x0[i], y0[i])
Shapes.Rotate(pin[i], 0)
Shapes.ShowShape(pin[i])
EndFor
EndSub

Sub MoveBall
' param xs - ball start x position
xb = (gw / 2) * (1 - yb / gh) + xs * yb / gh
ab = ab - 4
DrawBall()
Program.Delay(delay)
EndSub

Sub OnKeyDown
keyDown = "True"
EndSub

Sub Position
Shapes.ShowShape(ball)
positioning = "True"
While positioning
If keyDown Then
If GraphicsWindow.LastKey = "Up" Then
positioning = "False"
ElseIf GraphicsWindow.LastKey = "Left" Then
If 0 <= xb - 10 Then
xb = xb - 10
DrawBall()
EndIf
ElseIf GraphicsWindow.LastKey = "Right" Then
If xb + 10 <= gw Then
xb = xb + 10
DrawBall()
EndIf
EndIf
keyDown = "False"
EndIf
EndWhile
EndSub

Sub SelectPins
For i = 1 To 10
If Math.Remainder(Math.GetRandomNumber(2), 2) = 0 Then
fell[i] = "True"
Else
fell[i] = "False"
EndIf
If x0[i] = "" Then
x0[i] = Shapes.GetLeft(pin[i])
EndIf
If y0[i] = "" Then
y0[i] = Shapes.GetTop(pin[i])
EndIf
x[i] = x0[i]
y[i] = y0[i]
a[i] = 0
EndFor
EndSub

Sub ThrowBall
Shapes.ShowShape(shadow)
xs = xb
For yb = gh - 1 To 208 Step -2
MoveBall()
EndFor
Shapes.HideShape(shadow)
EndSub

Sub Color_Blacken
' Color | Blacken given color
' param color - given color
' param rate - 0..1
' return color - color blackened
Color_NameToColor()
Color_ColorToRGB()
r = Math.Floor(r * (1 - rate))
g = Math.Floor(g * (1 - rate))
b = Math.Floor(b * (1 - rate))
color = GraphicsWindow.GetColorFromRGB(r, g, b)
EndSub

Sub Color_ColorToRGB
' Color | Convert color to RGB values
' param color - "#rrggbb" (hexadecimal values)
' return r, g, b - RGB values 0..255
sR = Text.GetSubText(color, 2, 2)
sG = Text.GetSubText(color, 4, 2)
sB = Text.GetSubText(color, 6, 2)
hex = sR
Math_Hex2Dec()
r = dec
hex = sG
Math_Hex2Dec()
g = dec
hex = sB
Math_Hex2Dec()
b = dec
EndSub

Sub Color_NameToColor
' Color | Convert color name to color
' param color - color name
' return color -"#rrggbb"
If Text.StartsWith(color, "#") Then
color = Text.ConvertToUpperCase(color)
Else
color = Text.ConvertToLowerCase(color)
color = colors[color]
EndIf
EndSub

Sub Colors_Init
' Colors | Initialize 140 colors array
txt = "aliceblue,antiquewhite,aqua,aquamarine,"
txt = txt + "azure,beige,bisque,black,blanchedalmond,"
txt = txt + "blue,blueviolet,brown,burlywood,"
txt = txt + "cadetblue,chartreuse,chocolate,coral,"
txt = txt + "cornflowerblue,cornsilk,crimson,"
txt = txt + "cyan,darkblue,darkcyan,darkgoldenrod,"
txt = txt + "darkgray,darkgreen,darkkhaki,"
txt = txt + "darkmagenta,darkolivegreen,darkorange,"
txt = txt + "darkorchid,darkred,darksalmon,darkseagreen,"
txt = txt + "darkslateblue,darkslategray,"
txt = txt + "darkturquoise,darkviolet,deeppink,"
txt = txt + "deepskyblue,dimgray,dodgerblue,"
txt = txt + "firebrick,floralwhite,forestgreen,"
txt = txt + "fuchsia,gainsboro,ghostwhite,gold,"
txt = txt + "goldenrod,gray,green,greenyellow,"
txt = txt + "honeydew,hotpink,indianred,indigo,"
txt = txt + "ivory,khaki,lavender,lavenderblush,"
txt = txt + "lawngreen,lemonchiffon,lightblue,"
txt = txt + "lightcoral,lightcyan,lightgoldenrodyellow,"
txt = txt + "lightgray,lightgreen,lightpink,"
txt = txt + "lightsalmon,lightseagreen,lightskyblue,"
txt = txt + "lightslategray,lightsteelblue,"
txt = txt + "lightyellow,lime,limegreen,linen,"
txt = txt + "magenta,maroon,mediumaquamarine,mediumblue,"
txt = txt + "mediumorchid,mediumpurple,mediumseagreen,"
txt = txt + "mediumslateblue,mediumspringgreen,"
txt = txt + "mediumturquoise,mediumvioletred,midnightblue,"
txt = txt + "mintcream,mistyrose,moccasin,navajowhite,"
txt = txt + "navy,oldlace,olive,olivedrab,orange,"
txt = txt + "orangered,orchid,palegoldenrod,palegreen,"
txt = txt + "paleturquoise,palevioletred,papayawhip,"
txt = txt + "peachpuff,peru,pink,plum,powderblue,"
txt = txt + "purple,red,rosybrown,royalblue,saddlebrown,"
txt = txt + "salmon,sandybrown,seagreen,seashell,"
txt = txt + "sienna,silver,skyblue,slateblue,slategray,"
txt = txt + "snow,springgreen,steelblue,"
txt = txt + "tan,teal,thistle,tomato,turquoise,violet,"
txt = txt + "wheat,white,whitesmoke,yellow,yellowgreen"
delim = ","
Text_Split()
saved = GraphicsWindow.PenColor
n = Array.GetItemCount(arry)
For i = 1 To n
GraphicsWindow.PenColor = arry[i]
c = GraphicsWindow.PenColor
colors[arry[i]] = c
EndFor
GraphicsWindow.PenColor = saved
EndSub

Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param hex
' return dec
dec = 0
len = Text.GetLength(hex)
For ptr = 1 To len
dec = dec * 16 + Text.GetIndexOf("123456789ABCDEF", Text.GetSubText(hex, ptr, 1))
EndFor
EndSub

Sub Text_Split
' Text | Split text to array by delimiter
' param txt - to split
' param delim - delimiter
' return arry - splitted
len = Text.GetLength(txt)
p = 1
n = 0
d = Text.GetIndexOf(Text.GetSubTextToEnd(txt, p), delim)
While (0 < d) And (p <= len)
n = n + 1
arry[n] = Text.GetSubText(txt, p, d - 1)
p = p + d
d = Text.GetIndexOf(Text.GetSubTextToEnd(txt, p), delim)
EndWhile
n = n + 1
arry[n] = Text.GetSubTextToEnd(txt, p)
EndSub