Hyluss

ball with timer

Oct 30th, 2018
581
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 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 : Sprite
  10.     {
  11.         private float _timer = 0f; // Incrementing the speed over time
  12.         private Vector2? _startPosition = null;
  13.         private float? _startSpeed;
  14.         private bool _isPlaying;
  15.  
  16.         public Score Score;
  17.         public int SpeedIncrementSpan = 10; // How often the speed will increment
  18.  
  19.         public Ball(Texture2D texture)
  20.           : base(texture)
  21.         {
  22.             Speed = 3f;
  23.         }
  24.  
  25.         public override void Update(GameTime gameTime, List<Sprite> sprites)
  26.         {
  27.             if (_startPosition == null)
  28.             {
  29.                 _startPosition = Position;
  30.                 _startSpeed = Speed;
  31.  
  32.                 Restart();
  33.             }
  34.  
  35.             if (Keyboard.GetState().IsKeyDown(Keys.Space))
  36.                 _isPlaying = true;
  37.  
  38.             if (!_isPlaying)
  39.                 return;
  40.  
  41.             _timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
  42.  
  43.             if (_timer > SpeedIncrementSpan)
  44.             {
  45.                 Speed++;
  46.                 _timer = 0;
  47.             }
  48.  
  49.             foreach (var sprite in sprites)
  50.             {
  51.                 if (sprite == this)
  52.                     continue;
  53.  
  54.                 if (this.Velocity.X > 0 && this.IsTouchingLeft(sprite))
  55.                     this.Velocity.X = -this.Velocity.X;
  56.                 if (this.Velocity.X < 0 && this.IsTouchingRight(sprite))
  57.                     this.Velocity.X = -this.Velocity.X;
  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 + _texture.Height >= Game1.ScreenHeight)
  65.                 Velocity.Y = -Velocity.Y;
  66.  
  67.             if (Position.X <= 0)
  68.             {
  69.                 Score.Score2++;
  70.                 Restart();
  71.             }
  72.  
  73.             if (Position.X + _texture.Width >= Game1.ScreenWidth)
  74.             {
  75.                 Score.Score1++;
  76.                 Restart();
  77.             }
  78.  
  79.             Position += Velocity * Speed;
  80.         }
  81.  
  82.         public void Restart()
  83.         {
  84.             var direction = Game1.Random.Next(0, 4);
  85.  
  86.             switch (direction)
  87.             {
  88.                 case 0:
  89.                     Velocity = new Vector2(1, 1);
  90.                     break;
  91.                 case 1:
  92.                     Velocity = new Vector2(1, -1);
  93.                     break;
  94.                 case 2:
  95.                     Velocity = new Vector2(-1, -1);
  96.                     break;
  97.                 case 3:
  98.                     Velocity = new Vector2(-1, 1);
  99.                     break;
  100.             }
  101.  
  102.             Position = (Vector2)_startPosition;
  103.             Speed = (float)_startSpeed;
  104.             _timer = 0;
  105.             _isPlaying = false;
  106.         }
  107.     }
  108.    
  109. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment