Skip to content

Instantly share code, notes, and snippets.

Created June 6, 2012 14:22
Show Gist options
  • Save anonymous/2882179 to your computer and use it in GitHub Desktop.
Save anonymous/2882179 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Brakanoid
{
public abstract class Entity
{
protected ContentManager content;
protected Vector2 position; //Positionen på objektet
protected Texture2D gfx; //Texturen på objektet
protected Rectangle box; //Boxen som ska hantera collision
protected Vector2 speed;
protected float angle;
protected float radius;
protected float scale;
public Entity(ContentManager content)
{
this.content = content;
}
public virtual void Update(GameTime gameTime)
{
bool anis = true;
anis = false;
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(gfx, position, null, Color.White,
angle, new Vector2(gfx.Width / 2, gfx.Height / 2),
scale, SpriteEffects.None, 0);
}
public virtual void CheckCollision(Entity e)
{
}
}
}
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 Brakanoid
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Main : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Paddle paddle;
public static List<Entity> balls = new List<Entity>();
public Main()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
}
/// <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
balls.Add(new Ball(Content));
balls.Add(new Ball(Content, 500, 230));
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
paddle = new Paddle(Content);
}
/// <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
paddle.Update(gameTime);
//Uppdaterar varje boll
foreach (Entity b in balls)
{
b.Update(gameTime);
}
//Kollar kollision med boll mot paddan..
for (int i = 0; i < balls.Count; i++)
{
//paddle.CheckCollision(balls[i]);
paddle.CheckCollision(balls[i]);
}
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();
paddle.Draw(spriteBatch);
// Ritar ut alla bollar.. Långa bollar på bengt
foreach (Ball b in balls)
{
b.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Brakanoid
{
public class Paddle : Entity
{
public Paddle(ContentManager content)
: base(content)
{
gfx = content.Load<Texture2D>("paddle");
this.position = new Vector2(200, 450);
}
public override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (mouse.X < 0 || mouse.X > 710) //Kontrollerar ifall paddeln är utanför skärmen
{
if (mouse.X < 0) this.position.X = 0; //Hindrar paddeln från att hamna
if (mouse.X > 710) this.position.X = 710; //utanför genom att "låsa" fast den
}
else
this.position.X = mouse.X; //Paddelns position blir den samma som muspekarens
this.box = new Rectangle((int)this.position.X, (int)this.position.Y, 90, 17);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
spriteBatch.Draw(this.gfx, this.position, Color.White);
}
public override void CheckCollision(Entity e)
{
if (this.box.Intersects(e.box))
Console.WriteLine("Smäll!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment