Exemplo n.º 1
0
        //Checks ball position and updates it depending on whether collides or not
        private void updateBallPosition()
        {
            //Check if game is started
            if (isStarted)
            {
                //Check winning condition
                if (_bricks.Count <= 0)
                {
                    winningCondition = true;
                    isStarted        = false;
                }
                else
                {
                    //Get ball's position values
                    float xPos = ball.getX();
                    float yPos = ball.getY();

                    //Check collision against borders
                    if (xPos > GameCanvas.Width - ball.getWidth())
                    {
                        Canvas.SetLeft(ball.getBall(), GameCanvas.ActualWidth - ball.getWidth());
                        ball.setXVector(ball.getXVector() * -1);
                    }
                    if (xPos < 0)
                    {
                        Canvas.SetLeft(ball.getBall(), 0);
                        ball.setXVector(ball.getXVector() * -1);
                    }
                    if (yPos < 0)
                    {
                        Canvas.SetTop(ball.getBall(), 0);
                        ball.setYVector(ball.getYVector() * -1);
                    }
                    //Check bottom border
                    if (yPos > GameCanvas.Height - ball.getHeight())
                    {
                        gameOver();
                    }
                    //If ball is close enough to top border
                    if (ball.getY() < 150)
                    {
                        //Check collision againt every brick
                        foreach (Brick brick in _bricks)
                        {
                            //If collision occurs
                            if (brick.collides(ball.getHitBox()))
                            {
                                //Break brick
                                brick.Break();
                                //Determine effect
                                this.impactEffect(brick);
                                //Add Score
                                this.calculateScore();
                                //Remove if fully broken
                                if (brick.isBrickBroken())
                                {
                                    _bricks.Remove(brick);
                                    GameCanvas.Children.Remove(brick.getBrick());
                                }
                                //Determine ball direction depending of it hitting the side (left/right o top/bottom)
                                if (ball.getX() >= brick.getX() && ball.getX() <= brick.getX() + brick.getWidth() || (ball.getX() * ball.getHeight()) >= brick.getX() && (ball.getX() * ball.getHeight()) <= brick.getX() + brick.getWidth())
                                {
                                    ball.setYVector(ball.getYVector() * -1);
                                }
                                else if (ball.getY() >= brick.getY() && ball.getY() <= brick.getY() + brick.getHeight() || (ball.getY() * ball.getHeight()) >= brick.getY() && (ball.getY() * ball.getHeight()) <= brick.getY() + brick.getWidth())
                                {
                                    ball.setXVector(ball.getXVector() * -1);
                                }
                                break;
                            }
                        }
                    }
                    //If ball is near the bottom enough calculate collision against paddle
                    if (ball.getY() > 200)
                    {
                        if (paddle.collides(ball.getHitBox()))
                        {
                            ball.setY(paddle.getY() - 11);
                            ball.setYVector(ball.getYVector() * -1);
                        }
                    }

                    //Set Ball new psoition
                    xPos += ball.getXVector() * ball.getSpeed();
                    yPos += ball.getYVector() * ball.getSpeed();
                    ball.setX((int)xPos);
                    ball.setY((int)yPos);
                    Canvas.SetTop(ball.getBall(), ball.getY());
                    Canvas.SetLeft(ball.getBall(), ball.getX());
                }
            }
        }