Lab 14:
Graphics and Randomization
Spring 2001
Reading to be done for
Labs 13 and 14:
·
p. 272-278 (top of 278)
in Chapter 10 of Computer Programming Fundamentals with Applications in
Visual Basic 6.0
Introduction:
This is an extension of Lab 13. Working forward from where Lab 13 left off, we do more with iteration and cover the Randomize statement as a way to introduce an element of surprise and creativity into your program.
Objectives:
TO DO:
· Name the button cmdShrinkBox
· Make the Caption “I’m Shrinking!”
·
Set the visibility to false
·
Inside of the condition If clickcount = 5, make
the cmdShrinkBox visible.
cmdShrinkBox.Visible = True
· Assign xPos and yPos the value of 3000.
·
Assign xPos2 and yPos2 the value of 8000.
Private Sub
cmdShrinkBox_Click()
Dim xPos2 As
Integer, yPos2 As Integer
xPos = 3000
yPos = 3000
xPos2 = 8000
yPos2 = 8000
End Sub
· The beginning points (xPos and yPos) should increment.
· The end points (xPos2 and yPos2) should decrement.
·
Since one end of the box in increasing and one is
decreasing, the loop should run until they meet. That is, it should run for as long as xPos does not equal xPos2
(or as long as yPos does not equal yPos2).
You can use either one for the iteration.
·
Draw the new box with the new coordinates
·
Clear the box so that the loop can run again and it
looks as though there is movement
Your loop should look like this:
Do While xPos <> xPos2 ‘run
the loop until xPos matches yPos, then stop
xPos = xPos + 1 ‘increment xPos and yPos
yPos = yPos + 1 ‘by
one each time through
xPos2 = xPos2 – 1 ‘decrement
xPos2 and yPos2
yPos2 = yPos2 – 1 ‘by
one each time through
Line (xPos, yPos)-(xPos2, yPos2), RGB(0, 0, 0), B
Cls ‘This
clears the form of any objects currently on it
Loop
Using the RANDOMIZE Statement
The Randomize statement and the Rnd function are extremely useful (especially
in this project) when you want to randomly generate a number.
You might want that number for Box coordinates, Circle coordinates, the radius of a circle, Fill styles (they go from 0 to 7), color, etc.
· Go into the cmdShrinkBox click event.
· Inside the loop, change the fill style to 0 so there is a solid color allowed for the boxes
· Make the Fill color a randomly generated number between 0 and 255 (there are 256 possibilities) and make it change to display as an integer.
Your loop should now look like:
Do While xPos <> xPos2
FillStyle = 0
FillColor = RGB(Int(Rnd() * 256), Int(Rnd() * 256), Int(Rnd() * 256)) ‘generates a random number, not
‘exceeding 255 for red, green and blue!
xPos = xPos + 1
yPos = yPos + 1
xPos2 = xPos2 - 1
yPos2 = yPos2 - 1
Line (xPos, yPos)-(xPos2, yPos2), RGB(0, 0, 0), B
Cls
Loop
· Can you create a series of statements that will slowly increase the size of a box?
· Can you generate a circle that gets smaller and then bigger?
Try to answer these questions in your program outside of lab to give you practice for your project.
Going back through what you created
for Lab 13 and 14, try adding in more of:
· The use of the timer (or add additional timers)
· Do while loops
· Procedures that when called make boxes and circles
· Randomly created boxes, circles, colors, etc.
Use these to labs and the lecture content to help you create a cool, innovative program that will astound your friends!