Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

IS660Z Programming Games Using Visual Basic Overview of Cannonball.
Visual Basic: ballistics
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
How to get started with Excel VBA. We need to enable programming in Excel  the “Developer menu”
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
Exploring Microsoft Excel 2002 Chapter 8 Chapter 8 Automating Repetitive Tasks: Macros and Visual Basic for Applications By Robert T. Grauer Maryann Barber.
Controls General Discussion. VB Controls Visual Basic Controls A control is the generic name for any object placed on a form Controls may be images,
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
05/09/ Introducing Visual Basic Sequence Programming.
Making a Timer in Alice.
Multimedia ! Graphics ! Animation ! Sound Tip Of the Day 4 Tip of the Day: When using graphic methods at Form Load, the AutoRedraw property of the container.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Multiple Forms and Standard Modules
InvEasy (Project1) Please use speaker notes for additional information!
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Visual Basic Games: Prepare for Hangman
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
FIRST GADGETEER PROJECT. Where are you? Making a VS project Parts of a C# program Basics of C# syntax Debugging in VS Questions? 2.
Programming games Flash drawing trick(s). Past due: Your completion of rock-paper-scissors) Classwork: Bouncing ball: read tutorial. Start coding.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Practical Programming COMP153-08S Lecture: Repetition.
Programming games using Visual Basic Detecting a position on top/within a rectangle, near a point, past a line Mouse events.
Controlling Program Flow with Decision Structures.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
Week 1 Lecture 1 Slide 1 CP2028 Visual Basic Programming 2 “The VB Team” Copyright © University of Wolverhampton CP2028 Visual Basic Programming 2 v Week.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Simple Windows Applications
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CS285 Introduction - Visual Basic
Visual Basic: Week 5 Review User defined functions
If, Subroutines and Functions
Chapter 8 - Functions and Functionality
3.2 Working with Data Scope of variables 29/07/2019.
Introduction to Computer Programming IT-104
Presentation transcript:

Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3

Global versus Local Variables Variables (aka internal variables) hold data independent of properties in controls. Global variables are defined (persist) independently of any particular procedure. Declared (using DIM) in the (General) section ‘above’ all the procedures. Local variables are defined & only persist within a procedure. Rule: local, except when a value must be global. Rule: don’t economize on names or length of names. Use i, j, n, for simple indexing, otherwise use meaningful names. Convention: use int, sng, bln, etc. as part of name.

Option Explicit Type in as first line in (General) section (this is first line in the code window). This will force an error if and when you use a variable without first declaring it in a DIM statement. WHY do this? This will warn you of any spelling errors.

Parameters Procedures (such as event procedures or procedures you will define yourself in the later projects) can have parameters. These are values set by VB or by the calling program. Sub lblCard_Click(Index as Integer) Sub Form_KeyDown(KeyCode as Integer, Shift as Integer) When you define your own procedures, these two facts will be significant (and will be repeated) –Parameters are ‘call by reference’. The called procedure can change the value –The name of the parameter has local scope. The name in the calling procedure can be different than the name in the called procedure. In Cannonball, hittarget is called for two different reasons and different names are used.

Conditional statements IF condition Then 1 or more lines of code [Else 1 or more lines of code] End If Select Case expression Case 1 or more values OR conditions code Case 1 or more values or conditions code [Case Else] code ] End Select

Chance game features Rules of the game: –Add up values on the dice –on a ‘first throw’, 7 or 11 wins, 2, 3 or 12 loses. For anything else, the value becomes ‘the point’ –On follow-up throws, throwing the point again means a win. Throwing a 7 means a loss. Implications for implementation: need to maintain information on what the status is. This is called the state of the game. The state of the chance game is –Is it a first or a follow-up throw? –What is ‘the point’

Chance Design Tasks State of the game—done with global variables Get (pseudo-) random values—use Randomize and Rnd. Also use expression: Int(Rnd*6) + 1 Selectively show images of dice—done with image controls, the Visible property, and using an extra control in the case of doubles (e.g., two 2s) Display rules: command button code: MsgBox “…” End game: use command button code: End Rules of game: done using Select statements within Select statement (outer statement distinguishes between first throw and follow-up throw)

Preview: Timers The timer control has one event: timer. and two critical properties: –Interval –Enabled The timer event will happen if Enabled is set to True and Interval is non-zero. The timer event will happen every Interval milliseconds. –If Interval is set to 500, the timer event will happen every ½ second. Timer is more like an alarm clock.

Top Left Top has been increased Top and Left of the bounding rectangle

Moving ball Place a timer control on the form (it will not be visible). Set interval to 300, enabled to True. Place a shape control called shpBall. (make it a solid circle). Code in timer1.timer: shpBall.Top = shpBall.Top +.5*shpBall.Height Run program. You will need to use the stop button to stop it.

How to move ball… twips: –up –left –right How to move ball a different amount?

Project: control direction of object Objective: use keys in keypad to set (change) direction of moving ball. Need –Way to move (animate) a ball –Event that signals key down = Form_KeyDown –Key codes associated with the selected keys –Select statement for specifying action for each case –Way to encode right, left, up, down, and later diagonal directions

Move a ball Do animation using timer control and a start button that sets the interval and enables the time. Code in tmr.Timer (happens every interval) shpBall.Top = shpBall.Top + vspeed * shpBall.Height shpBall.Left = shpBall.Left + hspeed * shpBall.Width vspeed and hspeed will be changed in the event procedure for KeyDown

Setting the two speeds Sub Form_KeyDown(KeyCode as Integer, Shift as Integer) Select Case KeyCode case leftkey hspeed = -unit vspeed = 0 case upkey hspeed = 0 vspeed = -unit case sekey hspeed = unit vspeed = unit ….

Case Else Player should be given feedback for a bad keystroke … Case else MsgBox “Click one of the direction keys” or (for your own debugging) MsgBox “You clicked key” & Str(KeyCode) End Select

KeyDown Need to make sure that the Form_KeyDown event is triggered. This requires you do disable (set the enabled property to false) other controls on the board and also setfocus for the form. How do you know to do this: I learned from experience….

cmdStart_Click tmrBall.Interval = 100 tmrBall.Enabled = True cmdStart.Enabled = False frmControl.SetFocus

Form_Load unit =.3 upkey = 104 rightkey = 102 downkey = 98 leftkey = 100 nekey = 105 sekey = 99 swkey = 97 nwkey = 103

How to stop? Use a designated key (add to Form_Load) stopkey = 101 In the Select Case statement in KeyDown add, Case stopkey tmrBall.Enabled = False cmdStart.Enabled = True

Global variables Normally, variables are active / accessible / alive just in the local subroutines and functions … but, you need all these variables to persist between events –vspeed, hspeed are set in KeyDown and used in tmrBall.Timer –leftkey, rightkey, etc. are set in form_Load and used in KeyDown So, you need to have a DIM statement (called a declaration) in the (General) section

Review: general Incremental development Important: you don’t think of things in the complete, proper, final order. Divide and conquer = take the task and divide it into subtasks. Incremental testing: do first with just two directions (left and right), then up and down, then diagonals, then stop.

Review: specific tmrBall.Timer eventhandler moves the ball using the vspeed and hspeed, which were set elsewhere form_keydown eventhandler uses cases within a select case statement to set vspeed and hspeed –one of the cases will turn off the timer and reset the start button cmdStart_Click eventhandler starts off the animation by setting tmrBall.Interval and tmrBall.Enabled. It also turns itself off (sets cmdStart.Enabled to false) and frmControl.Focus to True.

Assignments In lab, –implement the control exercise or –work on chance (dice) game Work on controlling ball exercise Complete chance to show. Work on bouncing ball, if you finish chance early.