Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.01 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 WindowsGame2
  15. {
  16.  
  17. /// <summary>
  18. /// This is the main type for your game
  19. /// </summary>
  20. public class Game1 : Microsoft.Xna.Framework.Game
  21. {
  22. GraphicsDeviceManager graphics;
  23. SpriteBatch spriteBatch;
  24.  
  25. Ball ball1;
  26. Ball ball2;
  27. bool colide = false;
  28.  
  29. Texture2D ballTexture;
  30. Rectangle ballRectangle;
  31. Texture2D batText;
  32. Rectangle player1Bat;
  33. Rectangle player2Bat;
  34. float timer = 0;
  35. float interval = 1000.0f;
  36. int ballXspeed;
  37. int ballYspeed;
  38. int ball2Xspeed;
  39. int ball2Yspeed;
  40.  
  41. int batspeed;
  42.  
  43. int score1;
  44. int score2;
  45.  
  46. SpriteFont scorefont;
  47.  
  48.  
  49. KeyboardState keyboardstate;
  50. KeyboardState prekeystate;
  51.  
  52. public Game1()
  53. {
  54. graphics = new GraphicsDeviceManager(this);
  55. Content.RootDirectory = "Content";
  56. }
  57.  
  58. /// <summary>
  59. /// Allows the game to perform any initialization it needs to before starting to run.
  60. /// This is where it can query for any required services and load any non-graphic
  61. /// related content. Calling base.Initialize will enumerate through any components
  62. /// and initialize them as well.
  63. /// </summary>
  64. ///
  65.  
  66. private Rectangle GSREC(Texture2D texture, float widthFactor)
  67. {
  68. float aspectRatio = (float)texture.Width / texture.Height;
  69. float width = GraphicsDevice.Viewport.Width / widthFactor;
  70. float height = width / aspectRatio;
  71.  
  72. Rectangle result = new Rectangle(
  73. 0,
  74. 0,
  75. (int)width,
  76. (int)height);
  77.  
  78. return result;
  79. }
  80. protected override void Initialize()
  81. {
  82.  
  83.  
  84. base.Initialize();
  85. }
  86.  
  87. /// <summary>
  88. /// LoadContent will be called once per game and is the place to load
  89. /// all of your content.
  90. /// </summary>
  91. protected override void LoadContent()
  92. {
  93. // Create a new SpriteBatch, which can be used to draw textures.
  94. spriteBatch = new SpriteBatch(GraphicsDevice);
  95.  
  96.  
  97. ballTexture = Content.Load<Texture2D>(@"Images\ball");
  98.  
  99. ball1 = new Ball(
  100. GSREC(ballTexture, 20),
  101. ballTexture,
  102. GraphicsDevice.Viewport.Width / 120,
  103. GraphicsDevice.Viewport.Width / 120,
  104. Color.White);
  105.  
  106. ball2 = new Ball(
  107. GSREC(ballTexture, 20),
  108. ballTexture,
  109. GraphicsDevice.Viewport.Width / 120,
  110. -GraphicsDevice.Viewport.Width / 120,
  111. Color.White);
  112.  
  113. ball1.Rectangle = CenterBall(ball1);
  114. ball2.Rectangle = CenterBall(ball2);
  115.  
  116.  
  117. batText = Content.Load<Texture2D>(@"Images\bat");
  118.  
  119. player1Bat = GSREC(batText, 25);
  120.  
  121. player2Bat = player1Bat;
  122.  
  123.  
  124. player1Bat.X = player1Bat.Width;
  125. player1Bat.Y = 300;
  126.  
  127. player2Bat.X = GraphicsDevice.Viewport.Width - (2 * player2Bat.Width);
  128. player2Bat.Y = 100;
  129.  
  130. batspeed = GraphicsDevice.Viewport.Height / 100;
  131. }
  132.  
  133. protected Rectangle CenterBall(Ball ball)
  134. {
  135. return new Rectangle(
  136. (GraphicsDevice.Viewport.Width / 2) - (ball1.Rectangle.Width / 2),
  137. (GraphicsDevice.Viewport.Height / 2) - (ball1.Rectangle.Height / 2),
  138. ball1.Rectangle.Width,
  139. ball1.Rectangle.Height);
  140.  
  141. }
  142.  
  143.  
  144. /// <summary>
  145. /// UnloadContent will be called once per game and is the place to unload
  146. /// all content.
  147. /// </summary>
  148. protected override void UnloadContent()
  149. {
  150.  
  151. }
  152.  
  153. /// <summary>
  154. /// Allows the game to run logic such as updating the world,
  155. /// checking for collisions, gathering input, and playing audio.
  156. /// </summary>
  157. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  158. protected override void Update(GameTime gameTime)
  159. {
  160. prekeystate = keyboardstate;
  161. keyboardstate = Keyboard.GetState();
  162.  
  163.  
  164. if (ball2.ballstate == Ballstate.stopped)
  165. {
  166. ball2.timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
  167.  
  168. if (ball2.timer > interval)
  169. {
  170. ball2.ballstate = Ballstate.going;
  171.  
  172. ball2.timer = 0f;
  173. }
  174. }
  175.  
  176. if (ball1.ballstate == Ballstate.stopped)
  177. {
  178. ball1.timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
  179.  
  180. if (ball1.timer > interval)
  181. {
  182. ball1.ballstate = Ballstate.going;
  183.  
  184. ball1.timer = 0f;
  185. }
  186. }
  187.  
  188.  
  189.  
  190.  
  191. // Allows the game to exit
  192. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  193. this.Exit();
  194.  
  195.  
  196.  
  197.  
  198. if (ball1.ballstate == Ballstate.going)
  199. {
  200. ball1.Rectangle.X = ball1.Rectangle.X + ball1.XSpeed;
  201. }
  202.  
  203. if (((ball1.Rectangle.X + ball1.Rectangle.Width) > GraphicsDevice.Viewport.Width) || (ball1.Rectangle.X < 0))
  204. {
  205. ball1.Rectangle = CenterBall(ball1);
  206. ball1.ballstate = Ballstate.stopped;
  207. }
  208.  
  209. if (ball1.ballstate == Ballstate.going)
  210. {
  211. ball1.Rectangle.Y = ball1.Rectangle.Y + ball1.YSpeed;
  212. }
  213. if (((ball1.Rectangle.Y + ball1.Rectangle.Height) > GraphicsDevice.Viewport.Height) || (ball1.Rectangle.Y < 0))
  214. {
  215. ball1.YSpeed = ball1.YSpeed * -1;
  216. }
  217. if (ball2.ballstate == Ballstate.going)
  218. {
  219. ball2.Rectangle.X = ball2.Rectangle.X + ball2.XSpeed;
  220. }
  221. if (((ball2.Rectangle.X + ball2.Rectangle.Width) > GraphicsDevice.Viewport.Width) || (ball2.Rectangle.X < 0))
  222. {
  223. ball2.Rectangle = CenterBall(ball2);
  224. ball2.ballstate = Ballstate.stopped;
  225. }
  226.  
  227. if (ball2.ballstate == Ballstate.going)
  228. {
  229. ball2.Rectangle.Y = ball2.Rectangle.Y + ball2.YSpeed;
  230. }
  231. if (((ball2.Rectangle.Y + ball2.Rectangle.Height) > GraphicsDevice.Viewport.Height) || (ball2.Rectangle.Y < 0))
  232. {
  233. ball2.YSpeed = ball2.YSpeed * -1;
  234. }
  235.  
  236. if (player1Bat.Intersects(ball1.Rectangle) ||
  237. player2Bat.Intersects(ball1.Rectangle))
  238. {
  239. ball1.XSpeed = ball1.XSpeed * -1;
  240. }
  241. if (player1Bat.Intersects(ball2.Rectangle) ||
  242. player2Bat.Intersects(ball2.Rectangle))
  243. {
  244. ball2.XSpeed = ball2.XSpeed * -1;
  245. }
  246.  
  247. if (((ball1.Rectangle.Y + ball1.Rectangle.Height > ball2.Rectangle.Y) &&
  248. (ball1.Rectangle.Y < ball2.Rectangle.Y + ball2.Rectangle.Height)) &&
  249. ((ball1.Rectangle.X + ball1.Rectangle.Width > ball2.Rectangle.X) &&
  250. (ball1.Rectangle.X < ball2.Rectangle.X + ball2.Rectangle.Width)) &&
  251. !colide)
  252. {
  253. colide = true;
  254. ball1.YSpeed = ball1.YSpeed * -1;
  255. ball2.XSpeed = ball2.XSpeed * -1;
  256. }
  257. else
  258. {
  259. colide = false;
  260. }
  261. //
  262. /*
  263. GamePadState gamepad1 = GamePad.GetState(PlayerIndex.One);
  264.  
  265. if (gamepad1.DPad.Up == ButtonState.Pressed)
  266. {
  267. player1Bat.Y = player1Bat.Y - batspeed;
  268. }
  269.  
  270. if (gamepad1.DPad.Up == ButtonState.Pressed)
  271. {
  272. player1Bat.Y = player1Bat.Y + batspeed;
  273. }
  274.  
  275. GamePadState gamepad2 = GamePad.GetState (PlayerIndex.Two);
  276.  
  277. if (gamepad2.DPad.Up == ButtonState.Pressed)
  278. {
  279. player1Bat.Y = player1Bat.Y - batspeed;
  280. }
  281.  
  282. if (gamepad2.DPad.Up == ButtonState.Pressed)
  283. {
  284. player1Bat.Y = player1Bat.Y + batspeed;
  285. }
  286. */
  287.  
  288.  
  289. if (keyboardstate.IsKeyDown(Keys.W))
  290. {
  291. if (player1Bat.Y > 0)
  292. {
  293. player1Bat.Y = player1Bat.Y - batspeed;
  294. }
  295. }
  296.  
  297. if (keyboardstate.IsKeyDown(Keys.S))
  298. {
  299. if (player1Bat.Y + player1Bat.Height < GraphicsDevice.Viewport.Height)
  300. {
  301. player1Bat.Y = player1Bat.Y + batspeed;
  302. }
  303. }
  304.  
  305. if (keyboardstate.IsKeyDown(Keys.Up))
  306. {
  307. if (player2Bat.Y > 0)
  308. {
  309. player2Bat.Y = player2Bat.Y - batspeed;
  310. }
  311. }
  312.  
  313. if (keyboardstate.IsKeyDown(Keys.Down))
  314. {
  315. if (player2Bat.Y + player2Bat.Height < GraphicsDevice.Viewport.Height)
  316. {
  317. player2Bat.Y = player2Bat.Y + batspeed;
  318. }
  319. }
  320.  
  321. if (player1Bat.Intersects(ball1.Rectangle) ||
  322. player2Bat.Intersects(ball1.Rectangle))
  323. {
  324. ballXspeed = ballXspeed * -1;
  325. }
  326.  
  327.  
  328.  
  329.  
  330. base.Update(gameTime);
  331. }
  332.  
  333. /// <summary>
  334. /// This is called when the game should draw itself.
  335. /// </summary>
  336. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  337. protected override void Draw(GameTime gameTime)
  338. {
  339. GraphicsDevice.Clear(Color.CornflowerBlue);
  340.  
  341. scorefont = Content.Load<SpriteFont>(@"Score1");
  342.  
  343.  
  344.  
  345. spriteBatch.Begin();
  346.  
  347. spriteBatch.Draw(
  348. batText,
  349. player1Bat,
  350. Color.YellowGreen);
  351.  
  352. spriteBatch.Draw(
  353. batText,
  354. player2Bat,
  355. Color.Tomato);
  356.  
  357. spriteBatch.Draw(
  358. ball1.Texture,
  359. ball1.Rectangle,
  360. ball1.BallColor);
  361.  
  362. spriteBatch.Draw(
  363. ball2.Texture,
  364. ball2.Rectangle,
  365. Color.Red);
  366.  
  367. spriteBatch.DrawString(scorefont, "Player1's score:" + score1,
  368. new Vector2(0, 0),
  369. Color.Black);
  370. spriteBatch.DrawString(scorefont, "Player2's score:" + score2,
  371. new Vector2(0, GraphicsDevice.Viewport.Width / 4),
  372. Color.Black);
  373.  
  374.  
  375. spriteBatch.End();
  376.  
  377. base.Draw(gameTime);
  378. }
  379. }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement