Guest User

Untitled

a guest
Jul 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  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. using Microsoft.Xna.Framework.Net;
  12. using Microsoft.Xna.Framework.Storage;
  13.  
  14. namespace Pool
  15. {
  16. /// <summary>
  17. /// Arcade Pool
  18. /// Graphics by Matt Pickering
  19. /// </summary>
  20. public class Game1 : Microsoft.Xna.Framework.Game
  21. {
  22. GraphicsDeviceManager graphics;
  23. SpriteBatch spriteBatch;
  24. Texture2D backgroundImage;
  25. Texture2D whiteBall;
  26. Vector2 whiteSpeed;
  27. Vector2 whitePosition;
  28. Rectangle rectangle;
  29. int backgroundNumber;
  30. bool backgroundChanged;
  31.  
  32. public Game1()
  33. {
  34. graphics = new GraphicsDeviceManager(this);
  35. Content.RootDirectory = "Content";
  36.  
  37. graphics.PreferredBackBufferHeight = 594;
  38. graphics.PreferredBackBufferWidth = 1030;
  39. }
  40.  
  41. /// <summary>
  42. /// Allows the game to perform any initialization it needs to before starting to run.
  43. /// This is where it can query for any required services and load any non-graphic
  44. /// related content. Calling base.Initialize will enumerate through any components
  45. /// and initialize them as well.
  46. /// </summary>
  47. protected override void Initialize()
  48. {
  49. // TODO: Add your initialization logic here
  50.  
  51. base.Initialize();
  52. }
  53.  
  54. /// <summary>
  55. /// LoadContent will be called once per game and is the place to load
  56. /// all of your content.
  57. /// </summary>
  58. protected override void LoadContent()
  59. {
  60. // Create a new SpriteBatch, which can be used to draw textures.
  61. spriteBatch = new SpriteBatch(GraphicsDevice);
  62.  
  63. backgroundImage = Content.Load<Texture2D>(@"Tables/table_blue");
  64. backgroundNumber = 0;
  65. backgroundChanged = false;
  66. whiteBall = Content.Load<Texture2D>(@"Balls/white1");
  67. whitePosition = new Vector2(200, 280);
  68. whiteSpeed = Vector2.Zero;
  69.  
  70. rectangle = new Rectangle(-35, -24, graphics.GraphicsDevice.Viewport.Width + 70, graphics.GraphicsDevice.Viewport.Height + 48);
  71. }
  72.  
  73. /// <summary>
  74. /// UnloadContent will be called once per game and is the place to unload
  75. /// all content.
  76. /// </summary>
  77. protected override void UnloadContent()
  78. {
  79. // TODO: Unload any non ContentManager content here
  80. }
  81.  
  82. /// <summary>
  83. /// Allows the game to run logic such as updating the world,
  84. /// checking for collisions, gathering input, and playing audio.
  85. /// </summary>
  86. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  87. protected override void Update(GameTime gameTime)
  88. {
  89. // Allows the game to exit
  90. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  91. this.Exit();
  92.  
  93. if (GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder == ButtonState.Released && backgroundChanged)
  94. backgroundChanged = false;
  95.  
  96. if (GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder == ButtonState.Pressed && !backgroundChanged)
  97. {
  98. TableColour();
  99. backgroundChanged = true;
  100. }
  101.  
  102. // Allow player to control the white ball
  103. if (whiteSpeed == Vector2.Zero)
  104. {
  105. GamePadState padState = GamePad.GetState(PlayerIndex.One);
  106.  
  107. if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
  108. {
  109. whiteSpeed.X = 8 * padState.ThumbSticks.Left.X;
  110. whiteSpeed.Y = 8 * -padState.ThumbSticks.Left.Y;
  111. }
  112. }
  113.  
  114. // Move the ball and friction
  115. whitePosition += whiteSpeed;
  116. if (whiteSpeed != Vector2.Zero)
  117. whiteSpeed *= new Vector2(0.99f, 0.99f);
  118.  
  119. // Stop the ball
  120. if (Math.Abs(whiteSpeed.X) < 0.26f && Math.Abs(whiteSpeed.Y) < 0.26f)
  121. whiteSpeed = Vector2.Zero;
  122.  
  123. // Check bouncing against cushion.
  124. if (whitePosition.X < 72 || whitePosition.X > Window.ClientBounds.Width - (whiteBall.Width + 72))
  125. whiteSpeed.X *= -1;
  126.  
  127. if (whitePosition.Y < 76 || whitePosition.Y > Window.ClientBounds.Height - (whiteBall.Height + 76))
  128. whiteSpeed.Y *= -1;
  129.  
  130. base.Update(gameTime);
  131. }
  132.  
  133. /// <summary>
  134. /// This is called when the game should draw itself.
  135. /// </summary>
  136. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  137. protected override void Draw(GameTime gameTime)
  138. {
  139. GraphicsDevice.Clear(Color.CornflowerBlue);
  140.  
  141. spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
  142.  
  143. spriteBatch.Draw(backgroundImage, rectangle, Color.White);
  144. spriteBatch.Draw(whiteBall, whitePosition, Color.White);
  145.  
  146. spriteBatch.End();
  147.  
  148. base.Draw(gameTime);
  149. }
  150.  
  151. public void TableColour()
  152. {
  153. switch (backgroundNumber)
  154. {
  155. case 0:
  156. backgroundImage = Content.Load<Texture2D>(@"Tables/table_green");
  157. backgroundNumber = 1;
  158. break;
  159. case 1:
  160. backgroundImage = Content.Load<Texture2D>(@"Tables/table_purple");
  161. backgroundNumber = 2;
  162. break;
  163. case 2:
  164. backgroundImage = Content.Load<Texture2D>(@"Tables/table_red");
  165. backgroundNumber = 3;
  166. break;
  167. case 3:
  168. backgroundImage = Content.Load<Texture2D>(@"Tables/table_yellow");
  169. backgroundNumber = 4;
  170. break;
  171. case 4: backgroundImage = Content.Load<Texture2D>(@"Tables/table_blue");
  172. backgroundNumber = 0;
  173. break;
  174. }
  175. }
  176. }
  177. }
Add Comment
Please, Sign In to add comment