Microsoft Small Basic

Program Listing: ZRX774
' Last update 2016-09-05
' Challenge 2016-09
title = "Multitasking Challenge"
Not = "True=False;False=True;"
black = "True"
timers = "0.5=0.5;1=1;2.5=2.5;3.2=3.2;"
dw = Desktop.Width
dh = Desktop.Height
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
Turtle.PenUp()
Turtle.X = 0
Turtle.Y = 0
Turtle.Angle = 90
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
e1 = Shapes.AddEllipse(20, 20)
x1 = Math.GetRandomNumber(gw - 20) - 1
y1 = Math.GetRandomNumber(gh - 20) - 1
Shapes.Move(e1, x1, y1)
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
e2 = Shapes.AddEllipse(20, 20)
x2 = Math.GetRandomNumber(gw - 20) - 1
y2 = Math.GetRandomNumber(gh - 20) - 1
dx = 5
dy = 5
Shapes.Move(e2, x2, y2)
ms = Clock.ElapsedMilliseconds
lap = 0
GraphicsWindow.Title = title + " - Lap=" + lap
Timer.Interval = 100
Timer.Tick = OnTick
While "True"
' (2) Get the Turtle to keep moving round the outside edge of the GraphicsWindow
' in a rectangle
Turtle.Move(gw)
Turtle.TurnRight()
Turtle.Move(gh)
Turtle.TurnRight()
EndWhile

Sub CheckTimers
' (1) Continually change the GraphicsWindow background color every second
' between balck and white.
If timers[1] <= 0 Then
If black Then
GraphicsWindow.BackgroundColor = "Black"
Else
GraphicsWindow.BackgroundColor = "White"
EndIf
black = Not[black]
timers[1] = 1
EndIf
' (3) Keep randomly repositing the GraphicsWindow on the desktop every
' 2.5 seconds
If timers[2.5] <= 0 Then
GraphicsWindow.Left = Math.GetRandomNumber(dw - gw) - 1
GraphicsWindow.Top = Math.GetRandomNumber(dh - gh) - 1
timers[2.5] = 2.5
EndIf
' (4) Play the Click sound every 3.2 seconds
If timers[3.2] <= 0 Then
Sound.PlayClick()
timers[3.2] = 3.2
EndIf
' (5) Add an ellipse shape that changes its color every 0.5 seconds
If timers[0.5] <= 0 Then
Shapes.Remove(e1)
GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()
e1 = Shapes.AddEllipse(20, 20)
Shapes.Move(e1, x1, y1)
timers[0.5] = 0.5
EndIf
' (6) Continually move an ellipse shape in the GraphicsWindow like a bouncing
' ball
x2 = x2 + dx
If x2 < 0 Then
x2 = -x2
dx = -dx
ElseIf gw - 20 < x2 Then
x2 = 2 * (gw - 20) - x2
dx = -dx
EndIf
y2 = y2 + dy
If y2 < 0 Then
y2 = -y2
dy = -dy
ElseIf gh - 20 < y2 Then
y2 = 2 * (gh - 20) - y2
dy = -dy
EndIf
Shapes.Move(e2, x2, y2)
' (7) Anything else you can think of - Show lap time in the title
lap = Math.Floor((Clock.ElapsedMilliseconds - ms) / 100) / 10
GraphicsWindow.Title = title + " - Lap=" + lap
EndSub

Sub OnTick
n = Array.GetItemCount(timers)
index = Array.GetAllIndices(timers)
For i = 1 To n
timers[index[i]] = timers[index[i]] - 0.1
EndFor
CheckTimers()
EndSub