Ejemplo n.º 1
0
        public void Run(Ai.Difficulty difficultyLeft, Ai.Difficulty difficultyRight)
        {
            if (difficultyLeft != _aiLeft.AIDifficulty || difficultyRight != _aiRight.AIDifficulty)
            {
                ReInitialisePlayerAndAI(difficultyLeft, difficultyRight);
            }



            double elapsedTime = (double)_stopwatch.ElapsedTicks / (double)Stopwatch.Frequency * 1000;

            _lag += elapsedTime;

            _stopwatch.Restart();

            Input(elapsedTime);
            while (_lag >= MS_PER_UPDATE)
            {
                Update(MS_PER_UPDATE);
                _lag -= MS_PER_UPDATE;
            }
            Redraw();

            _stopwatch.Stop();
        }
        public void Touched(Vector2D position)
        {
            for (int i = 0; i < _checkBoxes.Count; i++)
            {
                _checkBoxes[i].Touched(position);

                if (_checkBoxes[i].IsChecked)
                {
                    switch (i)
                    {
                    case 0:
                        _difficultyLeft = Ai.Difficulty.Human;
                        break;

                    case 1:
                        _difficultyRight = Ai.Difficulty.Human;
                        break;

                    case 2:
                        _difficultyLeft = Ai.Difficulty.None;
                        break;

                    case 3:
                        _difficultyRight = Ai.Difficulty.None;
                        break;

                    case 4:
                        _difficultyLeft = Ai.Difficulty.Normal;
                        break;

                    case 5:
                        _difficultyRight = Ai.Difficulty.Normal;
                        break;

                    default: return;
                    }


                    int jStart = 0;

                    if (i % 2 != 0)
                    {
                        jStart = 1;
                    }


                    for (int j = jStart; j < _checkBoxes.Count; j += 2)
                    {
                        if (i != j)
                        {
                            _checkBoxes[j].IsChecked = false;
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void Initialise(Ai.Difficulty difficultyLeft, Ai.Difficulty difficultyRight)
        {
            _window = new RenderWindow(new VideoMode((uint)_windowSize.X, (uint)_windowSize.Y), "PONG");
            _window.SetActive();

            _circlePosition = new Vector2f(_windowSize.X / 2, _windowSize.Y / 2);


            _rectangleObject1           = new RectangleObject(new Vector2f(_rectangleSizeX, _rectangleSizeY));
            _rectangleObject1.FillColor = Color.White;
            _rectangleObject1.Attach(new SoundObserver(_soundManager));
            _rectangleObject2           = new RectangleObject(new Vector2f(_rectangleSizeX, _rectangleSizeY));
            _rectangleObject2.FillColor = Color.White;
            _rectangleObject2.Attach(new SoundObserver(_soundManager));

            _circleObject = new CircleObject(_circleSize);
            _circleObject.Attach(new SoundObserver(_soundManager));
            _circleObject.FillColor = Color.White;


            _prefabPowerUps.Add(new SpeedDown(_circleObject));
            _prefabPowerUps.Add(new BiggerRectangleObject(_circleObject));
            _prefabPowerUps.Add(new BiggerRectangleObject(_circleObject));
            _prefabPowerUps.Add(new Disco(_circleObject));
            _prefabPowerUps.Add(new Disco(_circleObject));



            _circleObject.Position = _circlePosition;
            _circleObject.Update(0);


            _rectangleObject1.Position = new Vector2f(_rectanglePositionX, _windowSize.Y / 2);
            _rectangleObject2.Position = new Vector2f(_windowSize.X - _rectanglePositionX, _windowSize.Y / 2);

            _scoreLeftText          = new Text(_scoreLeft.ToString(), _font, _characterSizeScore);
            _scoreLeftText.Position = new Vector2f(_windowSize.X / 2 - _scoreLeftText.GetGlobalBounds().Width - 100, 100);

            _scoreRightText          = new Text(_scoreRight.ToString(), _font, _characterSizeScore);
            _scoreRightText.Position = new Vector2f(_windowSize.X / 2 + 100, 100);

            _middleLine          = new Text("I", _font);
            _middleLine.Position = new Vector2f(_windowSize.X / 2 - _middleLine.GetGlobalBounds().Width / 2, 0);
            while (_middleLine.GetGlobalBounds().Height < _windowSize.Y)
            {
                _middleLine.DisplayedString += "\n\nI";
            }

            _aiLeft  = new Ai(_rectangleObject1, _circleObject, _windowSize, difficultyLeft);
            _aiRight = new Ai(_rectangleObject2, _circleObject, _windowSize, difficultyRight);

            _nextPowerUps.Add(Random(0, _prefabPowerUps.Count));
        }
Ejemplo n.º 4
0
        public void Start(Ai.Difficulty difficultyLeft, Ai.Difficulty difficultyRight)
        {
            Initialise(difficultyLeft, difficultyRight);

            double previous = DateTime.Now.TimeOfDay.TotalMilliseconds;

            while (_window.IsOpen)
            {
                double current     = DateTime.Now.TimeOfDay.TotalMilliseconds;
                double elapsedTime = current - previous;
                previous = current;

                Input(elapsedTime);
                Update(elapsedTime);
                Redraw();
            }
        }
Ejemplo n.º 5
0
        private void ReInitialisePlayerAndAI(Ai.Difficulty difficultyLeft, Ai.Difficulty difficultyRight)
        {
            _firstRectanglePositionY  = Program.windowSize.Y - _rectangleSizeY / 2 - 50;
            _secondRectanglePositionY = _firstRectanglePositionY + _rectangleSizeY * 2;

            if (difficultyLeft != Ai.Difficulty.None)
            {
                _player1 = new RectangleObject(new Vector2D(_rectangleSizeX, _rectangleSizeY), 0);
                _player1.Rectangle.Position = new Vector2D(Program.windowSize.X / 2, _firstRectanglePositionY);
            }
            else
            {
                _player1 = null;
            }

            if (difficultyRight != Ai.Difficulty.None)
            {
                _player2 = new RectangleObject(new Vector2D(_rectangleSizeX, _rectangleSizeY), 3);
                if (_player1 == null)
                {
                    _player2.Rectangle.Position = new Vector2D(Program.windowSize.X / 2, _firstRectanglePositionY);
                }
                else
                {
                    _player2.Rectangle.Position = new Vector2D(Program.windowSize.X / 2, _secondRectanglePositionY);
                }
            }
            else
            {
                _player2 = null;
            }

            _aiLeft  = new Ai(_player1, _circleObject, Program.windowSize, difficultyLeft);
            _aiRight = new Ai(_player2, _circleObject, Program.windowSize, difficultyRight);

            _players.Clear();
            _players.Add(_player1);
            _players.Add(_player2);
        }
Ejemplo n.º 6
0
        private void Input()
        {
            if (Keyboard.IsKeyPressed(Keyboard.Key.Return) || Keyboard.IsKeyPressed(Keyboard.Key.Escape))
            {
                _window.Close();
            }

            if (Mouse.IsButtonPressed(Mouse.Button.Left))
            {
                _mouseCircle.Position = (Vector2f)Mouse.GetPosition(_window);

                for (int i = 0; i < _checkBoxes.Count; i++)
                {
                    if (_mouseCircle.GetGlobalBounds().Intersects(_checkBoxes[i].GetGlobalBounds()))
                    {
                        if (i % 2 == 0)
                        {
                            _xLeft.Position = _checkBoxes[i].Position;
                            _difficultyLeft = (Ai.Difficulty)(i / 2);
                            break;
                        }
                        else
                        {
                            _xRight.Position = _checkBoxes[i].Position;
                            _difficultyRight = (Ai.Difficulty)((i - 1) / 2);
                            break;
                        }
                    }
                }


                if (_mouseCircle.GetGlobalBounds().Intersects(_pressToContinue.GetGlobalBounds()))
                {
                    _window.Close();
                }
            }
        }
Ejemplo n.º 7
0
 public void Start(Ai.Difficulty difficultyLeft, Ai.Difficulty difficultyRight)
 {
     ReInitialisePlayerAndAI(difficultyLeft, difficultyRight);
     Initialise();
 }