コード例 #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);
        }
コード例 #2
0
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }


            MouseState    mouse = Mouse.GetState();
            KeyboardState keys  = Keyboard.GetState();

            // mouse movement when left-button
            if (mouse.LeftButton == ButtonState.Pressed)
            {
                float x = (lastMouse.X - mouse.X) * 0.2f;
                float y = (lastMouse.Y - mouse.Y) * 0.2f;

                paddle2.Position += new JVector(-x, y, 0f);
                paddle1.Position += new JVector(-x, y, 0f);
            }

            // tilt controls for paddles
            if (keys.IsKeyDown(Keys.Left))
            {
                JMatrix rotated =
                    Conversion.ToJitterMatrix(Matrix.CreateRotationY(MathHelper.ToRadians(1)));

                paddle2.Orientation *= rotated;
                paddle1.Orientation *= rotated;
            }

            if (keys.IsKeyDown(Keys.Right))
            {
                JMatrix rotated =
                    Conversion.ToJitterMatrix(Matrix.CreateRotationY(MathHelper.ToRadians(-1)));

                paddle2.Orientation *= rotated;
                paddle1.Orientation *= rotated;
            }

            if (keys.IsKeyDown(Keys.Up))
            {
                JMatrix rotated =
                    Conversion.ToJitterMatrix(Matrix.CreateRotationX(MathHelper.ToRadians(1)));

                paddle2.Orientation *= rotated;
                paddle1.Orientation *= rotated;
            }

            if (keys.IsKeyDown(Keys.Down))
            {
                JMatrix rotated =
                    Conversion.ToJitterMatrix(Matrix.CreateRotationX(MathHelper.ToRadians(-1)));

                paddle2.Orientation *= rotated;
                paddle1.Orientation *= rotated;
            }


            if (keys.IsKeyDown(Keys.Space))
            {
                ball.AddForce(JVector.Up * 100f);
            }

            // ball and paddle-orientation reset
            if (keys.IsKeyDown(Keys.R))
            {
                ball.IsStatic       = false;
                ball.LinearVelocity = JVector.Zero;
                ball.Position       = new JVector(0f, 9f, 18f);

                float x_variation = (float)(generator.Next(6000) - 3000);

                ball.AddForce(new JVector(x_variation, 1000f, -10000f));

                paddle1.Orientation = JMatrix.Identity;
                paddle2.Orientation = JMatrix.Identity;
            }


            //player.HandleInput(gameTime);

            world.Step(1.0f / 50.0f, true);

            lastMouse = mouse;

            base.Update(gameTime);
        }