Skip to content

Instantly share code, notes, and snippets.

Created December 6, 2012 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4225880 to your computer and use it in GitHub Desktop.
Save anonymous/4225880 to your computer and use it in GitHub Desktop.
Contains Sprite classes
#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;
#endregion
namespace MonoGameWindowsApplication1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Test test;
private TestZ testz, testz2, testz3;
public Random randVar = new Random();
//private Texture2D test;
public Game1()
: base()
{
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
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);
Rectangle screenBounds = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
// TODO: use this.Content to load your game content here
test = new Test(Content.Load<Texture2D>("test"),Content.Load<Texture2D>("test2"),Vector2.Zero, screenBounds);
testz = new TestZ(Content.Load<Texture2D>("testz"), Content.Load<Texture2D>("test2"), Vector2.Zero, screenBounds);
testz2 = new TestZ(Content.Load<Texture2D>("test2"), Content.Load<Texture2D>("test2"), Vector2.Zero, screenBounds);
testz3 = new TestZ(Content.Load<Texture2D>("testz"), Content.Load<Texture2D>("test2"), Vector2.Zero, screenBounds);
}
/// <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();
test.Update(gameTime);
testz.Update(gameTime);
testz2.Update(gameTime);
testz3.Update(gameTime);
// TODO: Add your update logic here
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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
test.Draw(spriteBatch);
testz.Draw(spriteBatch);
testz2.Draw(spriteBatch);
testz3.Draw(spriteBatch);
//spriteBatch.Draw(test, Vector2.One, Color.AntiqueWhite);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace MonoGameWindowsApplication1
{
/// <summary>
/// The main class.
/// </summary>
public static class Program
{
protected static Game1 game;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
game = new Game1();
game.Run();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
namespace MonoGameWindowsApplication1
{
public class Test : Sprite
{
Rectangle screenBounds;
public Test(Texture2D texture, Texture2D texture2, Vector2 location, Rectangle screenBounds)
: base(texture, texture2, location)
{
this.screenBounds = screenBounds;
}
public override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Down))
velocity = new Vector2(0, 3);
else if (Keyboard.GetState().IsKeyDown(Keys.Up))
velocity = new Vector2(0, -3);
else if (Keyboard.GetState().IsKeyDown(Keys.Right))
velocity = new Vector2(3, 0);
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
velocity = new Vector2(-3, 0);
else
velocity = new Vector2(0, 0);
if ((Keyboard.GetState().IsKeyDown(Keys.LeftShift)) || Keyboard.GetState().IsKeyDown(Keys.RightShift))
{
velocity.X = velocity.X * 5;
velocity.Y = velocity.Y * 5;
} //This is the sprint! Hold to boost velocity.
base.Update(gameTime);
}
protected override void checkBounds()
{
location.X = MathHelper.Clamp(location.X, 0, (screenBounds.Width - texture.Width));
location.Y = MathHelper.Clamp(location.Y, 0, (screenBounds.Height - texture.Height));
}
}
public class TestZ : Sprite
{
Rectangle screenBounds;
public int ticked = 0;
public TestZ(Texture2D texture, Texture2D texture2, Vector2 location, Rectangle screenBounds)
: base(texture, texture2, location)
{
this.screenBounds = screenBounds;
location.X = randVar.Next(0,100) * .01f * screenBounds.Width;
location.Y = randVar.Next(0, 100) * .01f * screenBounds.Height;
}
public override void Update(GameTime gameTime)
{
ticked++;
if (ticked >= 1000)
{
location.X = location.X + randVar.Next(-10, 10);
location.Y = location.Y + randVar.Next(-10, 10);
ticked = 0;
}
if (Keyboard.GetState().GetPressedKeys().Contains(Keys.Space))
{
location.X = randVar.Next(0, 100) * .01f * screenBounds.Width;
location.Y = randVar.Next(0, 100) * .01f * screenBounds.Height;
}
base.Update(gameTime);
}
protected override void checkBounds()
{
location.X = MathHelper.Clamp(location.X, 0, (screenBounds.Width - texture.Width));
location.Y = MathHelper.Clamp(location.Y, 0, (screenBounds.Height - texture.Height));
}
}
public abstract class Sprite
{
protected Texture2D texture;
protected Texture2D texture2;
protected Vector2 location;
protected Vector2 velocity = Vector2.Zero;
public Sprite(Texture2D texture, Texture2D texture2, Vector2 location)
{
this.texture = texture;
this.texture2 = texture2;
this.location = location;
}
public virtual void Update(GameTime gameTime)
{
location += velocity;
checkBounds();
}
protected abstract void checkBounds();
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture,location,Color.AntiqueWhite);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment