Chapter 9 Value-Returning Functions and Mouse … 9 gaddis.pdf© 2010 Pearson Addison-Wesley. All...
Transcript of Chapter 9 Value-Returning Functions and Mouse … 9 gaddis.pdf© 2010 Pearson Addison-Wesley. All...
Addison Wesley
is an imprint of
© 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 9
Value-Returning Functions and Mouse
Input
Starting Out with
Games & Graphics in C++
Tony Gaddis
Copyright © 2010 Pearson Addison-Wesley
9.1 Writing a Value-Returning
Function
1-2
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.
Copyright © 2010 Pearson Addison-Wesley
9.1 Writing a Value-Returning
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
1-3
Figure 9-1 A statement that calls the dbRND function
Figure 9-2 The random function returns a value
Copyright © 2010 Pearson Addison-Wesley
9.1 Writing a Value-Returning
Function
• 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
1-4
Making the Most of the Return Statement
Writing Your Own Value-Returning Functions
Simplified version returns an expression
Copyright © 2010 Pearson Addison-Wesley
9.1 Writing a Value-Returning
Function
• 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
1-5
Returning Boolean Values
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with the Mouse
1-6
Concept:
The Dark GDK provides functions that
your program can use to interact with the
mouse.
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with the Mouse
• You can call the dbMouseX
and dbMouseY functions to
get the current position of the
mouse pointer
1-7
Getting the Mouse Coordinates
Figure 9-12 Example output of Program 9-6
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with 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
1-8
Showing, Hiding, and Positioning the Mouse
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with the Mouse
• 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
1-9
Detecting Button Clicks
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with the Mouse
• 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
1-10
Processing Full Mouse Clicks
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with the Mouse
• 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
1-11
Clicking Sprites
Figure 9-15 A sprite’s upper-left and
lower-right corner coordinates
Copyright © 2010 Pearson Addison-Wesley
9.2 Working with the Mouse
• 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
coordinates1-12
Creating a Custom Mouse Pointer
Copyright © 2010 Pearson Addison-Wesley
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
1-13
Copyright © 2010 Pearson Addison-Wesley
9.3 The Bug Zapper Game
1-14
Figure 9-18 Screens from the Bug Zapper game
Addison Wesley
is an imprint of
© 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 9
Value-Returning Functions and Mouse Input
QUESTIONS
?