1 / 15

Chapter 9 Value-Returning Functions and Mouse Input

Chapter 9 Value-Returning Functions and Mouse Input. Starting Out with Games & Graphics in C++ Tony Gaddis. 9.1 Writing a Value-Returning Function. Concept:

spiro
Télécharger la présentation

Chapter 9 Value-Returning Functions and Mouse Input

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. Chapter 9Value-Returning Functions and Mouse Input Starting Out with Games & Graphics in C++ Tony Gaddis

  2. 9.1 Writing a Value-Returning Function Concept: A value-returning function is a function that returns a value to the part of the program that called it. The Dark GDK provides various value-returning functions that you have already used. You can also write your own value-returning functions.

  3. 9.1 Writing a Value-Returning Function Figure 9-2 The random function returns a value Figure 9-1 A statement that calls the dbRND function • A value-returning function returns a value to the statement that called it • Can be used like any other value • Assigned to a variable • Displayed on the screen • Used in a mathematical expression • And so on

  4. 9.1 Writing a Value-Returning Function Writing Your Own Value-Returning Functions Making the Most of the Return Statement Simplified version returns an expression • You write a value-returning function in the same way you write a void function, with two exceptions: • You must specify a data type • You must have a return statement

  5. 9.1 Writing a Value-Returning Function Returning Boolean Values Boolean functions return the bool values true or false Used to test a condition Return either true or false to indicate whether or not the condition exists

  6. 9.2 Working with the Mouse Concept: The Dark GDK provides functions that your program can use to interact with the mouse.

  7. 9.2 Working with the Mouse Getting the Mouse Coordinates Figure 9-12 Example output of Program 9-6 You can call the dbMouseX and dbMouseY functions to get the current position of the mouse pointer

  8. 9.2 Working with the Mouse Showing, Hiding, and Positioning the Mouse • Mouse pointer is visible by default • You can hide the mouse pointer with the dbHideMouse function • Still active within the program’s window • Can still get position with dbMouseX and dbMouseY functions • You can show the mouse pointer with the dbShowMouse function • You can position the mouse anywhere in the program’s window, at any time, by calling the dbPositionMouse function, passing the X and Y coordinates as arguments

  9. 9.2 Working with the Mouse Detecting Button Clicks • You can use the dbMouseClick function to determine whether the user is pressing a mouse button • The dbMouseClick function can detect up to four buttons • Returns an integer value that indicates which, if any, of the mouse buttons is being pressed • 0 if none of the mouse buttons are being pressed • 1 if the left mouse button is being pressed • 2 if the right mouse button is being pressed • 4 if the third mouse button is being pressed • 8 if the fourth mouse button is being pressed

  10. 9.2 Working with the Mouse Processing Full Mouse Clicks • In some situations you only want to perform an action once per mouse click • This functionality is not provided by the Dark GDK, so we’ll have to write our own function • Determine whether or not the mouse button is being pressed • If so, get the mouse coordinates • Perform a loop that makes the program wait until the mouse button is released before continuing

  11. 9.2 Working with the Mouse Clicking Sprites Figure 9-15 A sprite’s upper-left and lower-right corner coordinates Clicking images with the mouse is a common technique used in games and graphics programs If we know the XY coordinates of the sprite’s upper-left and lower-right corners we can determine if the mouse is inside the sprite’s bounding rectangle

  12. 9.2 Working with the Mouse Creating a Custom Mouse Pointer • Create a sprite with the image you want to use for the mouse pointer • Hide the regular mouse pointer • In the game loop • Get mouse pointer’s location • Move sprite using mouse pointer’s coordinates

  13. 9.3 The Bug Zapper Game The Bug Zapper game displays an animated sprite of a bug The player zaps the bug by clicking it with the mouse After a bug is clicked, a new one appears at a random location on the screen The game will run for 10 seconds, then end Zap as many bugs as possible before time runs out When the game is over, a screen displays how many bugs were zapped

  14. 9.3 The Bug Zapper Game Figure 9-18 Screens from the Bug Zapper game

  15. Chapter 9Value-Returning Functions and Mouse Input QUESTIONS ?

More Related