Game Development Reference
In-Depth Information
Chapter 5
Knowing What the Player Is Doing
5.1 Introduction
In the previous chapter, we have seen a few examples of loading and displaying
sprites. We have also seen that we can use the current time information to change
the position of a sprite. In this chapter, we will discuss how to deal with player
input in our game. We will show how to retrieve what the player is doing and how
the game world changes depending on that information. We will start with a simple
extension of the FlyingSprites program, which draws the balloon at the position of the
mouse pointer. In the next chapter, we will look into different types of input, such
as keyboard or gamepad input.
5.2 A Sprite Following the Mouse Pointer
5.2.1 Retrieving the Mouse Position
Now that we know how to display sprites on the screen, let us see if we can use
player input to control the position of a sprite. Have a look at the program Balloon1
(see Listing 5.1 ). Actually, there is not a lot of difference between this program and
the FlyingSprites program. In the FlyingSprites program, we calculated the position of
the balloon by using the milliseconds fraction of the passed game time:
int yposition = 480
gameTime.TotalGameTime.Milliseconds / 2;
balloonPosition = new Vector2(300, yposition);
The position we calculated is stored in the variable balloonPosition . Now we want to
create a program where the balloon position is not calculated based on the passed
time, but we want the balloon position to be the same as the current mouse position.
Getting the current mouse position is very easy. We have to use the class Mouse
 
Search WWH ::




Custom Search