Exemplo n.º 1
0
        private void MoveBall(double secondsElapsed)
        {
            lock (_syncRoot)
            {
                // calculate new position
                _ballPosition += _ballDirection * (_ballSpeed * secondsElapsed);

                // check for collisions
                // up
                if(_ballPosition.Y < BallRadius)
                {
                    _ballPosition += new Vector(0, -(_ballPosition.Y - BallRadius));
                    _ballDirection = _ballDirection.MirrorY();
                }
                // down
                if (_ballPosition.Y > FieldHeight - BallRadius) 
                {
                    _ballPosition += new Vector(0, -(_ballPosition.Y - (FieldHeight - BallRadius)));
                    _ballDirection = _ballDirection.MirrorY();
                }
                // left player
                if (_ballPosition.X < PlayerReach + BallRadius &&
                    _ballPosition.Y <= _players[LeftPlayer].YPos + (PlayerHeight / 2) && _ballPosition.Y >= _players[LeftPlayer].YPos - (PlayerHeight / 2))
                {
                    _ballPosition += new Vector(-(_ballPosition.X - (BallRadius + PlayerReach)), 0);
                    _ballDirection = _ballDirection.MirrorX();
                    // speed things up to make them more interesing
                    _ballSpeed += BallSpeedIncrease;
                }
                // right player
                if (_ballPosition.X > FieldWidth - (BallRadius + PlayerReach) &&
                    _ballPosition.Y <= _players[RightPlayer].YPos + (PlayerHeight / 2) && _ballPosition.Y >= _players[RightPlayer].YPos - (PlayerHeight / 2))
                {
                    _ballPosition += new Vector(-(_ballPosition.X - (FieldWidth - (BallRadius + PlayerReach))), 0);
                    _ballDirection = _ballDirection.MirrorX();
                    // speed things up to make them more interesing
                    _ballSpeed += BallSpeedIncrease;
                }

                // check for scores
                if (_ballPosition.X < 0 || _ballPosition.X > FieldWidth)
                {
                    _score[_ballPosition.X < 0 ? RightPlayer : LeftPlayer]++;
                    // broadcast score message
                    BroadcastMessage(new ScoreMessage { Score = _score });

                    //reset ball
                    var random = new Random();
                    _ballPosition = new Vector(FieldWidth / 2, BallRadius + random.Next(FieldHeight - 2 * BallRadius));
                    _ballDirection = Vector.Directions[random.Next(Vector.Directions.Length - 1)];
                    _ballSpeed = BallStartingSpeedPixPerSecond;
                }

                // broadcast ball position message
                BroadcastMessage(new BallPositionMessage { XPos = (int)_ballPosition.X, YPos = (int)_ballPosition.Y });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Движение мяча
        /// </summary>
        /// <param name="secondsElapsed">
        /// Сколько секунд прошло
        /// </param>
        private void MoveBall(double secondsElapsed)
        {
            lock (_syncRoot)
            {
                // вычисление новой позиции
                _ballPosition += _ballDirection * (_ballSpeed * secondsElapsed);

                // проверка столкновений со стенками
                // верхняя граница
                if(_ballPosition.Y < BallRadius)
                {
                    _ballPosition += new Vector(0, -(_ballPosition.Y - BallRadius));
                    _ballDirection = _ballDirection.MirrorY();
                }
                // нижняя граница
                if (_ballPosition.Y > FieldHeight - BallRadius)
                {
                    _ballPosition += new Vector(0, -(_ballPosition.Y - (FieldHeight - BallRadius)));
                    _ballDirection = _ballDirection.MirrorY();
                }
                // левый игрок
                if (_ballPosition.X < PlayerReach + BallRadius &&
                    _ballPosition.Y <= _players[LeftPlayer].YPos + (PlayerHeight / 2) && _ballPosition.Y >= _players[LeftPlayer].YPos - (PlayerHeight / 2))
                {
                    _ballPosition += new Vector(-(_ballPosition.X - (BallRadius + PlayerReach)), 0);
                    _ballDirection = _ballDirection.MirrorX();
                    // speed things up to make them more interesing
                    _ballSpeed += BallSpeedIncrease;
                }
                // правый игрок
                if (_ballPosition.X > FieldWidth - (BallRadius + PlayerReach) &&
                    _ballPosition.Y <= _players[RightPlayer].YPos + (PlayerHeight / 2) && _ballPosition.Y >= _players[RightPlayer].YPos - (PlayerHeight / 2))
                {
                    _ballPosition += new Vector(-(_ballPosition.X - (FieldWidth - (BallRadius + PlayerReach))), 0);
                    _ballDirection = _ballDirection.MirrorX();
                    // ускорение мяча
                    _ballSpeed += BallSpeedIncrease;
                }

                // проверка счета
                if (_ballPosition.X < 0 || _ballPosition.X > FieldWidth)
                {
                    _score[_ballPosition.X < 0 ? RightPlayer : LeftPlayer]++;
                    // отправить игрокам сообщение с текущим счетом
                    BroadcastMessage(new ScoreMessage { Score = _score });

                    //'перезагрузить мяч'
                    var random = new Random();
                    _ballPosition = new Vector(FieldWidth / 2, BallRadius + random.Next(FieldHeight - 2 * BallRadius));
                    _ballDirection = Vector.Directions[random.Next(Vector.Directions.Length - 1)];
                    _ballSpeed = BallStartingSpeedPixPerSecond;
                }

                // отправить игрокам сообщение с новой позицией мяча
                BroadcastMessage(new BallPositionMessage { XPos = (int)_ballPosition.X, YPos = (int)_ballPosition.Y });
            }
        }