//********************************************************************************************************************************************* // // Method Name: Particle // // Description: // TODO: Add description. // // Arguments: // theCoordinateX - The X-Coordinate to start the bullet at. // theCoordinateY - The Y-Coordinate to start the bullet at. // // Return: // N/A // //********************************************************************************************************************************************* public Particle(float theCoordinateX, float theCoordinateY, int theWidthAndLength, int theMinimumAngle, int theMaximumAngle, int theMilliseconds, Color theColor, BreakoutGame theBreakoutGame) { // Set the particle starting location based on the passed in parameters. mLocation = new PointF(theCoordinateX, theCoordinateY); // Set the how long the particle will live before being destroyed. mLiveTime = new TimeSpan(0, 0, 0, 0, theMilliseconds); // Create a velocity vector within the cone (between min and max angle). int angleDegrees = theBreakoutGame.RandomNumberGenerator.Next(theMinimumAngle, theMaximumAngle); mVelocity = new Vector2D((float)Math.Cos(angleDegrees), (float)Math.Sin(angleDegrees)); // TODO: Add particle size as parameter. mParticleSize = theWidthAndLength; // Hold the color information mColor = theColor; mCurrentAlpha = mColor.A; // The fade step is set by determining how many times an update will be called before the particle is destroyed and then dividing that // number by the starting alpha (transparency) to get the amount of fade to add per update. mFadeStride = mColor.A / (theMilliseconds / BreakoutConstants.TIMER_INTERVAL); }
//********************************************************************************************************************************************* // // Method Name: ExecutePowerUp // // Description: // Adds a additional ammunition for the paddle attachment, but does not exceed the maximum amount of ammunition. // // Arguments: // theBreakoutGame - Reference to the breakout game. // // Return: // N/A // //********************************************************************************************************************************************* public override void ExecutePowerUp(BreakoutGame theBreakoutGame) { if (theBreakoutGame.GunAmmunition < 35) { theBreakoutGame.GunAmmunition += 1; } }
//********************************************************************************************************************************************* // // Method Name: ExecutePowerUp // // Description: // Adds an additional life to the players pool of remaining lives, but does not exceed the maximum number of lives. // // Arguments: // theBreakoutGame - Reference to the breakout game. // // Return: // N/A // //********************************************************************************************************************************************* public override void ExecutePowerUp(BreakoutGame theBreakoutGame) { if (theBreakoutGame.NumberOfLives < 7) { theBreakoutGame.NumberOfLives += 1; } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { using (BreakoutGame game = new BreakoutGame()) { game.Run(); } }
//********************************************************************************************************************************************* // // Method Name: ExecutePowerUp // // Description: // Adds a new mini ball to the game centered to the paddle (directly above the paddle). // // Arguments: // theBreakoutGame - Reference to the breakout game. // // Return: // N/A // //********************************************************************************************************************************************* public override void ExecutePowerUp(BreakoutGame theBreakoutGame) { theBreakoutGame.MiniBalls.Add(new MiniBall(Image.FromFile("../../../Images/MiniBall.png"), theBreakoutGame.Paddle.HitBox.X + (theBreakoutGame.Paddle.HitBox.X / BreakoutConstants.HALF) - BreakoutConstants.MINI_BALL_WIDTH_AND_HEIGHT, theBreakoutGame.Paddle.HitBox.Y - BreakoutConstants.MINI_BALL_WIDTH_AND_HEIGHT)); }
//********************************************************************************************************************************************* // // Method Name: Destroyed // // Description: // When called on destruction of the brick, this method adds the hidden power up to the game power up list if the hidden power up exists. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void Destroyed(BreakoutGame theBreakoutGame) { // Check to see if a power up was hidden in this brick, and add it to the power up list if so. if (mPowerUp != null) { theBreakoutGame.GetPowerUpList().Add(mPowerUp); } for (int count = 0; count < 30; count++) { // Determine a random location within the bricks. int theRandomXPosition = theBreakoutGame.RandomNumberGenerator.Next((int)HitBox.X, (int)(HitBox.X + HitBox.Width)); int theRandomYPosition = theBreakoutGame.RandomNumberGenerator.Next((int)HitBox.Y, (int)(HitBox.Y + HitBox.Height)); // Add a new particle for the brick explosion at the new random location that travels at a random velocity in any direction. theBreakoutGame.Particles.Add(new Particle(theRandomXPosition, theRandomYPosition, BreakoutConstants.BRICK_EXPLOSION_WIDTH_AND_LENGTH, BreakoutConstants.BRICK_EXPLOSION_MINIMUM_ANGLE, BreakoutConstants.BRICK_EXPLOSION_MAXIMUM_ANGLE, BreakoutConstants.BRICK_EXPLOSION_TIME_MILLISECONDS, Color.FromArgb(255, 34, 177, 76), theBreakoutGame)); } }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnBricks // // Description: // Determines if the ball has collided with any bricks. If there is a case the ball will reverse velocity dependent on the edge of the brick // the ball hit. The brick will lose a level and be destroyed when the level drops below the destruction level. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void CheckBallCollisionOnBricks(BreakoutGame theBreakoutGame) { // Check if the ball hits a brick for (var index = 0; index < theBreakoutGame.Bricks.Count; index++) { if (mHitBox.IntersectsWith(theBreakoutGame.Bricks[index].HitBox)) { // Since the ball hit the brick, lower that bricks level. theBreakoutGame.Bricks[index].BrickLevel = theBreakoutGame.Bricks[index].BrickLevel - mDamage; // Check which side of the brick the ball collided and update the balls velocity. CheckRectangleEdgeCollision(theBreakoutGame.Bricks[index]); // Check if the brick level has hit the destroy level and remove the brick form the array list since it is destroyed. if (theBreakoutGame.Bricks[index].BrickLevel <= BreakoutConstants.BRICK_DESTRUCTION_LEVEL) { theBreakoutGame.Bricks[index].Destroyed(theBreakoutGame); // Remove the brick from the brick list since it is now destroyed. theBreakoutGame.Bricks.RemoveAt(index--); } // Since the brick is not destroyed, update the brick image. else { theBreakoutGame.Bricks[index].UpdateBrickImage("../../../Images/BrickLevel" + theBreakoutGame.Bricks[index].BrickLevel.ToString() + ".png"); } break; } } }
//********************************************************************************************************************************************* // // Method Name: StartScreenState // // Description: // TODO: Add description. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* public StartScreenState(BreakoutGame theBreakoutGame) { // Holds reference to the state machine. mBreakoutGame = theBreakoutGame; // Start the selection as the top enumeration (same as the top of list of selections on the screen). mSelection = Selection.NEW_GAME; }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnBottomBorder // // Description: // Check if the ball has collided past the bottom border and remove this mini ball from the game if so. // // Arguments: // theBreakoutGame - TODO: Add description. // // Return: // N/A // //********************************************************************************************************************************************* protected override void CheckBallCollisionOnBottomBorder(BreakoutGame theBreakoutGame) { // Check if the ball hits the bottom border and remove the mini ball from the list of active mini balls. if (mHitBox.Y > BreakoutConstants.SCREEN_PLAY_AREA_HEIGHT) { theBreakoutGame.MiniBallRemoveList.Add(this); } }
//********************************************************************************************************************************************* // // Method Name: Paddle // // Description: // Constructor for the paddle object. The paddle is created at the center of the screen with a slight padding away from the bottom border. // The paddle starts with no horizontal movement. // // Arguments: // theBreakoutGame - Reference to the game object. // // Return: // N/A // //********************************************************************************************************************************************* public Paddle(BreakoutGame theBreakoutGame) : base(Image.FromFile(BreakoutConstants.PADDLE_IMAGE_FILE), (BreakoutConstants.SCREEN_PLAY_AREA_WIDTH / BreakoutConstants.HALF) - (BreakoutConstants.PADDLE_WIDTH / BreakoutConstants.HALF), BreakoutConstants.SCREEN_PLAY_AREA_HEIGHT - BreakoutConstants.PADDLE_BOUNDARY_PADDING, new Vector2D(0.0F, 0.0F)) { mBreakoutGame = theBreakoutGame; // The paddle starts with no movement. mMovePaddleLeft = false; mMovePaddleRight = false; }
public void Run() { InitializePeripherals(); game = new BreakoutGame(128, 160); DrawText("Breakout!"); while (true) { GameLoop(); } }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnPaddle // // Description: // Determines if the ball has collided with the paddle. If the center of the ball is within the width of the paddle then ball will reverse // in its Y velocity. Otherwise the ball hit the side or corner of the paddle and the X and Y velocity are reversed. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void CheckBallCollisionOnPaddle(BreakoutGame theBreakoutGame) { // Check if the ball hits the paddle. if (mHitBox.IntersectsWith(theBreakoutGame.Paddle.HitBox)) { // Keep the ball above the paddle to avoid the ball getting stuck inside the paddle. mHitBox.Y = theBreakoutGame.Paddle.HitBox.Y - mHitBox.Height; mInnerHitDetection.Y = mHitBox.Y + mInnerHitPadding; // The Y (vertical) component is always reversed on a paddle hit. Vector.ReverseComponentY(); // Transfer a percentage of the paddles velocity to the ball. This helps the player to change to angle of the ball with the paddle. Vector.SetComponentX(Vector.ComponentX + (theBreakoutGame.Paddle.Vector.ComponentX * BreakoutConstants.PADDLE_VECTOR_TRANSFER_PERCENTAGE)); } }
//********************************************************************************************************************************************* // // Method Name: DetermineHiddenPowerUp // // Description: // This method determines if a power up is to be hidden where the brick is until the bricks destruction. If a power up is to be created the // brick generates a random power up. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void DetermineHiddenPowerUp(BreakoutGame theBreakoutGame) { // Use random number generator to potentially create a new power up. Random randomNumberGenerator = new Random(); // Give a specified chance a power up will drop. int nextRandomNumber = theBreakoutGame.RandomNumberGenerator.Next(BreakoutConstants.ZERO_PERCENT, BreakoutConstants.ONE_HUNDRED_PERCENT + BreakoutConstants.RANDOM_NUMBER_INCLUSION); if (nextRandomNumber <= BreakoutConstants.POWER_UP_DROP_PERCENT) { // Create a random power up. mPowerUp = theBreakoutGame.GetPowerUpFactory().GetPowerUp(HitBox, randomNumberGenerator.Next(BreakoutConstants.ZERO_PERCENT, BreakoutConstants.ONE_HUNDRED_PERCENT + BreakoutConstants.RANDOM_NUMBER_INCLUSION)); } }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnBottomBorder // // Description: // Check if the ball hits the bottom border and decrement the number of lives the player has left. If lives goes below the threshold for game // over, the game reverts back to the start screen. Otherwise a new match begins. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* protected override void CheckBallCollisionOnBottomBorder(BreakoutGame theBreakoutGame) { if (mHitBox.Y > BreakoutConstants.SCREEN_PLAY_AREA_HEIGHT) { theBreakoutGame.NumberOfLives = theBreakoutGame.NumberOfLives - BreakoutConstants.LIFE_LOST; // Check if the number of lives have been used up and end the game if so. if (theBreakoutGame.NumberOfLives < 0) { theBreakoutGame.NumberOfLives = BreakoutConstants.INITIAL_LIVES_REMAINING; theBreakoutGame.PopState(); } // The player still have spare lives to play so a new match is started. else { NewMatch(); theBreakoutGame.Paddle.NewMatch(); } } }
//********************************************************************************************************************************************* // // Method Name: PlayState // // Description: // TODO: Add description. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* public PlayState(BreakoutGame theBreakoutGame) { // Holds reference to the state machine. mBreakoutGame = theBreakoutGame; // Start the paddle and ball in position for a new match to start. mBreakoutGame.Paddle.NewMatch(); mBreakoutGame.Ball.NewMatch(); // Start the game at the level of a new game. mCurrentLevel = BreakoutConstants.NEW_GAME_LEVEL; // Remove any possible leftover bricks. mBreakoutGame.Bricks.Clear(); // Remove any possible leftover power ups. mBreakoutGame.PowerUps.Clear(); mBreakoutGame.MiniBalls.Clear(); mBreakoutGame.MiniBallRemoveList.Clear(); mBreakoutGame.Bullets.Clear(); // Load the starting level of the game. LoadLevel(mCurrentLevel); }
//********************************************************************************************************************************************* // // Method Name: Breakout // // Description: // Constructor to create the form components, kick off the game, indicate key input callbacks, and start timer call back for updating the // game. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* public BreakoutForm() { InitializeComponent(); // Start the breakout game. mBreakoutGame = new BreakoutGame(this); // Indicate the painting callback method to use when a repaint is called. this.Paint += Draw; // Indicate that key events will be used in this window. this.KeyPreview = true; // Indicate the callback method to use when a key is pressed down. this.KeyDown += BreakoutKeyDown; // Indicate the callback method to use when a key is released. this.KeyUp += BreakoutKeyUp; // Start up the periodic timer. mTimer = new Timer(); mTimer.Tick += new EventHandler(TimerTick); mTimer.Interval = BreakoutConstants.TIMER_INTERVAL; mTimer.Enabled = true; mTimer.Start(); }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnBottomBorder // // Description: // Abstract class all ball objects must implement for when they reach collision with the bottom border. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* protected abstract void CheckBallCollisionOnBottomBorder(BreakoutGame theBreakoutGame);
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnBorders // // Description: // Call to determine any collision from the ball against other game objects or play area borders. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void CheckBallCollisionOnBorders(BreakoutGame theBreakoutGame) { CheckBallCollisionOnTopBorder(); CheckBallCollisionOnSideBorders(); CheckBallCollisionOnBottomBorder(theBreakoutGame); }
static void Main() { using (var game = new BreakoutGame()) game.Run(); }
//********************************************************************************************************************************************* // // Method Name: PlayState // // Description: // TODO: Add description. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* public PauseState(BreakoutGame theBreakoutGame) { // Holds reference to the state machine. mBreakoutGame = theBreakoutGame; }
//********************************************************************************************************************************************* // // Method Name: ExecutePowerUp // // Description: // Abstract method that all concrete classes must implement to handle what the power up does upon the player collecting the power up. // // Arguments: // theBreakoutGame - Reference to the breakout game. // // Return: // N/A // //********************************************************************************************************************************************* public abstract void ExecutePowerUp(BreakoutGame theBreakoutGame);
//********************************************************************************************************************************************* // // Method Name: Brick // // Description: // Constructor that creates the brick object. The brick is placed at the specified location with the image being saved. The brick level is // set here. // // Arguments: // theImage - The image to retain for the object. // theCoordianteX - The initial X-Coordinate the object is at. // theCoordinateY - The initial Y-Coordinate the object is at. // theBrickLevel - The level the brick is at upon creation. // // Return: // N/A // //********************************************************************************************************************************************* public Brick(Image theImage, float theCoordinateX, float theCoordinateY, int theBrickLevel, BreakoutGame theBreakoutGame) : base(theImage, theCoordinateX, theCoordinateY, new Vector2D(0.0F, 0.0F)) { mBrickLevel = theBrickLevel; // Start the power up as null in case a power up is not hidden in this brick. mPowerUp = null; // Determine if this brick has a hidden power up and store the power up if so. DetermineHiddenPowerUp(theBreakoutGame); }