FIT
100
Lab 14:
Animation and Indexing Autumn 2001 Reading for Labs 14: ·
Chapter
15 of FIT ·
Other
helpful links on graphics, etc are at: http://courses.washington.edu/gbw/fit100/webref.htm
Keep this picture in mind:
Things to remember:
authorArray(index) Note
that the index must be an Integer You can index with any expression that ends up being an Integer. In the following statement, i is ok as long as it was declared an Integer variable earlier: authorArray(i) = “Twain” This comes in handy when you want to use loops…
Dim array_name (max_index)
as type type will be integer, string, or double max_index is the maximum index for the array, which is one less than the total number of elements, because Visual Basic numbers elements starting from 0 not 1.
With the example array authorArray above, it is ok to do the following: MsgBox “Why not read something by ” & authorArray(5) & “?” Or this assignment: authorArray(3) = “Angelou”
If you declare an array at the top of your code, right after Option Explicit, it is a global array, i.e. it exists throughout your program while it is running and is available to all procedures (sub routines). Array Vocabulary:
What is wrong with the following lines of code? Each of these lines has a syntax error, a mistake, in the usage of the above array. What’s wrong? If authorArray(7) = “Vargas” Then … authorArray(“two”) = “Melville” i = 0 Do while i < 6 MsgBox “Go read a book by ” & authorArray(i) & “!” i = i + 1
End Sub Introduction: Lecture this week discusses the idea of animation in programming as the process of drawing a shape, erasing it, moving it a certain distance and redrawing it. Using indices, you can reference a sequence of related items (in this case string values), and display them as part of your program while you are drawing shapes and allowing a scene to unfold before you. Objectives:
A simple, nonsense use of an array:
Option Explicit Dim rain(19)
As String Dim timerCount
As Integer Private Sub Form_Click() tmrClock.Interval = 1000 End Sub Private Sub Form_Load() WindowState
= 2 BackColor
= RGB(139, 195, 233) timerCount =
0 rain(0)
= "Mist" rain(1)
= "Drizzle" rain(2)
= "Showers" rain(3)
= "Drenching Rain" rain(4)
= "Downpours" rain(5)
= "More Mist" rain(6)
= "More Drizzle" rain(7)
= "More Showers" rain(8)
= "More Drenching Rain" rain(9)
= "More Downpours" rain(10)
= "Even More Mist" rain(11)
= "Even More Drizzle" rain(12)
= "Even More Showers" rain(13)
= "Even More Drenching Rain" rain(14)
= "Even More Downpours" rain(15)
= "No Snow, Just Mist" rain(16)
= "No Snow, Just Drizzle" rain(17)
= "No Snow, Just Showers" rain(18)
= "No Snow, Just Drenching Rain" rain(19)
= "No Snow, Just Downpours" lblWhy.BackColor
= RGB(139, 195, 233) lblWhy.Width
= ScaleWidth lblWhy.Caption
= "Why Snowmen don't live in Call snowman End Sub Private Sub tmrClock_Timer() If timerCount
< 20 Then lblWhy.Caption = rain(timerCount) End If Call snowman Call water lblWhy.Top = Int(Rnd * 4000) lblWhy.Left = Int(Rnd * 6000) If timerCount =
20 Then tmrClock.Interval
= 0 End If
timerCount
= timerCount + 1 End Sub Private Sub snowMan() Dim xpos As Integer Dim ypos As Integer xpos = 4000 ypos = timerCount Call
circles(xpos, ypos) End Sub Private Sub water() FillColor = RGB(0, 0,
255) Line (ScaleWidth, ScaleHeight)-(0, ScaleHeight - timerCount * 140), RGB(0, 0, 255), B End Sub Private Sub circles(x As
Integer, y As Integer) Cls FillStyle = 0 FillColor = RGB(255, 255, 255) Circle
(x, 9080 + y * 200), 2000, RGB(255, 255, 255) Circle
(x, 6080 + y * 200), 1000, RGB(255, 255, 255) Circle
(4000, 4580 + timerCount * 200), 500, RGB(255, 255, 255) End Sub
|