Advertisement
Alisator

XNA 4.0 hra 03.03.12

Mar 3rd, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace AnimatedSprites
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         #region variables
  20.         GraphicsDeviceManager graphics;
  21.         SpriteBatch spriteBatch;
  22.         MouseState prevMouseState;
  23.  
  24.         // Texture stuff
  25.         Texture2D texture;
  26.         Texture2D skullTexture;
  27.         Texture2D plusTexture;
  28.         Point ringFrameSize = new Point(75, 75);
  29.         Point ringCurrentFrame = new Point(0, 0);
  30.         Point ringSheetSize = new Point(6, 8);
  31.         Point skullFrameSize = new Point(75, 75);
  32.         Point skullCurrentFrame = new Point(0, 0);
  33.         Point skullSheetSize = new Point(6, 8);
  34.         Point plusFrameSize = new Point(75, 75);
  35.         Point plusSheetSize = new Point(6, 4);
  36.         Point plusCurrentFrame = new Point(0, 0);
  37.         Vector2 ringPosition = new Vector2(100, 100);
  38.         Vector2 skullPosition = new Vector2(300, 300);
  39.         Vector2 plusPosition = new Vector2(0, 0);
  40.         float ringSpeed = 6f;
  41.         float skullSpeed = 2f;
  42.         float plusSpeed = 2f;
  43.  
  44.      // Framerate stuff
  45.         int ringTimeSinceLastFrame = 0;
  46.         int ringMillisecondsPerFrame = 50;
  47.         int skullTimeSinceLastFrame = 0;
  48.         int skullMillisecondsPerFrame = 50;
  49.         int plusTimeSinceLastFrame = 0;
  50.         int plusMillisecondsPerFrame = 50;
  51.         #endregion
  52.  
  53.         public Game1()
  54.         {
  55.             graphics = new GraphicsDeviceManager(this);
  56.             Content.RootDirectory = "Content";
  57.      }
  58.         #region init and load
  59.         /// <summary>
  60.         /// Allows the game to perform any initialization it needs to before starting to run.
  61.         /// This is where it can query for any required services and load any non-graphic
  62.         /// related content.  Calling base.Initialize will enumerate through any components
  63.         /// and initialize them as well.
  64.         /// </summary>
  65.         protected override void Initialize()
  66.         {
  67.             // TODO: Add your initialization logic here
  68.  
  69.             base.Initialize();
  70.         }
  71.  
  72.         /// <summary>
  73.         /// LoadContent will be called once per game and is the place to load
  74.         /// all of your content.
  75.         /// </summary>
  76.         protected override void LoadContent()
  77.         {
  78.             // Create a new SpriteBatch, which can be used to draw textures.
  79.             spriteBatch = new SpriteBatch(GraphicsDevice);
  80.  
  81.             texture = Content.Load<Texture2D>(@"images\threerings");
  82.             skullTexture = Content.Load<Texture2D>(@"images\skullball");
  83.             plusTexture = Content.Load<Texture2D>(@"images\plus");
  84.         }
  85.         #endregion
  86.         #region unload
  87.         /// <summary>
  88.         /// UnloadContent will be called once per game and is the place to unload
  89.         /// all content.
  90.         /// </summary>
  91.         protected override void UnloadContent()
  92.         {
  93.             // TODO: Unload any non ContentManager content here
  94.         }
  95.         #endregion
  96.         #region update
  97.         /// <summary>
  98.         /// Allows the game to run logic such as updating the world,
  99.         /// checking for collisions, gathering input, and playing audio.
  100.         /// </summary>
  101.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  102.         protected override void Update(GameTime gameTime)
  103.         {
  104.             // Allows the game to exit
  105.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  106.                 this.Exit();
  107.             updateFrameRateImages(ringTimeSinceLastFrame, ringMillisecondsPerFrame,
  108.                 ringCurrentFrame, ringSheetSize, gameTime);
  109.         //frameRateImages directly - metoda je výše, ale nefunkční -> animace nefunguje
  110.             plusTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  111.             if (plusTimeSinceLastFrame > plusMillisecondsPerFrame)
  112.             {
  113.                 plusTimeSinceLastFrame -= plusMillisecondsPerFrame;
  114.                 ++plusCurrentFrame.X;
  115.                 if (plusCurrentFrame.X >= plusSheetSize.X)
  116.                 {
  117.                     plusCurrentFrame.X = 0;
  118.                     ++plusCurrentFrame.Y;
  119.                     if (plusCurrentFrame.Y >= plusSheetSize.Y)
  120.                         plusCurrentFrame.Y = 0;
  121.                 }
  122.             }
  123.  
  124.             skullTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  125.             if (skullTimeSinceLastFrame > skullMillisecondsPerFrame)
  126.             {
  127.                 skullTimeSinceLastFrame -= skullMillisecondsPerFrame;
  128.                 ++skullCurrentFrame.X;
  129.                 if (skullCurrentFrame.X >= skullSheetSize.X)
  130.                 {
  131.                     skullCurrentFrame.X = 0;
  132.                     ++skullCurrentFrame.Y;
  133.                     if (skullCurrentFrame.Y >= skullSheetSize.Y)
  134.                         skullCurrentFrame.Y = 0;
  135.                 }
  136.             }
  137.  
  138.             KeyboardState keyboardState = Keyboard.GetState();
  139.             if (keyboardState.IsKeyDown(Keys.Left) == true)
  140.                 ringPosition.X -= ringSpeed;
  141.             if (keyboardState.IsKeyDown(Keys.Right) == true)
  142.                 ringPosition.X += ringSpeed;
  143.             if (keyboardState.IsKeyDown(Keys.Up) == true)
  144.                 ringPosition.Y -= ringSpeed;
  145.             if (keyboardState.IsKeyDown(Keys.Down) == true)
  146.                 ringPosition.Y += ringSpeed;
  147.             MouseState mouseState = Mouse.GetState();
  148.             if (mouseState.X != prevMouseState.X || mouseState.Y != prevMouseState.Y)
  149.                 ringPosition = new Vector2(mouseState.X, mouseState.Y);
  150.             prevMouseState = mouseState;
  151.             if (ringPosition.X < 0)
  152.                 ringPosition.X = 0;
  153.             if (ringPosition.Y < 0)
  154.                 ringPosition.Y = 0;
  155.             if (ringPosition.X > Window.ClientBounds.Width - ringFrameSize.X)
  156.                 ringPosition.X = Window.ClientBounds.Width - ringFrameSize.X;
  157.             if (ringPosition.Y > Window.ClientBounds.Height - ringFrameSize.Y)
  158.                 ringPosition.Y = Window.ClientBounds.Height - ringFrameSize.Y;
  159.     base.Update(gameTime);
  160.         }
  161.     //metoda na pomalejší animaci, která nefunguje
  162.         public void updateFrameRateImages(int timeSinceLastFrame, int millisecondsPerFrame,
  163.             Point currentFrame, Point sheetSize, GameTime gt)
  164.         {
  165.             timeSinceLastFrame += gt.ElapsedGameTime.Milliseconds;
  166.             if (timeSinceLastFrame > millisecondsPerFrame)
  167.             {
  168.                 timeSinceLastFrame -= millisecondsPerFrame;
  169.                 ++currentFrame.X;
  170.                 if (currentFrame.X >= sheetSize.X)
  171.                 {
  172.                     currentFrame.X = 0;
  173.                     ++currentFrame.Y;
  174.                     if (currentFrame.Y >= sheetSize.Y)
  175.                         currentFrame.Y = 0;
  176.                 }
  177.             }
  178.         }
  179.         #endregion
  180.         #region draw
  181.         /// <summary>
  182.         /// This is called when the game should draw itself.
  183.         /// </summary>
  184.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  185.         protected override void Draw(GameTime gameTime)
  186.         {
  187.             GraphicsDevice.Clear(Color.White);
  188.  
  189.             spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
  190.  
  191.             spriteBatch.Draw(texture, ringPosition,
  192.                 new Rectangle(ringCurrentFrame.X * ringFrameSize.X,
  193.                     ringCurrentFrame.Y * ringFrameSize.Y,
  194.                     ringFrameSize.X,
  195.                     ringFrameSize.Y),
  196.                 Color.White, 0, Vector2.Zero,
  197.                 1, SpriteEffects.None, 1);
  198.  
  199.             spriteBatch.Draw(skullTexture, skullPosition,
  200.                             new Rectangle(skullCurrentFrame.X * skullFrameSize.X,
  201.                                 skullCurrentFrame.Y * skullFrameSize.Y,
  202.                                 skullFrameSize.X,
  203.                                 skullFrameSize.Y),
  204.                                 Color.White, 0, Vector2.Zero,
  205.                                 1, SpriteEffects.None, 0);
  206.  
  207.             spriteBatch.Draw(plusTexture, plusPosition,
  208.                             new Rectangle(plusCurrentFrame.X * plusCurrentFrame.X,
  209.                                 plusCurrentFrame.Y * plusCurrentFrame.Y,
  210.                 plusCurrentFrame.X, plusCurrentFrame.Y),
  211.                                 Color.White, 0, Vector2.Zero, 1,
  212.                                 SpriteEffects.None, 0);
  213.  
  214.             spriteBatch.End();
  215.  
  216.             base.Draw(gameTime);
  217.         }
  218.         #endregion
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement