sneak/Jumpy.Shared/JumpyGame.cs

87 lines
2.7 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
namespace Jumpy {
public class JumpyGame : Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D playerTexture;
SpriteFont font;
KeyboardInput keyboardInput = new KeyboardInput();
bool fullScreen = false;
IDisplay display;
public JumpyGame() {
graphics = new GraphicsDeviceManager(this);
IsMouseVisible = true;
Content.RootDirectory = "Content";
}
// Performs initialization that's needed before starting to run.
protected override void Initialize() {
display = (IDisplay) Services.GetService(typeof(IDisplay));
display.Initialize(Window, graphics);
display.SetFullScreen(fullScreen);
base.Initialize();
}
// Called once per game. Loads all game content.
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font");
playerTexture = Content.Load<Texture2D>("player");
}
// Called once per game. Unloads all game content.
protected override void UnloadContent() {
// TODO: Unload any non ContentManager content here.
}
// Updates the game world.
protected override void Update(GameTime gameTime) {
keyboardInput.Update();
List<Keys> keysDown = keyboardInput.NewKeysDown();
if (keysDown.Contains(Keys.F12)) {
fullScreen = !fullScreen;
display.SetFullScreen(fullScreen);
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
keysDown.Contains(Keys.Escape)) {
Exit();
}
base.Update(gameTime);
}
// Called when the game should draw itself.
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
const int spriteSize = 144;
int frameNum = gameTime.TotalGameTime.Milliseconds / 250;
if (frameNum == 3) {
frameNum = 1;
}
int sourceX = spriteTileSize * frameNum + spriteTileSize * 3;
int sourceY = spriteTileSize * 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.Draw(playerTexture, position, source, Color.White, 0f, spriteCenter, Vector2.One, SpriteEffects.FlipHorizontally, 0f);
spriteBatch.End();
// spriteBatch.DrawString(font, "hello world", new Vector2(100, 100), Color.Black);
base.Draw(gameTime);
}
}
}