Created
April 15, 2013 08:25
-
-
Save scan/5386695 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
using Box2D.XNA; | |
namespace Tomorrow | |
{ | |
public class TomorrowGame : Microsoft.Xna.Framework.Game | |
{ | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
World world; | |
Body ground; | |
Body body; | |
Texture2D muffin; | |
public TomorrowGame() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
this.world = new World(new Vector2(0, 25f), true); | |
} | |
protected override void Initialize() | |
{ | |
base.Initialize(); | |
BodyDef groundBodyDef = new BodyDef(); | |
groundBodyDef.type = BodyType.Static; | |
groundBodyDef.position = new Vector2(100.0f, 400.0f); | |
ground = world.CreateBody(groundBodyDef); | |
PolygonShape groundBox = new PolygonShape(); | |
groundBox.SetAsBox(50.0f, 10.0f); | |
ground.CreateFixture(groundBox, 0.0f); | |
BodyDef bodyDef = new BodyDef(); | |
bodyDef.type = BodyType.Dynamic; | |
bodyDef.position = new Vector2(50.0f, 0.0f); | |
body = world.CreateBody(bodyDef); | |
PolygonShape dynamicBox = new PolygonShape(); | |
dynamicBox.SetAsBox(64, 64); | |
FixtureDef fixtureDef = new FixtureDef(); | |
fixtureDef.shape = dynamicBox; | |
fixtureDef.density = 1; | |
fixtureDef.friction = 0.3f; | |
body.CreateFixture(fixtureDef); | |
} | |
protected override void LoadContent() | |
{ | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
muffin = Content.Load<Texture2D>("muffin"); | |
} | |
protected override void Update(GameTime gameTime) | |
{ | |
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) | |
this.Exit(); | |
if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed) | |
{ | |
body.SetAwake(true); | |
body.ApplyLinearImpulse(new Vector2(0, -0.85f * 25f), body.GetWorldCenter()); | |
} | |
world.Step(1f / 60f, 8, 3); | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
spriteBatch.Begin(); | |
spriteBatch.Draw(muffin, body.GetPosition(), null, Color.White, body.GetAngle(), Vector2.Zero, 1, SpriteEffects.None, 0); | |
spriteBatch.End(); | |
base.Draw(gameTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment