Advertisement
Alan468

GR_2

Oct 30th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.21 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. namespace PingPong
  8. {
  9.  
  10. public class AnimatedSprite : Sprite
  11. {
  12. public int Rows, Columns;
  13. protected int _currentFrame, _totalFrames;
  14.  
  15. protected int _width, _height;
  16.  
  17. int timeSinceLastFrame = 0;
  18. int milisecondsPerFrame;
  19.  
  20. protected Texture2D explosionTexture;
  21.  
  22. protected static readonly TimeSpan explosionInterval = TimeSpan.FromMilliseconds(900);
  23. protected TimeSpan waitingTime;
  24. protected Rectangle explosionRectangle;
  25.  
  26. protected bool isExplosionVisible = false;
  27.  
  28.  
  29. public AnimatedSprite(Texture2D texture, int rows, int columns, int missingFrames, int msecondsPerFrame, Texture2D explosion)
  30. : base(texture)
  31. {
  32. milisecondsPerFrame = msecondsPerFrame;
  33. Rows = rows;
  34. Columns = columns;
  35. _currentFrame = 0;
  36. _totalFrames = Rows * Columns - missingFrames;
  37.  
  38. _width = texture.Width / Columns;
  39. _height = texture.Height / Rows;
  40. explosionTexture = explosion;
  41.  
  42. base._textureWidth = _width;
  43. base._textureHeight = _height;
  44. explosionRectangle = new Rectangle(0, 0, _width, _height);
  45. }
  46.  
  47. public override void Update(GameTime gameTime, List<Sprite> sprites)
  48. {
  49. if (isExplosionVisible)
  50. {
  51. waitingTime += gameTime.ElapsedGameTime;
  52. if (waitingTime > explosionInterval)
  53. {
  54. isExplosionVisible = false;
  55. waitingTime = new TimeSpan();
  56. }
  57. }
  58. timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  59. if (timeSinceLastFrame > milisecondsPerFrame)
  60. {
  61. timeSinceLastFrame -= milisecondsPerFrame;
  62.  
  63. _currentFrame++;
  64. if (_currentFrame == _totalFrames)
  65. _currentFrame = 0;
  66. }
  67.  
  68. }
  69.  
  70. public override void Draw(SpriteBatch spriteBatch)
  71. {
  72. if (isExplosionVisible)
  73. {
  74. int row2 = (int)((float)_currentFrame / (float)5);
  75. int column2 = _currentFrame % 5;
  76.  
  77. Rectangle sourceRectangle2 = new Rectangle(_width * column2, _height * row2, _width, _height);
  78. var lastRect = new Rectangle((int)base.Position.X, (int)base.Position.Y, _width, _height);
  79. //if (explosionRectangle == Rectangle.Empty)
  80. // explosionRectangle = new Rectangle((int)base.Position.X, (int)base.Position.Y, _width, _height);
  81.  
  82. spriteBatch.Draw(explosionTexture, explosionRectangle, sourceRectangle2, Color.White);
  83. }
  84. else
  85. {
  86. int row = (int)((float)_currentFrame / (float)Columns);
  87. int column = _currentFrame % Columns;
  88.  
  89. Rectangle sourceRectangle = new Rectangle(_width * column, _height * row, _width, _height);
  90. Rectangle destinationRectangle = new Rectangle((int)base.Position.X, (int)base.Position.Y, _width, _height);
  91.  
  92. spriteBatch.Draw(base._texture, destinationRectangle, sourceRectangle, Color.White);
  93. }
  94.  
  95. }
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. public class Ball : AnimatedSprite
  106. {
  107. private Vector2? _startPosition = null;
  108. private float? _startSpeed;
  109. private bool _isPlaying;
  110.  
  111. public Score Score;
  112.  
  113. public int explosionRows = 5;
  114. public int explosionColums = 5;
  115.  
  116.  
  117.  
  118. public Ball(Texture2D texture, int rows, int columns, Texture2D explosion)
  119. : base(texture, rows, columns, 9, 16, explosion)
  120. {
  121. Speed = 3f;
  122. }
  123.  
  124. public override void Update(GameTime gameTime, List<Sprite> sprites)
  125. {
  126. if (!isExplosionVisible)
  127. {
  128. if (_startPosition == null)
  129. {
  130. _startPosition = Position;
  131. _startSpeed = Speed;
  132.  
  133. Restart();
  134. }
  135.  
  136. if (Keyboard.GetState().IsKeyDown(Keys.Space))
  137. _isPlaying = true;
  138.  
  139. if (!_isPlaying)
  140. return;
  141.  
  142. foreach (var sprite in sprites)
  143. {
  144. if (sprite == this)
  145. continue;
  146.  
  147. if (this.Velocity.X > 0 && this.IsTouchingLeft(sprite))
  148. {
  149. this.Velocity.X = -this.Velocity.X;
  150. Speed++;
  151.  
  152. sprite.Shining = true;
  153. }
  154. if (this.Velocity.X < 0 && this.IsTouchingRight(sprite))
  155. {
  156. this.Velocity.X = -this.Velocity.X;
  157. Speed++;
  158.  
  159. sprite.Shining = true;
  160. }
  161. if (this.Velocity.Y > 0 && this.IsTouchingTop(sprite))
  162. this.Velocity.Y = -this.Velocity.Y;
  163. if (this.Velocity.Y < 0 && this.IsTouchingBottom(sprite))
  164. this.Velocity.Y = -this.Velocity.Y;
  165. }
  166.  
  167. if (Position.Y <= 0 || Position.Y + base._textureHeight >= Game1.ScreenHeight)
  168. Velocity.Y = -Velocity.Y;
  169.  
  170.  
  171.  
  172. if (Position.X <= 0)
  173. {
  174. Kaboom();
  175. base.explosionRectangle.Location = new Point((int)Position.X,(int)Position.Y);
  176. Score.Score2++;
  177. Restart();
  178. }
  179.  
  180. if (Position.X + base._textureWidth >= Game1.ScreenWidth)
  181. {
  182. Kaboom();
  183. base.explosionRectangle.Location = new Point((int)Position.X,(int)Position.Y);
  184. Score.Score1++;
  185. Restart();
  186. }
  187.  
  188. Position += Velocity * Speed;
  189.  
  190. }
  191.  
  192. base.Update(gameTime, sprites);
  193.  
  194. }
  195.  
  196. void Kaboom()
  197. {
  198. base.isExplosionVisible = true;
  199. }
  200.  
  201. public void Restart()
  202. {
  203. var direction = Game1.Random.Next(0, 4);
  204.  
  205. switch (direction)
  206. {
  207. case 0:
  208. Velocity = new Vector2(1, 1);
  209. break;
  210. case 1:
  211. Velocity = new Vector2(1, -1);
  212. break;
  213. case 2:
  214. Velocity = new Vector2(-1, -1);
  215. break;
  216. case 3:
  217. Velocity = new Vector2(-1, 1);
  218. break;
  219. }
  220.  
  221. Position = (Vector2)_startPosition;
  222. Speed = (float)_startSpeed;
  223.  
  224. _isPlaying = false;
  225. }
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236. public class Bat : Sprite
  237. {
  238. int timeSinceLastFrame = 0;
  239. int shiningMiliseconds = 300;
  240.  
  241.  
  242. public Bat(Texture2D texture)
  243. : base(texture)
  244. {
  245. Speed = 5f;
  246. }
  247.  
  248. public override void Update(GameTime gameTime, List<Sprite> sprites)
  249. {
  250. if (Keyboard.GetState().IsKeyDown(Input.Up))
  251. Velocity.Y = -Speed;
  252. else if (Keyboard.GetState().IsKeyDown(Input.Down))
  253. Velocity.Y = Speed;
  254.  
  255. Position += Velocity;
  256.  
  257. Position.Y = MathHelper.Clamp(Position.Y, 0, Game1.ScreenHeight - _texture.Height);
  258.  
  259. Velocity = Vector2.Zero;
  260.  
  261. if (Shining)
  262. {
  263. ShineBat(gameTime);
  264. }
  265. }
  266.  
  267. void ShineBat(GameTime gameTime)
  268. {
  269. base.Color = Color.Yellow;
  270. timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
  271. if (timeSinceLastFrame > shiningMiliseconds)
  272. {
  273. timeSinceLastFrame -= shiningMiliseconds;
  274. base.Color = Color.White;
  275. base.Shining = false;
  276. }
  277. }
  278. }
  279.  
  280.  
  281.  
  282.  
  283. public class Input
  284. {
  285. public Keys Up;
  286. public Keys Down;
  287. }
  288.  
  289.  
  290.  
  291.  
  292.  
  293. public class Score
  294. {
  295. public int Score1;
  296. public int Score2;
  297.  
  298. private SpriteFont _font;
  299.  
  300. public Score(SpriteFont font)
  301. {
  302. _font = font;
  303. }
  304.  
  305. public void Draw(SpriteBatch spriteBatch)
  306. {
  307. spriteBatch.DrawString(_font, Score1.ToString(), new Vector2(320, 70), Color.White);
  308. spriteBatch.DrawString(_font, Score2.ToString(), new Vector2(460, 70), Color.White);
  309. }
  310. }
  311.  
  312.  
  313.  
  314.  
  315.  
  316. public class Sprite
  317. {
  318. protected Texture2D _texture;
  319.  
  320. public Vector2 Position;
  321. public Vector2 Velocity;
  322. public float Speed;
  323. public Input Input;
  324.  
  325. int offset = 10;
  326.  
  327. protected int _textureWidth, _textureHeight;
  328.  
  329. public bool Shining = false;
  330.  
  331. public Color Color = Color.White;
  332.  
  333. public Rectangle Rectangle
  334. {
  335. get
  336. {
  337. return new Rectangle((int)Position.X, (int)Position.Y, _textureWidth, _textureHeight);
  338. }
  339. }
  340.  
  341.  
  342. public Sprite(Texture2D texture)
  343. {
  344. _texture = texture;
  345. _textureWidth = texture.Width;
  346. _textureHeight = texture.Height;
  347. }
  348.  
  349. public virtual void Update(GameTime gameTime, List<Sprite> sprites)
  350. {
  351.  
  352. }
  353.  
  354. public virtual void Draw(SpriteBatch spriteBatch)
  355. {
  356.  
  357. spriteBatch.Draw(_texture, Position, Color);
  358. }
  359.  
  360. //kolizja
  361.  
  362. protected bool IsTouchingLeft(Sprite sprite)
  363. {
  364. return this.Rectangle.Right + this.Velocity.X - offset > sprite.Rectangle.Left &&
  365. this.Rectangle.Left < sprite.Rectangle.Left &&
  366. this.Rectangle.Bottom > sprite.Rectangle.Top &&
  367. this.Rectangle.Top < sprite.Rectangle.Bottom;
  368. }
  369.  
  370. protected bool IsTouchingRight(Sprite sprite)
  371. {
  372. return this.Rectangle.Left + this.Velocity.X + offset < sprite.Rectangle.Right &&
  373. this.Rectangle.Right > sprite.Rectangle.Right &&
  374. this.Rectangle.Bottom > sprite.Rectangle.Top &&
  375. this.Rectangle.Top < sprite.Rectangle.Bottom;
  376. }
  377.  
  378. protected bool IsTouchingTop(Sprite sprite)
  379. {
  380. return this.Rectangle.Bottom + this.Velocity.Y - offset > sprite.Rectangle.Top &&
  381. this.Rectangle.Top < sprite.Rectangle.Top &&
  382. this.Rectangle.Right > sprite.Rectangle.Left &&
  383. this.Rectangle.Left < sprite.Rectangle.Right;
  384. }
  385.  
  386. protected bool IsTouchingBottom(Sprite sprite)
  387. {
  388. return this.Rectangle.Top + this.Velocity.Y + offset < sprite.Rectangle.Bottom &&
  389. this.Rectangle.Bottom > sprite.Rectangle.Bottom &&
  390. this.Rectangle.Right > sprite.Rectangle.Left &&
  391. this.Rectangle.Left < sprite.Rectangle.Right;
  392. }
  393. }
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. /// <summary>
  401. /// This is the main type for your game.
  402. /// </summary>
  403. public class Game1 : Game
  404. {
  405. GraphicsDeviceManager graphics;
  406. SpriteBatch spriteBatch;
  407.  
  408. public static int ScreenWidth;
  409. public static int ScreenHeight;
  410. public static Random Random;
  411.  
  412. private Score _score;
  413. private List<Sprite> _sprites;
  414.  
  415. Texture2D bcgTexture;
  416.  
  417. public Game1()
  418. {
  419. graphics = new GraphicsDeviceManager(this);
  420. Content.RootDirectory = "Content";
  421. }
  422.  
  423. /// <summary>
  424. /// Allows the game to perform any initialization it needs to before starting to run.
  425. /// This is where it can query for any required services and load any non-graphic
  426. /// related content. Calling base.Initialize will enumerate through any components
  427. /// and initialize them as well.
  428. /// </summary>
  429. protected override void Initialize()
  430. {
  431. // TODO: Add your initialization logic here
  432. ScreenWidth = graphics.PreferredBackBufferWidth;
  433. ScreenHeight = graphics.PreferredBackBufferHeight;
  434. Random = new Random();
  435.  
  436. base.Initialize();
  437. }
  438.  
  439. /// <summary>
  440. /// LoadContent will be called once per game and is the place to load
  441. /// all of your content.
  442. /// </summary>
  443. protected override void LoadContent()
  444. {
  445. // Create a new SpriteBatch, which can be used to draw textures.
  446. spriteBatch = new SpriteBatch(GraphicsDevice);
  447.  
  448. bcgTexture = Content.Load<Texture2D>("pongBackground");
  449.  
  450. var batTexture = Content.Load<Texture2D>("paddle1a");
  451. var batTexture2 = Content.Load<Texture2D>("paddle2a");
  452. var explosionTexture = Content.Load<Texture2D>("explosion64"); //5,5 row
  453.  
  454. var ballAnimTexture = Content.Load<Texture2D>("ball-anim");
  455.  
  456. _score = new Score(Content.Load<SpriteFont>("font"));
  457.  
  458. _sprites = new List<Sprite>()
  459. {
  460.  
  461. new Bat(batTexture)
  462. {
  463. Position = new Vector2(20, (ScreenHeight / 2) - (batTexture.Height / 2)),
  464. Input = new Input()
  465. {
  466. Up = Keys.Q,
  467. Down = Keys.A,
  468. }
  469. },
  470. new Bat(batTexture2)
  471. {
  472. Position = new Vector2(ScreenWidth - 20 - batTexture.Width, (ScreenHeight / 2) - (batTexture.Height / 2)),
  473. Input = new Input()
  474. {
  475. Up = Keys.P,
  476. Down = Keys.L,
  477. }
  478. },
  479. new Ball(ballAnimTexture,5,16,explosionTexture)
  480. {
  481. Position = new Vector2((ScreenWidth / 2) - (32), (ScreenHeight / 2) - (32)),
  482. Score = _score,
  483. }
  484. };
  485. }
  486.  
  487. /// <summary>
  488. /// UnloadContent will be called once per game and is the place to unload
  489. /// game-specific content.
  490. /// </summary>
  491. protected override void UnloadContent()
  492. {
  493. // TODO: Unload any non ContentManager content here
  494. }
  495.  
  496. /// <summary>
  497. /// Allows the game to run logic such as updating the world,
  498. /// checking for collisions, gathering input, and playing audio.
  499. /// </summary>
  500. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  501. protected override void Update(GameTime gameTime)
  502. {
  503. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  504. Exit();
  505.  
  506. foreach (var sprite in _sprites)
  507. {
  508. sprite.Update(gameTime, _sprites);
  509. }
  510.  
  511. base.Update(gameTime);
  512. }
  513.  
  514. /// <summary>
  515. /// This is called when the game should draw itself.
  516. /// </summary>
  517. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  518. protected override void Draw(GameTime gameTime)
  519. {
  520. GraphicsDevice.Clear(Color.CornflowerBlue);
  521.  
  522. // TODO: Add your drawing code here
  523. spriteBatch.Begin();
  524.  
  525. spriteBatch.Draw(bcgTexture, GraphicsDevice.Viewport.Bounds, Color.White);
  526.  
  527. foreach (var sprite in _sprites)
  528. sprite.Draw(spriteBatch);
  529.  
  530. _score.Draw(spriteBatch);
  531.  
  532. spriteBatch.End();
  533.  
  534. base.Draw(gameTime);
  535. }
  536. }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement