shooter game
Download
Skip this Video
Download Presentation
Shooter Game

Loading in 2 Seconds...

play fullscreen
1 / 59

Shooter Game - PowerPoint PPT Presentation


  • 81 Views
  • Uploaded on

Shooter Game. XBOX LIVE indie games. Add new class. by pressing SHIFT + ALT + C, and typing in the name Player.cs . Press ENTER. Player.cs (Player class ).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Shooter Game' - dudley


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
shooter game

Shooter Game

XBOX LIVE indie games

add new class
Add new class
  • by pressing SHIFT + ALT + C, and typing in the name Player.cs. Press ENTER.
player cs player class
Player.cs (Player class)

using System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;namespace Shooter{ class Player { public void Initialize() { } public void Update() { } public void Draw() { } }}

player class
Player class

// Animation representing the playerpublic Texture2D PlayerTexture;// Position of the Player relative to the upper left side of the screenpublic Vector2 Position;// State of the playerpublic bool Active;// Amount of hit points that player haspublic int Health;// Get the width of the player shippublic int Width{ get { return PlayerTexture.Width; }}// Get the height of the player shippublic int Height{ get { return PlayerTexture.Height; }}

the initialize method
the Initialize() method

Replace the Initialize() method inside the Player class (including the { } marks) with the following code:

public void Initialize(Texture2D texture, Vector2 position){PlayerTexture = texture; // Set the starting position of the player around the middle of the screen and to the back Position = position;// Set the player to be active Active = true;// Set the player health Health = 100;}

slide6

Replace the Draw() method inside the Player class (including the { } marks) with the following code:

public void Draw(SpriteBatchspriteBatch){ spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,SpriteEffects.None, 0f);}

inside game1 cs
inside game1.cs

Start at the top of the code, find the string marked public class Game1 : Microsoft.Xna.Framework.Game. A few lines down, below SpriteBatchspriteBatch, add these lines:

// Represents the playerPlayerplayer;

Look down the code, find the method called protected override void Initialize(). Inside that method, add these lines:

// Initialize the player classplayer = new Player();

Look down the code, find the method called protected override void LoadContent(). Inside that method, below theSpriteBatch assignment operator, add these lines:

// Load the player resources Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X,

GraphicsDevice.Viewport.TitleSafeArea.Y+GraphicsDevice.Viewport.TitleSafeArea.Height / 2);player.Initialize(Content.Load<Texture2D>("player"), playerPosition);

slide8

Look down the code, find the method called protected override void Draw(GameTimegameTime). Inside that method, belowGraphicsDevice.Clear(Color.CornflowerBlue); add these lines:

// Start drawingspriteBatch.Begin();

// Draw the Playerplayer.Draw(spriteBatch);

// Stop drawingspriteBatch.End();

game1 class
Game1 class

Look for the first { mark under the start of the Game1 class, and go just below the Player player; instantiation call you made in the last step. Add a new line and then add the following:

// Keyboard states used to determine key pressesKeyboardStatecurrentKeyboardState;KeyboardStatepreviousKeyboardState;// Gamepad states used to determine button pressesGamePadStatecurrentGamePadState;GamePadStatepreviousGamePadState; // A movement speed for the playerfloat playerMoveSpeed;

Scan down the code for the Initialize() method, and below the player = new Player (); code you wrote in the last step, add these lines:

// Set a constant player move speedplayerMoveSpeed = 8.0f;//Enable the FreeDrag gesture.TouchPanel.EnabledGestures = GestureType.FreeDrag;

slide10

Add UpdatePlayermethod

private void UpdatePlayer(GameTimegameTime)

{// Get Thumbstick Controlsplayer.Position.X += currentGamePadState.ThumbSticks.Left.X *playerMoveSpeed;player.Position.Y -= currentGamePadState.ThumbSticks.Left.Y *playerMoveSpeed;// Use the Keyboard / Dpad if (currentKeyboardState.IsKeyDown(Keys.Left) ||currentGamePadState.DPad.Left == ButtonState.Pressed) {player.Position.X -= playerMoveSpeed; } if (currentKeyboardState.IsKeyDown(Keys.Right) ||currentGamePadState.DPad.Right == ButtonState.Pressed) {player.Position.X += playerMoveSpeed; } if (currentKeyboardState.IsKeyDown(Keys.Up) ||currentGamePadState.DPad.Up == ButtonState.Pressed) {player.Position.Y -= playerMoveSpeed; } if (currentKeyboardState.IsKeyDown(Keys.Down) ||currentGamePadState.DPad.Down == ButtonState.Pressed) {player.Position.Y += playerMoveSpeed; }

// Make sure that the player does not go out of boundsplayer.Position.X = MathHelper.Clamp(player.Position.X, 0,GraphicsDevice.Viewport.Width - player.Width);player.Position.Y = MathHelper.Clamp(player.Position.Y, 0,GraphicsDevice.Viewport.Height - player.Height);

}

the update method inside the game1 class
the Update() method inside the Game1 class

// Save the previous state of the keyboard and game pad so we can determinesingle key/button pressespreviousGamePadState = currentGamePadState;previousKeyboardState = currentKeyboardState;// Read the current state of the keyboard and gamepad and store itcurrentKeyboardState = Keyboard.GetState();currentGamePadState = GamePad.GetState(PlayerIndex.One);//Update the playerUpdatePlayer(gameTime);

inside animation cs
inside animation.cs

Delete all the using statements at the top, and replace them with the following lines

// Animation.cs

// Using declarations

using System;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.Graphics;

slide13

// The image representing the collection of images used for animation

Texture2D spriteStrip;// The scale used to display the sprite strip

float scale;// The time since we last updated the frame

intelapsedTime;// The time we display a frame until the next one

intframeTime;// The number of frames that the animation contains

intframeCount;// The index of the current frame we are displaying

intcurrentFrame;// The color of the frame we will be displaying

Color color;// The area of the image strip we want to display

Rectangle sourceRect = new Rectangle();// The area where we want to display the image strip in the game

Rectangle destinationRect = new Rectangle();// Width of a given frame

public intFrameWidth;// Height of a given frame

public intFrameHeight;// The state of the Animation

public bool Active;// Determines if the animation will keep playing or deactivate after one run

public bool Looping;// Width of a given frame

public Vector2 Position;public void Initialize() { }

public void Update() { }

public void Draw() { }

Place your cursor just after the { mark inside the Animation class

replace the initialize method you just filled in with this version
Replace the Initialize() method you just filled in with this version

public void Initialize(Texture2D texture, Vector2 position,

intframeWidth, intframeHeight, intframeCount,

intframetime, Color color, float scale, bool looping)

{

// Keep a local copy of the values passed in

this.color = color;

this.FrameWidth = frameWidth;

this.FrameHeight = frameHeight;

this.frameCount = frameCount;

this.frameTime = frametime;

this.scale = scale;

Looping = looping;

Position = position;

spriteStrip = texture;

// Set the time to zero

elapsedTime = 0;

currentFrame = 0;

// Set the Animation to active by default

Active = true;

}

slide15

Replace the Update() method you just filled in above with this version

public void Update(GameTimegameTime) {

// Do not update the game if we are not active

if (Active == false) return;// Update the elapsed time

elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;// If the elapsed time is larger than the frame time

// we need to switch frames

if (elapsedTime > frameTime) { // Move to the next frame

currentFrame++;

// If the currentFrame is equal to frameCount reset currentFrame to zero

if (currentFrame == frameCount) {

currentFrame = 0; // If we are not looping deactivate the animation

if (Looping == false) Active = false;

} // Reset the elapsed time to zero

elapsedTime = 0;

} // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width

sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);// Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width

destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,

(int)Position.Y - (int)(FrameHeight * scale) / 2,

(int)(FrameWidth * scale),

(int)(FrameHeight * scale));

}

replace the draw method you just filled in above with this version
Replace the Draw() method you just filled in above with this version

// Draw the Animation Strip

public void Draw(SpriteBatchspriteBatch)

{

// Only draw the animation when we are active

if (Active)

{

spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color);

}

}

inside player cs
inside player.cs

Near the top of the Player.cs file, just after the first { mark after the Player class begins, replace the public Texture2D PlayerTexture; line with the following:

// Animation representing the player

public Animation PlayerAnimation;

the Initialize() method

// Initialize the player

public void Initialize(Animation animation, Vector2 position)

{

PlayerAnimation = animation;

// Set the starting position of the player around the middle of the screen and to the back

Position = position;

// Set the player to be active

Active = true;

// Set the player health

Health = 100;

}

slide18

Find the Width and Height properties in the Player class, and replace them with the following:

// Get the width of the player shippublic int Width{ get { return PlayerAnimation.FrameWidth; }}// Get the height of the player shippublic int Height{ get { return PlayerAnimation.FrameHeight; }}

slide19

Find the Draw() method and replace it with the following:

// Draw the player

public void Draw(SpriteBatchspriteBatch)

{

PlayerAnimation.Draw(spriteBatch);

}

Finally, find the Update() method in the Player class and replace it with the following

// Update the player animation

public void Update(GameTimegameTime)

{

PlayerAnimation.Position = Position;

PlayerAnimation.Update(gameTime);

}

inside game1 cs1
inside game1.cs

Find the LoadContent() method. Find the line marked player.Initialize(Content.Load<Texture2D>("player"), playerPosition);and replace it with the following:

// Load the player resources

Animation playerAnimation = new Animation();

Texture2D playerTexture = Content.Load<Texture2D>("shipAnimation");

playerAnimation.Initialize(playerTexture, Vector2.Zero, 115, 69, 8, 30, Color.White, 1f,true);

Vector2 playerPosition = new Vector2 (GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y

+ GraphicsDevice.Viewport.TitleSafeArea.Height / 2);

player.Initialize(playerAnimation, playerPosition);

Find the UpdatePlayer() method. At the top of the method, add the following line:

player.Update(gameTime);

inside parallaxingbackground cs
inside parallaxingbackground.cs

// ParallaxingBackground.cs

using System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.Graphics;

slide22

Place your cursor just after the { mark inside the ParallaxingBackground class. Add a new line and add the following lines:

// The image representing the parallaxing backgroundTexture2D texture;// An array of positions of the parallaxing background

Vector2 [] positions;// The speed which the background is movingint speed;

public void Initialize()

{

}

public void Update()

{

}

public void Draw()

{

}

slide23

Replace the Initialize() method you just stubbed in with this version:

public void Initialize(ContentManager content, String texturePath,

intscreenWidth, intspeed)

{

// Load the background texture we will be using

texture = content.Load<Texture2D>(texturePath);

// Set the speed of the background

this.speed = speed;

// If we divide the screen with the texture width then we can determine the number of tiles need.

// We add 1 to it so that we won\'t have a gap in the tiling

positions = new Vector2[screenWidth / texture.Width + 1];

// Set the initial positions of the parallaxing background

for (inti = 0; i < positions.Length; i++)

{

// We need the tiles to be side by side to create a tiling effect

positions[i] = new Vector2(i * texture.Width, 0);

}

}

slide24

Replace the Update() method you just stubbed in with this version:

public void Update() {

// Update the positions of the background

for (inti = 0; i < positions.Length; i++) {

// Update the position of the screen by adding the speed

positions[i].X += speed;

// If the speed has the background moving to the left

if (speed <= 0) {

// Check the texture is out of view then put that texture at the end of the screen

if (positions[i].X <= -texture.Width) {

positions[i].X = texture.Width * (positions.Length - 1);

}

}// If the speed has the background moving to the right

else {

// Check if the texture is out of view then position it to the start of the screen

if (positions[i].X >= texture.Width * (positions.Length - 1)) {

positions[i].X = -texture.Width;

}

}

}

}

slide25

Replace the Draw() method you just stubbed in with this version:

public void Draw(SpriteBatchspriteBatch){ for (inti = 0; i < positions.Length; i++) {spriteBatch.Draw(texture, positions[i], Color.White); }}

inside game1 cs2
inside game1.cs

Look for the first { mark under the start of the Game1 class, and go just below the float playerMoveSpeed you added in the input step. Add a new line and then add the following:

// Image used to display the static backgroundTexture2D mainBackground;// Parallaxing LayersParallaxingBackground bgLayer1;ParallaxingBackground bgLayer2;

Look down the code, find the Initialize() method. Inside that method, add these lines:

bgLayer1 = new ParallaxingBackground();bgLayer2 = new ParallaxingBackground();

Look down the code, find the method called protected override void LoadContent(). Inside that method, below theplayer.Initialize call, add these lines:

// Load the parallaxing backgroundbgLayer1.Initialize(Content, "bgLayer1", GraphicsDevice.Viewport.Width, -1);bgLayer2.Initialize(Content, "bgLayer2", GraphicsDevice.Viewport.Width, -2);mainBackground = Content.Load<Texture2D>("mainbackground");

slide27

Look for the Update() method inside the Game1 class, and after the UpdatePlayer call, add the following lines:

// Update the parallaxing backgroundbgLayer1.Update();bgLayer2.Update();

Look for the Draw() method. Inside the method, before you call player.Draw(), add these lines:

spriteBatch.Draw(mainBackground, Vector2.Zero, Color.White);// Draw the moving backgroundbgLayer1.Draw(spriteBatch);bgLayer2.Draw(spriteBatch);

inside enemy cs
inside enemy.cs

using System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;

slide29

Place your cursor just after the { mark inside the Enemy class. Add a new line and add the following lines:

// Animation representing the enemypublic Animation EnemyAnimation;// The position of the enemy ship relative to the top left corner of thescreenpublic Vector2 Position;// The state of the Enemy Shippublic bool Active;// The hit points of the enemy, if this goes to zero the enemy diespublic int Health;// The amount of damage the enemy inflicts on the player shippublic int Damage;// The amount of score the enemy will give to the playerpublic int Value;// Get the width of the enemy shippublic int Width { get { return EnemyAnimation.FrameWidth; } }// Get the height of the enemy shippublic int Height { get { return EnemyAnimation.FrameHeight; } }// The speed at which the enemy movesfloat enemyMoveSpeed;

public void Initialize() { }public void Update() { }public void Draw() { }

slide30

Replace the Initialize() method you just stubbed in with this version:

public void Initialize(Animation animation,Vector2 position){// Load the enemy ship textureEnemyAnimation = animation; // Set the position of the enemy Position = position;// We initialize the enemy to be active so it will be update in the game Active = true;// Set the health of the enemy Health = 10;// Set the amount of damage the enemy can do Damage = 10;// Set how fast the enemy movesenemyMoveSpeed = 6f;// Set the score value of the enemy Value = 100;}

slide31

Replace the Update() method you just stubbed in with this version:

public void Update(GameTimegameTime){ // The enemy always moves to the left so decrement it\'s xpositionPosition.X -= enemyMoveSpeed;// Update the position of the AnimationEnemyAnimation.Position = Position;// Update AnimationEnemyAnimation.Update(gameTime);// If the enemy is past the screen or its health reaches 0 then deactivateit if (Position.X < -Width || Health <= 0) {// By setting the Active flag to false, the game will remove this objet fromthe // active game list Active = false; }}

slide32

Replace the Draw() method you just stubbed in with this version:

public void Draw(SpriteBatchspriteBatch){// Draw the animationEnemyAnimation.Draw(spriteBatch);}

inside game1 cs3
inside game1.cs

// Enemies

Texture2D enemyTexture;List<Enemy> enemies;

// The rate at which the enemies appear

TimeSpanenemySpawnTime;TimeSpanpreviousSpawnTime;

// A random number generator

Random random;

slide34

Look down the code, find the Initialize() method. Inside that method, add these lines:

// Initialize the enemies listenemies = new List<Enemy> ();

// Set the time keepers to zeropreviousSpawnTime = TimeSpan.Zero;

// Used to determine how fast enemy respawnsenemySpawnTime = TimeSpan.FromSeconds(1.0f);

// Initialize our random number generatorrandom = new Random();

slide35

Look down the code, find the LoadContent() method. Inside that method, below the bgLayer2.Initialize call, add these lines:

enemyTexture = Content.Load<Texture2D>("mineAnimation");

Add the following lines to make your new AddEnemy() method:

private void AddEnemy(){ // Create the animation object Animation enemyAnimation = new Animation();// Initialize the animation with the correct animation informationenemyAnimation.Initialize(enemyTexture, Vector2.Zero, 47, 61, 8, 30,Color.White, 1f, true);// Randomly generate the position of the enemy Vector2 position = new Vector2(GraphicsDevice.Viewport.Width +enemyTexture.Width / 2, random.Next(100, GraphicsDevice.Viewport.Height -100));// Create an enemy Enemy enemy = new Enemy();// Initialize the enemyenemy.Initialize(enemyAnimation, position); // Add the enemy to the active enemies listenemies.Add(enemy);}

slide36

Add the following lines to make an UpdateEnemies() method:

private void UpdateEnemies(GameTimegameTime){// Spawn a new enemy enemy every 1.5 seconds if (gameTime.TotalGameTime - previousSpawnTime > enemySpawnTime)  {previousSpawnTime = gameTime.TotalGameTime;// Add an EnemyAddEnemy(); }// Update the Enemies for (inti = enemies.Count - 1; i >= 0; i--)  { enemies[i].Update(gameTime); if (enemies[i].Active == false) {enemies.RemoveAt(i); }  }}

slide37

Look for the Update() method inside the Game1 class, and after the bgLayer2.Update() call, add the following lines:

// Update the enemiesUpdateEnemies(gameTime);

Look for the Draw() method. Inside the method, after you call bgLayer2.Draw(), add these lines:

// Draw the Enemiesfor (inti = 0; i < enemies.Count; i++){enemies[i].Draw(spriteBatch);}

slide38

inside game1.cs

Add the following lines to make your new UpdateCollision() method:

private void UpdateCollision() { // Use the Rectangle\'s built-in intersect function to  // determine if two objects are overlapping Rectangle rectangle1; Rectangle rectangle2;// Only create the rectangle once for the player rectangle1 = new Rectangle((int)player.Position.X,(int)player.Position.Y, player.Width, player.Height);// Do the collision between the player and the enemies for (inti = 0; i <enemies.Count; i++) { rectangle2 = new Rectangle((int)enemies[i].Position.X, (int)enemies[i].Position.Y, enemies[i].Width, enemies[i].Height);// Determine if the two objects collided with each // other if(rectangle1.Intersects(rectangle2)) {// Subtract the health from the player based on // the enemy damageplayer.Health -= enemies[i].Damage;// Since the enemy collided with the player // destroy it enemies[i].Health = 0;// If the player health is less than zero we died if (player.Health <= 0) player.Active = false;  } }}

calculating collision

slide39

Look for the Update() method inside the Game1 class, and after the UpdateEnemies() call, add the following lines:

// Update the collisionUpdateCollision();

inside projectile cs
inside projectile.cs

// Projectile.cs

// Using declarationsusing System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;

slide41

Now, as we did in our other classes, we’ll set up all the data we’ll need and stub in our standard Initialize(), Update() and Draw()methods to fill in later.

// Image representing the Projectilepublic Texture2D Texture;// Position of the Projectile relative to the upper left side of the screenpublic Vector2 Position;// State of the Projectilepublic bool Active;// The amount of damage the projectile can inflict to an enemypublic int Damage;// Represents the viewable boundary of the gameViewport viewport;// Get the width of the projectile shippublic int Width {

get { return Texture.Width; }

}// Get the height of the projectile shippublic int Height { get { return Texture.Height; }}// Determines how fast the projectile movesfloat projectileMoveSpeed;public void Initialize() { }

public void Update() { }

public void Draw() { }

slide42

This data needs to be filled in, so let’s make our Initialize() method to take care of that. Replace the Initialize() method you just stubbed in with this version:

public void Initialize(Viewport viewport, Texture2D texture, Vector2 position){ Texture = texture; Position = position;this.viewport = viewport; Active = true; Damage = 2;projectileMoveSpeed = 20f;}

slide43

Replace the Update() method you just stubbed in with this version:

public void Update(){// Projectiles always move to the rightPosition.X += projectileMoveSpeed;// Deactivate the bullet if it goes out of screen if (Position.X + Texture.Width / 2 > viewport.Width) Active = false;}

slide44

Finally, we need a Draw() method to ensure these projectiles are drawing on the screen just like the other game objects. Replace the Draw() method you just stubbed in with this version:

public void Draw(SpriteBatchspriteBatch){spriteBatch.Draw(Texture, Position, null, Color.White, 0f, new Vector2(Width / 2, Height / 2), 1f, SpriteEffects.None, 0f);}

inside game1 cs4
inside game1.cs

Look for the first { mark under the start of the Game1 class, and go just below Random random; that you added in the previous step. Add a new line and then add the following:

Texture2D projectileTexture;List<Projectile> projectiles;// The rate of fire of the player laserTimeSpanfireTime;TimeSpanpreviousFireTime;

slide46

Those are all the variables we need – let’s make sure they’re initialized. Look down the code, find the Initialize() method. Inside that method, add these lines:

projectiles = new List<Projectile>();// Set the laser to fire every quarter secondfireTime = TimeSpan.FromSeconds(.15f);

Since the projectiles have a graphic associated with them, we’ll need to load that from disk in LoadContent(). Look down the code, find the LoadContent() method. Inside that method, below the enemyTexture = call, add these lines:

projectileTexture = Content.Load<Texture2D>("laser");

slide47

Find some empty space inside Game1 class, preferably near the Update() method, make a new line, and you’re ready to go. Add the following lines to make your new AddProjectile() method:

private void AddProjectile(Vector2 position){ Projectile projectile = new Projectile(); projectile.Initialize(GraphicsDevice.Viewport, projectileTexture,position); projectiles.Add(projectile);}

Look for the UpdatePlayer() method inside the Game1 class, and add the following lines at the end:

// Fire only every interval we set as the fireTimeif (gameTime.TotalGameTime - previousFireTime > fireTime){// Reset our current timepreviousFireTime = gameTime.TotalGameTime;// Add the projectile, but add it to the front and center of the playerAddProjectile(player.Position + new Vector2(player.Width / 2, 0));}

slide48

Add the following lines to make an UpdateProjectiles() method:

private void UpdateProjectiles(){// Update the Projectiles for (inti = projectiles.Count - 1; i >= 0; i--)  { projectiles[i].Update(); if (projectiles[i].Active == false) {projectiles.RemoveAt(i); }  }}

We have to call UpdateProjectiles(), of course – a good place for this is inside the Update() method. Look for the Update()method inside the Game1 class, and after the UpdateCollision() call, add the following lines:

// Update the projectilesUpdateProjectiles();

slide49

Perfect. We’re adding projectiles, and we’re updating them; we just need to draw them. Add the following code to the Draw()method, after the call to enemies.Draw():

// Draw the Projectilesfor (inti = 0; i < projectiles.Count; i++) { projectiles[i].Draw(spriteBatch);}

Look for the UpdateCollision() method inside the Game1 class, and at the bottom of the method, add the following lines:

// Projectile vs Enemy Collisionfor (inti = 0; i < projectiles.Count; i++) { for (int j = 0; j < enemies.Count; j++) {// Create the rectangles we need to determine if we collided with each other rectangle1 = new Rectangle((int)projectiles[i].Position.X -  projectiles[i].Width / 2,(int)projectiles[i].Position.Y -  projectiles[i].Height / 2,projectiles[i].Width, projectiles[i].Height); rectangle2 = new Rectangle((int)enemies[j].Position.X - enemies[j].Width / 2,(int)enemies[j].Position.Y - enemies[j].Height / 2,enemies[j].Width, enemies[j].Height);// Determine if the two objects collided with each other if (rectangle1.Intersects(rectangle2)) { enemies[j].Health -= projectiles[i].Damage; projectiles[i].Active = false; } }}

slide50

inside game1.cs

Find the Width and Height properties in the Player class, and replace them with the following:

Texture2D explosionTexture;List<Animation> explosions;

We need to initialize the list and the texture. Let’s start with the list, in Initialize() .Look down the code, find the Initialize()method. Inside that method, add this line:

explosions = new List<Animation>();

Now, the texture. That’s done in ContentLoad(). Find the LoadContent() method. Inside that method, below the projectileTexture = call, add this single line:

explosionTexture = Content.Load<Texture2D>("explosion");

slide51

Just like Projectile, you’ll need a way to easily add an explosion when the time comes. Create your own AddExplosion() method; find some empty space in the Game1 class and add these lines:

private void AddExplosion(Vector2 position)

{

Animation explosion = new Animation();

explosion.Initialize(explosionTexture,position, 134, 134, 12, 45, Color.White, 1f,false);

explosions.Add(explosion);

}

Scroll down and find the UpdateEnemies() method. Look for the line marked if (enemies [i].Active == false) and go just below the { mark. Add the following lines:

// If not active and health <= 0

if (enemies[i].Health <= 0)

{

// Add an explosion

AddExplosion(enemies[i].Position);

}

slide52

Add the following lines to make your new UpdateExplosions() method:

private void UpdateExplosions(GameTimegameTime)

{

for (inti = explosions.Count - 1; i >= 0; i--)

{

explosions[i].Update(gameTime);

if (explosions[i].Active == false)

{

explosions.RemoveAt(i);

}

}

}

And, of course, we must call this new method from the Update() method in the game loop. Look for the Update() method inside the Game1 class, and after the UpdateProjectiles() call, add the following lines:

// Update the explosions

UpdateExplosions(gameTime);

Finally, we need to draw any active explosions on the screen. Just like the projectiles, it’s an easy matter of adding the following lines to the Draw() call:

// Draw the explosions

for (inti = 0; i < explosions.Count; i++)

{

explosions[i].Draw(spriteBatch);

}

slide53

playing sounds

Look for the first { mark under the start of the Game1 class, and go just below List<Animation> explosions; that you added in the previous step. Add a new line and then add the following:

// The sound that is played when a laser is fired

SoundEffectlaserSound;

// The sound used when the player or an enemy dies

SoundEffectexplosionSound;

// The music played during gameplay

Song gameplayMusic;

Find the LoadContent() method. Inside that method, below the explosionTexture = call, add these lines:

// Load the music

gameplayMusic = Content.Load<Song>("sound/gameMusic");

// Load the laser and explosion sound effect

laserSound = Content.Load<SoundEffect>("sound/laserFire");

explosionSound = Content.Load<SoundEffect>("sound/explosion");

// Start the music right away

PlayMusic(gameplayMusic);

slide54

private void PlayMusic(Song song)

{

// Due to the way the MediaPlayer plays music,

// we have to catch the exception. Music will play when the game is not tethered

try

{

// Play the music

MediaPlayer.Play(song);

// Loop the currently playing song

MediaPlayer.IsRepeating= true;

}

catch { }

}

slide55

Scroll down and find the UpdatePlayer() method. Look for the line marked AddProjectile(player.Position + new Vector2(player.Width / 2, 0); just below it, add the following lines:

// Play the laser sound

laserSound.Play();

Scroll down and find the UpdateEnemies() method. Look for the line marked AddExplosion(enemies[i].Position); just below it, add the following lines:

// Play the explosion sound

explosionSound.Play();

slide56

Look for the first { mark under the start of the Game1 class, and go just below Song gameplayMusic; that you added in the previous step. Add a new line and then add the following:

//Number that holds the player score

int score;

// The font used to display UI elements

SpriteFont font;

Find the Initialize() method. Inside that method, below the explosions = new List() call, add this line:

// Set player\'s score to zero

score = 0;

Since the SpriteFont is content, we need to load it through the LoadContent() method. Find the LoadContent() method. Inside that method, above the call to PlayMusic(), add the following:

// Load the score font

font = Content.Load<SpriteFont>("gameFont");

slide57

Scroll down and find the UpdateEnemies() method. Look for the line marked explosionSound.Play(); just below it, add the following lines:

// Add to the player\'s score

score += enemies[i].Value;

Find the Draw() method, and insert the following lines just below the } mark underneath the call toexplosions[i].Draw:

// Draw the score

spriteBatch.DrawString(font, "score: " + score, newVector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y),Color.White);

// Draw the player health

spriteBatch.DrawString(font, "health: " + player.Health, newVector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + 30), Color.White);

slide58

 You’ll want to do this at the very bottom of UpdatePlayer():

// reset score if player health goes to zero

if (player.Health <= 0)

{

player.Health= 100;

score = 0;

}

slide59

Question

&

Answer

ad