Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.77 KB | None | 0 0
  1. #region Using Statements
  2. using System;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Storage;
  6. using Microsoft.Xna.Framework.Input;
  7.  
  8. #endregion
  9. namespace VipersPong
  10. {
  11. /// <summary>
  12. /// This is the main type for your game
  13. /// </summary>
  14. public class Game1 : Game
  15. {
  16. KeyboardState MovingPaddles;
  17. GraphicsDeviceManager graphics;
  18. SpriteBatch spriteBatch;
  19. Texture2D backgroundTexture;
  20. int backgroundXPos;
  21. int backgroundYPos;
  22. int backgroundWidth;
  23. int backgroundHeight;
  24.  
  25. Texture2D paddleTexture;
  26. int paddleXPos;
  27. int paddleYPos;
  28. int paddleWidth;
  29. int paddleHeight;
  30.  
  31. Texture2D ballTexture;
  32. int ballXPos;
  33. int ballYPos;
  34. int ballWidth;
  35. int ballHeight;
  36.  
  37. Texture2D paddle2Texture;
  38. int paddle2XPos;
  39. int paddle2YPos;
  40. int paddle2Width;
  41. int paddle2Height;
  42.  
  43. Texture2D menuTexture;
  44. int menuTextureXPos;
  45. int menuTextureYPos;
  46. int menuTextureWidth;
  47. int menuTextureHeight;
  48.  
  49. enum EGameStates
  50. {
  51. MENU,
  52. GAME
  53. }
  54.  
  55.  
  56. public Game1 ()
  57. {
  58. graphics = new GraphicsDeviceManager (this);
  59. Content.RootDirectory = "Content";
  60. graphics.IsFullScreen = true;
  61. }
  62.  
  63. /// <summary>
  64. /// Allows the game to perform any initialization it needs to before starting to run.
  65. /// This is where it can query for any required services and load any non-graphic
  66. /// related content. Calling base.Initialize will enumerate through any components
  67. /// and initialize them as well.
  68. /// </summary>
  69. protected override void Initialize ()
  70. {
  71. // TODO: Add your initialization logic here
  72. base.Initialize ();
  73.  
  74. Vector2 backgroundTexture_pos = new Vector2(0, 0);
  75.  
  76. backgroundWidth = 512;
  77. backgroundHeight = 512;
  78.  
  79. Vector2 paddleTexture_pos = new Vector2(100, 0);
  80.  
  81. paddleWidth = 64;
  82. paddleHeight = 64;
  83.  
  84. Vector2 ballTexture_pos = new Vector2(256, 256);
  85. Vector2 ball_vel= new Vector2( 3, 7);
  86.  
  87. ballWidth = 16;
  88. ballHeight = 16;
  89.  
  90. Vector2 paddle2Texture_pos = new Vector2 (100, 512);
  91.  
  92. paddle2Width = 16;
  93. paddle2Height = 16;
  94.  
  95. Vector2 menuTexture_pos = new Vector2 (0, 0);
  96.  
  97. menuTextureWidth = 512;
  98. menuTextureHeight = 512;
  99.  
  100.  
  101.  
  102. }
  103.  
  104. /// <summary>
  105. /// LoadContent will be called once per game and is the place to load
  106. /// all of your content.
  107. /// </summary>
  108. protected override void LoadContent ()
  109. {
  110. // Create a new SpriteBatch, which can be used to draw textures.
  111. spriteBatch = new SpriteBatch (GraphicsDevice);
  112.  
  113. //TODO: use this.Content to load your game content here
  114. backgroundTexture = Content.Load<Texture2D>("background");
  115. paddleTexture = Content.Load<Texture2D> ("paddle");
  116. ballTexture = Content.Load<Texture2D> ("ball");
  117. paddle2Texture = Content.Load<Texture2D> ("paddle2");
  118. menuTexture = Content.Load<Texture2D> ("menuTexture");
  119. }
  120.  
  121. /// <summary>
  122. /// Allows the game to run logic such as updating the world,
  123. /// checking for collisions, gathering input, and playing audio.
  124. /// </summary>
  125. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  126. protected override void Update (GameTime gameTime)
  127. {
  128. // For Mobile devices, this logic will close the Game when the Back button is pressed
  129. if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
  130. Exit ();
  131. }
  132. // TODO: Add your update logic here
  133. base.Update (gameTime);
  134.  
  135.  
  136. EGameStates gameState = EGameStates.MENU;
  137.  
  138. switch (gameState)
  139. {
  140. case EGameStates.GAME: UpdateGame (gameTime); break;
  141. case EGameStates.MENU: UpdateMenu (gameTime); break;
  142. default:Console.WriteLine ("An Error Has Occured."); break;
  143. }
  144. }
  145. void UpdateGame(GameTime gameTime)
  146.  
  147. {
  148. if (paddleXPos > graphics.PreferredBackBufferWidth)
  149. {
  150. paddleXPos = 0;
  151. }
  152.  
  153. if (paddleYPos > graphics.PreferredBackBufferHeight)
  154. {
  155. paddleYPos = 0;
  156. }
  157.  
  158. if (paddle2XPos > graphics.PreferredBackBufferWidth)
  159. {
  160. paddle2XPos = 0;
  161. }
  162.  
  163. if (paddle2YPos > graphics.PreferredBackBufferHeight)
  164. {
  165. paddle2YPos = 0;
  166. }
  167.  
  168. if (ballXPos > graphics.PreferredBackBufferWidth)
  169. {
  170. Vector2 ball_vel = new Vector2( 7, 3);
  171. }
  172.  
  173. if (ballYPos > graphics.PreferredBackBufferHeight)
  174. {
  175. ballYPos = 0;
  176. }
  177.  
  178. if(state.IsKeyDown(Keys.Left))
  179. {
  180. paddleXPos -= 5;
  181. }
  182.  
  183. if (state.IsKeyDown (Keys.Right))
  184. {
  185. paddeXPos += 5;
  186. }
  187.  
  188. if (state.IsKeyDown (Keys.A))
  189. {
  190. padde2XPos -= 5;
  191. }
  192.  
  193. if (state.IsKeyDown (Keys.D))
  194. {
  195. padde2XPos += 5;
  196. }
  197.  
  198. if (Rectangle.Intersect(paddleTextre, ballTexure) != Rectangle.Empty )
  199. {
  200. Vector2 ball_vel = new Vector2( 7, 3);
  201. }
  202. if (Rectangle.Intersect(paddle2Texture, ballTexture) != Rectangle.Empty )
  203. {
  204. Vector2 ball_vel = new Vector2( 7, 3);
  205. }
  206. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  207. {
  208. EGameStates gameState = EGameStates.MENU;
  209. }
  210. }
  211.  
  212.  
  213. void UpdateMenu(GameTime gameTime)
  214. {
  215. if (Keyboard.GetState().IsKeyDown(Keys.Space))
  216. {
  217. EGameStates gameState = EGameStates.GAME;
  218. }
  219. }
  220. void DrawGame(GameTime gameTime)
  221. {
  222. //Draw Game Resources Here
  223. spriteBatch.Begin();
  224. spriteBatch.Draw(backgroundTexture,
  225. new Rectangle(backgroundXPos, backgroundYPos, backgroundWidth, backgroundHeight),
  226. Color.White);
  227. spriteBatch.End();
  228.  
  229. spriteBatch.Begin();
  230. spriteBatch.Draw(paddleTexture,
  231. new Rectangle(paddleXPos, paddleYPos, paddleWidth, paddleHeight),
  232. Color.White);
  233. spriteBatch.End();
  234.  
  235. spriteBatch.Begin();
  236. spriteBatch.Draw(ballTexture,
  237. new Rectangle(ballXPos, ballYPos, ballWidth, ballHeight),
  238. Color.White);
  239. spriteBatch.End();
  240.  
  241. spriteBatch.Begin();
  242. spriteBatch.Draw(paddle2Texture,
  243. new Rectangle(paddle2XPos, paddle2YPos, paddle2Width, paddle2Height),
  244. Color.White);
  245. spriteBatch.End();
  246.  
  247. }
  248. void DrawMenu(GameTime gameTime)
  249. {
  250. spriteBatch.Begin();
  251. spriteBatch.Draw(menuTexture,
  252. new Rectangle(menuTextureXPos, menuTextureYPos, menuTextureWidth, menuTextureHeight),
  253. Color.White);
  254. spriteBatch.End();
  255. }
  256.  
  257.  
  258.  
  259. /// <summary>
  260. /// This is called when the game should draw itself.
  261. /// </summary>
  262. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  263. protected override void Draw (GameTime gameTime)
  264. {
  265. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  266.  
  267. //TODO: Add your drawing code here
  268.  
  269. switch (gameState)
  270. {
  271. case EGameStates.GAME: DrawGame(); break;
  272. case EGameStates.MENU: DrawMenu(); break;
  273. default: Console.WriteLine("An Error Has Occured."); break;
  274. }
  275.  
  276. base.Draw (gameTime);
  277. }
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement