コード例 #1
0
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (end)
            {
                return;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Enter) && prevKeyboardState.IsKeyUp(Keys.Enter) && level < 2)
            {
                currentLevel = levels[++level];
            }

            if (Keyboard.GetState().IsKeyDown(Keys.R) && prevKeyboardState.IsKeyUp(Keys.R))
            {
                ball.Position = bar.Position + Vector2.UnitY * bar.Size.Y;
                ball.Velocity = Vector2.Zero;
            }

            prevKeyboardState = Keyboard.GetState();

            if (Mouse.GetState().LeftButton == ButtonState.Pressed && ball.Velocity == Vector2.Zero)
            {
                ball.Velocity = Vector2.Normalize(Vector2.UnitY) * speed;
            }

            float move = Mouse.GetState().X - center.X;

            bar.Position += Vector2.UnitX * move * 0.1f;

            Mouse.SetPosition(center.X, center.Y);

            if (bar.Position.X + bar.Size.X / 2.0f > currentLevel.Width / 2.0f)
            {
                bar.Position = new Vector2(currentLevel.Width / 2.0f - bar.Size.X / 2.0f, bar.Position.Y);
            }
            else if (bar.Position.X - bar.Size.X / 2.0f < -currentLevel.Width / 2.0f)
            {
                bar.Position = new Vector2(-currentLevel.Width / 2.0f + bar.Size.X / 2.0f, bar.Position.Y);
            }

            if (ball.Position.Y < -2.0f)
            {
                lives--;
                ball.Velocity = Vector2.Zero;
                life.Play();
                currentLevel.ClearLastCollision();
            }

            if (ball.Velocity == Vector2.Zero && lives > 0)
            {
                ball.Position = bar.Position + Vector2.UnitY * bar.Size.Y;
            }
            else if (ball.Velocity.Y < 0.0f && ball.BoundingSphere.Intersects(bar.BoundingBox))
            {
                currentLevel.ClearLastCollision();

                bounce.Play();

                float   f      = (bar.Position.X - ball.Position.X) / (bar.Size.X / 2.0f);
                Vector2 normal = Vector2.Transform(Vector2.UnitY, Matrix.CreateRotationZ(f * MathHelper.PiOver4));
                ball.Velocity = Vector2.Reflect(ball.Velocity, normal);

                if (Math.Abs(ball.Velocity.X) > Math.Abs(ball.Velocity.Y))
                {
                    ball.Velocity = Vector2.Normalize((ball.Velocity.X > 0.0f) ? Vector2.One : new Vector2(-1.0f, 1.0f)) * speed;
                }
            }

            currentLevel.Update(gameTime);

            base.Update(gameTime);

            if (currentLevel.CheckCollision(ball))
            {
                score++;
            }

            if (currentLevel.IsDone())
            {
                level++;

                if (level < levels.Length)
                {
                    currentLevel  = levels[level];
                    bar.Position  = Vector2.Zero;
                    ball.Position = Vector2.UnitY;
                    ball.Velocity = Vector2.Zero;
                }
            }

            if (lives == 0 || level == 3)
            {
                end   = true;
                level = 2;
            }
        }