Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace AnimatedSprites
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- #region variables
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- MouseState prevMouseState;
- // Texture stuff
- Texture2D texture;
- Texture2D skullTexture;
- Texture2D plusTexture;
- Point ringFrameSize = new Point(75, 75);
- Point ringCurrentFrame = new Point(0, 0);
- Point ringSheetSize = new Point(6, 8);
- Point skullFrameSize = new Point(75, 75);
- Point skullCurrentFrame = new Point(0, 0);
- Point skullSheetSize = new Point(6, 8);
- Point plusFrameSize = new Point(75, 75);
- Point plusSheetSize = new Point(6, 4);
- Point plusCurrentFrame = new Point(0, 0);
- Vector2 ringPosition = new Vector2(100, 100);
- Vector2 skullPosition = new Vector2(300, 300);
- Vector2 plusPosition = new Vector2(0, 0);
- float ringSpeed = 6f;
- float skullSpeed = 2f;
- float plusSpeed = 2f;
- // Framerate stuff
- int ringTimeSinceLastFrame = 0;
- int ringMillisecondsPerFrame = 50;
- int skullTimeSinceLastFrame = 0;
- int skullMillisecondsPerFrame = 50;
- int plusTimeSinceLastFrame = 0;
- int plusMillisecondsPerFrame = 50;
- #endregion
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- #region init and load
- /// <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
- 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);
- texture = Content.Load<Texture2D>(@"images\threerings");
- skullTexture = Content.Load<Texture2D>(@"images\skullball");
- plusTexture = Content.Load<Texture2D>(@"images\plus");
- }
- #endregion
- #region unload
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- #endregion
- #region update
- /// <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)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- updateFrameRateImages(ringTimeSinceLastFrame, ringMillisecondsPerFrame,
- ringCurrentFrame, ringSheetSize, gameTime);
- //frameRateImages directly - metoda je výše, ale nefunkční -> animace nefunguje
- plusTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
- if (plusTimeSinceLastFrame > plusMillisecondsPerFrame)
- {
- plusTimeSinceLastFrame -= plusMillisecondsPerFrame;
- ++plusCurrentFrame.X;
- if (plusCurrentFrame.X >= plusSheetSize.X)
- {
- plusCurrentFrame.X = 0;
- ++plusCurrentFrame.Y;
- if (plusCurrentFrame.Y >= plusSheetSize.Y)
- plusCurrentFrame.Y = 0;
- }
- }
- skullTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
- if (skullTimeSinceLastFrame > skullMillisecondsPerFrame)
- {
- skullTimeSinceLastFrame -= skullMillisecondsPerFrame;
- ++skullCurrentFrame.X;
- if (skullCurrentFrame.X >= skullSheetSize.X)
- {
- skullCurrentFrame.X = 0;
- ++skullCurrentFrame.Y;
- if (skullCurrentFrame.Y >= skullSheetSize.Y)
- skullCurrentFrame.Y = 0;
- }
- }
- KeyboardState keyboardState = Keyboard.GetState();
- if (keyboardState.IsKeyDown(Keys.Left) == true)
- ringPosition.X -= ringSpeed;
- if (keyboardState.IsKeyDown(Keys.Right) == true)
- ringPosition.X += ringSpeed;
- if (keyboardState.IsKeyDown(Keys.Up) == true)
- ringPosition.Y -= ringSpeed;
- if (keyboardState.IsKeyDown(Keys.Down) == true)
- ringPosition.Y += ringSpeed;
- MouseState mouseState = Mouse.GetState();
- if (mouseState.X != prevMouseState.X || mouseState.Y != prevMouseState.Y)
- ringPosition = new Vector2(mouseState.X, mouseState.Y);
- prevMouseState = mouseState;
- if (ringPosition.X < 0)
- ringPosition.X = 0;
- if (ringPosition.Y < 0)
- ringPosition.Y = 0;
- if (ringPosition.X > Window.ClientBounds.Width - ringFrameSize.X)
- ringPosition.X = Window.ClientBounds.Width - ringFrameSize.X;
- if (ringPosition.Y > Window.ClientBounds.Height - ringFrameSize.Y)
- ringPosition.Y = Window.ClientBounds.Height - ringFrameSize.Y;
- base.Update(gameTime);
- }
- //metoda na pomalejší animaci, která nefunguje
- public void updateFrameRateImages(int timeSinceLastFrame, int millisecondsPerFrame,
- Point currentFrame, Point sheetSize, GameTime gt)
- {
- timeSinceLastFrame += gt.ElapsedGameTime.Milliseconds;
- if (timeSinceLastFrame > millisecondsPerFrame)
- {
- timeSinceLastFrame -= millisecondsPerFrame;
- ++currentFrame.X;
- if (currentFrame.X >= sheetSize.X)
- {
- currentFrame.X = 0;
- ++currentFrame.Y;
- if (currentFrame.Y >= sheetSize.Y)
- currentFrame.Y = 0;
- }
- }
- }
- #endregion
- #region draw
- /// <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.White);
- spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
- spriteBatch.Draw(texture, ringPosition,
- new Rectangle(ringCurrentFrame.X * ringFrameSize.X,
- ringCurrentFrame.Y * ringFrameSize.Y,
- ringFrameSize.X,
- ringFrameSize.Y),
- Color.White, 0, Vector2.Zero,
- 1, SpriteEffects.None, 1);
- spriteBatch.Draw(skullTexture, skullPosition,
- new Rectangle(skullCurrentFrame.X * skullFrameSize.X,
- skullCurrentFrame.Y * skullFrameSize.Y,
- skullFrameSize.X,
- skullFrameSize.Y),
- Color.White, 0, Vector2.Zero,
- 1, SpriteEffects.None, 0);
- spriteBatch.Draw(plusTexture, plusPosition,
- new Rectangle(plusCurrentFrame.X * plusCurrentFrame.X,
- plusCurrentFrame.Y * plusCurrentFrame.Y,
- plusCurrentFrame.X, plusCurrentFrame.Y),
- Color.White, 0, Vector2.Zero, 1,
- SpriteEffects.None, 0);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement