Algorithm:
- Create a class for the paddle, the ball, and the bricks
- In the paddle class:
- Include a constructor that will initialize the size and placement of the paddle
- Include a function that will redraw the paddle shifted to the left if the ‘<‘ button is hit, as well as redrawing the rest of the game
- Include a function that will redraw the paddle shifted to the right if the ‘>’ button is hit, as well as redrawing the rest of the game
- Create variables that will act as the position of the paddle that can be changed from the above functions
- In the ball class;
- Include a constructor that will initialize the radius and placement of the ball
- This constructor will also include the velocity at which the ball will travel, which is really just the rate at which the x and y coordinates that draw the ball will be changed
- It will also include the score that is taken from the ball hitting different objects and the time between screen refreshes, which will slowly decrease as the objects are hit more and more, causing the ball to appear to be moving faster.
- Include a function which will redraw the ball giving it the appearance of motion
- Include a function that will send the ball to the left or right depending on which direction was selected by the player
- Include a constructor that will initialize the radius and placement of the ball
- In the brick class;
- Include a constructor that will create the bricks that will be broken in the game
- These bricks will have the same width and height but will be placed at different x and y values
- Include a function that will draw the bricks in their starting positions
- Include a constructor that will create the bricks that will be broken in the game
- In the paddle class:
- In the main function, initialize a game variable to be able to turn the game off and on, a high-score variable which will always be set to 0 when the game turns on, and a counter variable
- Create a do-while loop which all of the game programs will be run in
- Clear the screen in preparation for the main menu
- Write out the different options that the user can choose from, being ‘Play’, ‘High Score’, ‘Instruction Manual’, and ‘Credits’
- Draw rectangles so that the user knows where to tap to access each part of the program
- Wait for the user’s touch and save where he touched the screen
- Use this knowledge to see which rectangle was touched and to then run the program linked to that rectangle
- If the ‘Play’ button was touched:
- Clear the screen
- Construct an object called paddle from the paddle class
- Construct an object called sphere from the ball class
- Draw the buttons that the user will be pressing to move the paddle from side to side
- Draw a line which act as the ceiling for the ball
- Label which boxes do what, including a box which will allow the user to quit out of the game
- Construct an object called Bricky from the class brick
- Draw the bricks into the screen
- Create a while loop which checks to make sure a variable called play is equal to 1
- Create an if statement which checks to see if the game has been touched once the while loop has began. If it hasn’t, run the function Initial Touch which will set an x and y velocity on the ball depending on which side of the screen is touched.
- Then set touches = 1 so that this little if statement won’t run again when the loop repeats
- Create another while loop to check that play is still equal to 1
- Create another if-statement that will see if the screen has been touched
- If it hasn’t been touched, call the redraw ball function
- If the screen has been touched, depending on which button was pressed, call the block placement function
- If the ‘High Score’ button was touched:
- Clear the screen
- Write to the screen what the high score is
- If the ‘Instruction Manual’ button was pressed:
- Clear the screen
- Write to the screen the controls, the goal, and in general, how to play the game brick breaker
- Explain the scoring system and how difficulty increases as score increases
- Create a while loop which allow the user to go to the previous screen if they hit the back button
- Wait for a touch and save the coordinates of the touch
- If the touch was within the box that allows the user to leave the page, exit the function and go back to the main menu
- If the ‘Credits’ button was selected:
- Clear the screen
- Write to the screen who created the game as well as any acknowledgements and inspirations behind creating the game. Thank who you want for the help and guidance
- Repeat steps 4-6 so that the user can leave the screen and go back to the main menu when they wish to
- At the end of the do-while loop, include the while check to make sure that the program should run again (which it should, as the program should always go back to the main menu unless it is turned off)
- Create the block constructor:
- Include the x and y coordinate of the constructor and draw the initial space for the paddle
- Create the ball constructor:
- Set the x and y coordinates of the ball
- Set the radius of the ball
- Draw the ball
- Initialize the x and y velocity of the ball and the maximum and minimum positions that the ball can go, as well as the wait time between changing the balls position and the score that the player gets from hitting the ball against the bricks or paddle.
- Create the brick constructor:
- Initialize the width, height, and x and y positions of the bricks
- Define the function to draw bricks
- Iterate from 0 to 21 to draw the bricks
- If the iterator < 11, the brick will be drawn in the top row, and calculations for the brick’s coordinates are done accordingly
- If the iterator = 11, the brick will be drawn in the first position of the bottom row, and calculations for the brick’s coordinates are done accordingly
- If the iterator > 11, the brick will be drawn in the bottom row, and calculations for the brick’s coordinates are done accordingly
- Only draws each brick if the value of the corresponding ‘broken’ counter has not been changed to 1 yet
- Iterate from 0 to 21 to draw the bricks
- Define the function to draw the paddle , and this function will call two other functions which will redraw the paddle (and everything else) to the right or to the left depending on which button is pressed
- Define the functions that draw the paddle to the left and to the right
- Since it is impossible to only redraw the rectangle, make sure that all things on the screen are redrawn each time
- Define the function that will initially send the ball to the left or right depending on the user’s input at the very beginning of each game
- This is mainly done by setting the x and y velocity of the ball
- Define the function that redraws the ball each time
- This is done by changing the position of the ball by the x and y velocities
- Include if statements that will change the x and y velocity of the ball based on if a ball hits a brick, hits a wall, or hits the paddle, as well as increasing the speed of the ball
- If the ball passes the paddle and the y-max position, clear the screen and write a game-over and thanks-for-playing statements as well as displaying the score achieved from that game
- If the ball touches the bottom of a block switch the velocity so it ‘bounces’ and change the counter of that brick so it is not longer drawn or able to be bounced off. Also increase the score by 1