Advertisement
tandaleyo

ProgrammingAssignment3Game1

Jul 3rd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 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 ProgrammingAssignment3
  8. {
  9. /// <summary>
  10. /// This is the main type for your game.
  11. /// </summary>
  12. public class Game1 : Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16.  
  17. const int WindowWidth = 800;
  18. const int WindowHeight = 600;
  19.  
  20. Random rand = new Random();
  21. Vector2 centerLocation = new Vector2(
  22. WindowWidth / 2, WindowHeight / 2);
  23.  
  24. // STUDENTS: declare variables for 3 rock sprites
  25. Texture2D greenrock;
  26. Texture2D magentarock;
  27. Texture2D whiterock;
  28.  
  29.  
  30.  
  31.  
  32. // STUDENTS: declare variables for 3 rocks
  33. Rock rock0;
  34. Rock rock1;
  35. Rock rock2;
  36.  
  37.  
  38.  
  39. // delay support
  40. const int TotalDelayMilliseconds = 1000;
  41. int elapsedDelayMilliseconds = 0;
  42.  
  43. // random velocity support
  44. const float BaseSpeed = 0.15f;
  45. Vector2 upLeft = new Vector2(-BaseSpeed, -BaseSpeed);
  46. Vector2 upRight = new Vector2(BaseSpeed, -BaseSpeed);
  47. Vector2 downRight = new Vector2(BaseSpeed, BaseSpeed);
  48. Vector2 downLeft = new Vector2(-BaseSpeed, BaseSpeed);
  49.  
  50. public Game1()
  51. {
  52. graphics = new GraphicsDeviceManager(this);
  53. Content.RootDirectory = "Content";
  54.  
  55. // change resolution
  56. graphics.PreferredBackBufferWidth = WindowWidth;
  57. graphics.PreferredBackBufferHeight = WindowHeight;
  58. }
  59.  
  60. /// <summary>
  61. /// Allows the game to perform any initialization it needs to before starting to run.
  62. /// This is where it can query for any required services and load any non-graphic
  63. /// related content. Calling base.Initialize will enumerate through any components
  64. /// and initialize them as well.
  65. /// </summary>
  66. protected override void Initialize()
  67. {
  68. // TODO: Add your initialization logic here
  69.  
  70. base.Initialize();
  71. }
  72.  
  73. /// <summary>
  74. /// LoadContent will be called once per game and is the place to load
  75. /// all of your content.
  76. /// </summary>
  77. protected override void LoadContent()
  78. {
  79. // Create a new SpriteBatch, which can be used to draw textures.
  80. spriteBatch = new SpriteBatch(GraphicsDevice);
  81.  
  82. // STUDENTS: Load content for 3 sprites
  83.  
  84. greenrock = Content.Load<Texture2D>(@"graphics/greenrock");
  85. magentarock = Content.Load<Texture2D>(@"graphics/magentarock");
  86. whiterock = Content.Load<Texture2D>(@"graphics/whiterock");
  87.  
  88. rock0 = GetRandomRock();
  89. rock1 = GetRandomRock();
  90. rock2 = GetRandomRock();
  91.  
  92.  
  93. }
  94.  
  95. /// <summary>
  96. /// UnloadContent will be called once per game and is the place to unload
  97. /// game-specific 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. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  112. Exit();
  113.  
  114. // STUDENTS: update rocks
  115. if (rock0 != null)
  116. {
  117. rock0.Update(gameTime);
  118. }
  119.  
  120. if (rock1 != null)
  121. {
  122. rock1.Update(gameTime);
  123. }
  124.  
  125. if (rock2 != null)
  126. {
  127. rock2.Update(gameTime);
  128. }
  129.  
  130.  
  131.  
  132.  
  133. // update timer
  134. elapsedDelayMilliseconds += gameTime.ElapsedGameTime.Milliseconds;
  135. if (elapsedDelayMilliseconds >= TotalDelayMilliseconds)
  136. {
  137. // STUDENTS: timer expired, so spawn new rock if fewer than 3 rocks in window
  138. // Call the GetRandomRock method to do this
  139. if (rock0 == null)
  140. {
  141. rock0 = GetRandomRock();
  142. }
  143.  
  144. if (rock1 == null)
  145. {
  146. rock1 = GetRandomRock();
  147. }
  148.  
  149. if (rock2 == null)
  150. {
  151. rock2 = GetRandomRock();
  152. }
  153.  
  154.  
  155. // restart timer
  156. elapsedDelayMilliseconds = 0;
  157. }
  158.  
  159. // STUDENTS: Check each rock to see if it's outside the window. If so
  160. // spawn a new random rock for it by calling the GetRandomRock method
  161. // Caution: Only check the property if the variable isn't null
  162.  
  163. if (rock0.OutsideWindow)
  164. {
  165. rock0 = GetRandomRock();
  166. }
  167.  
  168. if (rock1.OutsideWindow)
  169. {
  170. rock1 = GetRandomRock();
  171. }
  172.  
  173. if (rock2.OutsideWindow)
  174. {
  175. rock2 = GetRandomRock();
  176. }
  177.  
  178.  
  179.  
  180.  
  181.  
  182. base.Update(gameTime);
  183. }
  184.  
  185. /// <summary>
  186. /// This is called when the game should draw itself.
  187. /// </summary>
  188. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  189. protected override void Draw(GameTime gameTime)
  190. {
  191. GraphicsDevice.Clear(Color.CornflowerBlue);
  192.  
  193. // STUDENTS: draw rocks
  194. spriteBatch.Begin();
  195.  
  196. rock0.Draw(spriteBatch);
  197. rock1.Draw(spriteBatch);
  198. rock2.Draw(spriteBatch);
  199.  
  200.  
  201.  
  202.  
  203. spriteBatch.End();
  204.  
  205. base.Draw(gameTime);
  206. }
  207.  
  208. /// <summary>
  209. /// Gets a rock with a random sprite and velocity
  210. /// </summary>
  211. /// <returns>the rock</returns>
  212. private Rock GetRandomRock()
  213. {
  214. // STUDENTS: Uncomment and complete the code below to randomly pick a rock sprite by calling the GetRandomSprite method
  215. Texture2D sprite = GetRandomSprite();
  216.  
  217. // STUDENTS: Uncomment and complete the code below to randomly pick a velocity by calling the GetRandomVelocity method
  218. Vector2 velocity = GetRandomVelocity();
  219.  
  220. // STUDENTS: After completing the two lines of code above, delete the following two lines of code
  221. // They're only included so the code I provided to you compiles
  222.  
  223.  
  224. // return a new rock, centered in the window, with the random sprite and velocity
  225. return new Rock(sprite, centerLocation, velocity, WindowWidth, WindowHeight);
  226. }
  227.  
  228. /// <summary>
  229. /// Gets a random sprite
  230. /// </summary>
  231. /// <returns>the sprite</returns>
  232. private Texture2D GetRandomSprite()
  233. {
  234. // STUDENTS: Uncommment and modify the code below as appropriate to return
  235. // a random sprite
  236. int spriteNumber = rand.Next(0, 3);
  237. if (spriteNumber == 0)
  238. {
  239. return greenrock;
  240. }
  241. else if (spriteNumber == 1)
  242. {
  243. return magentarock;
  244. }
  245. else
  246. {
  247. return whiterock;
  248. }
  249.  
  250. // STUDENTS: After completing the code above, delete the following line of code
  251. // It's only included so the code I provided to you compiles
  252.  
  253. }
  254.  
  255. /// <summary>
  256. /// Gets a random velocity
  257. /// </summary>
  258. /// <returns>the velocity</returns>
  259. private Vector2 GetRandomVelocity()
  260. {
  261. // STUDENTS: Uncommment and modify the code below as appropriate to return
  262. // a random velocity
  263. int velocityNumber = rand.Next(0, 3);
  264. if (velocityNumber == 0)
  265. {
  266. return upLeft;
  267. }
  268. else if (velocityNumber == 1)
  269. {
  270. return upRight;
  271. }
  272. else if (velocityNumber == 2)
  273. {
  274. return downRight;
  275. }
  276. else
  277. {
  278. return downLeft;
  279. }
  280.  
  281. // STUDENTS: After completing the code above, delete the following line of code
  282. // It's only included so the code I provided to you compiles
  283.  
  284. }
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement