예제 #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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            var touchState = Keyboard.GetState();
            var bounds     = GraphicsDevice.Viewport.Bounds;

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, bounds.Left, bounds.Right - PaddleBottom.Width);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, bounds.Left, bounds.Right - PaddleBottom.Width);
            }
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleTop.X = MathHelper.Clamp(PaddleTop.X, bounds.Left, bounds.Right - PaddleTop.Width);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
                PaddleTop.X = MathHelper.Clamp(PaddleTop.X, bounds.Left, bounds.Right - PaddleTop.Width);
            }
            var ballPositionChange = ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * ball.Speed);

            ball.X += ballPositionChange.X;
            ball.Y += ballPositionChange.Y;

            // Ball - side walls
            if (Walls.Any(w => CollisionDetector.Overlaps(ball, w)))
            {
                ball.BounceX();
                ball.Acceleration();
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(ball, w)))
            {
                ball.X     = 225;
                ball.Y     = 460;
                ball.Speed = GameConstants.DefaultInitialBallSpeed;
                HitSound.Play();
            }
            // Paddle - ball collision
            if (CollisionDetector.Overlaps(ball, PaddleTop) && ball.Direction.Y < 0 ||
                (CollisionDetector.Overlaps(ball, PaddleBottom) && ball.Direction.Y > 0))
            {
                ball.BounceY();
                ball.Acceleration();
            }

            base.Update(gameTime);
        }
예제 #2
0
 private void ObjectItemCollision()
 {
     foreach (IObject o in objects)
     {
         Rectangle obj             = o.Position;
         bool      isFallingObject = true;
         foreach (IItems i in items)
         {
             Rectangle     item = i.Position;
             CollisionSide type = CollisionDetector.Detect(obj, item);
             if (type != CollisionSide.None)
             {
                 new ObjectItemCollisionResponse(o, i, type);
             }
             Rectangle copy = obj;
             copy.Offset(0, 1);
             CollisionSide bound = CollisionDetector.Detect(copy, item);
             if (bound == CollisionSide.Top)
             {
                 isFallingObject = false;
             }
         }
         if (isFallingObject)
         {
             o.Fall();
         }
         else
         {
             o.Land();
         }
     }
 }
예제 #3
0
        private void MarioItemCollision()
        {
            IItems        largestItem      = null;
            int           largestArea      = 0;
            CollisionSide largestCollision = CollisionSide.None;
            bool          isFalling        = true;

            for (int i = 0; i < items.Count; i++)
            {
                IItems item = items[i];

                Rectangle block = item.Position;

                Rectangle     mario = g.Character.Position;
                CollisionSide type  = CollisionDetector.Detect(mario, block);
                if (type != CollisionSide.None)
                {
                    if (largestItem == null)
                    {
                        Rectangle intersectRect = Rectangle.Intersect(mario, block);
                        largestItem      = item;
                        largestArea      = intersectRect.Width * intersectRect.Height;
                        largestCollision = type;
                    }
                    else
                    {
                        Rectangle testRect = Rectangle.Intersect(mario, block);

                        if (largestArea < (testRect.Width * testRect.Height))
                        {
                            largestItem      = item;
                            largestArea      = testRect.Width * testRect.Height;
                            largestCollision = type;
                        }
                    }
                }

                mario.Offset(0, 1);
                CollisionSide bound = CollisionDetector.Detect(mario, block);

                if (bound == CollisionSide.Top)
                {
                    isFalling = false;
                    if (item is TransitionPipeItem && g.Character.Movement.IsCrouching)
                    {
                        g.CurrentState = new HiddenLevelState(g, g.CurrentState, items);
                    }
                }
            }

            if (largestItem != null)
            {
                new MarioItemCollisionResponse(g.Character, largestItem, largestCollision, items, objects, g);
            }

            if (isFalling)
            {
                g.Character.Movement.Fall();
            }
        }
예제 #4
0
        public MarioNightmareEnemyCollisionDetector(Game1 g, INightmareEnemy n)
        {
            Rectangle mario = g.Character.Position;
            Rectangle nm    = n.Position;

            CollisionSide type = CollisionDetector.Detect(mario, nm);

            if (type != CollisionSide.None)
            {
                new MarioNighmareEnemyCollisionResponsecs(g.Character, n, type, g);
            }
        }
예제 #5
0
        public MarioNightmareEnemyCollisionDetector(Game1 g, FlyingEnemy n)
        {
            Rectangle mario = g.Character.Position;
            Rectangle nm    = new Rectangle((int)n.Position.X, (int)n.Position.Y, EnemyFactory.Instance.flyingEnemy.Height, EnemyFactory.Instance.flyingEnemy.Width / 2);

            CollisionSide type = CollisionDetector.Detect(mario, nm);


            if (type != CollisionSide.None)
            {
                new MarioNighmareEnemyCollisionResponsecs(g.Character, n, type, g);
            }
        }
예제 #6
0
        public PlayGameState(Game1 game, GraphicsDevice gd)
        {
            g              = game;
            Timer          = 200;
            graphicsDevice = gd;
            MediaPlayer.Play(g.marioTheme);
            //Add list initializations and collision detector initialization
            items       = ItemCreator.LoadItems();
            enemies     = EnemyCreator.LoadEnemy();
            projectiles = new List <IProjectile>();
            objects     = new List <IObject>();

            collisionDetect = new CollisionDetector(g.Character, items, enemies, projectiles, objects, g);
        }
예제 #7
0
        private void ProjectileMarioCollision()
        {
            Rectangle mario = g.Character.Position;

            for (int i = 0; i < projectiles.Count; i++)
            {
                IProjectile   p    = projectiles[i];
                Rectangle     proj = p.Position;
                CollisionSide type = CollisionDetector.Detect(proj, mario);
                if (type != CollisionSide.None)
                {
                    new ProjectileMarioCollisionResponse(g.Character, p, type);
                }
            }
        }
예제 #8
0
 private void ProjectileEnemyCollision()
 {
     foreach (IProjectile p in projectiles)
     {
         foreach (IEnemy e in enemies)
         {
             Rectangle     fireball      = p.Position;
             Rectangle     enemy         = e.Position;
             CollisionSide collisionType = CollisionDetector.Detect(fireball, enemy);
             if (collisionType != CollisionSide.None)
             {
                 new ProjectileEnemyCollsionResponse(p, e, collisionType, g.Character);
             }
         }
     }
 }
예제 #9
0
        private void MarioFlyingEnemyCollision()
        {
            for (int i = 0; i < flyingEnemy.Count; i++)
            {
                FlyingEnemy fe    = flyingEnemy[i];
                Rectangle   enemy = fe.Position;
                Rectangle   m     = g.Character.Position;

                CollisionSide cs = CollisionDetector.Detect(m, enemy);

                if (cs != CollisionSide.None)
                {
                    new MarioNightmareEnemyCollisionResponse(g.Character, fe, cs, g);
                }
            }
        }
예제 #10
0
        private void MarioZombieCollision()
        {
            for (int i = 0; i < nightmareZombie.Count; i++)
            {
                IZombie   z      = nightmareZombie[i];
                Rectangle zombie = z.Position;
                Rectangle m      = g.Character.Position;

                CollisionSide cs = CollisionDetector.Detect(m, zombie);

                if (cs != CollisionSide.None)
                {
                    new ZombieMarioCollisionResponse(z, g.Character, cs, g);
                }
            }
        }
예제 #11
0
        private void EnemyItemCollision()
        {
            ArrayList fallingEnemies = new ArrayList();

            foreach (INightmareEnemy e in enemies)
            {
                fallingEnemies.Add(e);
            }

            for (int i = 0; i < items.Count; i++)
            {
                IItems I1 = items[i];
                I1.HasEnemyOnIt = false;
                Rectangle item = I1.Position;

                foreach (INightmareEnemy e in enemies)
                {
                    Rectangle enemy = e.Position;
                    //enemy.Offset(0, -1);

                    CollisionSide type = CollisionDetector.Detect(enemy, item);
                    if (type != CollisionSide.None)
                    {
                        new NightmareEnemyItemCollisionResponse(e, I1, type, g);
                    }
                    Rectangle copy = enemy;
                    enemy.Offset(0, 3);
                    CollisionSide bound = CollisionDetector.Detect(enemy, item);

                    if (bound == CollisionSide.Top)
                    {
                        if (fallingEnemies.Contains(e))
                        {
                            fallingEnemies.Remove(e);
                        }
                        I1.HasEnemyOnIt = true;
                    }
                }
            }

            foreach (INightmareEnemy e in fallingEnemies)
            {
                e.Fall();
            }
        }
예제 #12
0
        private void ProjectileProjectileCollision()
        {
            for (int i = 0; i < projectiles.Count - 1; i++)
            {
                IProjectile p1    = projectiles[i];
                Rectangle   proj1 = p1.Position;
                for (int j = i; j < projectiles.Count; j++)
                {
                    IProjectile p2 = projectiles[j];

                    Rectangle     proj2 = p2.Position;
                    CollisionSide type  = CollisionDetector.Detect(proj1, proj2);
                    if (type != CollisionSide.None)
                    {
                        new ProjectileProjectileCollisionResponse(p1, p2, type);
                    }
                }
            }
        }
예제 #13
0
        private void MarioNightmareEnemyCollision()
        {
            for (int i = 0; i < nightmareEnemyList.Count; i++)
            {
                INightmareEnemy e = nightmareEnemyList[i];

                if (!e.IsKilled)
                {
                    Rectangle mario = g.Character.Position;
                    Rectangle enemy = e.Position;

                    CollisionSide type = CollisionDetector.Detect(mario, enemy);

                    if (type != CollisionSide.None)
                    {
                        new MarioNightmareEnemyCollisionResponse(g.Character, e, nightmareZombie, type, g);
                    }
                }
            }
        }
예제 #14
0
        private void MarioEnemyCollision()
        {
            for (int i = 0; i < enemies.Count; i++)
            {
                IEnemy e = enemies[i];

                if (!e.IsKilled)
                {
                    Rectangle mario = g.Character.Position;
                    Rectangle enemy = e.Position;

                    CollisionSide type = CollisionDetector.Detect(mario, enemy);

                    if (type != CollisionSide.None)
                    {
                        new MarioEnemyCollisionResponse(g.Character, e, type, enemies, projectiles, g);
                        Console.WriteLine(projectiles.Count);
                    }
                }
            }
        }
예제 #15
0
        private void ZombieEnemyCollisionDetect()
        {
            for (int i = 0; i < zombies.Count; i++)
            {
                IZombie   zombie = zombies[i];
                Rectangle z      = zombie.Position;

                for (int j = 0; j < nightmareEnemyList.Count; j++)
                {
                    INightmareEnemy ne = nightmareEnemyList[j];
                    Rectangle       n  = ne.Position;

                    CollisionSide cs = CollisionDetector.Detect(z, n);

                    if (cs != CollisionSide.None)
                    {
                        new ZombieNightmareEnemyCollisionResponse(zombie, ne, zombies, cs);
                    }
                }
            }
        }
예제 #16
0
        private void ProjectileItemCollision()
        {
            foreach (IProjectile p in projectiles)
            {
                IItems        largestItem = null;
                int           largestArea = 0;
                CollisionSide largestSide = CollisionSide.None;

                foreach (IItems i in items)
                {
                    Rectangle     projectile    = p.Position;
                    Rectangle     item          = i.Position;
                    CollisionSide collisionType = CollisionDetector.Detect(projectile, item);
                    if (collisionType != CollisionSide.None)
                    {
                        if (largestItem == null)
                        {
                            largestItem = i;
                            Rectangle tempRect = Rectangle.Intersect(projectile, item);
                            largestArea = tempRect.Width * tempRect.Height;
                            largestSide = collisionType;
                        }
                        else
                        {
                            Rectangle testIntersection = Rectangle.Intersect(projectile, item);
                            if ((largestArea) < (testIntersection.Width * testIntersection.Height))
                            {
                                largestArea = testIntersection.Width * testIntersection.Height;
                                largestItem = i;
                                largestSide = collisionType;
                            }
                        }
                    }
                }
                if (largestItem != null)
                {
                    new ProjectileItemCollisionResponse(p, largestItem, largestSide, items);
                }
            }
        }
예제 #17
0
 private void EnemyEnemyCollision()
 {
     for (int i = 0; i < enemies.Count - 1; i++)
     {
         INightmareEnemy e1 = enemies[i];
         if (!e1.IsKilled)
         {
             Rectangle enemy1 = e1.Position;
             for (int j = i; j < enemies.Count; j++)
             {
                 INightmareEnemy e2 = enemies[j];
                 if (!e2.IsKilled)
                 {
                     Rectangle     enemy2 = e2.Position;
                     CollisionSide type   = CollisionDetector.Detect(enemy1, enemy2);
                     if (type != CollisionSide.None)
                     {
                         new NightmareEnemyEnemyCollisionResponse(e1, e2, type);
                     }
                 }
             }
         }
     }
 }
예제 #18
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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            var touchState = Keyboard.GetState();

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed * gameTime.ElapsedGameTime.TotalMilliseconds);
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, 500 - PaddleBottom.Width);
            PaddleTop.X    = MathHelper.Clamp(PaddleTop.X, 0, 500 - PaddleTop.Width);

            var ballPositionChange = Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

            Ball.X += ballPositionChange.X;
            Ball.Y += ballPositionChange.Y;

            // return true if overlaps , false otherwise ...
            // Ball - side walls
            // If ball has collision with any of the side wall
            // Reverse X direction of the ball
            // Increase the ball speed by bump speed increase factor
            if (CollisionDetector.Overlaps(Ball, Walls[0]) || CollisionDetector.Overlaps(Ball, Walls[1]))
            {
                Ball.Direction = -Ball.Direction * new Vector2(-1, 1);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }
            // If ball has collision with winning walls ( goals )
            // Move ball to the center
            // Reset ball speed
            // Play hit sound with : HitSound . Play ();
            else if (CollisionDetector.Overlaps(Ball, Goals[0]) || CollisionDetector.Overlaps(Ball, Goals[1]))
            {
                Ball.X     = 250;
                Ball.Y     = 450;
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                HitSound.Play();
            }
            // If ball has collision with paddles ( with appropriate movement direction !!)
            // Reverse Y direction of the ball
            // Increase the ball speed by bump speed increase factor
            else if (CollisionDetector.Overlaps(Ball, PaddleBottom) || CollisionDetector.Overlaps(Ball, PaddleTop))
            {
                Ball.Direction = Ball.Direction * new Vector2(1, -1);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }

            base.Update(gameTime);
        }
예제 #19
0
파일: Game1.cs 프로젝트: hrga10/RAUPJC-DZ1
        /// <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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            var touchState = Keyboard.GetState();

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }

            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, graphics.PreferredBackBufferWidth - PaddleBottom.Width);
            PaddleTop.X    = MathHelper.Clamp(PaddleTop.X, 0, graphics.PreferredBackBufferWidth - PaddleTop.Width);

            var ballPositionChange = Ball.Direction * (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

            Ball.X += ballPositionChange.X;
            Ball.Y += ballPositionChange.Y;

            // Ball - side walls
            if (Walls.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.Direction = new Vector2(-Ball.Direction.X, Ball.Direction.Y);
                Ball.Speed     = Ball.Speed * Ball.BumpSpeedIncreaseFactor;
            }
            // Ball - winning walls
            if (Goals.Any(w => CollisionDetector.Overlaps(Ball, w)))
            {
                Ball.X     = GraphicsDevice.Viewport.Bounds.Center.X; // bounds.Center.ToVector2().X;
                Ball.Y     = GraphicsDevice.Viewport.Bounds.Center.Y; // bounds.Center.ToVector2().Y;
                Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                //HitSound.Play();
            }
            // Paddle - ball collision
            if (CollisionDetector.Overlaps(Ball, PaddleTop) && Ball.Direction.Y < 0 ||
                (CollisionDetector.Overlaps(Ball, PaddleBottom) && Ball.Direction.Y > 0))
            {
                Ball.Direction = new Vector2(Ball.Direction.X, -Ball.Direction.Y);
                Ball.Speed    *= Ball.BumpSpeedIncreaseFactor;
            }

            base.Update(gameTime);
        }
예제 #20
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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            var touchState = Keyboard.GetState();

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, GraphicsDevice.Viewport.Bounds.Left, GraphicsDevice.Viewport.Bounds.Right -
                                              PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, GraphicsDevice.Viewport.Bounds.Left, GraphicsDevice.Viewport.Bounds.Right -
                                           PaddleTop.Width);
            var ballPositionChange = Ball.Direction *
                                     (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

            Ball.X += ballPositionChange.X;
            Ball.Y += ballPositionChange.Y;

            foreach (Wall wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, PaddleTop))
                {
                    switch (Ball.Direction.BallDirection)
                    {
                    case GameVector.Direction.UpLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.DownLeft);
                        break;

                    case GameVector.Direction.UpRight:
                        Ball.Direction = new GameVector(GameVector.Direction.DownRight);
                        break;
                    }
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
                if (CollisionDetector.Overlaps(Ball, PaddleBottom))
                {
                    switch (Ball.Direction.BallDirection)
                    {
                    case GameVector.Direction.DownLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.UpLeft);
                        break;

                    case GameVector.Direction.DownRight:
                        Ball.Direction = new GameVector(GameVector.Direction.UpRight);
                        break;
                    }
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
                if (CollisionDetector.Overlaps(wall, Ball))
                {
                    switch (Ball.Direction.BallDirection)
                    {
                    case GameVector.Direction.DownLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.DownRight);
                        break;

                    case GameVector.Direction.UpLeft:
                        Ball.Direction = new GameVector(GameVector.Direction.UpRight);
                        break;

                    case GameVector.Direction.DownRight:
                        Ball.Direction = new GameVector(GameVector.Direction.DownLeft);
                        break;

                    case GameVector.Direction.UpRight:
                        Ball.Direction = new GameVector(GameVector.Direction.UpLeft);
                        break;
                    }
                    Ball.Speed *= Ball.BumpSpeedIncreaseFactor;
                }
            }
            foreach (Wall wall in Goals)
            {
                if (CollisionDetector.Overlaps(wall, Ball))
                {
                    Ball.X     = GraphicsDevice.Viewport.Bounds.Width / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Y     = GraphicsDevice.Viewport.Bounds.Height / 2f - GameConstants.DefaultBallSize / 2;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                    HitSound.Play();
                }
            }

            base.Update(gameTime);
        }
예제 #21
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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            var touchState   = Keyboard.GetState();
            var screenBounds = GraphicsDevice.Viewport.Bounds;

            if (touchState.IsKeyDown(Keys.Left))
            {
                PaddleBottom.X = PaddleBottom.X - (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.Right))
            {
                PaddleBottom.X = PaddleBottom.X + (float)(PaddleBottom.Speed *
                                                          gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.A))
            {
                PaddleTop.X = PaddleTop.X - (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }
            if (touchState.IsKeyDown(Keys.D))
            {
                PaddleTop.X = PaddleTop.X + (float)(PaddleTop.Speed *
                                                    gameTime.ElapsedGameTime.TotalMilliseconds);
            }

            PaddleBottom.X = MathHelper.Clamp(PaddleBottom.X, 0, screenBounds.Width -
                                              PaddleBottom.Width);
            PaddleTop.X = MathHelper.Clamp(PaddleTop.X, 0, screenBounds.Width -
                                           PaddleBottom.Width);

            var ballPositionChange = Ball.Direction *
                                     (float)(gameTime.ElapsedGameTime.TotalMilliseconds * Ball.Speed);

            Ball.X += ballPositionChange.X;

            Ball.Y += ballPositionChange.Y;

            foreach (var wall in Walls)
            {
                if (CollisionDetector.Overlaps(Ball, wall))
                {
                    Ball.Direction = Ball.Direction * new Vector2(-1, 1);
                    Ball.Speed     = Ball.Speed * GameConstants.DefaultBallBumpSpeedIncreaseFactor;
                    continue;
                }
            }

            foreach (var goal in Goals)
            {
                if (CollisionDetector.Overlaps(Ball, goal))
                {
                    HitSound.Play();
                    Ball.X     = screenBounds.Width / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Y     = screenBounds.Height / 2f - GameConstants.DefaultBallSize / 2f;
                    Ball.Speed = GameConstants.DefaultInitialBallSpeed;
                }
            }

            if (CollisionDetector.Overlaps(Ball, PaddleBottom) || CollisionDetector.Overlaps(Ball, PaddleTop))
            {
                Ball.Direction = Ball.Direction * new Vector2(1, -1);
                Ball.Speed     = Ball.Speed * GameConstants.DefaultBallBumpSpeedIncreaseFactor;
            }



            base.Update(gameTime);
        }