vb games: preparing for memory
DESCRIPTION
VB Games: Preparing for Memory. Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy wait Homework: Complete Chance, read & work on Memory. Questions. Controls Coding Logic ?. - PowerPoint PPT PresentationTRANSCRIPT
VB Games: Preparing for Memory
Brainstorm controls & eventsParallel structures (again), Visibility,
LoadPicture, User-defined procedures, Do While/Loop,busy
waitHomework: Complete Chance, read
& work on Memory
Questions
• Controls• Coding• Logic• ?
Review & Preview: How to build VB projects
• Place controls on the form. Rename and, possibly, change properties
• Write code for event procedures
• Some controls may not always or may never be visible (name.Visible = False)
• Write user-defined procedures to be called by event procedures
How does/should Memory work?
• What are controls?• What are events?
• Do not assume you will think of everything at the start.
Controls for cards/flags
• Nothing is turned over.• Instead, your code sets the Picture property
of a Picture control to either a blank image or a flag image.
Clicking one element of a Picture control array
• Note first that many other controls besides command buttons have Click events.
Sub picFlag_Click(Index as Integer)
• The Index parameter will hold the number of the specific control array element clicked. So your program will use that value.
Parallel arrays in Memory
• Cards represented by Picture controls and Label controls holding the name of the card/flag. Set this up in Form_Load. The Label controls are always invisible.
• Picture controls have a blank picture (blank.bmp). You could design a fancy ‘back of card’ OR
• Picture controls have a picture of a flag (assume player clicks on the Index card).
picFlag(Index.Picture)=LoadPicture(lblFlagName(Index))
Checking for matches
• The picFlag_Click procedure must keep track of whether this is a first pick or second pick. Do this using a Boolean variable: blnFirstTurn. Store first pick in global variable: intFirstPick.
• Checking is done using the names of the image files, not ‘looking at’ the pictures.
• Text suggests using a user-defined procedure to do the check. The user-defined procedure will have two parameters:
Sub test_for_match(pick1 as Integer, pick2 as Integer)
Tactic in Memory
• To make this part of the game automatic, the clicked cards are shown (turned over) and – if there is a match, the program removes them
from the board, after a delay– If there is no match, the program replaces the
flag images with the blank image, after a delay• What are alternatives?
Looping structures
• You have used For/Next loops to do lines of code a set number of times, using an index value
• Another looping structure isDo While condition …Loop
• You need to make sure that something happens to change the condition so that it becomes false. If not, the loop will keep going.
How to do delay?
• The chapter suggests what is called a busy wait. It uses two built-in Visual Basic procedures: – Timer: returns the number of seconds elapsed
since midnight on your computer’s internal clock
– DoEvents: allows your computer to do other things, such as printing
Delay user-defined procedure
Call delay with parameter indicating the amount of timeSub delay(interval as Single)
Dim sngStart as SinglesngStart = Timer ‘sets start timeDo While Timer < (SngStart + interval)DoEvents ‘do nothing here, but
‘system can take overLoop
When complete, delay procedure ends. Control returns to calling program.
How do you know when to define a user-defined procedure?
• There are rules-of-thumb, but not absolute rules.
• If lines of code would be repeated, then it is a good idea to define a procedure.
• If, otherwise, a procedure would be too big then it is a good idea to divide it.
• Subjective: if the code seems to be a coherent thing…
Ideas for enhancement of Memory
• Acquire your own images.• Change number of cards.• Add scoring.• Implement different delay intervals. • Changing it so the computer plays:
– Randomly– Perfect memory of picked cards– Some where in-between (very challenging)
• You may choose to come back to work on this for a final project.
Reflections
• Most common problems (so far) are mistakes in names.– Use Option Explicit in code to catch use of variable not
named in a Dim statement• Others?
• The system, that is, Visual Basic, will catch syntax errors immediately. VB may catch logic errors during run time.