Advertisement
adolciaaa

PingPong

Oct 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 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.  
  12. namespace WindowsGame4
  13. {
  14. class Paddle
  15. {
  16. public Paddle(int width, int height)
  17. {
  18. position = new Vector2(0, 0);
  19. rect = new Rectangle(0, 0, width, height);
  20. }
  21.  
  22. public void Update()
  23. {
  24. rect.X = (int)position.X;
  25. rect.Y = (int)position.Y;
  26. }
  27.  
  28. public void Draw(SpriteBatch spritebatch, Texture2D _texture)
  29. {
  30. spritebatch.Draw(_texture, position, Color.White);
  31. }
  32.  
  33. public Vector2 position;
  34. public Rectangle rect;
  35. public const int speed = 400;
  36. public int point;
  37. public bool looser;
  38. }
  39.  
  40. class Ball
  41. {
  42. public Ball(int width, int height)
  43. {
  44. position = new Vector2(400, 285);
  45. rect = new Rectangle(0, 0, width, height);
  46. angle = 20;
  47. speed = 300;
  48. }
  49.  
  50. public void Update()
  51. {
  52. rect.X = (int)position.X;
  53. rect.Y = (int)position.Y;
  54. }
  55.  
  56. public void Draw(SpriteBatch spriteBatch, Texture2D _texture)
  57. {
  58. spriteBatch.Draw(_texture, position, Color.White);
  59. }
  60.  
  61. public Vector2 position;
  62. public Rectangle rect;
  63. public float angle;
  64. public int speed;
  65. public const int maxSpeed = 1000;
  66. }
  67.  
  68. public class Game1 : Microsoft.Xna.Framework.Game
  69. {
  70. GraphicsDeviceManager graphics;
  71. SpriteBatch spriteBatch;
  72. SpriteFont spriteFont;
  73.  
  74. Texture2D background, redPaddle, bluePaddle, ballText;
  75. Paddle red, blue;
  76. Ball ball;
  77.  
  78. double deltaTime;
  79. bool isPlaying;
  80.  
  81. public Game1()
  82. {
  83. graphics = new GraphicsDeviceManager(this);
  84. graphics.PreferredBackBufferWidth = 800;
  85. graphics.PreferredBackBufferHeight = 600;
  86. red = new Paddle(25, 100);
  87. blue = new Paddle(25, 100);
  88. blue.position.X = 775;
  89. blue.position.Y = red.position.Y = 250;
  90. ball = new Ball(20, 20);
  91. isPlaying = red.looser = blue.looser = false;
  92. red.point = blue.point = 0;
  93. Content.RootDirectory = "Content";
  94. }
  95.  
  96. protected override void Initialize()
  97. {
  98. base.Initialize();
  99. }
  100.  
  101. protected override void LoadContent()
  102. {
  103. spriteBatch = new SpriteBatch(GraphicsDevice);
  104. background = Content.Load<Texture2D>("Background2");
  105. redPaddle = Content.Load<Texture2D>("RedPaddle");
  106. bluePaddle = Content.Load<Texture2D>("BluePaddle");
  107. ballText = Content.Load<Texture2D>("Ball");
  108. spriteFont = this.Content.Load<SpriteFont>("Arial");
  109. }
  110.  
  111. protected override void UnloadContent()
  112. { }
  113.  
  114. protected override void Update(GameTime gameTime)
  115. {
  116. deltaTime = (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
  117. if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit();
  118.  
  119. if (Keyboard.GetState().IsKeyDown(Keys.Space))
  120. {
  121. if (!isPlaying)
  122. isPlaying = true;
  123. }
  124.  
  125. if (isPlaying)
  126. {
  127. if (Keyboard.GetState().IsKeyDown(Keys.Q))
  128. {
  129. if (red.position.Y > 5)
  130. red.position.Y += (float)(-Paddle.speed * deltaTime);
  131. }
  132. if (Keyboard.GetState().IsKeyDown(Keys.P))
  133. {
  134. if (blue.position.Y > 5)
  135. blue.position.Y += (float)(-Paddle.speed * deltaTime);
  136. }
  137. if (Keyboard.GetState().IsKeyDown(Keys.A))
  138. {
  139. if (red.position.Y < 600 - red.rect.Height - 5)
  140. red.position.Y += (float)(Paddle.speed * deltaTime);
  141. }
  142. if (Keyboard.GetState().IsKeyDown(Keys.L))
  143. {
  144. if (blue.position.Y < 600 - blue.rect.Height - 5)
  145. blue.position.Y += (float)(Paddle.speed * deltaTime);
  146. }
  147. }
  148.  
  149. // Ball move
  150. ball.position.X -= (float)(Math.Cos(ball.angle) * (ball.speed * deltaTime));
  151. ball.position.Y += (float)(Math.Sin(ball.angle) * (ball.speed * deltaTime));
  152.  
  153. if (ball.position.Y < 0)
  154. {
  155. ball.angle = -ball.angle;
  156. ball.position.Y = 0.1f;
  157. }
  158. else if (ball.position.Y + ball.rect.Height > 600)
  159. {
  160. ball.angle = -ball.angle;
  161. ball.position.Y = (600 - ball.rect.Width - 0.1f);
  162. }
  163. if (ball.position.X < 0 && (isPlaying == true))
  164. {
  165. blue.point++;
  166. red.looser = true;
  167. Reset();
  168. }
  169. if (ball.position.X > 775 && (isPlaying == true))
  170. {
  171. red.point++;
  172. blue.looser = true;
  173. Reset();
  174. }
  175.  
  176. red.Update();
  177. blue.Update();
  178. ball.Update();
  179.  
  180. if (red.rect.Intersects(ball.rect))
  181. {
  182. ball.angle = MathHelper.Pi - ball.angle;
  183. ball.position.X = (red.position.X + red.rect.Width + 0.1f);
  184. ball.speed += 10;
  185. }
  186. else if (blue.rect.Intersects(ball.rect))
  187. {
  188. ball.angle = MathHelper.Pi - ball.angle;
  189. ball.position.X = (blue.position.X - ball.rect.Width - 0.1f);
  190. ball.speed += 10;
  191. }
  192.  
  193. base.Update(gameTime);
  194. }
  195.  
  196. protected override void Draw(GameTime gameTime)
  197. {
  198. spriteBatch.Begin();
  199. spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
  200.  
  201. spriteBatch.DrawString(spriteFont, "Czerwony: " + red.point, new Vector2(50, 10), Color.White);
  202. spriteBatch.DrawString(spriteFont, "Niebieski: " + blue.point, new Vector2(650, 10), Color.White);
  203.  
  204. red.Draw(spriteBatch, redPaddle);
  205. blue.Draw(spriteBatch, bluePaddle);
  206.  
  207. if(isPlaying == true)
  208. {
  209. ball.Draw(spriteBatch, ballText);
  210.  
  211. if ((ball.position.X < 0) || (ball.position.X + ball.rect.Width > 800))
  212. isPlaying = false;
  213. }
  214.  
  215. spriteBatch.End();
  216. base.Draw(gameTime);
  217. }
  218.  
  219. public void Reset()
  220. {
  221. blue.position.X = 775;
  222. blue.position.Y = red.position.Y = 250;
  223.  
  224. if (red.looser == true)
  225. ball.position.X += (float)(Math.Cos(ball.angle) * (ball.speed * deltaTime));
  226. else if (blue.looser == true)
  227. ball.position.X -= (float)(Math.Cos(ball.angle) * (ball.speed * deltaTime));
  228.  
  229. ball.position.Y = 250;
  230. ball.angle = 10;
  231. ball.speed = 300;
  232.  
  233. isPlaying = false;
  234. }
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement