Hyluss

[Monogame PingPong] Ball

Oct 30th, 2018
613
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System.Collections.Generic;
  5.  
  6. namespace PingPong
  7. {
  8.  
  9.     public class Ball : AnimatedSprite
  10.     {        
  11.         private Vector2? _startPosition = null;
  12.         private float? _startSpeed;
  13.         private bool _isPlaying;
  14.  
  15.         public Score Score;
  16.  
  17.         public Ball(Texture2D texture, int rows, int columns)
  18.           : base(texture,rows,columns,9,16)
  19.         {
  20.             Speed = 3f;
  21.         }
  22.  
  23.         public override void Update(GameTime gameTime, List<Sprite> sprites)
  24.         {            
  25.             if (_startPosition == null)
  26.             {
  27.                 _startPosition = Position;
  28.                 _startSpeed = Speed;
  29.  
  30.                 Restart();
  31.             }
  32.  
  33.             if (Keyboard.GetState().IsKeyDown(Keys.Space))
  34.                 _isPlaying = true;
  35.  
  36.             if (!_isPlaying)
  37.                 return;
  38.  
  39.             foreach (var sprite in sprites)
  40.             {
  41.                 if (sprite == this)
  42.                     continue;
  43.  
  44.                 if (this.Velocity.X > 0 && this.IsTouchingLeft(sprite))
  45.                 {
  46.                     this.Velocity.X = -this.Velocity.X;
  47.                     Speed++;
  48.  
  49.                     sprite.Shining = true;
  50.                 }
  51.                 if (this.Velocity.X < 0 && this.IsTouchingRight(sprite))
  52.                 {
  53.                     this.Velocity.X = -this.Velocity.X;
  54.                     Speed++;
  55.  
  56.                     sprite.Shining = true;
  57.                 }
  58.                 if (this.Velocity.Y > 0 && this.IsTouchingTop(sprite))
  59.                     this.Velocity.Y = -this.Velocity.Y;
  60.                 if (this.Velocity.Y < 0 && this.IsTouchingBottom(sprite))
  61.                     this.Velocity.Y = -this.Velocity.Y;
  62.             }
  63.  
  64.             if (Position.Y <= 0 || Position.Y + base._textureHeight >= Game1.ScreenHeight)
  65.                 Velocity.Y = -Velocity.Y;
  66.  
  67.             if (Position.X <= 0)
  68.             {
  69.                 Kaboom();
  70.                 Score.Score2++;
  71.                 Restart();
  72.             }
  73.  
  74.             if (Position.X + base._textureWidth >= Game1.ScreenWidth)
  75.             {
  76.                 Kaboom();
  77.                 Score.Score1++;
  78.                 Restart();
  79.             }
  80.  
  81.             Position += Velocity * Speed;
  82.  
  83.             base.Update(gameTime, sprites);
  84.         }
  85.  
  86.         void Kaboom()
  87.         {
  88.            
  89.         }
  90.  
  91.         public void Restart()
  92.         {
  93.             var direction = Game1.Random.Next(0, 4);
  94.  
  95.             switch (direction)
  96.             {
  97.                 case 0:
  98.                     Velocity = new Vector2(1, 1);
  99.                     break;
  100.                 case 1:
  101.                     Velocity = new Vector2(1, -1);
  102.                     break;
  103.                 case 2:
  104.                     Velocity = new Vector2(-1, -1);
  105.                     break;
  106.                 case 3:
  107.                     Velocity = new Vector2(-1, 1);
  108.                     break;
  109.             }
  110.  
  111.             Position = (Vector2)_startPosition;
  112.             Speed = (float)_startSpeed;
  113.            
  114.             _isPlaying = false;
  115.         }
  116.     }
  117.    
  118. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment