Programming games using Visual Basic Detecting a position on top/within a rectangle, near a point, past a line Mouse events.

Slides:



Advertisements
Similar presentations
IS660Z Programming Games Using Visual Basic Overview of Cannonball.
Advertisements

Visual Basic: ballistics
1.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
VB Controls and Events Week 7: Picture Box, Image Box, Option, Check box, Mouse over, Frames, Shapes.
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
IS660Z – Programming Games Using Visual Basic Required session 3 June 16, 2004.
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
Non-Perfect Squares Learning Goal:
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Apply Sub Procedures/Methods and User Defined Functions
Scratch the Cat. Object Oriented Programing Writing computer programs Based on Objects Instead of Actions Based on Data Instead of Logic.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Visual Basic Games: Prepare for Hangman
Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3.
Programming & Scratch. Programming Learning to program is ultimately about learning to think logically and to approach problems methodically. The building.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
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.
Chapter 4: The Selection Process in Visual Basic.
Programming Games in Visual Basic: Data base Catch up questions Data base: generalities & specifics on Visual Basic links Lab: work on projects.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Mouse Events. Mouse Driven Events Unlike control_click() which is passed no arguments (unless it's an index for an array), MouseDown, MouseUp and MouseMove.
Basic Controls & Properties Chapter 2. Overview u VB-IDE u Basic Controls  Command Button  Label  Text Box  Picture Box u Program Editor  Setting.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
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.
Programming games Show your version of Bo the dog. Start cannonball Preview: video, audio work session (cannonball) Homework: Cannonball with ball in a.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
COM148X1 Interactive Programming Lecture 7. Topics Today HCI Event Handling.
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”
Introduction to Using the Notebook 10 Software for SMART Board Day 2 LIVINGSTON PARISH PUBLIC SCHOOLS Facilitated by S. Waltman.
Programming Games Reprise on drawing on canvas. Jargon (concepts): objects. Demonstrate slingshot. Mouse events. Work on your cannonball. Homework: Finish.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Build-A-Button Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, October 8, 2003.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
[My] Experiences building games in Visual Basic & Flash Focus on 'cannonball' Jeanine Meyer Math Senior Seminar.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Computer Game Design ActionScript is… Object-oriented programming Everything you do in ActionScript does something to some object* Some objects.
Pay Example (PFirst98) Please use speaker notes for additional information!
1 Project designed and created by M. Shajith Kumar.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Variables and Random Numbers Computer App Session 4.
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Lesson Seven: Using Flags. What Are Flags? Flags are Variables or Switches Used to Help With Logic Control of your character. Normally, Flags are used.
Computer Science Up Down Controls, Decisions and Random Numbers.
Visual Basic Fundamental Concepts
Complex Conditionals Human languages are ambiguous
Programming & Scratch.
Scratch for Interactivity
Keyboard Input.
Intro & Point-to-Box, Circle-to-Circle, Point-to-Circle
Collision Detection Box-to-Box.
Let's Race! Typing on the Home Row
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Visual Basic..
Complex Conditionals Human languages are ambiguous
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Visual Basic: Week 5 Review User defined functions
CIS125GA_03 Things that help
Presentation transcript:

Programming games using Visual Basic Detecting a position on top/within a rectangle, near a point, past a line Mouse events

Motivation For cannonball (and, possibly for your own game), you may want to detect –A control ‘on top of’/ ‘inside’ a shape (say, a rectangle) on the form –Pressing down on the mouse button ‘on top of’/ ‘inside’ a shape VB has no sense of ‘on top of’ or ‘inside’. Your code must make the calculation.

Note: Many different controls have click events but shapes do not. (Alternative approach to using a shape is using a label.) This discussion will go back and forth between the called procedure that calculates if there is a hit and various places to call this procedure.

User-defined procedure Sub hittarget(x as Single, y as Single) as Boolean User defined procedure with two arguments (representing a horizontal and vertical position) Procedure returns a value: true or false. Coded by assigning value to hittarget hittarget = True CALLING code can use call in an expression, for example: If hittarget(xx,yy) then …

Why data type single? … because MouseDown uses parameters of type Single. But aren’t positions on the form given in twips (1400/inch) and aren’t these whole numbers? Answer: You can change the unit on the form to pixels or inches or centimeters or other things!

Determine… Is position X, Y within rectangle b b.left, b.top b.width b.height

Conditions to check Y >= b.Top Y <= b.Top + b.Height X >= b.Left X <= b.Left + b.Width All 4 must be true for X, Y position to be ‘on’ the rectangle.

Benefits of hittarget Cannonball’s hittarget is a good example of the benefits of defining a user-defined procedure as opposed to putting the code in the event procedure(s) –There are TWO places: MouseDown to be used to move the target by dragging and Timer for checking if the cannonball has hit the target. –More subjective reason: hittarget performs a well-defined, distinct task.

Check if cannonball hits the ground Equivalent to checking if a position X, Y lies below a line Y > sngGrass Do not need to check anything regarding X

Discrete versus Continuous movement Movement of cannonball (and most movements in computer games) are discrete = quantized, not continuous. So…. Cannonball may move from being above ground to being below surface of ground Also, cannonball is not a single point If YY > sngGrass – ballrad then Beep YY = sngGrass – ballrad timFlight.Enabled = False End If

Closetocannon(XX as Single, YY as Single) As Boolean You cannot require the player to click exactly on the cannon tip. So, you program a check that has a ‘tolerance’ or ‘margin’ If (Abs(XX – linCannon.X2) < 100) And _ (Abs(YY – linCannon.Y2) < 100)) Then Closetocannon = True Else Closetocannon = False End If Used to continue to next line

Mouse events Dragging requires attention to three mouse events –Form_MouseDown –Form_MouseMove –Form_MouseUp For cannonball, two things can be dragged: the target or the tip of the cannon.

Strategy In MouseDown, check if the mouse position (X,Y) is on the target or close to the cannon tip. If one or the other is true, set the associated Boolean value (also known as a flag) In MouseMove, if the flag is set, move target or cannon tip. In MouseUp, reset flags to false.

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If closetocannon(X, Y) Then blnCannonmove = True Else blnCannonmove = False End If If hittarget(X, Y) Then blnTargetmove = True sngDragx = X - shpTarget.Left sngDragy = Y - shpTarget.Top Else blnTargetmove = False End If End Sub Offsets to compensate for NOT picking up at corner

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If blnTargetmove Then shpTarget.Left = X - sngDragx shpTarget.Top = Y - sngDragy End If If blnCannonmove Then linCannon.X2 = X linCannon.Y2 = Y End If End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) blnCannonmove = False blnTargetmove = False formCannonball.Refresh End Sub

Lab/Homework Work on projects. Read chapter 6, if you haven’t done so already, to be able to ask questions. Check out courseinfo –Notes –Schedule –Grading allocation