Here is the Code:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Shooter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Represents the player
Player player;
// Keyboard states used to determine key presses
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;
// Gamepad states used to determine button presses
GamePadState currentGamePadState;
GamePadState previousGamePadState;
// A movement speed for the player
float playerMoveSpeed;
Texture2D mainBackground;
// Parallaxing Layers
ParallaxingBackground bgLayer1;
ParallaxingBackground bgLayer2;
// Enemies
Texture2D enemyTexture;
List<Enemy> enemies;
// The rate at which the enemies appear
TimeSpan enemySpawnTime;
TimeSpan previousSpawnTime;
// A random number generator
Random random;
Texture2D projectileTexture;
List<Projectile> projectiles;
// The rate of fire of the player laser
TimeSpan fireTime;
TimeSpan previousFireTime;
Texture2D explosionTexture;
List<Animation> explosions;
//Number that holds the player score
float score;
// The font used to display UI elements
SpriteFont font;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
// Initialize the player class
player = new Player();
// Set a constant player move speed
playerMoveSpeed = 8.0f;
bgLayer1 = new ParallaxingBackground();
bgLayer2 = new ParallaxingBackground();
// Initialize the enemies list
enemies = new List<Enemy>();
// Set the time keepers to zero
previousSpawnTime = TimeSpan.Zero;
// Used to determine how fast enemy respawns
enemySpawnTime = TimeSpan.FromSeconds(1.0f);
// Initialize our random number generator
random = new Random();
projectiles = new List<Projectile>();
// Set the laser to fire every quarter second
fireTime = TimeSpan.FromSeconds(.15f);
explosions = new List<Animation>();
//Set player's score to zero
score = 0;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load the player resources
// 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);
// Load the parallaxing background
bgLayer1.Initialize(Content, "bgLayer1", GraphicsDevice.Viewport.Width, -1);
bgLayer2.Initialize(Content, "bgLayer2", GraphicsDevice.Viewport.Width, -2);
mainBackground = Content.Load<Texture2D>("mainbackground");
enemyTexture = Content.Load<Texture2D>("mineAnimation");
projectileTexture = Content.Load<Texture2D>("laser");
explosionTexture = Content.Load<Texture2D>("explosion");
font = Content.Load<SpriteFont>("gameFont");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
// Save the previous state of the keyboard and game pad so we can determinesingle key/button presses
previousGamePadState = currentGamePadState;
previousKeyboardState = currentKeyboardState;
// Read the current state of the keyboard and gamepad and store it
currentKeyboardState = Keyboard.GetState();
currentGamePadState = GamePad.GetState(PlayerIndex.One);
bgLayer1.Update();
bgLayer2.Update();
//Update the player
UpdatePlayer(gameTime);
// Update the enemies
UpdateEnemies(gameTime);
UpdateCollision();
UpdateProjectiles();
UpdateExplosions(gameTime);
base.Update(gameTime);
}
private void AddEnemy()
{
// Create the animation object
Animation enemyAnimation = new Animation();
// Initialize the animation with the correct animation information
enemyAnimation.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 enemy
enemy.Initialize(enemyAnimation, position);
// Add the enemy to the active enemies list
enemies.Add(enemy);
}
private void UpdateEnemies(GameTime gameTime)
{
// Spawn a new enemy enemy every 1 second
if (gameTime.TotalGameTime - previousSpawnTime > enemySpawnTime)
{
previousSpawnTime = gameTime.TotalGameTime;
// Add an Enemy
AddEnemy();
}
// Update the Enemies
for (int i = enemies.Count - 1; i >= 0; i--)
{
enemies[i].Update(gameTime);
if (enemies[i].Active == false)
{
// If not active and health <= 0
if (enemies[i].Health <= 0)
{
// Add an explosion
AddExplosion(enemies[i].Position);
score += enemies[i].Value;
}
enemies.RemoveAt(i);
}
}
}
private void UpdateExplosions(GameTime gameTime)
{
for (int i = explosions.Count - 1; i >= 0; i--)
{
explosions[i].Update(gameTime);
if (explosions[i].Active == false)
{
explosions.RemoveAt(i);
}
}
}
private void AddExplosion(Vector2 position)
{
Animation explosion = new Animation();
explosion.Initialize(explosionTexture, position, 134, 134, 12, 45, Color.White, 1f, false);
explosions.Add(explosion);
}
private void UpdatePlayer(GameTime gameTime)
{
player.Update(gameTime);
// Get Thumbstick Controls
player.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 bounds
player.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);
// Fire only every interval we set as the fireTime
if (gameTime.TotalGameTime - previousFireTime > fireTime)
{
// Reset our current time
previousFireTime = gameTime.TotalGameTime;
// Add the projectile, but add it to the front and center of the player
AddProjectile(player.Position + new Vector2(player.Width / 2, 0));
}
if (player.Health <= 0)
{
player.Health = 100;
score = 0;
}
}
private void UpdateProjectiles()
{
// Update the Projectiles
for (int i = projectiles.Count - 1; i >= 0; i--)
{
projectiles[i].Update();
if (projectiles[i].Active == false)
{
projectiles.RemoveAt(i);
}
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{