Microsoft Small Basic

Program Listing: PTT575
' Balloons
' Version 0.1
' Copyright © 2018 Nonki Takahashi. The MIT License.

GraphicsWindow.Title = "Balloons"
Init()
GraphicsWindow.PenWidth = 0
For i = 1 To n
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
AddBalloon()
x = Math.GetRandomNumber(gw - 2 * ew) + ew
y = Math.GetRandomNumber(gh / 2 - 2 * eh) + eh
MoveBalloon()
EndFor
GraphicsWindow.MouseDown = OnMouseDown
Timer.Interval = dt * 1000
Timer.Tick = OnTick
While "True"
If mouseDown Then
CheckBalloonClick()
If 0 < balloonClicked Then
v[balloonClicked] = v0
EndIf
mouseDown = "False"
Else
Program.Delay(200)
EndIf
EndWhile

Sub AddBalloon
' param ew - width
' param eh = height
' param i = index
e[i] = Shapes.AddEllipse(ew, eh)
t[i] = Shapes.AddTriangle(0, 0, tw, 0, tw / 2, th)
v[i] = 0
a[i] = 0
EndSub

Sub CheckBalloonClick
balloonClicked = 0
For i = n To 1 Step -1
If ex[i] <= mx And mx <= ex[i] + ew And ey[i] <= my And my <= ey[i] + eh Then
balloonClicked = i
i = 1 ' exit For
ElseIf tx[i] <= mx And mx <= tx[i] + tw And ty[i] <= my And my <= ty[i] + th Then
balloonClicked = i
i = 1 ' exit For
EndIf
EndFor
EndSub

Sub MoveBalloon
' param i - index
' param x, y - position
ex[i] = x - ew / 2
ey[i] = y - eh / 2
Shapes.Move(e[i], ex[i], ey[i])
tx[i] = x - tw / 2
ty[i] = y + th
Shapes.Move(t[i], tx[i], ty[i])
EndSub

Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "SkyBlue"
ew = 70
eh = 90
sqr2 = Math.SquareRoot(2)
tw = ew / sqr2
th = eh / sqr2 / 2
n = 10
dt = 0.2 ' sec
g = 3 ' px/sec^2
v0 = -20 ' px/sec
EndSub

Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
mouseDown = "True"
EndSub

Sub OnTick
For _i = 1 To n
_v = v[_i]
v[_i] = _v + g * dt
ey[_i] = ey[_i] + (_v + v[_i]) * dt / 2
Shapes.Move(e[_i], ex[_i], ey[_i])
ty[_i] = ty[_i] + (_v + v[_i]) * dt / 2
Shapes.Move(t[_i], tx[_i], ty[_i])
EndFor
EndSub