Game Development Reference
In-Depth Information
if
(currentKeyboardState.IsKeyDown(Keys.R)
&& previousKeyboardState.IsKeyUp(Keys.R))
currentColor = colorRed;
If the player did press the 'R' key, we assign the red colored ball sprite to the
currentColor
variable. We follow the same procedure for the 'G' and 'B' keys, which
gives us the following
if
-instruction:
if
(currentKeyboardState.IsKeyDown(Keys.R)
&& previousKeyboardState.IsKeyUp(Keys.R))
currentColor = colorRed;
else if
(currentKeyboardState.IsKeyDown(Keys.G)
&& previousKeyboardState.IsKeyUp(Keys.G))
currentColor = colorGreen;
else if
(currentKeyboardState.IsKeyDown(Keys.B)
&& previousKeyboardState.IsKeyUp(Keys.B))
currentColor = colorBlue;
Finally, we only need to add an additional drawing call to the
Draw
method so that
the current colored ball is drawn on top of the cannon:
spriteBatch.Draw(currentColor, barrelPosition,
null
, Color.White, 0f,
colorOrigin, 1.0f, SpriteEffects.None, 0);
Try to run the
Painter2
program now, and see how the program responds to moving
the mouse and pressing the 'R', 'G' or 'B' keys.
6.6 What You Have Learned
In this chapter, you have learned:
•
what an enumerated type is;
•
how to react to mouse clicks and button presses using the
if
-instruction;
•
how to formulate conditions for these instructions using boolean values;
•
how to use
if
-instructions with different alternatives;
•
how to deal with keyboard and gamepad input.