160 likes | 174 Views
Learn how to detect positions on top/within shapes like rectangles, near points, and past lines in Visual Basic for game programming. Understand techniques for handling mouse events, detecting hits, and controlling objects. Discover the benefits of user-defined procedures and check if certain conditions are met within a game environment. This tutorial covers essential concepts for creating interactive games and developing logic for game mechanics.
E N D
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.height b.width
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