Small Basic Sample: Moving Eyes
This is a Small Basic sample program that moves eyes formed by Shapes object. This sample also shows how to use Timer object.
Screen Shot
Following picture is the screen shot of the moving eyes sample.
Code
The source code of the program is listed below. This program has three subroutines:
- Init - initialization
- AddEyes - to add eyes
- MoveEyes - timer event handler to move eyes
' Moving Eyes Sample
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Program ID LGR690
Init()
AddEyes()
Timer.Interval = 100
Timer.Tick = MoveEyes
Sub Init
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
GraphicsWindow.BackgroundColor = "DarkSeaGreen"
EndSub
Sub AddEyes
sx["eye"] = 240
sy["eye"] = 300
sx["iris"] = 180
sy["iris"] = 225
distance = sx["eye"] * 1.1
bc["eye"] = "White"
pc["eye"] = "Black"
pw["eye"] = 10
bc["iris"] = "Black"
pc["iris"] = "LightSeaGreen"
pw["iris"] = 55
target[1] = "eye"
target[2] = "iris"
For it = 1 To 2
' for eye and iris
GraphicsWindow.BrushColor = bc[target[it]]
GraphicsWindow.PenColor = pc[target[it]]
GraphicsWindow.PenWidth = pw[target[it]]
For i = 1 To 2
' for left and right
sign = 2 * i - 3
xo[i] = gw / 2 + sign * distance / 2
yo = gh / 2
shp[target[it] + i] = Shapes.AddEllipse(sx[target[it]], sy[target[it]])
Shapes.Move(shp[target[it] + i], xo[i] - sx[target[it]] / 2, yo - sy[target[it]] / 2)
EndFor
EndFor
dx = sx["eye"] / 2 - pw["eye"] - sx["iris"] / 2
EndSub
Sub MoveEyes
_a = _a + Math.Pi / 18
For i = 1 To 2
x = xo[i] + dx * Math.Sin(_a)
Shapes.Move(shp["iris" + i], x - sx["iris"] / 2, yo - sy["iris"] / 2)
EndFor
EndSub
See Also
- Other samples (in [[articles:Wiki: Small Basic Portal]])
- [[articles:Small Basic: Shapes]]
- [[articles:Small Basic: Timer]]