skip to main |
skip to sidebar
There are three levels in the game. Each level must be completed, by deleting all of the bricks in the level before the player can progress to the next level. Each level has a different brick design, to make the difference between the levels clear visually.
The levels also differ in the ease with which they are completed. This increases the difficulty rate of each level as the player progresses through the game.
Level 1 = Each brick hit once then it's deleted
Level 2 = Each brick hit twice then it's deletedLevel 3 = Each brick hit three times then it's deleted
Each time the ball and the brick collide the player receives 10 points. The 10 points are added to the score in the bottom-right of the game play window.
To understand how to create the High Score Board I needed to research this topic and how score boards are created for computer games. The scores need to be saved in a separate text file. The file is loaded into the structure of the memory at the beginning of the game.
I have structured the text files as follows:
Name (3 characters) Tab Score
To write the headings, “High Score Board”, “Name” and “Score” to the screen I used the sprintf code. I’ve also used the sprintf code in printing the text from the separate file to the screen.
To read the information from the separate file I have used the fscanf code. I began by pointing the computer to the relevant file and have created an integer variable to deal with the scores and a character variable to deal with the names. The code used then opens the relevant file and reads it: fileHandle – fopen(“highscores.txt”, “r”);
I then read the string, tab across, integer and create a new line. This points to the name and score strings. A copy of the name and score are then created. This process is repeated three times to then be used in displaying the names and scores of the three highest scorers.
The ball must be projected on to the paddle and the point at which the ball would have contacted the paddle and this point must be calculated. As the paddle has curved edges I tried to angle the edges of the paddle collision detection lines, however, having tried to use the intersection of two lines I could not get the collision detection process to work as desired. I have decided to extend the line across the top of the paddle, so that the collision occurs whenever the ball collides within the length of the paddle.
paddley = top edge of paddle in the y axis
Test:
paddley > position in y – radius
y1=paddley – ball position y + radius
t=y1/vely
x1=t*velx
Test condition, ensure ball hits between paddle edges:
previous ball position x + x1 > paddle position – paddle half width
&&
previous ball position x + x1 < paddle position + paddle half width
ball position x = previous ball position x + x1
ball position y = previous ball position y + y1
vely=-vely
ball position x = ball position x + (1-t)*velx
ball position y = ball position y + (1-t)*vely
Test:
maximum y < position in y + radius
y1=maximum y – ball position – radius
t=y1/vely
x1=t*velx
ball position x=previous ball position x + x1
ball position y=previous ball position y + y1
vely=-vely
ball position x=ball position x + (1-t)* velx
ball position y=ball position y + (1-t)* vely
x1=maximum x - ball position x – ball radius
t=x1/velx
y1=t*vely
ball position x=previous ball position x + x1
ball position y=previous ball position y + y1
velx=-velx
ball position x=ball position x + (1-t)* velx
ball position y=ball position y + (1-t)* vely
The intermediate point is the collision point.
ball position x = previous ball position x – x1
ball position y = previous ball position y + y1
ball position x = ball position x + (1-t)* velx //end ball position in x equals whole distance to be travelled minus distance travelled before intermediate point, multiplied by velocity in x
ball position y = ball position y + (1-t)* vely //end ball position in y equals whole distance to be travelled minus distance travelled before intermediate point, multiplied by velocity in y
Collision detection needs to be calculated, as the ball contacts and needs to rebound from the borders around the game board, the paddle and the bricks.
This works on the physics equation:
speed=distance/time
To calculate the movement of the ball after collision I need to know how much time it has taken to reach the collision, as a proportion of a time step. The remaining time in the time step after the collision is used by the bal
l travelling in the opposite direction.
x1=ball position x – minimum x + ball radius //calculate distance ball travelled before collision

t //amount of time step as proportion, between 0 and 1
y1 //distance travelled in y before collision
velx //total distance moved (real and projected) in x in time step
vely //total distance moved (real and projected) in y in time step
t=x1/velx //distance travelled in x in proportion to whole distance to be travelled in time step
y1=t*vely //calculate distance travelled in y before time step, multiply whole distance the ball would travel in time step by proportion already travelled
velx=-velx //reverse direction
The movement of the ball is based on the mathematical principles of trigonometry and Pythagoras theorem, so these principles were researched.
h = overall velocity of the ball (the hypotenuse)
x = x component of the ball velocity
y = y component of the ball velocity
0 = theta, angle of the path of the ball, set to a randomly generated number between 35o and 55o
x = sin 0 * h
y = cos 0 * h
Trigonometry:
Trigonometry can be used to calculate angles. In right-angled triangles, where there is a hypotenuse, the term SOHCAHTOA can be used:
- SOH = sine = opposite/hypotenuse
- CAH = cosine = adjacent/hypotenuse
- TOA = tan = opposite/adjacent
Pythagoras theorem:
In a right-angled triangle, the hypotenuse is equal to the sum of the other two sides. Often written as: a2+b2=c2
Random number generation:
For the ball to leave the paddle at the start of each game using a different angle, a random number between two set boundary numbers should be generated and control the ball’s initial movement. The boundary numbers are 35o and 55o as these were considered suitable angles between which to project the ball from the paddle.
To create a random number between these boundaries the srand code is implemented to initialise the random number generator, using processor clicks, as these values are different every time the program runs. The boundary angles are then set and the angle must be between these values, it is then multiplied by 2pi/360, and converted to radians. The ball velocity in x and y are then calculated and the remainder of the random number is divided by 2, giving an answer of 0 or 1, allowing the x component to be reversed, changing to direction the ball takes as it leaves the paddle.