1 / 59

Shooter Game

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 ).

dudley
Download Presentation

Shooter Game

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shooter Game XBOX LIVE indie games

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

  3. 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() { } }}

  4. 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; }}

  5. 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;}

  6. 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);}

  7. 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);

  8. 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();

  9. 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;

  10. 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); }

  11. 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);

  12. 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;

  13. // 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

  14. 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; }

  15. 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)); }

  16. 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); } }

  17. 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; }

  18. 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; }}

  19. 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); }

  20. 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);

  21. inside parallaxingbackground.cs // ParallaxingBackground.cs using System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.Graphics;

  22. 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() { }

  23. 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); } }

  24. 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; } } } }

  25. 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); }}

  26. 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");

  27. 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);

  28. inside enemy.cs using System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;

  29. 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() { }

  30. 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;}

  31. 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; }}

  32. Replace the Draw() method you just stubbed in with this version: public void Draw(SpriteBatchspriteBatch){// Draw the animationEnemyAnimation.Draw(spriteBatch);}

  33. inside game1.cs // Enemies Texture2D enemyTexture;List<Enemy> enemies; // The rate at which the enemies appear TimeSpanenemySpawnTime;TimeSpanpreviousSpawnTime; // A random number generator Random random;

  34. 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();

  35. 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);}

  36. 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); }  }}

  37. 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);}

  38. 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

  39. Look for the Update() method inside the Game1 class, and after the UpdateEnemies() call, add the following lines: // Update the collisionUpdateCollision();

  40. inside projectile.cs // Projectile.cs // Using declarationsusing System;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;

  41. 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() { }

  42. 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;}

  43. 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;}

  44. 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);}

  45. 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;

  46. 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");

  47. 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));}

  48. 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();

  49. 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; } }}

  50. 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");

More Related