コード例 #1
0
ファイル: Ball.cs プロジェクト: dcf417/Pong
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!((PongGame)Game).GameInProgress)
            {
                return;
            }

            float timeLapse = (float)gameTime.ElapsedGameTime.Milliseconds;

            _position += _speed * timeLapse;

            _position.X = MathHelper.Clamp(_position.X, _minX, _maxX);
            _position.Y = MathHelper.Clamp(_position.Y, _minY, _maxY);

            if (_position.Y == _minY || _position.Y == _maxY)
            {
                _bounce.Play();
                _speed.Y *= -1;
            }

            if (_position.X == _minX)
            {
                ((PongGame)Game).ScoreKeeper.Score(Player.Two);
                _position = _startPosition;
                _speed.X *= -1;
            }

            if (_position.X == _maxX)
            {
                ((PongGame)Game).ScoreKeeper.Score(Player.One);
                _position = _startPosition;
                _speed.X *= -1;
            }

            PaddleCollision collision = DidHitPaddle();

            switch (collision)
            {
            case PaddleCollision.Left:
            case PaddleCollision.Right:
                _bounce.Play();
                _speed.X *= -1;
                break;

            case PaddleCollision.Top:
            case PaddleCollision.Bottom:
                _bounce.Play();
                _speed.Y *= -1;
                break;

            default:
                break;
            }

            base.Update(gameTime);
        }
コード例 #2
0
    void FixedUpdate()
    {
        if (lastBounceThisFrame != null)
        {
            bounce();
        }
        else if (reflectionThisFrame != null)
        {
            reflect();
        }

        lastBounceThisFrame        = null;
        penultimateBounceThisFrame = null;
        reflectionThisFrame        = null;
    }
コード例 #3
0
        // Sees what part of the Paddle collises with the ball (if it does)
        public bool Collides(Ball ball, out PaddleCollision typeOfCollision)
        {
            typeOfCollision = PaddleCollision.None;

            // Make sure enough time has passed for a new collisions
            // (this prevents a bug where a user can build up a lot of speed in the ball)
            if (DateTime.Now < (_lastCollisiontime.Add(_minCollisionTimeGap)))
            {
                return(false);
            }

            // Top & bottom get first priority
            if (ball.CollisionArea.Intersects(TopCollisionArea))
            {
                typeOfCollision    = PaddleCollision.WithTop;
                _lastCollisiontime = DateTime.Now;
                return(true);
            }

            if (ball.CollisionArea.Intersects(BottomCollisionArea))
            {
                typeOfCollision    = PaddleCollision.WithBottom;
                _lastCollisiontime = DateTime.Now;
                return(true);
            }

            // And check the front
            if (ball.CollisionArea.Intersects(FrontCollisionArea))
            {
                typeOfCollision    = PaddleCollision.WithFront;
                _lastCollisiontime = DateTime.Now;
                return(true);
            }

            // Nope, nothing
            return(false);
        }
コード例 #4
0
ファイル: Arena.cs プロジェクト: yjd6808/CSharpNetworking
        // Modifies the ball state based on what the collision is
        private void _processBallHitWithPaddle(PaddleCollision collision)
        {
            // Safety check
            if (collision == PaddleCollision.None)
            {
                return;
            }

            // Increase the speed
            _ball.Speed.X *= _map((float)_random.NextDouble(), 0, 1, 1, 1.25f);
            _ball.Speed.Y *= _map((float)_random.NextDouble(), 0, 1, 1, 1.25f);

            // Shoot in the opposite direction
            _ball.Speed.X *= -1;

            // Hit with top or bottom?
            if ((collision == PaddleCollision.WithTop) || (collision == PaddleCollision.WithBottom))
            {
                _ball.Speed.Y *= -1;
            }

            // Play a sound on the client
            _playSoundEffect("ballHit");
        }
コード例 #5
0
 public void RegisterReflection(PaddleCollision collision)
 {
     reflectionThisFrame = collision;
 }
コード例 #6
0
 public void RegisterBounce(PaddleCollision collision)
 {
     penultimateBounceThisFrame = lastBounceThisFrame;
     lastBounceThisFrame        = collision;
 }