add player sprite with looping animation

GitOrigin-RevId: 2e77393290d3b2e332ab73a0ed021f41d49c9eb4
This commit is contained in:
Colin McMillen 2019-12-06 20:00:42 -05:00
parent 3f603d6bd3
commit fb50667459

View File

@ -8,6 +8,7 @@ namespace Jumpy {
public class JumpyGame : Game { public class JumpyGame : Game {
GraphicsDeviceManager graphics; GraphicsDeviceManager graphics;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
Texture2D playerTexture;
SpriteFont font; SpriteFont font;
KeyboardInput keyboardInput = new KeyboardInput(); KeyboardInput keyboardInput = new KeyboardInput();
bool fullScreen = false; bool fullScreen = false;
@ -24,9 +25,6 @@ namespace Jumpy {
display = (IDisplay) Services.GetService(typeof(IDisplay)); display = (IDisplay) Services.GetService(typeof(IDisplay));
display.Initialize(Window, graphics); display.Initialize(Window, graphics);
display.SetFullScreen(fullScreen); display.SetFullScreen(fullScreen);
// TODO: something like this for mobile devices?
// graphics.SupportedOrientations =
// DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
base.Initialize(); base.Initialize();
} }
@ -35,6 +33,7 @@ namespace Jumpy {
protected override void LoadContent() { protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font"); font = Content.Load<SpriteFont>("font");
playerTexture = Content.Load<Texture2D>("player");
} }
// Called once per game. Unloads all game content. // Called once per game. Unloads all game content.
@ -64,10 +63,22 @@ namespace Jumpy {
protected override void Draw(GameTime gameTime) { protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.Clear(Color.CornflowerBlue);
int frameNum = gameTime.TotalGameTime.Milliseconds / 250;
if (frameNum == 3) {
frameNum = 1;
}
int sourceX = 144 * frameNum + 144 * 6;
int sourceY = 144 * 0;
Rectangle source = new Rectangle(sourceX, sourceY, 144, 144);
// Vector2 position = new Vector2(0, 0);
Vector2 position = new Vector2(100, 100);
Vector2 spriteCenter = new Vector2(144 / 2, 144 / 2);
spriteBatch.Begin(); spriteBatch.Begin();
spriteBatch.DrawString(font, "hello world", new Vector2(100, 100), Color.Black); spriteBatch.Draw(playerTexture, position, source, Color.White, 0f, spriteCenter, Vector2.One, SpriteEffects.FlipHorizontally, 0f);
spriteBatch.End(); spriteBatch.End();
// spriteBatch.DrawString(font, "hello world", new Vector2(100, 100), Color.Black);
base.Draw(gameTime); base.Draw(gameTime);
} }
} }