context: using Microsoft.Phone.Tasks;
  using Microsoft.Xna.Framework;
  using Microsoft.Xna.Framework.Audio;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework.Input;
  using Microsoft.Xna.Framework.Input.Touch;
  using Microsoft.Xna.Framework.Media;
  using System;
  using System.Collections.Generic;
  
  namespace Break_em_All
  {
      enum GameState { START, RUNNING, END }
  
      /// <summary>
      /// This is the main type for your game
      /// </summary>
      public class Game1 : Microsoft.Xna.Framework.Game
      {
          private GraphicsDeviceManager graphics;
          private SpriteBatch spriteBatch;
  
          private Paddle paddleObj;
          private Ball ballObj;
  
          int numBricksInX;
          int numBricksInY;
          private Texture2D brickTexture;
          private Brick[,] bricksArray;
          private float opacity = 0.7f;
  
          private bool isMoreIconExpanded = false;
          private bool isSound = true;
          private bool isGamePaused = false;
          private bool isInfoPaneVisible = false;
          private Rectangle infoPaneRect;
  
          // Title as shown in the start screen
          private Texture2D title;
  
          #region Icons and their bounding rectangles
          // Play icon shown in start screen and pause pane.
          private Texture2D playIcon;
          private Rectangle playIconStartScreenRect;
  
          // More icons shown in start and end screen.
          private Texture2D moreIcon;
          private Rectangle moreIconRect;
          private Texture2D soundOnIcon;
          private Texture2D soundOffIcon;
          private Rectangle soundIconRect;
          private Texture2D infoIcon;
          private Rectangle infoIconRect;
          private Texture2D rateIcon;
          private Rectangle rateIconRect;
  
          // Pause icon as shown during the game play.
          private Texture2D pauseIcon;
          private Rectangle pauseIconRect;
  
          // Icons shown in the pause pane. Play and sound are already mentioned above.
          private Texture2D resumeIcon;
          private Texture2D replayIcon;
          private Rectangle replayIconPausePaneRect;
          private Rectangle resumeIconRect;
          private Rectangle soundIconPausePaneRect;
  
          // Icon for end screen.
          private Rectangle replayIconEndScreenRect;
          #endregion
  
          private Rectangle pausePaneRect;
          private Texture2D blackTexture;
          private Texture2D whiteTexture;
          private Texture2D creditsTexture;
  
          public static Rectangle gameContentRect;
          Rectangle adUnitRect;
          private int gameScore;
  
          // The font used to display UI elements
          private SpriteFont font24, font36;
  
          private GameState gameState;
  
          private Background backgroundObj;
  
          // Used for adding new bricks every x seconds.
          private TimeSpan oldElapsedGameTime;
  
          private SoundEffect ballPaddleCollisionSound;
          private SoundEffect brickBallCollisionSound;
  
          //// Advertisement
          //private static readonly string applicationIdStr = "test_client";
          //private static readonly string adUnitIdStr = "Image320_50"; //other test values: Image480_80, Image300_50, TextAd
          private static readonly string applicationIdStr = "3179fba6-21f8-412c-a41a-081a160cc475";
          private static readonly string adUnitIdStr = "10736295"; //other test values: Image480_80, Image300_50, TextAd
          private Advertisement advertisement;
  
          private HighScores highScores;
  
          public Game1()
          {
              graphics = new GraphicsDeviceManager(this);
              Content.RootDirectory = "Content";
  
              // Frame rate is 30 fps by default for Windows Phone.
              //TargetElapsedTime = TimeSpan.FromTicks(333333);
  
              // Extend battery life under lock.
              //InactiveSleepTime = TimeSpan.FromSeconds(1);
  
              //graphics.PreferredBackBufferWidth = 750;
              //graphics.PreferredBackBufferHeight = 600;
  
          }
  
          /// <summary>
          /// Allows the game to perform any initialization it needs to before starting to run.
          /// This is where it can query for any required services and load any non-graphic
          /// related content.  Calling base.Initialize will enumerate through any components
          /// and initialize them as well.
          /// </summary>
          protected override void Initialize()
          {
              TouchPanel.EnabledGestures = GestureType.Tap | GestureType.HorizontalDrag;
  
              Advertisement.Initialize(this, applicationIdStr);
  
              this.highScores = new HighScores();
  
              // Create a new SpriteBatch, which can be used to draw textures.
              this.spriteBatch = new SpriteBatch(GraphicsDevice);
  
              // TODO: Hide notification center if tried to open directly.
  
              base.Initialize();
          }
  
          //void GameActivated(object sender, ActivatedEventArgs e)
          //{
          //    bug.Position = (Vector2)(PhoneApplicationService.Current.State["BugPos"]);
          //    bug.Rotation = (float)(PhoneApplicationService.Current.State["BugRot"]);
          //    bug.Target = (Vector2)(PhoneApplicationService.Current.State["BugTarget"]);
          //    bug.Moving = (bool)(PhoneApplicationService.Current.State["BugMoving"]);
          //    foodLocations = (List<Vector2>)(PhoneApplicationService.Current.State["foodLocations"]);
          //    gameIsPaused = true;
          //    pausedImageOnScreen = false;
          //}
  
          protected override void OnDeactivated(Object sender, EventArgs args)
          {
              if (this.gameState == GameState.RUNNING)
              {
                  this.isGamePaused = true;
              }
          }
  
          //void GameDeactivated(object sender, DeactivatedEventArgs e)
          //{
          //    PhoneApplicationService.Current.State["BugPos"] = bug.Position;
          //    PhoneApplicationService.Current.State["BugRot"] = bug.Rotation;
          //    PhoneApplicationService.Current.State["BugTarget"] = bug.Target;
          //    PhoneApplicationService.Current.State["BugMoving"] = bug.Moving;
          //    PhoneApplicationService.Current.State["foodLocations"] = foodLocations;
          //}
  
          /// <summary>
          /// LoadContent will be called once per game and is the place to load
          /// all of your content.
          /// </summary>
          protected override void LoadContent()
          {
              this.title = Content.Load<Texture2D>("title single line");
              this.playIcon = Content.Load<Texture2D>("Icons/play256");
  
              this.moreIcon = Content.Load<Texture2D>("Icons/more64");
              this.infoIcon = Content.Load<Texture2D>("Icons/info64");
              this.rateIcon = Content.Load<Texture2D>("Icons/rate64");
              this.soundOnIcon = Content.Load<Texture2D>("Icons/soundOn64");
              this.soundOffIcon = Content.Load<Texture2D>("Icons/soundOff64");
  
              this.pauseIcon = Content.Load<Texture2D>("Icons/pause64");
              this.resumeIcon = Content.Load<Texture2D>("Icons/play256");
              this.replayIcon = Content.Load<Texture2D>("Icons/replay128");
  
              this.blackTexture = Content.Load<Texture2D>("Colors/black");
              this.whiteTexture = Content.Load<Texture2D>("Colors/white");
              this.creditsTexture = Content.Load<Texture2D>("credits");
  
              this.ballPaddleCollisionSound = Content.Load<SoundEffect>("sound/beep1");
              this.brickBallCollisionSound = Content.Load<SoundEffect>("sound/beep6");
              this.font24 = Content.Load<SpriteFont>("font/Kootenay24");
              this.font36 = Content.Load<SpriteFont>("font/Kootenay36");
              this.brickTexture = Content.Load<Texture2D>("brickTranslucent");
  
              // Initialize dimensions fo game content. brickTexture.Width is used here as gameContent.Width should be N * brickTexture.Width.
              var availScreenWidthForGame = this.graphics.PreferredBackBufferWidth;
              var paddingInDirectionX = availScreenWidthForGame % this.brickTexture.Width; // This value of padding will be divided on either side.
              var actualScreenWidthUsedForGame = availScreenWidthForGame - paddingInDirectionX;
              var availScreenHeightForGame = this.graphics.PreferredBackBufferHeight;
              Game1.gameContentRect = new Rectangle( // TODO: center the rectangle.
               (paddingInDirectionX / 2) + (paddingInDirectionX % 2), 0, // position on screen from where onwards (inclusive), game components could be drawn.
               actualScreenWidthUsedForGame,
               availScreenHeightForGame);
              // Now create an actual ad for display.
              // Create a banner ad for the game. Advertisement is shown below the game content.
              int adUnitWidth = 320, adUnitHeight = 50;
              adUnitRect = new Rectangle(
                  (GraphicsDevice.Viewport.Bounds.Width - adUnitWidth) / 2, // centered on the display
                  Game1.gameContentRect.Y + Game1.gameContentRect.Height - adUnitHeight,
                  adUnitWidth, adUnitHeight);
              this.advertisement = new Advertisement(adUnitRect, applicationIdStr, adUnitIdStr);
  
              Texture2D paddleTexture = Content.Load<Texture2D>("paddleTranslucent");
              this.paddleObj = new Paddle(paddleTexture, Game1.gameContentRect);
  
              Texture2D ballTexture = Content.Load<Texture2D>("ball");
              this.ballObj = new Ball(ballTexture, Game1.gameContentRect);
  
              //initialize number of brick slots on the screen.
              this.numBricksInY = (Game1.gameContentRect.Height - paddleTexture.Height - ballTexture.Height) / this.brickTexture.Height;
              this.numBricksInX = Game1.gameContentRect.Width / this.brickTexture.Width;
  
              Texture2D backgroundTexture = Content.Load<Texture2D>("stars256x256");
              Rectangle backgroundTextureBounds = new Rectangle(0, 0, backgroundTexture.Width, backgroundTexture.Height);
              backgroundObj = new Background(backgroundTexture, backgroundTextureBounds);
  
              Song song = Content.Load<Song>("Sound/Vegas Glitz");  // Put the name of your song in instead of "song_title"
              MediaPlayer.IsRepeating = true;
              MediaPlayer.Play(song);
  
              this.gameState = GameState.START;
  
              StartGame();
          }
  
          /// <summary>
          /// updated these with start values: paddle, ball and bricks. 
          /// </summary>
          private void StartGame()
          {
              paddleObj.SetInStartPosition();
              ballObj.SetInStartPosition(paddleObj.GetBounds());
  
              bricksArray = new Brick[numBricksInX, numBricksInY];
  
gt:             for (int y = 0; y < numBricksInY; y++)