Пример #1
0
        public override void Update(GameTime gameTime)
        {
            if (InputHandler.IsKeyJustPressed(Keys.Up) || InputHandler.IsKeyJustPressed(Keys.Down))
            {
                highlighted = highlighted == 0 ? 1 : 0;
                Constants.G_SOUNDS_PADDLE_HIT.Play();
            }

            if (InputHandler.IsKeyJustPressed(Keys.Enter))
            {
                Constants.G_SOUNDS_CONFIRM.Play();
                if (highlighted == 0)
                {
                    Paddle       paddle     = new Paddle(this.Game, GameRef.SpriteBatch);
                    List <Brick> bricks     = LevelMaker.CreateMap(this.Game, GameRef.SpriteBatch, random);
                    ServeState   serveState = new ServeState(this.Game, paddle, 3, 0, bricks);
                    manager.ChangeState(serveState);
                }
            }

            if (InputHandler.IsKeyJustPressed(Keys.Escape))
            {
                Game.Exit();
            }

            base.Update(gameTime);
        }
Пример #2
0
        public override void Update(GameTime gameTime)
        {
            if (paused)
            {
                if (InputHandler.IsKeyJustPressed(Keys.Space))
                {
                    paused = false;
                    Constants.G_SOUNDS_PAUSE.Play();
                }
                else
                {
                    return;
                }
            }
            else if (InputHandler.IsKeyJustPressed(Keys.Space))
            {
                paused = true;
                Constants.G_SOUNDS_PAUSE.Play();
            }

            base.Update(gameTime);

            if (ball.Collides(paddle.BoundingBox))
            {
                ball.Y  = paddle.BoundingBox.Y - ball.BoundingBox.Height;
                ball.Dy = -ball.Dy;

                if (ball.X < paddle.BoundingBox.X + (paddle.BoundingBox.Width / 2) && paddle.Dx < 0)
                {
                    ball.Dx = -50 + -(8 * (paddle.BoundingBox.X + paddle.BoundingBox.Width / 2 - ball.X));
                }
                else if (ball.X > paddle.BoundingBox.X + (paddle.BoundingBox.Width / 2) && paddle.Dx > 0)
                {
                    ball.Dx = 50 + (8 * Math.Abs(paddle.BoundingBox.X + paddle.BoundingBox.Width / 2 - ball.X));
                }
                Constants.G_SOUNDS_PADDLE_HIT.Play();
            }

            foreach (Brick brick in bricks)
            {
                if (brick.InPlay && ball.Collides(brick.BoundingBox))
                {
                    brick.Hit();
                    score += 10;

                    if (ball.X + 2 < brick.BoundingBox.X && ball.Dx > 0)
                    {
                        ball.Dx = -ball.Dx;
                        ball.X  = brick.BoundingBox.X - 8;
                    }
                    else if (ball.X + 6 > brick.BoundingBox.X + brick.BoundingBox.Width && ball.Dx < 0)
                    {
                        ball.Dx = -ball.Dx;
                        ball.X  = brick.BoundingBox.X + 32;
                    }
                    else if (ball.Y < brick.BoundingBox.Y)
                    {
                        ball.Dy = -ball.Dy;
                        ball.Y  = brick.BoundingBox.Y - 8;
                    }
                    else
                    {
                        ball.Dy = -ball.Dy;
                        ball.Y  = brick.BoundingBox.Y + 16;
                    }
                    ball.Dy = ball.Dy * 1.02f;
                }
            }

            if (ball.Y >= Constants.VIRTUAL_HEIGHT)
            {
                health--;
                Constants.G_SOUNDS_HURT.Play();
                if (health == 0)
                {
                    manager.ChangeState(new GameOverState(this.Game, score));
                }
                else
                {
                    ServeState serveState = new ServeState(this.Game, paddle, health, score, bricks);
                    manager.ChangeState(serveState);
                }
            }

            if (InputHandler.IsKeyJustPressed(Keys.Escape))
            {
                Game.Exit();
            }
        }