1 / 16

Programming games using Visual Basic

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

Download Presentation

Programming games using Visual Basic

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. Programming games using Visual Basic Detecting a position on top/within a rectangle, near a point, past a line Mouse events

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related