Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.

Similar presentations


Presentation on theme: "VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy."— Presentation transcript:

1 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

2 Questions Controls Coding Logic ?

3 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

4 How does/should Memory work? What are controls? What are events? Do not assume you will think of everything at the start.

5 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.

6 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.

7 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))

8 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)

9 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?

10 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 is Do 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.

11 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

12 Delay user-defined procedure Call delay with parameter indicating the amount of time Sub delay(interval as Single) Dim sngStart as Single sngStart = Timer ‘sets start time Do While Timer < (SngStart + interval) DoEvents ‘do nothing here, but ‘system can take over Loop When complete, delay procedure ends. Control returns to calling program.

13 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…

14 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.

15 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.


Download ppt "VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy."

Similar presentations


Ads by Google