Advertisement
Guest User

Game1.cs

a guest
Oct 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4.  
  5. namespace Pong
  6. {
  7. /// <summary>
  8. /// This is the main type for your game.
  9. /// </summary>
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14. Texture2D background;
  15. Texture2D rocket1;
  16. Texture2D rocket2;
  17. Texture2D ball;
  18.  
  19. Rockets rockets = new Rockets();
  20.  
  21. Rectangle ballRectangle;
  22. Rectangle rocket1Rectangle;
  23. Rectangle rocket2Rectangle;
  24.  
  25. int rocket1XValue = 0;
  26. int rocket1YValue = 250;
  27. int rocket2XValue = 775;
  28. int rocket2YValue = 250;
  29. int ballXValue = 390;
  30. int ballYValue = 290;
  31.  
  32. int one = 0;
  33. int two = 0;
  34.  
  35. float speedX;
  36. float speedY;
  37.  
  38. Vector2 ballVelocity;
  39.  
  40. public Game1()
  41. {
  42. graphics = new GraphicsDeviceManager(this);
  43. graphics.PreferredBackBufferHeight = 600;
  44. graphics.PreferredBackBufferWidth = 800;
  45. Window.AllowUserResizing = false;
  46. IsMouseVisible = true;
  47. Content.RootDirectory = "Content";
  48. }
  49.  
  50. /// <summary>
  51. /// Allows the game to perform any initialization it needs to before starting to run.
  52. /// This is where it can query for any required services and load any non-graphic
  53. /// related content. Calling base.Initialize will enumerate through any components
  54. /// and initialize them as well.
  55. /// </summary>
  56. protected override void Initialize()
  57. {
  58. // TODO: Add your initialization logic here
  59. speedX = 5;
  60. speedY = 0;
  61. base.Initialize();
  62.  
  63.  
  64. }
  65.  
  66. /// <summary>
  67. /// LoadContent will be called once per game and is the place to load
  68. /// </summary>
  69. /// all of your content.
  70. protected override void LoadContent()
  71. {
  72. // Create a new SpriteBatch, which can be used to draw textures.
  73. spriteBatch = new SpriteBatch(GraphicsDevice);
  74.  
  75. background = Content.Load<Texture2D>("pongBackground");
  76.  
  77. rockets.LoadContent(Content);
  78.  
  79.  
  80. ball = Content.Load<Texture2D>("ball");
  81.  
  82. ballRectangle = new Rectangle(ballXValue, ballYValue, ball.Width - 4, ball.Height - 5);
  83.  
  84.  
  85. // TODO: use this.Content to load your game content here
  86. }
  87.  
  88. /// <summary>
  89. /// UnloadContent will be called once per game and is the place to unload
  90. /// game-specific content.
  91. /// </summary>
  92. protected override void UnloadContent()
  93. {
  94. // TODO: Unload any non ContentManager content here
  95. }
  96.  
  97. /// <summary>
  98. /// Allows the game to run logic such as updating the world,
  99. /// checking for collisions, gathering input, and playing audio.
  100. /// </summary>
  101. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  102. protected override void Update(GameTime gameTime)
  103. {
  104. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  105. Exit();
  106.  
  107. ballRectangle.X += (int)ballVelocity.X;
  108. ballRectangle.Y += (int)ballVelocity.Y;
  109. ballVelocity = new Vector2(speedX, speedY);
  110.  
  111.  
  112. if(Keyboard.GetState().IsKeyDown(Keys.Space))
  113. {
  114. this.Initialize();
  115. }
  116. if (ballRectangle.Intersects(rockets.rocketUp1))
  117. {
  118. speedX = 5;
  119. speedY = -5;
  120. }
  121. if(ballRectangle.Intersects(rockets.rocketMiddle1))
  122. {
  123. speedX = 5;
  124. speedY = 0;
  125. }
  126. if (ballRectangle.Intersects(rockets.rocketDown1))
  127. {
  128. speedX = 5;
  129. speedY = 5;
  130. }
  131. if (ballRectangle.Intersects(rockets.rocketUp2))
  132. {
  133. speedX = -5;
  134. speedY = -5;
  135. }
  136. if (ballRectangle.Intersects(rockets.rocketMiddle2))
  137. {
  138. speedX = -5;
  139. speedY = 0;
  140. }
  141. if (ballRectangle.Intersects(rockets.rocketDown2))
  142. {
  143. speedX = -5;
  144. speedY = 5;
  145. }
  146. if(ballRectangle.X <= 0)
  147. {
  148. two++;
  149. //speedX = 5;
  150. //speedY = 0;
  151. //Initialize();
  152. }
  153. if(ballRectangle.X >=800)
  154. {
  155. one++;
  156. //speedX = -5;
  157. //speedY = 0;
  158. //Initialize();
  159. }
  160. if(ballRectangle.Y <= 0)
  161. {
  162. speedY = -speedY;
  163. ballRectangle.Y += 10;
  164. }
  165. if(ballRectangle.Y >= 600-10)
  166. {
  167. speedY = -speedY;
  168. ballRectangle.Y -= 10;
  169. }
  170.  
  171. rockets.Update(gameTime);
  172.  
  173. base.Update(gameTime);
  174. }
  175.  
  176. /// <summary>
  177. /// This is called when the game should draw itself.
  178. /// </summary>
  179. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  180. protected override void Draw(GameTime gameTime)
  181. {
  182. GraphicsDevice.Clear(Color.CornflowerBlue);
  183. spriteBatch.Begin();
  184. spriteBatch.Draw(background, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
  185.  
  186. rockets.Draw(spriteBatch);
  187.  
  188. spriteBatch.Draw(ball, ballRectangle, Color.White);
  189.  
  190. spriteBatch.End();
  191. base.Draw(gameTime);
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement