Advertisement
Guest User

Untitled

a guest
Jul 20th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 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.         GraphicsDeviceManager graphics;
  20.         SpriteBatch spriteBatch;
  21.         Texture2D texture;
  22.         Vector2 position;
  23.         float horizontalSpeed = 4;
  24.         float verticalSpeed = 4;
  25.         Point frameSize = new Point(32, 48);
  26.         Point currentFrame = new Point(0, 0);
  27.         Point sheetSize = new Point(4, 4);
  28.  
  29.         public Game1()
  30.         {
  31.             graphics = new GraphicsDeviceManager(this);
  32.             Content.RootDirectory = "Content";
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Allows the game to perform any initialization it needs to before starting to run.
  37.         /// This is where it can query for any required services and load any non-graphic
  38.         /// related content.  Calling base.Initialize will enumerate through any components
  39.         /// and initialize them as well.
  40.         /// </summary>
  41.         protected override void Initialize()
  42.         {
  43.             // TODO: Add your initialization logic here
  44.  
  45.             base.Initialize();
  46.         }
  47.  
  48.         /// <summary>
  49.         /// LoadContent will be called once per game and is the place to load
  50.         /// all of your content.
  51.         /// </summary>
  52.         protected override void LoadContent()
  53.         {
  54.             // Create a new SpriteBatch, which can be used to draw textures.
  55.             spriteBatch = new SpriteBatch(GraphicsDevice);
  56.             texture = Content.Load<Texture2D>(@"Images\indy");
  57.             // TODO: use this.Content to load your game content here
  58.         }
  59.  
  60.         /// <summary>
  61.         /// UnloadContent will be called once per game and is the place to unload
  62.         /// all content.
  63.         /// </summary>
  64.         protected override void UnloadContent()
  65.         {
  66.             // TODO: Unload any non ContentManager content here
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Allows the game to run logic such as updating the world,
  71.         /// checking for collisions, gathering input, and playing audio.
  72.         /// </summary>
  73.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  74.         protected override void Update(GameTime gameTime)
  75.         {
  76.             // Allows the game to exit
  77.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  78.                 this.Exit();
  79.  
  80.             ++currentFrame.X;
  81.             if (currentFrame.X >= sheetSize.X)
  82.             {
  83.                 currentFrame.X = 0;
  84.                 ++currentFrame.Y;
  85.  
  86.                 if (currentFrame.Y >= sheetSize.Y)
  87.                 {
  88.                     currentFrame.Y = 0;
  89.                 }
  90.             }
  91.  
  92.             position.X += horizontalSpeed;
  93.             position.Y += verticalSpeed;
  94.             if (position.X > Window.ClientBounds.Width - frameSize.X || position.X < 0)
  95.             {
  96.                 horizontalSpeed *= -1;
  97.             }
  98.             if (position.Y > Window.ClientBounds.Height - frameSize.Y || position.Y < 0)
  99.             {
  100.                 verticalSpeed *= -1;
  101.             }
  102.  
  103.  
  104.             base.Update(gameTime);
  105.         }
  106.  
  107.         /// <summary>
  108.         /// This is called when the game should draw itself.
  109.         /// </summary>
  110.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  111.         protected override void Draw(GameTime gameTime)
  112.         {
  113.             GraphicsDevice.Clear(Color.Black);
  114.  
  115.             spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
  116.  
  117.             spriteBatch.Draw(texture,
  118.                                 position,
  119.                                 new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y),
  120.                                 Color.White,
  121.                                 0,
  122.                                 Vector2.Zero,
  123.                                 1,
  124.                                 SpriteEffects.None,
  125.                                 0);
  126.  
  127.             spriteBatch.End();
  128.  
  129.             base.Draw(gameTime);
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement