SHARE
TWEET

Untitled

a guest Oct 19th, 2018 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5.  
  6. using MonoGame.Extended;
  7. using MonoGame.Extended.Entities;
  8. using MonoGame.Extended.Tiled;
  9. using MonoGame.Extended.Tiled.Renderers;
  10.  
  11. namespace EscapeRoom
  12. {
  13.     /// <summary>
  14.     /// This is the main type for your game.
  15.     /// </summary>
  16.     public class Game1 : Game
  17.     {
  18.         Player player = new Player();
  19.  
  20.         OrthographicCamera camera;
  21.  
  22.         // The tile map
  23.         private TiledMap map;
  24.         // The renderer for the map
  25.         private TiledMapRenderer mapRenderer;
  26.  
  27.         GraphicsDeviceManager graphics;
  28.         SpriteBatch spriteBatch;
  29.        
  30.         public Game1()
  31.         {
  32.             graphics = new GraphicsDeviceManager(this)
  33.             {
  34.                 PreferredBackBufferWidth = 1920,
  35.                 PreferredBackBufferHeight = 1080,
  36.                 IsFullScreen = false
  37.             };
  38.             Content.RootDirectory = "Content";
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Allows the game to perform any initialization it needs to before starting to run.
  43.         /// This is where it can query for any required services and load any non-graphic
  44.         /// related content.  Calling base.Initialize will enumerate through any components
  45.         /// and initialize them as well.
  46.         /// </summary>
  47.         protected override void Initialize()
  48.         {
  49.             // TODO: Add your initialization logic here
  50.             player.Speed = 200f;
  51.            
  52.             camera = new OrthographicCamera(GraphicsDevice);
  53.  
  54.  
  55.             // Load the compiled map
  56.             map = Content.Load<TiledMap>("map");
  57.             // Create the map renderer
  58.             mapRenderer = new TiledMapRenderer(GraphicsDevice);
  59.  
  60.             base.Initialize();
  61.         }
  62.  
  63.         /// <summary>
  64.         /// LoadContent will be called once per game and is the place to load
  65.         /// all of your content.
  66.         /// </summary>
  67.         protected override void LoadContent()
  68.         {
  69.             // Create a new SpriteBatch, which can be used to draw textures.
  70.             spriteBatch = new SpriteBatch(GraphicsDevice);
  71.  
  72.             // TODO: use this.Content to load your game content here
  73.  
  74.             player.Texture = Content.Load<Texture2D>("Player");
  75.         }
  76.  
  77.         /// <summary>
  78.         /// UnloadContent will be called once per game and is the place to unload
  79.         /// game-specific content.
  80.         /// </summary>
  81.         protected override void UnloadContent()
  82.         {
  83.             // TODO: Unload any non ContentManager content here
  84.         }
  85.  
  86.         /// <summary>
  87.         /// Allows the game to run logic such as updating the world,
  88.         /// checking for collisions, gathering input, and playing audio.
  89.         /// </summary>
  90.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  91.         protected override void Update(GameTime gameTime)
  92.         {
  93.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  94.                 Exit();
  95.  
  96.             // TODO: Add your update logic here
  97.             var kstate = Keyboard.GetState();
  98.  
  99.             if (kstate.IsKeyDown(Keys.Up))
  100.             {
  101.                 player.Position.Y -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  102.                 camera.Move(new Vector2(player.Speed, 0));
  103.             }
  104.                
  105.             if (kstate.IsKeyDown(Keys.Down))
  106.             {
  107.                 player.Position.Y += player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  108.                 camera.Move(new Vector2(-player.Speed, 0));
  109.             }
  110.                
  111.             if (kstate.IsKeyDown(Keys.Left))
  112.             {
  113.                 player.Position.X -= player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  114.                 camera.Move(new Vector2(0, -player.Speed));
  115.             }
  116.                
  117.             if (kstate.IsKeyDown(Keys.Right))
  118.             {
  119.                 player.Position.X += player.Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  120.                 camera.Move(new Vector2(0, -player.Speed));
  121.             }
  122.  
  123.             player.Position.X = Math.Min(Math.Max(player.Texture.Width / 2, player.Position.X), graphics.PreferredBackBufferWidth - player.Texture.Width / 2);
  124.             player.Position.Y = Math.Min(Math.Max(player.Texture.Height / 2, player.Position.Y), graphics.PreferredBackBufferHeight - player.Texture.Height / 2);
  125.            
  126.             // Update the map
  127.             // map Should be the `TiledMap`
  128.             mapRenderer.Update(gameTime);
  129.  
  130.             base.Update(gameTime);
  131.         }
  132.  
  133.         /// <summary>
  134.         /// This is called when the game should draw itself.
  135.         /// </summary>
  136.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  137.         protected override void Draw(GameTime gameTime)
  138.         {
  139.             GraphicsDevice.Clear(Color.CornflowerBlue);
  140.  
  141.             // TODO: Add your drawing code here
  142.  
  143.             var transformMatrix = camera.GetViewMatrix();
  144.             spriteBatch.Begin(transformMatrix: transformMatrix);
  145.  
  146.             spriteBatch.Draw(
  147.             player.Texture,
  148.             player.Position,
  149.             null,
  150.             Color.White,
  151.             0f,
  152.             new Vector2(player.Texture.Width / 2, player.Texture.Height / 2),
  153.             Vector2.One,
  154.             SpriteEffects.None,
  155.             0f
  156.             );
  157.  
  158.  
  159.             spriteBatch.End();
  160.  
  161.             base.Draw(gameTime);
  162.         }
  163.     }
  164.  
  165.     class Player
  166.     {
  167.         public Texture2D Texture;
  168.         public Vector2 Position;
  169.         public float Speed;
  170.     }
  171. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top