#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;
#endregion
namespace Application
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
public static Player player = null;
List<Enemy> enemyList = new List<Enemy>();
Tile[,] tileArray = null;
float scrollSpeed = 64;
int scrollMax = 0;
Random random = new Random ();
float SpawnTimer = 3;
public static Bullet[] Bullets = new Bullet[50];
public Game1 ()
{
graphics = new GraphicsDeviceManager (this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = false;
}
/// <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
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);
//TODO: use this.Content to load your game content here
player = new Player ();
Texture2D Player = Content.Load <Texture2D> ("player.png");
player.Position = new Vector2
(graphics.GraphicsDevice.Viewport.Width / 2,
graphics.GraphicsDevice.Viewport.Height - 100);
player.Texture = Player;
Texture2D bulletTexture = Content.Load <Texture2D> ("Bullet.png");
InitializeBullets (bulletTexture);
player.ShootSfx = Content.Load<SoundEffect> ("playerPew");
float scrW = graphics.GraphicsDevice.Viewport.Width;
float scrH = graphics.GraphicsDevice.Viewport.Height;
Texture2D grass = Content.Load<Texture2D> ("grass.png");
float tileW = grass.Bounds.Width;
float tileH = grass.Bounds.Height;
int scrTileW = (int) (scrW / tileW) + 1;
int scrTileH = (int) (scrH / tileH) + 2;
tileArray = new Tile[scrTileH, scrTileW];
for (int y=0 ; y < scrTileH; y++)
{
for (int x=0 ; x < scrTileW; x++)
{
Tile t = new Tile ();
t.Texture = grass;
t.position = new Vector2 (x * tileW, y * tileH);
tileArray [y,x] = t;
}
}
scrollMax = (scrTileH - 1) * (int)tileH;
SpawnEnemy ();
font = Content.Load<SpriteFont>("CopperplateGothicBold");
}
public void InitializeBullets(Texture2D bulletTexture)
{
for (int i = 0; i < Bullets.Length; i++)
{
Bullets [i] = new Bullet ();
Bullets [i].Texture = bulletTexture;
}
}
public void SpawnEnemy ()
{
float xPos = random.Next(100,graphics.GraphicsDevice.Viewport.Width -100);
Enemy enemy = new Enemy ();
enemy.Position = new Vector2 (xPos, -100);
enemy.Texture = Content.Load <Texture2D> ("EnemyPlane.png");
enemy.IsDead =false;
enemyList.Add (enemy);
}
/// <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)
{
// For Mobile devices, this logic will close the Game when the Back button is pressed
if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
Exit ();
}
// TODO: Add your update logic here
float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (player != null)
player.Update (deltaTime);
foreach (Enemy enemy in enemyList) {
if (enemy != null && enemy.IsDead == false) {
enemy.Update (deltaTime);
if (enemy.Position.Y > graphics.GraphicsDevice.Viewport.Height + 100)
{
enemy.IsDead = true;
}
}
}
foreach (Enemy enemy in enemyList) {
if (enemy.IsDead == true) {
enemyList.Remove (enemy);
break;
}
}
SpawnTimer -= deltaTime;
if (SpawnTimer <= 0) {
SpawnEnemy ();
SpawnTimer = 2;
}
for (int y = 0; y < tileArray.GetLength (0); y++) {
for (int x = 0; x < tileArray.GetLength (1); x++) {
Tile t = tileArray [y, x];
t.position.Y += scrollSpeed * deltaTime;
if (t.position.Y > scrollMax) {
t.position.Y -= scrollMax + t.Texture.Height;
}
}
foreach (Bullet b in Bullets) {
b.Update (deltaTime);
if (b.IsDead == false)
{
if (b.isFromPlayer == true && b.Position.Y < 0)
b.IsDead = true;
if (b.isFromPlayer == false && b.Position.Y > graphics.GraphicsDevice.Viewport.Height)
b.IsDead = true;
}
base.Update (gameTime);
}
}
}
/// <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)
{
graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
//TODO: Add your drawing code here
spriteBatch.Begin ();
foreach ( Tile t in tileArray)
{
t.Draw (spriteBatch);
}
if (player != null)
player.Draw (spriteBatch);
foreach (Enemy enemy in enemyList)
{
if (enemy != null)
enemy.Draw (spriteBatch);
}
spriteBatch.DrawString (font,"Score: 0000", new Vector2 (20, 50), Color.White);
foreach (Bullet b in Bullets)
{
b.Draw (spriteBatch);
}
spriteBatch.End ();
base.Draw (gameTime);
}
}
}