vb games: preparing for memory

15
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

Upload: sook

Post on 22-Feb-2016

40 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: VB Games: Preparing for Memory

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

Page 2: VB Games: Preparing for Memory

Questions

• Controls• Coding• Logic• ?

Page 3: VB Games: Preparing for Memory

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

Page 4: VB Games: Preparing for Memory

How does/should Memory work?

• What are controls?• What are events?

• Do not assume you will think of everything at the start.

Page 5: VB Games: Preparing for Memory

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.

Page 6: VB Games: Preparing for Memory

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.

Page 7: VB Games: Preparing for Memory

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

Page 8: VB Games: Preparing for Memory

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)

Page 9: VB Games: Preparing for Memory

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?

Page 10: VB Games: Preparing for Memory

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.

Page 11: VB Games: Preparing for Memory

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

Page 12: VB Games: Preparing for Memory

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.

Page 13: VB Games: Preparing for Memory

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…

Page 14: VB Games: Preparing for Memory

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.

Page 15: VB Games: Preparing for Memory

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.