visual basic games: week 4 recap parallel structures initialization prepare for memory scoring...

23
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.

Upload: oscar-russell

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Visual Basic Games: Week 4Recap

Parallel structures

Initialization

Prepare for Memory

Scoring

Shuffling

Homework: when ready, move on to next game/chapter.

Page 2: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

First 3 projects

• What were the controls?

• What were the events (what event procedures did you write)?

• What global variables were required?– "State of the game" consists of what is present

on the screen plus any global variables.

Page 3: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Statements• Assignment statement:

intTotal = intDie1 + intDie2

• For/Next:For I = 0 to 6 imgDie(I).Visible = FalseNext I

• Select CaseSelect Case intTotalCase intPointnum

lblstatus = "You Win" blnfirstmove = true Case 7

lblstatus = "You Lose" blnfirstmove = trueEnd Select

• If/Else/End If or IF/End If

Page 4: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Another compound statement: Do While

• For/Next does the statements 'in' the loop a certain number of times (though it can be based on variables)

• Do While does the statements as long as a condition is true:Do while condition …Loop

Page 5: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Parallel structures

• In mix & match, the listbox items responded to the elements in the control arrays– Sets of sets here: the items in the lstHats listbox

corresponded to the elements of the imgHats control array: “floppy hat” imgHats(0)

• In memory, control array holding file names and a control array of images.

• Using parallel structures is a very common technique.

• YOU make it happen.

Page 6: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Is there another way?

• Using parallel structures, call them implicit parallel structures, is necessary because the control objects are distinct and can’t be linked except by your code.

• For internal variables, there is something called types that puts corresponding data together:Private Type bestdata bname As String * 20 bscore As Integer End Type

Page 7: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Initialization

• Start of execution (Run menu / Start or the start icon)– Form_Load– Form_Activate (used in Quiz). Necessary when

switching between forms.

• New game– Probably command button Click

• New moveYou have to understand, plan and program. Often

need to clear / tidy up board. Set variables to 0 or the empty string OR not (wait for first/next action to set values).

Page 8: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Initialization

• What was required initialization in– Rock-paper-scissors– Mix and match– Chance

Page 9: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Ideas to improve/enhance interface• rock paper scissors

– add to or replace text with images• player and computer?

– score– ?

• mix and match– better images—better fit of images– random feature?– ?

• chance– rolling dice and/or random locations– remove text?– score– ?

Page 10: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Memory / Concentration: Game

Board consists of 16 cards, all showing blank side.

Click on a card: it is 'turned over'. Click on a second card: it is 'turned over'. If images match, both cards are removed, otherwise, cards are 'flipped back'.

Game is over when all matches made.

Page 11: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Implementation Requirements

8 pairs of images plus blank images for the "backs"

names of images—all comparisons are done in terms of the names

Pause time before cards removed or flipped back

Page 12: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

What are events

for which you must write event procedures?

Page 13: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Event procedures

• Form_load– set up & shuffle cards. This will be done using user-

defined procedure so you don't have to write the code twice

• Click on image– will need to distinguish a first and a second turn

• New game button– restore cards and shuffle

• other buttons (Help, End)

Page 14: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

User (this means you, the programmer) defined procedure

• Unlike event procedures, you name it as well as program it. You also issue calls to it.

Private Sub delay(interval as Single)Dim sngStart as SinglesngStart = TimerDo While Timer < (sngStart + interval) DoEventsLoop

End Sub…

call delay(1)

built-in function. Returns seconds since midnightlet's computer to

other things, while waiting

Page 15: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

About images

• Our implementation of memory is (purposely) different from mix & match and chance.– LoadPicture method is used to put the

appropriate image into the control when required. This means that the images must be in the same drive/same folder as the program.

Page 16: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Scoring

• Can [just] use visible label, assume lblScore is the label control and intNewScore the internal integer variable holding the addition to the score

• Initialize: lblScore.Caption = 0

• Update: intOldscore = Val(lblScore.Caption)lblScore.Caption = Format(intNewScore + intOldScore, “##0”)

Note: this works even if intNewScore is negative.The “##0” makes sure 0 appears.

• What would be the changes if new scores were money AND if there could be scores with fractional parts?

Page 17: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Shuffling

• Have an array of N items of some data type• Want to shuffle, that is, re-arrange the

values to be randomly sorted.• What is the criteria for success?

– Answer: equal likelihood of any of the N possible permutations.

• How to do it?– Using Randomize, Rnd, etc.

Page 18: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

A Shuffle method

• 6 things (why six? Because I have a single die)– How many permutations are possible?

• Start first with the last position. Pick randomly one of the first six to swap this one. Note: so one possibility is swapping with itself, for no change.

• Now move to next to last (5th). Pick randomly one of the first 5 to swap with this one.

• …

Page 19: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Construct deck of cards

• strSuits (1 to 4) as String• strSuits(1) = “hearts”• strSuits(2) = “spades”• strSuits(3) = “diamonds”• strSuits(4) = “clubs”• strFaces(1 to 13) as String

– What do I need to do to produce “A”, “1” ,… “K” ??

Page 20: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Produce “deck”

Sub setupdeck() Dim i As Integer, j As Integer, k As Integer k = 1 For i = 1 To 4 For j = 1 To 13 strDeck(k) = strSuits(i) & strValue(j) k = k + 1 Next j Next iEnd Sub

Page 21: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Swapping

• Whenever you swap two things, you will need a temporary variable:

• thing1, thing2, holder each variables holding a value. An assignment statement takes the value of the right side, in this case, the value in that variable and puts it in (assigned it to) the variable named by the left side.holder = thing1thing1 = thing2thing2 = holder

Page 22: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Shuffle

Private Sub cmdShuffle_Click()Dim i As Integer, k As IntegerDim holder As StringFor i = 52 To 1 Step -1 k = Int(Rnd * i) + 1 ' k is random choice 1 to i holder = strDeck(k) strDeck(k) = strDeck(i) strDeck(i) = holder 'swapped values in k and i positions of strDeck arrayNext iEnd Sub

Page 23: Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter

Homework

• Read chapter 4 (Memory/Concentration) and start implementing it

• You can make your own images!