Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- using MonoGame.Extended;
- using MonoGame.Extended.Entities;
- using MonoGame.Extended.Tiled;
- using MonoGame.Extended.Tiled.Renderers;
- namespace EscapeRoom
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- Player player = new Player();
- OrthographicCamera camera;
- // The tile map
- private TiledMap map;
- // The renderer for the map
- private TiledMapRenderer mapRenderer;
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this)
- {
- PreferredBackBufferWidth = 1920,
- PreferredBackBufferHeight = 1080,
- IsFullScreen = false
- };
- 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
- player.Speed = 200f;
- camera = new OrthographicCamera(GraphicsDevice);
- // Load the compiled map
- map = Content.Load<TiledMap>("map");
- // Create the map renderer
- mapRenderer = new TiledMapRenderer(GraphicsDevice);
- 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
- player.Texture = Content.Load<Texture2D>("Player");
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific 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();
- // TODO: Add your update logic here
- var kstate = Keyboard.GetState();
- if (kstate.IsKeyDown(Keys.Up))
- {
- player.Position.Y -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
- camera.Move(new Vector2(player.Speed, 0));
- }
- if (kstate.IsKeyDown(Keys.Down))
- {
- player.Position.Y += player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
- camera.Move(new Vector2(-player.Speed, 0));
- }
- if (kstate.IsKeyDown(Keys.Left))
- {
- player.Position.X -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
- camera.Move(new Vector2(0, -player.Speed));
- }
- if (kstate.IsKeyDown(Keys.Right))
- {
- player.Position.X += player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
- camera.Move(new Vector2(0, -player.Speed));
- }
- player.Position.X = Math.Min(Math.Max(player.Texture.Width / 2, player.Position.X), graphics.PreferredBackBufferWidth - player.Texture.Width / 2);
- player.Position.Y = Math.Min(Math.Max(player.Texture.Height / 2, player.Position.Y), graphics.PreferredBackBufferHeight - player.Texture.Height / 2);
- // Update the map
- // map Should be the `TiledMap`
- mapRenderer.Update(gameTime);
- 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
- var transformMatrix = camera.GetViewMatrix();
- spriteBatch.Begin(transformMatrix: transformMatrix);
- spriteBatch.Draw(
- player.Texture,
- player.Position,
- null,
- Color.White,
- 0f,
- new Vector2(player.Texture.Width / 2, player.Texture.Height / 2),
- Vector2.One,
- SpriteEffects.None,
- 0f
- );
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- class Player
- {
- public Texture2D Texture;
- public Vector2 Position;
- public float Speed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement