public void move(OknoGry okno, Paddle belka) { if (visible) { x += speedX; y += speedY; if (x < 0) { x = 0; speedX *= -1; } else if (x + width > okno.Width) { x = okno.Width - width; speedX *= -1; } if (((x > belka.getX() && x < belka.getX() + belka.getWidth()) || (x + width > belka.getX() && x + width < belka.getX() + belka.getWidth())) && (y + height > belka.getY() && y + height < belka.getY() + belka.getHeight()))//warunek zderzenia z belka { applyBonus(bonus, belka, okno.pilka, okno.lista, okno); dezaktywuj(); } else if (y + height > okno.Height) { dezaktywuj(); } } }
/* * Instantiate GameCanvas and add game elements into it: ball, paddle and bricks. * */ private void setupGameField(int rows, int columns) { //Canvas size width 600 height 350 //Settting up canvas GameCanvas = new Canvas(); GameCanvas.Height = spCanvas.Height; GameCanvas.Width = spCanvas.Width; GameCanvas.Background = new SolidColorBrush(Colors.Black); spCanvas.Children.Add(GameCanvas); GameCanvas.Tapped += GameCanvas_Tapped; //Adding bricks 3.0 _bricks = new List <Brick>(); LevelBuilder lb = new LevelBuilder(); _bricks = lb.getNewRandomLevelLayout(rows, columns, GameCanvas); foreach (Brick brick in _bricks) { GameCanvas.Children.Add(brick.getBrick()); } //Adding Paddle 1.0 paddle = new Paddle((int)(GameCanvas.Width / 2) - 50, (int)(GameCanvas.Height) - 6, 100, 6); Canvas.SetLeft(paddle.getPaddle(), paddle.getX()); Canvas.SetTop(paddle.getPaddle(), paddle.getY()); GameCanvas.Children.Add(paddle.getPaddle()); //Adding Ball 1.0 ball = new Ball(paddle.getX() + (paddle.getWidth() / 2), paddle.getY() - 11, 10, 10); Canvas.SetLeft(ball.getBall(), ball.getX()); Canvas.SetTop(ball.getBall(), ball.getY()); GameCanvas.Children.Add(ball.getBall()); }
//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()); } } }