Advertisement
amitsly1

Untitled

Oct 4th, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 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 XNA_Game_Try_2
  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. private Texture2D megaman;
  22. private Texture2D megamanFlip;
  23. private Texture2D background;
  24. private KeyboardState keyBoard;
  25. private AnimatedSprite megamanRun;
  26. private AnimatedSprite megamanJump;
  27. private AnimatedSprite megamanJumpFlip;
  28. int timeSinceLastFrame = 0; // two vars to control
  29. int millisecondsPerFrame = 100; // the speed of the movement
  30. public string xScale = "Right"; //sprite state var
  31. Vector2 megamanPosition;
  32. float speed = 4;
  33. float jumpHeight = 4;
  34.  
  35.  
  36. public Game1()
  37. {
  38. graphics = new GraphicsDeviceManager(this);
  39. Content.RootDirectory = "Content";
  40. }
  41.  
  42. /// <summary>
  43. /// Allows the game to perform any initialization it needs to before starting to run.
  44. /// This is where it can query for any required services and load any non-graphic
  45. /// related content. Calling base.Initialize will enumerate through any components
  46. /// and initialize them as well.
  47. /// </summary>
  48. protected override void Initialize()
  49. {
  50. // TODO: Add your initialization logic here
  51.  
  52. base.Initialize();
  53. }
  54.  
  55. /// <summary>
  56. /// LoadContent will be called once per game and is the place to load
  57. /// all of your content.
  58. /// </summary>
  59. protected override void LoadContent()
  60. {
  61. // Create a new SpriteBatch, which can be used to draw textures.
  62. spriteBatch = new SpriteBatch(GraphicsDevice);
  63.  
  64. // TODO: use this.Content to load your game content here
  65. megaman = Content.Load<Texture2D>("MegamanX"); // Stand sprite facing to the right
  66. megamanFlip = Content.Load<Texture2D>("MegamanX_Flip"); // Stand sprite facing to the left
  67. background = Content.Load<Texture2D>("stage1"); // The temp background
  68. Texture2D texture = Content.Load<Texture2D>("Megaman Tileset"); // Character's running sprite loaded
  69. Texture2D texture2 = Content.Load<Texture2D>("MegamanX_Jump"); // Character's jumping sprite loaded
  70. megamanRun = new AnimatedSprite(texture, 1, 5); // Character's running sprite in a var
  71. megamanJump = new AnimatedSprite(texture2, 1, 7); // Character's jumping sprite in a var
  72. megamanPosition = new Vector2(150, 170); // Character's position upon starting the game
  73.  
  74.  
  75.  
  76.  
  77. }
  78.  
  79. /// <summary>
  80. /// UnloadContent will be called once per game and is the place to unload
  81. /// all content.
  82. /// </summary>
  83. protected override void UnloadContent()
  84. {
  85. // TODO: Unload any non ContentManager content here
  86. }
  87.  
  88. /// <summary>
  89. /// Allows the game to run logic such as updating the world,
  90. /// checking for collisions, gathering input, and playing audio.
  91. /// </summary>
  92. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  93. protected override void Update(GameTime gameTime)
  94. {
  95. // Allows the game to exit
  96. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  97. this.Exit();
  98.  
  99. // TODO: Add your update logic here
  100. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  101. this.Exit();
  102. keyBoard = Keyboard.GetState();
  103.  
  104. if (keyBoard.IsKeyDown(Keys.Right))
  105. {
  106. megamanPosition.X += speed;
  107. xScale = "Right";
  108. //Walk to the right
  109. }
  110.  
  111. if (keyBoard.IsKeyDown(Keys.Left))
  112. {
  113. megamanPosition.X -= speed;
  114. xScale = "Left";
  115. //Walk to the left
  116. }
  117. if (keyBoard.Equals(new KeyboardState()))
  118. {
  119. if (xScale == "Right")
  120. {
  121. xScale = "StandRight";
  122. }
  123. else if (xScale == "Left")
  124. {
  125. xScale = "StandLeft";
  126. }
  127. //No keys are pressed.
  128. }
  129.  
  130.  
  131.  
  132. //movment control speed
  133. timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  134. if (timeSinceLastFrame > millisecondsPerFrame)
  135. {
  136. timeSinceLastFrame -= millisecondsPerFrame;
  137. megamanRun.Update();
  138. }
  139.  
  140.  
  141.  
  142. base.Update(gameTime);
  143. }
  144.  
  145. /// <summary>
  146. /// This is called when the game should draw itself.
  147. /// </summary>
  148. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  149. protected override void Draw(GameTime gameTime)
  150. {
  151. GraphicsDevice.Clear(Color.CornflowerBlue);
  152.  
  153. // TODO: Add your drawing code here
  154. spriteBatch.Begin();
  155. spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
  156. spriteBatch.End();
  157.  
  158. if (xScale == "Right" || xScale == "Left")
  159. {
  160. //draw
  161. megamanRun.Draw(spriteBatch, megamanPosition, xScale);
  162. }
  163.  
  164. else if (xScale == "StandRight")
  165. {
  166. spriteBatch.Begin();
  167. spriteBatch.Draw(megaman, megamanPosition, Color.White);
  168. spriteBatch.End();
  169.  
  170. }
  171. else if(xScale == "StandLeft")
  172. {
  173. spriteBatch.Begin();
  174. spriteBatch.Draw(megamanFlip, megamanPosition, Color.White);
  175. spriteBatch.End();
  176. }
  177. else if (xScale == "JumpRight" || xScale == "JumpLeft")
  178. {
  179. megamanJump.Draw(spriteBatch, megamanPosition, xScale);
  180.  
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187. base.Draw(gameTime);
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement