/// <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.Black); spriteBatch.Begin(); _paddle.Draw(); _wall.Draw(); _gameBorder.Draw(); if (_ball.Visible) { bool inPlay = _ball.Move(_wall, _paddle); if (inPlay) { _ball.Draw(); } else { _remainingBalls--; _readyToServeBall = true; } } _staticBall.Draw(); string scoreMsg = "Score: " + _ball.Score.ToString("00000"); Vector2 space = gameContent.LabelFont.MeasureString(scoreMsg); spriteBatch.DrawString(gameContent.LabelFont, scoreMsg, new Vector2((_screenWidth - space.X) / 2, _screenHeight - 40), Color.White); if (_ball.bricksCleared >= 70) { _ball.Visible = false; _ball.bricksCleared = 0; _wall = new _wall(1, 50, spriteBatch, gameContent); _readyToServeBall = true; } if (_readyToServeBall) { if (_remainingBalls > 0) { string startMsg = "Press <Space> or Click Mouse to Start"; Vector2 startSpace = gameContent.LabelFont.MeasureString(startMsg); spriteBatch.DrawString(gameContent.LabelFont, startMsg, new Vector2((_screenWidth - startSpace.X) / 2, _screenHeight / 2), Color.White); } else { string endMsg = "Game Over"; Vector2 endSpace = gameContent.LabelFont.MeasureString(endMsg); spriteBatch.DrawString(gameContent.LabelFont, endMsg, new Vector2((_screenWidth - endSpace.X) / 2, _screenHeight / 2), Color.White); } } spriteBatch.DrawString(gameContent.LabelFont, _remainingBalls.ToString(), new Vector2(40, 10), Color.White); spriteBatch.End(); base.Draw(gameTime); }
private void ServeBall() { if (_remainingBalls < 1) { _remainingBalls = 3; _ball.Score = 0; _wall = new _wall(1, 50, spriteBatch, gameContent); } _readyToServeBall = false; float ballX = _paddle.X + (_paddle.Width) / 2; float ballY = _paddle.Y - _paddle.Height; _ball.Launch(ballX, ballY, -3, -3); }
/// <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); gameContent = new GameContent(this.Content); _screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; _screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; /* Set the game to 502x700 or screen max if smaller */ if (_screenWidth >= 502) { _screenWidth = 502; } if (_screenHeight >= 700) { _screenHeight = 700; } graphics.PreferredBackBufferWidth = _screenWidth; graphics.PreferredBackBufferHeight = _screenHeight; graphics.ApplyChanges(); /* Create game objects */ int paddleX = (_screenWidth - gameContent.PaddleImg.Width) / 2; // Center the image on the start int paddleY = _screenHeight - 100; // Paddle will be 100px from the bottom of the screen _paddle = new Paddle(paddleX, paddleY, _screenWidth, spriteBatch, gameContent); _wall = new _wall(1, 50, spriteBatch, gameContent); _gameBorder = new GameBorder(_screenWidth, _screenHeight, spriteBatch, gameContent); _ball = new Ball(_screenWidth, _screenHeight, spriteBatch, gameContent); _staticBall = new Ball(_screenWidth, _screenHeight, spriteBatch, gameContent); _staticBall.X = 25; _staticBall.Y = 25; _staticBall.Visible = true; _staticBall.UseRotation = false; MediaPlayer.Play(gameContent.MusicList[0]); MediaPlayer.Volume = 0.3f; }
public bool Move(_wall _wall, Paddle paddle) { if (!Visible) { return(false); } X = X + XVelocity; Y = Y + YVelocity; /* Check for _wall hits */ if (X < 1) { X = 1; XVelocity *= -1; PlaySound(gameContent.WallBounceSound); } if (X > ScreenWidth - Width + 5) { X = ScreenWidth - Width + 5; XVelocity *= -1; PlaySound(gameContent.WallBounceSound); } if (Y < 1) { Y = 1; YVelocity *= -1; PlaySound(gameContent.WallBounceSound); } if (Y + Height > ScreenHeight) { Visible = false; Y = 0; PlaySound(gameContent.MissSound); return(false); } /* Check for paddle hits * Paddle is 70 pixels. We'll divide it into segments that will determine the angle of the bounce. */ Rectangle paddleRect = new Rectangle((int)paddle.X, (int)paddle.Y, (int)paddle.Width, (int)paddle.Height); Rectangle ballRect = new Rectangle((int)X, (int)Y, (int)Width, (int)Height); if (HitTest(paddleRect, ballRect)) { PlaySound(gameContent.PaddleBounceSound); int offset = Convert.ToInt32((paddle.Width - (paddle.X + paddle.Width - X + Width / 2))); offset /= 5; if (offset < 0) { offset = 0; } switch (offset) { case 0: XVelocity = -6; break; case 1: XVelocity = -5; break; case 2: XVelocity = -4; break; case 3: XVelocity = -3; break; case 4: XVelocity = -2; break; case 5: XVelocity = -1; break; case 6: XVelocity = 1; break; case 7: XVelocity = 2; break; case 8: XVelocity = 3; break; case 9: XVelocity = 4; break; case 10: XVelocity = 5; break; default: XVelocity = 6; break; } YVelocity *= -1; Y = paddle.Y - Height + 1; return(true); } bool hitBrick = false; for (int i = 0; i < 7; i++) { if (!hitBrick) { for (int j = 0; j < 10; j++) { Brick brick = _wall.BrickWall[i, j]; if (brick.Visible) { Rectangle brickRect = new Rectangle((int)brick.X, (int)brick.Y, (int)brick.Width, (int)brick.Height); if (HitTest(ballRect, brickRect)) { PlaySound(gameContent.BrickSound); brick.Visible = false; Score += 7 - i; YVelocity *= -1; bricksCleared++; hitBrick = true; break; } } } } } return(true); }