Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 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. public string yScale = "Up"; //sprite state var
  32. Vector2 megamanPosition;
  33. Vector2 megamanStartingPosition = Vector2.Zero;
  34. float speed = 4;
  35. bool jumping; //Is the character jumping?
  36. float startY, jumpspeed = 0; //startY to tell us //where it lands, jumpspeed to see how fast it jumps
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. public Game1()
  48. {
  49. graphics = new GraphicsDeviceManager(this);
  50. Content.RootDirectory = "Content";
  51. }
  52.  
  53. /// <summary>
  54. /// Allows the game to perform any initialization it needs to before starting to run.
  55. /// This is where it can query for any required services and load any non-graphic
  56. /// related content. Calling base.Initialize will enumerate through any components
  57. /// and initialize them as well.
  58. /// </summary>
  59. protected override void Initialize()
  60. {
  61. // TODO: Add your initialization logic here
  62.  
  63. base.Initialize();
  64.  
  65.  
  66. }
  67.  
  68. /// <summary>
  69. /// LoadContent will be called once per game and is the place to load
  70. /// all of your content.
  71. /// </summary>
  72. protected override void LoadContent()
  73. {
  74. // Create a new SpriteBatch, which can be used to draw textures.
  75. spriteBatch = new SpriteBatch(GraphicsDevice);
  76.  
  77. // TODO: use this.Content to load your game content here
  78. megaman = Content.Load<Texture2D>("MegamanX"); // Stand sprite facing to the right
  79. megamanFlip = Content.Load<Texture2D>("MegamanX_Flip"); // Stand sprite facing to the left
  80. background = Content.Load<Texture2D>("stage1"); // The temp background
  81. Texture2D texture = Content.Load<Texture2D>("Megaman Tileset"); // Character's running sprite loaded
  82. Texture2D texture2 = Content.Load<Texture2D>("MegamanX_Jump"); // Character's jumping sprite loaded
  83. megamanRun = new AnimatedSprite(texture, 1, 5); // Character's running sprite in a var
  84. megamanJump = new AnimatedSprite(texture2, 1, 7); // Character's jumping sprite in a var
  85. megamanPosition = new Vector2(150, 170); // Character's position upon starting the game
  86. startY = megamanPosition.Y;//Starting position
  87. jumping = false;//Init jumping to false
  88. jumpspeed = 0;//Default no speed
  89.  
  90.  
  91.  
  92.  
  93. }
  94.  
  95. /// <summary>
  96. /// UnloadContent will be called once per game and is the place to unload
  97. /// all content.
  98. /// </summary>
  99. protected override void UnloadContent()
  100. {
  101. // TODO: Unload any non ContentManager content here
  102. }
  103.  
  104. /// <summary>
  105. /// Allows the game to run logic such as updating the world,
  106. /// checking for collisions, gathering input, and playing audio.
  107. /// </summary>
  108. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  109. protected override void Update(GameTime gameTime)
  110. {
  111. // Allows the game to exit
  112. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  113. this.Exit();
  114.  
  115. keyBoard = Keyboard.GetState();
  116.  
  117. if (keyBoard.IsKeyDown(Keys.Right))
  118. {
  119. megamanPosition.X += speed;
  120. xScale = "Right";
  121. //Walk to the right
  122. }
  123.  
  124. if (keyBoard.IsKeyDown(Keys.Left))
  125. {
  126. megamanPosition.X -= speed;
  127. xScale = "Left";
  128. //Walk to the left
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. if (keyBoard.Equals(new KeyboardState()))
  136. {
  137. if (xScale == "Right")
  138. {
  139. xScale = "StandRight";
  140. }
  141. else if (xScale == "Left")
  142. {
  143. xScale = "StandLeft";
  144. }
  145.  
  146.  
  147. //No keys are pressed.
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154. //movment control speed
  155. timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  156. if (timeSinceLastFrame > millisecondsPerFrame)
  157. {
  158. timeSinceLastFrame -= millisecondsPerFrame;
  159. megamanRun.Update();
  160. }
  161.  
  162.  
  163. if (jumping)
  164. {
  165. megamanPosition.Y += jumpspeed;//Making it go up
  166. jumpspeed += 1;
  167.  
  168. if (megamanPosition.Y >= startY)
  169. //If it's farther than ground
  170. {
  171. megamanPosition.Y = startY;//Then set it on
  172. jumping = false;
  173.  
  174. }
  175. }
  176. else
  177. {
  178. if (keyBoard.IsKeyDown(Keys.Up))
  179. {
  180. jumping = true;
  181. jumpspeed = -14;//Give it upward thrust
  182.  
  183. }
  184. }
  185.  
  186. base.Update(gameTime);
  187. }
  188.  
  189. /// <summary>
  190. /// This is called when the game should draw itself.
  191. /// </summary>
  192. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  193. protected override void Draw(GameTime gameTime)
  194. {
  195. GraphicsDevice.Clear(Color.CornflowerBlue);
  196.  
  197. spriteBatch.Begin();
  198. spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White);
  199. spriteBatch.End();
  200.  
  201. if (xScale == "Right" || xScale == "Left")
  202. {
  203. megamanRun.Draw(spriteBatch, megamanPosition, xScale);
  204. }
  205.  
  206. else if (xScale == "StandRight")
  207. {
  208. spriteBatch.Begin();
  209. spriteBatch.Draw(megaman, megamanPosition, Color.White);
  210. spriteBatch.End();
  211.  
  212. }
  213. else if(xScale == "StandLeft")
  214. {
  215. spriteBatch.Begin();
  216. spriteBatch.Draw(megamanFlip, megamanPosition, Color.White);
  217. spriteBatch.End();
  218. }
  219.  
  220. else if (jumping)
  221. {
  222. megamanJump.Draw(spriteBatch, megamanPosition, xScale);
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. base.Draw(gameTime);
  231. }
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement