PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/PongXNA/PongXNA/Game1.cs

http://firstpong.codeplex.com
C# | 261 lines | 173 code | 38 blank | 50 comment | 26 complexity | 6deb04e65051624c9425129764af159c MD5 | raw file
  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. namespace PongXNA
  12. {
  13. /// <summary>
  14. /// This is the main type for your game
  15. /// </summary>
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. SpriteFont font;
  21. /// <summary>
  22. /// TODO Decleration of variable used in this program
  23. /// </summary>
  24. ///
  25. private Rectangle FirstRacketHitBox { get; set; }
  26. private Rectangle SecondRacketHitBox { get; set; }
  27. private Rectangle BallHitBox { get; set; }
  28. private double currentPosXFirstRacket;
  29. private double currentPosYFirstRacket;
  30. private double currentPosXSecondRacket;
  31. private double currentPosYSecondRacket;
  32. private float currentPosXBall;
  33. private float currentPosYBall;
  34. private Vector2 vectorBall;
  35. private Scene scene = new Scene();
  36. private int leftScore;
  37. private int rightScore;
  38. private string score;
  39. private int vitesse = 6;
  40. public Game1()
  41. {
  42. graphics = new GraphicsDeviceManager(this);
  43. Content.RootDirectory = "Content";
  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. this.IsMouseVisible = true; // Allow the mouse to be visible
  55. /// This the declaration of the position of the first racket we create a hit box for testing in the future if the ball touching them
  56. /// <FirstRacket>
  57. currentPosXFirstRacket = 0;
  58. currentPosYFirstRacket = GraphicsDevice.Viewport.Height / 2 - 39;
  59. FirstRacketHitBox = new Rectangle((int)currentPosXFirstRacket, (int)currentPosYFirstRacket, 9 * 2 + 8 * 2, 78 * 2);
  60. /// </FirstRacket>
  61. ///
  62. /// This the declaration of the position of the second racket we create a hit box for testing in the future if the ball touching them
  63. /// <SecondRacket>
  64. currentPosXSecondRacket = GraphicsDevice.Viewport.Width - 9 * 2 - 8 * 2;
  65. currentPosYSecondRacket = GraphicsDevice.Viewport.Height / 2 - 39;
  66. SecondRacketHitBox = new Rectangle((int)currentPosXSecondRacket, (int)currentPosYSecondRacket, 9 * 2 + 8 * 2, 78 * 2);
  67. /// </SecondRacket>
  68. ///
  69. /// This the declaration of the position of the ball and we create a hit box
  70. /// <Ball>
  71. currentPosXBall = GraphicsDevice.Viewport.Width /2 -4;
  72. currentPosYBall= GraphicsDevice.Viewport.Height / 2 -4;
  73. BallHitBox = new Rectangle((int)currentPosXBall, (int)currentPosYBall, 8 * 2, 8 * 2);
  74. vectorBall.X = -1; vectorBall.Y = -1;
  75. /// </SecondRacket>
  76. scene.PosXLeftSide = -50;
  77. scene.PosYLeftSide = -50;
  78. scene.LeftSideHitBox = new Rectangle((int)scene.PosXLeftSide, (int)scene.PosYLeftSide, 8 * 2 + 50, GraphicsDevice.Viewport.Height + 100);
  79. scene.PosXRightSide = GraphicsDevice.Viewport.Width - 8 * 2;
  80. scene.PosYRightSide = 0;
  81. scene.RightSideHitBox = new Rectangle((int)scene.PosXRightSide, (int)scene.PosYRightSide, 8 * 2 + 50, GraphicsDevice.Viewport.Height + 100);
  82. scene.PosXUpSide = -50;
  83. scene.PosYUpSide = -50;
  84. scene.UpSideHitBox = new Rectangle((int)scene.PosXUpSide, (int)scene.PosYUpSide, GraphicsDevice.Viewport.Width + 100, 8 * 2 + 50);
  85. scene.PosXDownSide = -50;
  86. scene.PosYDownSide = GraphicsDevice.Viewport.Height - 8 * 2;
  87. scene.DownSideHitBox = new Rectangle((int)scene.PosXDownSide, (int)scene.PosYDownSide, GraphicsDevice.Viewport.Width + 100, 8 * 2 + 50);
  88. leftScore = 0;
  89. rightScore = 0;
  90. score = "Score : " + leftScore + " - " + rightScore;
  91. base.Initialize();
  92. }
  93. /// <summary>
  94. /// LoadContent will be called once per game and is the place to load
  95. /// all of your content.
  96. /// </summary>
  97. private Texture2D FirstRacket;
  98. private Texture2D SecondRacket;
  99. private Texture2D Ball;
  100. protected override void LoadContent()
  101. {
  102. // Create a new SpriteBatch, which can be used to draw textures.
  103. spriteBatch = new SpriteBatch(GraphicsDevice);
  104. font = Content.Load<SpriteFont>("Times New Roman");
  105. // TODO: use this.Content to load your game content here
  106. FirstRacket = Content.Load<Texture2D>("FirstRacket");
  107. SecondRacket = Content.Load<Texture2D>("SecondRacket");
  108. Ball = Content.Load<Texture2D>("Ball");
  109. }
  110. /// <summary>
  111. /// UnloadContent will be called once per game and is the place to unload
  112. /// all content.
  113. /// </summary>
  114. protected override void UnloadContent()
  115. {
  116. // TODO: Unload any non ContentManager content here
  117. }
  118. /// <summary>
  119. /// Allows the game to run logic such as updating the world,
  120. /// checking for collisions, gathering input, and playing audio.
  121. /// </summary>
  122. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  123. protected override void Update(GameTime gameTime)
  124. {
  125. // Allows the game to exit
  126. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  127. this.Exit();
  128. // TODO: Add your update logic here
  129. MouseState mouseState = Mouse.GetState(); // We just want to have the current position of mouse
  130. KeyboardState UpOrDown = Keyboard.GetState();
  131. if ((mouseState.Y - 39 * 2) > 0 && (mouseState.Y + 39 * 2) < GraphicsDevice.Viewport.Height)//we want to know if the mouse is in the window
  132. {
  133. currentPosYFirstRacket = mouseState.Y - 39 * 2;// to put the middle of the racket in front a the mouse
  134. }
  135. if (UpOrDown.IsKeyDown(Keys.Down) && currentPosYSecondRacket + 78 * 2 < GraphicsDevice.Viewport.Height)//to control the second racket manually
  136. {
  137. currentPosYSecondRacket = currentPosYSecondRacket + 4;
  138. }
  139. else if (UpOrDown.IsKeyDown(Keys.Up) && currentPosYSecondRacket > 0)
  140. {
  141. currentPosYSecondRacket = currentPosYSecondRacket - 4;
  142. }
  143. else if (currentPosYSecondRacket > 0 && vectorBall.X > 0 && currentPosYSecondRacket + 78 * 2 < GraphicsDevice.Viewport.Height)// to control the second racket with an AI
  144. {
  145. currentPosYSecondRacket = currentPosYSecondRacket + 8 * (currentPosYBall + 4 * 2 - currentPosYSecondRacket - 78) / Math.Abs(0.1 + (currentPosYBall + 4 * 2 - currentPosYSecondRacket - 78));
  146. }
  147. else
  148. { }
  149. if (currentPosYSecondRacket < 0)
  150. {
  151. currentPosYSecondRacket = currentPosYSecondRacket + 3;
  152. }
  153. else if (currentPosYSecondRacket >= GraphicsDevice.Viewport.Height)
  154. {
  155. currentPosYSecondRacket = currentPosYSecondRacket - 3;
  156. }
  157. else
  158. { }
  159. if (currentPosXBall < 0)
  160. {
  161. //point a droite
  162. currentPosXBall = GraphicsDevice.Viewport.Width / 2 - 4;
  163. currentPosYBall = GraphicsDevice.Viewport.Height / 2 - 4;
  164. rightScore++;
  165. vectorBall.X = -1;
  166. vectorBall.Y = 0;
  167. vitesse = 6;
  168. }
  169. else if (currentPosXBall > GraphicsDevice.Viewport.Width)
  170. {
  171. //point a gauche
  172. currentPosXBall = GraphicsDevice.Viewport.Width / 2 - 4;
  173. currentPosYBall = GraphicsDevice.Viewport.Height / 2 - 4;
  174. leftScore++;
  175. vectorBall.X = 1;
  176. vectorBall.Y = 0;
  177. vitesse = 6;
  178. }
  179. else if (currentPosYBall < 0 || currentPosYBall + 8 * 2 > GraphicsDevice.Viewport.Height)
  180. {
  181. vectorBall.Y *= -1;
  182. }
  183. else if (FirstRacketHitBox.Contains(BallHitBox))
  184. {
  185. vectorBall.X *= -1;
  186. vectorBall.Y += ((float)currentPosYBall + 8 - (float)currentPosYFirstRacket - 78) / 14;
  187. vitesse++;
  188. }
  189. else if (SecondRacketHitBox.Contains(BallHitBox))
  190. {
  191. vectorBall.X *= -1;
  192. vectorBall.Y += ((float)currentPosYBall + 8 - (float)currentPosYSecondRacket - 78) / 14;
  193. vitesse++;
  194. }
  195. else
  196. { }
  197. vectorBall.Normalize();
  198. currentPosXBall=currentPosXBall + vitesse * vectorBall.X;
  199. currentPosYBall=currentPosYBall + vitesse * vectorBall.Y;
  200. FirstRacketHitBox = new Rectangle((int)currentPosXFirstRacket, (int)currentPosYFirstRacket, 9*2 + 8 * 2, 78*2); // We just put the new position of mouse
  201. SecondRacketHitBox = new Rectangle((int)currentPosXSecondRacket, (int)currentPosYSecondRacket, 9 * 2 + 8 * 2, 78 * 2);//We do the same for a test
  202. BallHitBox = new Rectangle((int)currentPosXBall, (int)currentPosYBall, 8 * 2, 8 * 2);
  203. base.Update(gameTime);
  204. }
  205. /// <summary>
  206. /// This is called when the game should draw itself.
  207. /// </summary>
  208. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  209. protected override void Draw(GameTime gameTime)
  210. {
  211. GraphicsDevice.Clear(Color.CornflowerBlue);
  212. // TODO: Add your drawing code here
  213. graphics.GraphicsDevice.Clear(Color.White);
  214. spriteBatch.Begin();
  215. spriteBatch.Draw(FirstRacket, FirstRacketHitBox, Color.White);
  216. spriteBatch.Draw(SecondRacket, SecondRacketHitBox, Color.White);
  217. spriteBatch.Draw(Ball,BallHitBox,Color.White);
  218. spriteBatch.DrawString(font, score, new Vector2(GraphicsDevice.Viewport.Width / 2 - font.MeasureString(score).X /2, 10), Color.Black);
  219. spriteBatch.End();
  220. base.Draw(gameTime);
  221. }
  222. }
  223. }