Mihao

z napisami

Nov 8th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna;
  11.  
  12.  
  13. namespace Game1
  14. {
  15.     public class Game1 : Microsoft.Xna.Framework.Game
  16.     {
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.         SpriteFont font;
  20.         private Animated SpriteTexture;
  21.  
  22.         private const float Rotation = 0;
  23.         private const float Scale = 0.5f;
  24.  
  25.         private Viewport viewport;
  26.         private Vector2 Position = new Vector2(0, 0);
  27.         private const int FrameX = 6;
  28.         private const int FrameY = 3;
  29.         private const int FramesPerSec = 90;
  30.  
  31.         public Game1()
  32.         {
  33.             graphics = new GraphicsDeviceManager(this);
  34.             Content.RootDirectory = "Content";
  35.             this.IsMouseVisible = true;
  36.             graphics.PreferredBackBufferHeight = 600;
  37.             graphics.PreferredBackBufferWidth = 800;
  38.  
  39.             SpriteTexture = new Animated(Vector2.Zero, Rotation, Scale);
  40.  
  41.             TargetElapsedTime = TimeSpan.FromSeconds(1 / 24.0);
  42.         }
  43.  
  44.         protected override void Initialize()
  45.         {
  46.             spriteBatch = new SpriteBatch(GraphicsDevice);
  47.  
  48.             base.Initialize();
  49.         }
  50.  
  51.         protected override void LoadContent()
  52.         {
  53.             spriteBatch = new SpriteBatch(GraphicsDevice);
  54.  
  55.             SpriteTexture.Load(Content, "sprite", FrameX, FrameY);
  56.             font = Content.Load<SpriteFont>("Courier New");
  57.             viewport = graphics.GraphicsDevice.Viewport;
  58.             Position = new Vector2(viewport.Width / 2, viewport.Height / 2);
  59.         }
  60.  
  61.         protected override void Update(GameTime gameTime)
  62.         {
  63.             if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  64.                 this.Exit();
  65.  
  66.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  67.  
  68.             float posx = Position.X;
  69.             float posy = Position.Y;
  70.             int maxH = viewport.Height;
  71.             int maxW = viewport.Width;
  72.  
  73.             if (Keyboard.GetState().IsKeyDown(Keys.Left) && (posx > 0))
  74.             {
  75.                 posx--;
  76.             }
  77.  
  78.             if (Keyboard.GetState().IsKeyDown(Keys.Left) && (Keyboard.GetState().IsKeyDown(Keys.Up)) && (posx > 0))
  79.             {
  80.                 posx -= 2;
  81.             }
  82.  
  83.             if (Keyboard.GetState().IsKeyDown(Keys.Right) && (posx < maxW - 100))
  84.             {
  85.                 posx++;
  86.             }
  87.  
  88.             if (Keyboard.GetState().IsKeyDown(Keys.Right) && (Keyboard.GetState().IsKeyDown(Keys.Up)) && (posx < maxW - 100))
  89.             {
  90.                 posx += 2;
  91.             }
  92.  
  93.             Position.X = posx;
  94.  
  95.             SpriteTexture.UpdateFrame(elapsed, Position);
  96.  
  97.             base.Update(gameTime);
  98.         }
  99.  
  100.         protected override void Draw(GameTime gameTime)
  101.         {
  102.             GraphicsDevice.Clear(Color.CornflowerBlue);
  103.  
  104.             // TODO: Add your drawing code here
  105.             float game = (float)(1 / gameTime.ElapsedGameTime.TotalSeconds);
  106.  
  107.             spriteBatch.Begin();
  108.             SpriteTexture.DrawFrame(spriteBatch, Position);
  109.             spriteBatch.DrawString(font, "FPS:" + game.ToString(), new Vector2(0, 0), Color.White);
  110.             spriteBatch.End();
  111.  
  112.             base.Draw(gameTime);
  113.         }
  114.     }
  115. }
Add Comment
Please, Sign In to add comment