예제 #1
0
파일: TheGame.cs 프로젝트: harry-cpp/Pong
        protected override void Update(GameTime gameTime)
        {
            if (!GameContent.IsLoaded)
            {
                return;
            }

            InputManager.Update();

            if (InputManager.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            MainWorld.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);

            foreach (var obj in _objects)
            {
                obj.Update(gameTime);
            }

            if (InputManager.IsKeyPressed(Keys.F))
            {
                _graphics.IsFullScreen = !_graphics.IsFullScreen;
                _graphics.ApplyChanges();

                IsMouseVisible = !_graphics.IsFullScreen;
            }

            if (InputManager.IsKeyPressed(Keys.Space) ||
                InputManager.IsButtonPressed(0, Buttons.A) ||
                InputManager.IsButtonPressed(0, Buttons.Start) ||
                InputManager.IsButtonPressed(1, Buttons.A) ||
                InputManager.IsButtonPressed(1, Buttons.Start))
            {
                if (_mode == Mode.Start || _mode == Mode.NextStart)
                {
                    float forcex = (new Random()).Next(-20, 20);
                    float forcey = (new Random()).Next(-40, 40);

                    forcex = (forcex < 0) ? forcex - 20f : forcex + 20f;

                    _ball.Body.ApplyForce(new Vector2(forcex, forcey));

                    _score.Scored = false;
                    _mode         = Mode.Game;
                }
                else if (_mode == Mode.Continue)
                {
                    foreach (var obj in _objects)
                    {
                        obj.ResetPosition();
                    }

                    _mode = Mode.NextStart;
                }
            }

            if (!_score.Scored)
            {
                if (_ball.Position.X < 0)
                {
                    _score.ScoreForLeft();
                    _mode = Mode.Continue;
                }
                else if (_ball.Position.X > GameSettings.Width)
                {
                    _score.ScoreForRight();
                    _mode = Mode.Continue;
                }
            }

            base.Update(gameTime);
        }