1 / 23

Visual Basic Games: Week 4

A recap of Week 4 in Visual Basic Games, covering topics such as parallel structures, initialization, memory scoring, and shuffling. Move on to the next game/chapter when ready.

johnniec
Download Presentation

Visual Basic Games: Week 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Statements • Assignment statement: intTotal = intDie1 + intDie2 • For/Next: For I = 0 to 6 imgDie(I).Visible = False Next I • Select Case Select Case intTotal Case intPointnum lblstatus = "You Win" blnfirstmove = true Case 7 lblstatus = "You Lose" blnfirstmove = true End Select • If/Else/End If or IF/End If

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

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

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

  7. 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 move You 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).

  8. Initialization • What was required initialization in • Rock-paper-scissors • Mix and match • Chance

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

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

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

  12. What are events for which you must write event procedures?

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

  14. 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 Single sngStart = Timer Do While Timer < (sngStart + interval) DoEvents Loop End Sub … call delay(1) built-in function. Returns seconds since midnight let's computer to other things, while waiting

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

  16. 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?

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

  18. 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. • …

  19. 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” ??

  20. 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 i End Sub

  21. 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 = thing1 thing1 = thing2 thing2 = holder

  22. Shuffle Private Sub cmdShuffle_Click() Dim i As Integer, k As Integer Dim holder As String For 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 array Next i End Sub

  23. Homework • Read chapter 4 (Memory/Concentration) and start implementing it • You can make your own images!

More Related