Beispiel #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            smokeParticleSystem.Update(gameTime);
            coinParticleSystem.Update(gameTime);
            playerParticleSystem.Update(gameTime);

            newKS = Keyboard.GetState();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            paddle.Update(gameTime);
            ball.Update(gameTime);

            if (paddle.bounds.CollidesWith(ball.bounds))
            {
                ball.Velocity *= -1;
                Console.WriteLine("collided");
                Exit();
            }
            if (paddle.bounds.CollidesWith(coin.bounds))
            {
                coinPickupSFX.Play();
                Points += 1;
                Console.WriteLine("+1 point");

                double randX = Random.NextDouble();
                double randY = Random.NextDouble();

                randX         = MathHelper.Lerp(0, GraphicsDevice.Viewport.Width, (float)randX);
                randY         = MathHelper.Lerp(0, GraphicsDevice.Viewport.Height, (float)randY);
                coin.bounds.X = (float)randX;
                coin.bounds.Y = (float)randY;
            }

            oldKS = newKS;
            base.Update(gameTime);



            // TODO: Add your update logic here
        }
Beispiel #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            thisState = Keyboard.GetState();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (thisState.IsKeyDown(Keys.Space) && !oldState.IsKeyDown(Keys.Space))
            {
                SpawnBall(ballCount, BallSpawnPos);
            }

            paddle.Update(gameTime, thisState);

            if (paddle.Collision.Position.X > gameWidth - paddleSizeX)
            {
                paddle.Collision.Position.X = gameWidth - paddleSizeX;
            }
            if (paddle.Collision.Position.X < 0f)
            {
                paddle.Collision.Position.X = 0f;
            }


            foreach (Object ball in balls)
            {
                ball.Update(gameTime);
            }

            List <Object> ballsToRemove = new List <Object>();

            foreach (Object ball in balls)
            {
                if (ball.Collision.CollidesWith(bottom.Collision))
                {
                    ballsToRemove.Add(ball);
                    continue;
                }
                if (ball.Collision.CollidesWith(paddle.Collision))
                {
                    ball.BounceOff(paddle);
                }
                List <Brick> toRemove = new List <Brick>();
                foreach (CollisionBody body in grid.GetPotentialCollisions(ball.Collision).Values)
                {
                    if (ball.Collision.CollidesWith(body))
                    {
                        Brick brick = ((Brick)body.Parent);
                        ball.BounceOff(brick);
                        brick.Health--;
                        if (brick.Health <= 0)
                        {
                            BrickDeathSound();
                            toRemove.Add(brick);
                            grid.Remove(body);
                        }
                        else
                        {
                            BrickHitSound();
                        }
                    }
                }
                foreach (Brick brick in toRemove)
                {
                    Powerup toSpawn = brick.Powerup;
                    if (toSpawn != null)
                    {
                        toSpawn.isVisible  = true;
                        toSpawn.Velocity.Y = powerUpVelocity;
                        powerups.Add(toSpawn);
                        ParticleSystem temp    = new ParticleSystem(GraphicsDevice, ParticleManager, "star");
                        Vector2        starpos = new Vector2(toSpawn.Collision.Position.X + toSpawn.Collision.Region().X / 2, toSpawn.Collision.Position.Y + toSpawn.Collision.Region().Y / 2);
                        temp.Emitter = starpos;
                        ParticleManager.Add(temp, toSpawn);
                    }
                    bricks.Remove(brick);
                    ParticleSystem p   = new ParticleSystem(GraphicsDevice, ParticleManager, "debris");
                    Vector2        pos = new Vector2(brick.Collision.Position.X + brick.Collision.Region().Size.X / 2, brick.Collision.Position.Y + brick.Collision.Region().Size.Y / 2);
                    p.Emitter = pos;
                    ParticleManager.Add(p, 1);
                }
                foreach (Object wall in walls)
                {
                    if (ball.Collision.CollidesWith(wall.Collision))
                    {
                        ball.BounceOff(wall);
                    }
                }
            }
            foreach (Object ball in ballsToRemove)
            {
                ParticleManager.Remove(ball);
                balls.Remove(ball);
            }

            List <Powerup> powerupsToRemove = new List <Powerup>();

            foreach (Powerup powerup in powerups)
            {
                powerup.Update(gameTime);
                if (powerup.Collision.CollidesWith(paddle.Collision))
                {
                    if (powerup.powerUpType == PowerUpType.ExtraBall)
                    {
                        SpawnBall(Powerup.ExtraBallCount, powerup.Collision.Position);
                        powerupsToRemove.Add(powerup);
                    }
                }
            }
            foreach (Powerup powerup in powerupsToRemove)
            {
                powerups.Remove(powerup);
                ParticleManager.Remove(powerup);
            }

            if (balls.Count == 0)
            {
                TakeLife();
            }

            if (bricks.Count == 0)
            {
                Winner   = true;
                GameOver = false;
            }

            ParticleManager.Update(gameTime);

            oldState = thisState;

            base.Update(gameTime);
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            newKeyboardState = Keyboard.GetState();

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

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

            paddle.Update(gameTime, 0);
            paddleRight.Update(gameTime, 1);
            ball.Update(gameTime);

            if (paddle.Bounds.CollidesWith(ball.Bounds))
            {
                ball.Velocity.X *= -1;
                var delta = (paddle.Bounds.X + paddle.Bounds.Width) - (ball.Bounds.X - ball.Bounds.Radius);
                ball.Bounds.X += 2 * delta;
            }

            if (paddleRight.Bounds.CollidesWith(ball.Bounds))
            {
                ball.Velocity.X *= -1;
                var delta = ball.Bounds.Radius;
                ball.Bounds.X -= delta;
            }

            // TODO: Add your update logic here

            oldKeyboardState = newKeyboardState;
            if (ball.LeftCount() == 1)
            {
                LeftScore = Content.Load <Texture2D>("One");
            }
            else if (ball.LeftCount() == 2)
            {
                LeftScore = Content.Load <Texture2D>("Twoa");
            }
            else if (ball.LeftCount() == 3)
            {
                LeftScore     = Content.Load <Texture2D>("Three");
                Scoreboard    = Content.Load <Texture2D>("LeftWinds");
                ball.Velocity = Vector2.Zero;
            }


            if (ball.RightCount() == 1)
            {
                RightScore = Content.Load <Texture2D>("One");
            }
            else if (ball.RightCount() == 2)
            {
                RightScore = Content.Load <Texture2D>("TwoA");
            }
            else if (ball.RightCount() == 3)
            {
                RightScore    = Content.Load <Texture2D>("Three");
                Scoreboard    = Content.Load <Texture2D>("RightWinds");
                ball.Velocity = Vector2.Zero;
            }

            base.Update(gameTime);
        }