Game Development Reference
In-Depth Information
Chapter 11
Finishing the Game
11.1 Introduction
In this chapter, we will finish the
Painter
game by adding a few extra features, such as
motion effects, sounds and music, and maintaining and displaying a score. In order
to do the latter, we introduce the
string
and
char
types.
11.2 Adding Motion Effects
In order to make the game more visually appealing, we can introduce a nice rota-
tional effect in the movement of the paint cans to simulate the effect of wind and
friction on the falling down motion. In the
Painter10
program, you can find the final
version of the game where we added this motion effect to the cans. Adding such an
effect is not very complicated, only a single line needs to be changed in the
Draw
method. In order to achieve such an effect, we use the
Math.Sin
method. By letting
the value depend on the current position of the can, we get different values depend-
ing on the position of can. We use this value to apply a
rotation
on the sprite object.
In order to draw the rotated paint cans on the screen, we use the extended version
of the
Draw
method in the
SpriteBatch
class that we also used in the
Cannon
class. We
call this method as follows:
spriteBatch.Draw(currentColor, position,
null
, Color.White,
(
float
)Math.Sin(position.Y / 50.0)
∗
0.05f,
Vector2.Zero, 1.0f, SpriteEffects.None, 0);
You can see in this instruction that we use the
y
coordinate of the paint can position
to get different rotation values. Furthermore, we divide it by 50 to get a nice slow
movement, and we multiply the outcome with 0
.
05 to reduce the amplitude of the
sine, so that the rotation looks more or less realistic.