/* * 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()); }
private void updatePaddlePosition(AccelerometerReading reading) { // use the AccelerationY: if < -0.04 move right // if >0.04 move left //Check if is started if not, the ball will move along the paddle if (isStarted) { if (reading.AccelerationY > 0.04) { if (!((double)paddle.getPaddle().GetValue(Canvas.LeftProperty) <= 0)) { // move left paddle.setX(paddle.getX() - increment); Canvas.SetLeft(paddle.getPaddle(), paddle.getX()); } } else if (reading.AccelerationY < -0.04) { if (!((double)paddle.getPaddle().GetValue(Canvas.LeftProperty) >= (GameCanvas.ActualWidth - paddle.getPaddle().Width))) { // move right paddle.setX(paddle.getX() + increment); Canvas.SetLeft(paddle.getPaddle(), paddle.getX()); } } } else { if (reading.AccelerationY > 0.1) { if (!((double)paddle.getPaddle().GetValue(Canvas.LeftProperty) <= 0)) { // move left ball.setX(ball.getX() - increment); Canvas.SetLeft(ball.getBall(), ball.getX()); paddle.setX(paddle.getX() - increment); Canvas.SetLeft(paddle.getPaddle(), paddle.getX()); } } else if (reading.AccelerationY < -0.1) { if (!((double)paddle.getPaddle().GetValue(Canvas.LeftProperty) >= (GameCanvas.ActualWidth - paddle.getPaddle().Width))) { // move right ball.setX(ball.getX() + increment); Canvas.SetLeft(ball.getBall(), ball.getX()); paddle.setX(paddle.getX() + increment); Canvas.SetLeft(paddle.getPaddle(), paddle.getX()); } } } }