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:

 

Dim author As String

Do variable assignments where needed to assign the values shown with each index

 

“Salinger”

 
 

 

 

 

 

author  

                  String

 

 

 

 

 

Dim authorArray(5) As String

“Salinger”

 
 

 

 

 

 

authorArray(0)

Kesey

 
 

authorArray(1)

 

“Dickens”

 
authorArray(2)

 

Woolf

 
authorArray(3)

 

Neruda

 
authorArray(4)

Allende

 
 

authorArray(5)

                              String                       

 

Things to remember:

  • Arrays in VB are numbered collections of variables (or controls), all of the same type.
  • The array has one name, so indexing lets you get to the elements of the array:

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…

 

  • Declaration syntax:

 

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.

 

  • You can use an array element anywhere Visual Basic expects a value whose type is the same as the array type.

 

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”

 

  • Array variables, just like regular variables, have scope.  If you declare an array inside a procedure (Private Sub ….), it is a local array.  So, it only exists while the procedure is running and is not visible outside of the procedure.

 

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:

  • Element – one member of the collection of storage locations of an array
  • Index or subscript – the Integer value used to identify a specific element of an array

 

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”

Dim i as Integer

                   i = 0

                   Do while i < 6

                             MsgBox “Go read a book by ” & authorArray(i) & “!”

                             i = i + 1

 

                   Loop

          End Sub

 

Note that authorArray must be declared as a global array for this to work.

 

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:

  1. To declare and initialize an array that will be part of a display
  2. To use a timer incrementing variable values to access elements of the array.
  3. To use a timer and iteration to help draw a series of shapes on the form that will come together as a picture.

 

A simple, nonsense use of an array: 

 

  1. Can you walk yourself through the code below and understand what is happening with each event handling routine and procedure call? Work with a partner and follow the code through from global variable declaration all the way to the last procedure. 

 

 

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 Seattle"

 

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

 

 

  1. Now, open up VB and start a new Standard EXE project.

  2. The code for a very basic version of Melting Snowman is shown above.  You can work with creating your own arrays in lab today, or simply work on part 2 of your project, due next Wednesday.