Skip to content

Instantly share code, notes, and snippets.

@hamoungh
Last active August 29, 2015 14:14
Show Gist options
  • Save hamoungh/451e6a47602349fa2859 to your computer and use it in GitHub Desktop.
Save hamoungh/451e6a47602349fa2859 to your computer and use it in GitHub Desktop.
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using ShipFighterClasses;
#endregion
namespace ShipFighter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player playerShip;
Player background;
Random random;
Player[] blueEnemies;
MouseState prevMouseState;
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
random = new Random();
blueEnemies=new Player[7];
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);
background= new Player( Content.Load<Texture2D>("spaceBackground"),
Vector2.Zero, //position,
Vector2.Zero, //velocity,
Vector2.Zero, //acceleration,
false,
1.0f); //scale
playerShip = new Player(Content.Load<Texture2D>("shipSprite"),
Vector2.Zero, //position,
new Vector2(100,100), //velocity,
new Vector2(10, 10), //acceleration,
true,
1.0f); //scale
for (int i = 0; i < blueEnemies.Length; i++)
{
blueEnemies[i] = new Player(Content.Load<Texture2D>("blueEnemy"),
new Vector2(
random.Next(0,GraphicsDevice.Viewport.Width)
, 0), //position,
new Vector2(0, 50), //velocity,
new Vector2(0, 0), //acceleration,
true,
1.0f); //scale
}
}
/// <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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
UpdateInput();
playerShip.Update(gameTime, GraphicsDevice);
foreach(Player blueEnemy in blueEnemies){
blueEnemy.Update(gameTime);
if (playerShip.CollisionSprite(blueEnemy))
{
blueEnemy.Alive = false;
}
}
base.Update(gameTime);
}
private void UpdateInput()
{
bool keyPressed = false;
KeyboardState keyState = Keyboard.GetState();
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
if (keyState.IsKeyDown(Keys.Up)
|| gamePadState.DPad.Up == ButtonState.Pressed
|| gamePadState.ThumbSticks.Left.Y > 0 )
{
playerShip.Up(); keyPressed = true;
}
if (keyState.IsKeyDown(Keys.Down))
{
playerShip.Down(); keyPressed = true;
}
if (keyState.IsKeyDown(Keys.Left))
{
playerShip.Left(); keyPressed = true;
}
if (keyState.IsKeyDown(Keys.Right))
{
playerShip.Right();keyPressed = true;
}
if (!keyPressed) playerShip.Idle();
MouseState currMouseState = Mouse.GetState();
if(currMouseState.X != prevMouseState.X ||
currMouseState.Y != prevMouseState.Y)
{
playerShip.Position = new Vector2(
currMouseState.X,currMouseState.Y);
}
prevMouseState = currMouseState;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
background.Draw(spriteBatch);
playerShip.Draw(spriteBatch);
foreach (Player blueEnemy in blueEnemies)
{
blueEnemy.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment