Ship

The following code shows the beginning of the Ship class, which is also derived from the Sprite class:

class Ship : Sprite
{
// The ship's direction in degrees. (Initially right.)
public float Heading = 0;

// Points to draw the ship at the origin pointing up.
private PointF[] Points;

// Constructor.
private const float ShipRadius = 10;
public Ship(Rectangle spaceBounds)
: base(new RectangleF(
spaceBounds.X + spaceBounds.Width / 2 - ShipRadius,
spaceBounds.Y + spaceBounds.Height / 2 - ShipRadius,
2 * ShipRadius, 2 * ShipRadius),
new PointF(), Brushes.Silver)
{
// Define the points used to draw the ship when Heading = 0.
Points = new PointF[]
{
new PointF(-ShipRadius / 2, 0),
new PointF(-ShipRadius, -ShipRadius),
new PointF(ShipRadius, 0),
new PointF(-ShipRadius, ShipRadius),
};
}

This class inherits its position from the Sprite class. It adds a Heading value to indicate the direction that the ship is facing.

The Points array, which is initialized by the class's constructor, contains a list of points that define the ship's simple shape centered at the origin when its Heading is 0 so it is pointing to the right.

The following code shows how a ship draws itself:

    // Draw.
public override void Draw(Graphics gr)
{
if (IsDestroyed)
{
// Draw an explosion.
gr.DrawImage(Properties.Resources.boom, Bounds);
}
else
{
// Draw a normal ship.
// Transform to rotate and position the ship.
GraphicsState state = gr.Save();
gr.RotateTransform(Heading);
gr.TranslateTransform(
Bounds.X + Bounds.Width / 2,
Bounds.Y + Bounds.Height / 2,
MatrixOrder.Append);
gr.FillPolygon(Brush, Points);
gr.Restore(state);
}
}

The Draw method first checks the ship's IsDestroyed value. If IsDestroyed is true, the code draws the boom resource at the ship's location to display an explosion graphic.

If IsDestroyed is false, the code saves the Graphics object's current state. It then uses a rotation transformation to rotate the ship for its current heading. It follows the rotation with a translation to move the ship to its correct location on the playing area. The method then draws the ship and restores the Graphics object's saved state so the rotation and translation are removed for future drawing commands.

The following code controls the ship's movement:

    // Accelerate.
public void Accelerate()
{
const float accceleration = 0.5f;
double radians = Heading * Math.PI / 180;
Velocity.X += (float)(accceleration * Math.Cos(radians));
Velocity.Y += (float)(accceleration * Math.Sin(radians));
}

// Turn left.
private const float TurnDegrees = 6;
public void TurnLeft()
{
Heading -= TurnDegrees;
}

// Turn right.
public void TurnRight()
{
Heading += TurnDegrees;
}

The Accelerate method adds a small amount of acceleration to the ship's velocity, using some trigonometry to make the acceleration point in the direction that the ship is facing. This ship has no brakes and cannot move sideways. If you want to slow down, you need to turn the ship around and accelerate back in the direction opposite of the ship's velocity.

The TurnLeft and TurnRight methods change the ship's Heading by small amounts.

The last part of the Ship class deals with shooting bullets:

    // Return the position of the ship's nose.
public PointF NosePosition()
{
PointF center = Bounds.Center();
double radians = Heading * Math.PI / 180;
double x = center.X + ShipRadius * Math.Cos(radians);
double y = center.Y + ShipRadius * Math.Sin(radians);
return new PointF((int)x, (int)y);
}

// Make a bullet moving out of the ship's nose.
public Bubble MakeBullet()
{
const int bulletSpeed = 10;
const int bulletR = 2;
double radians = Heading * Math.PI / 180;
PointF velocity = new PointF(
(int)(bulletSpeed * Math.Cos(radians)),
(int)(bulletSpeed * Math.Sin(radians)));
PointF nose = NosePosition();
RectangleF bulletRect = new RectangleF(
nose.X - bulletR, nose.Y - bulletR, 2 * bulletR, 2 *
bulletR);
return new Bubble(bulletRect, velocity, Brushes.White);
}
}

The NosePosition method calculates the position of the ship's nose. The MakeBullet method uses the nose position to create a new small Bubble object to represent a bullet placed at the ship's nose and moving away from the ship.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset