/// <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() { // Screen bounds details . Use this information to set up game objects positions. var screenBounds = GraphicsDevice.Viewport.Bounds; PaddleBottom = new Paddle(GameConstants.PaddleDefaultWidth, GameConstants.PaddleDefaulHeight, GameConstants.PaddleDefaulSpeed); PaddleBottom.X = screenBounds.Width / 2f - PaddleBottom.Width / 2f; PaddleBottom.Y = screenBounds.Bottom - PaddleBottom.Height; PaddleTop = new Paddle(GameConstants.PaddleDefaultWidth, GameConstants.PaddleDefaulHeight, GameConstants.PaddleDefaulSpeed); PaddleTop.X = 0; PaddleTop.Y = 0; Ball = new Ball(GameConstants.DefaultBallSize, GameConstants.DefaultInitialBallSpeed, GameConstants.) { X = 0, Y = 0 }; Background = new Background(screenBounds.Width, screenBounds.Height); // Add our game objects to the sprites that should be drawn collection . SpritesForDrawList.Add(Background); SpritesForDrawList.Add(PaddleBottom); SpritesForDrawList.Add(PaddleTop); SpritesForDrawList.Add(Ball); base.Initialize(); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { // Start drawing . spriteBatch.Begin(); for (int i = 0; i < SpritesForDrawList.Count; i++) { SpritesForDrawList.GetElement(i).DrawSpriteOnScreen(spriteBatch); } // End drawing . // Send all gathered details to the graphic card in one batch . spriteBatch.End(); base.Draw(gameTime); }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); //Start drawing. spriteBatch.Begin(); for (int i = 0; i < SpritesForDrawList.Count; i++) { SpritesForDrawList.GetElement(i).Draw(spriteBatch); } // End drawing. // Send all gathered details to the graphic card in one batch. spriteBatch.End(); base.Draw(gameTime); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // Ask graphics device about screen bounds we are using. var screenBounds = GraphicsDevice.Viewport.Bounds; // Load paddle texture using Content.Load static method Texture2D paddleTexture = Content.Load <Texture2D>("paddle"); // Create bottom and top paddles and set their initial position PaddleBottom = new Paddle(paddleTexture); PaddleTop = new Paddle(paddleTexture); // Position both paddles with help screenBounds object PaddleBottom.Position = new Vector2(0, screenBounds.Bottom - PaddleBottom.Size.Height); PaddleTop.Position = new Vector2(screenBounds.Top); // Load ball texture Texture2D ballTexture = Content.Load <Texture2D>("ball"); // Create new ball object and set its initial position Ball = new Ball(ballTexture); Ball.Position = screenBounds.Center.ToVector2(); // Load background texture and create a new background object. Texture2D backgroundTexture = Content.Load <Texture2D>("background"); Background = new Background(backgroundTexture, screenBounds.Width, screenBounds.Height); // Load sounds HitSound = Content.Load <SoundEffect>("hit"); Music = Content.Load <Song>("music"); MediaPlayer.IsRepeating = true; // Start playing background music MediaPlayer.Play(Music); SpritesForDrawList.Add(Background); SpritesForDrawList.Add(PaddleBottom); SpritesForDrawList.Add(PaddleTop); SpritesForDrawList.Add(Ball); }
/// <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() { // Screen bounds details. Use this information to set up game objects positions. var screenBounds = GraphicsDevice.Viewport.Bounds; PaddleBottom = new Paddle(GameConstants.PaddleDefaultWidth, GameConstants.PaddleDefaulHeight, GameConstants.PaddleDefaulSpeed); PaddleBottom.X = GameConstants.ScreenWidth / 2 - GameConstants.PaddleDefaultWidth / 2; PaddleBottom.Y = GameConstants.ScreenHeight - GameConstants.PaddleDefaulHeight; PaddleTop = new Paddle(GameConstants.PaddleDefaultWidth, GameConstants.PaddleDefaulHeight, GameConstants.PaddleDefaulSpeed); PaddleTop.X = GameConstants.ScreenWidth / 2 - GameConstants.PaddleDefaultWidth / 2; PaddleTop.Y = 0; Ball = new Ball(GameConstants.DefaultBallSize, GameConstants.DefaultInitialBallSpeed, GameConstants.DefaultBallBumpSpeedIncreaseFactor) { X = GameConstants.ScreenWidth / 2 - GameConstants.DefaultBallSize / 2, Y = GameConstants.ScreenHeight / 2 - GameConstants.DefaultBallSize / 2 }; Background = new Background(GameConstants.ScreenWidth, GameConstants.ScreenHeight); Walls = new List <Wall>() { // try with 100 for default wall size! new Wall(-GameConstants.WallDefaultSize, 0, GameConstants.WallDefaultSize, screenBounds.Height), new Wall(screenBounds.Right, 0, GameConstants.WallDefaultSize, screenBounds.Height) }; Goals = new List <Wall>() { new Wall(0, screenBounds.Height, screenBounds.Width, GameConstants.WallDefaultSize), new Wall(screenBounds.Top, -GameConstants.WallDefaultSize, screenBounds.Width, GameConstants.WallDefaultSize), }; // Add our game objects to the sprites that should be drawn collection ..you ’ll see why in a second SpritesForDrawList.Add(Background); SpritesForDrawList.Add(PaddleBottom); SpritesForDrawList.Add(PaddleTop); SpritesForDrawList.Add(Ball); base.Initialize(); }