Advertisement
tandaleyo

ProgrammingAssignment2

Jun 29th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System;
  2.  
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace ProgrammingAssignment2
  8. {
  9. /// <summary>
  10. /// This is the main type for your game
  11. /// </summary>
  12. public class Game1 : Microsoft.Xna.Framework.Game
  13. {
  14. const int WindowWidth = 800;
  15. const int WindowHeight = 600;
  16.  
  17. GraphicsDeviceManager graphics;
  18. SpriteBatch spriteBatch;
  19.  
  20. Texture2D monster0;
  21. Texture2D monster1;
  22. Texture2D monster2;
  23.  
  24. int speedX;
  25. int speedY;
  26.  
  27. // used to handle generating random values
  28. Random rand = new Random();
  29. const int ChangeDelayTime = 1000;
  30. int elapsedTime = 0;
  31.  
  32. // used to keep track of current sprite and location
  33. Texture2D currentSprite;
  34. Rectangle drawRectangle = new Rectangle();
  35.  
  36. public Game1()
  37. {
  38. graphics = new GraphicsDeviceManager(this);
  39. Content.RootDirectory = "Content";
  40.  
  41. graphics.PreferredBackBufferWidth = WindowWidth;
  42. graphics.PreferredBackBufferHeight = WindowHeight;
  43. }
  44.  
  45. /// <summary>
  46. /// Allows the game to perform any initialization it needs to before starting to run.
  47. /// This is where it can query for any required services and load any non-graphic
  48. /// related content. Calling base.Initialize will enumerate through any components
  49. /// and initialize them as well.
  50. /// </summary>
  51. protected override void Initialize()
  52. {
  53. // TODO: Add your initialization logic here
  54.  
  55. base.Initialize();
  56. }
  57.  
  58. /// <summary>
  59. /// LoadContent will be called once per game and is the place to load
  60. /// all of your content.
  61. /// </summary>
  62. protected override void LoadContent()
  63. {
  64. // Create a new SpriteBatch, which can be used to draw textures.
  65. spriteBatch = new SpriteBatch(GraphicsDevice);
  66.  
  67. monster0 = Content.Load<Texture2D>(@"graphics/monster0");
  68. monster1 = Content.Load<Texture2D>(@"graphics/monster1");
  69. monster2 = Content.Load<Texture2D>(@"graphics/monster2");
  70.  
  71. currentSprite = monster0;
  72. }
  73.  
  74. /// <summary>
  75. /// UnloadContent will be called once per game and is the place to unload
  76. /// all content.
  77. /// </summary>
  78. protected override void UnloadContent()
  79. {
  80. // TODO: Unload any non ContentManager content here
  81. }
  82.  
  83. /// <summary>
  84. /// Allows the game to run logic such as updating the world,
  85. /// checking for collisions, gathering input, and playing audio.
  86. /// </summary>
  87. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  88. protected override void Update(GameTime gameTime)
  89. {
  90. // Allows the game to exit
  91. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  92. this.Exit();
  93.  
  94. elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
  95. if (elapsedTime > ChangeDelayTime)
  96. {
  97. elapsedTime = 0;
  98. int spriteNumber = rand.Next(0, 3);
  99.  
  100. if (spriteNumber == 0)
  101. {
  102. currentSprite = monster0;
  103. }
  104. else if (spriteNumber == 1)
  105. {
  106. currentSprite = monster1;
  107. }
  108. else if (spriteNumber == 2)
  109. {
  110. currentSprite = monster2;
  111. }
  112.  
  113. drawRectangle.Width = currentSprite.Width;
  114. drawRectangle.Height = currentSprite.Height;
  115.  
  116. drawRectangle.X = WindowWidth / 2 - drawRectangle.Width / 2;
  117. drawRectangle.Y = WindowHeight / 2 - drawRectangle.Height / 2;
  118.  
  119. speedX = rand.Next(-4, 5);
  120. speedY = rand.Next(-4, 5);
  121.  
  122. }
  123. drawRectangle.X += speedX;
  124. drawRectangle.Y += speedY;
  125.  
  126. base.Update(gameTime);
  127. }
  128.  
  129. /// <summary>
  130. /// This is called when the game should draw itself.
  131. /// </summary>
  132. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  133. protected override void Draw(GameTime gameTime)
  134. {
  135. GraphicsDevice.Clear(Color.CornflowerBlue);
  136. spriteBatch.Begin();
  137. spriteBatch.Draw(currentSprite, drawRectangle, Color.White);
  138. spriteBatch.End();
  139. base.Draw(gameTime);
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement