skip to main |
skip to sidebar
I have carried out some research into the use of sound in game and have decided to add sound to the game, to make playing the game a more interesting experience, as it involves more of the senses.
Times in the game that may benefit from sound:
- Ball and border collision
- Ball and paddle collision
- Ball and brick collision
- Brick explosion
Other times during the game when sound effects or music could be used:
- Losing a “life”
- Completing a level
- Congratulation state
- During the countdown
- Introduction to the game
The calculations shown in this blog are for the first row of bricks. The calculations are the same for each row, changing the number in the code, for example, bricks1 is used for the first row, bricks 2 for the second row and so on.
The collisions to the sides of the brick when the ball is travelling from above are the same as when the ball is travelling from below.
The code for this is similar to collision detection when the ball is travelling to the left, using i+1 instead of i-1, as used when the ball is travelling to the left.
x1 = (bricks1[i+1].posx – bricks1[i+1].halfwidth) – prevballx – gameball.radius + 4;
Again, check that the collision occurs between the top and bottom of the brick, add one to the hit count each time a brick is hit, add ten to the score when a brick is deleted, reverse the ball direction, calculate the final ball position and check if the level has been completed.
Collision on the side of a brick only occurs when the ball is travelling sideways and the brick next to it no longer exists. I need to create an if statement, which says if the ball is travelling to the left, but do not check if the brick being hit is the left-most brick (as there is no brick to the left of it to be hit), then check the side collision.
x1 = prevballx – (bricks1[i-1].posx + bricks1[i-1].halfwidth) – gameball.radius – 4;
t = (float)*1 / (float)gameball.velx;
y1 = (int) (t*(float)gameball.vely);
tempballposx = prevballx + x1;
tempballposy = prevbally + y1;
//Check that the collision is between the top and bottom of the brick
if (bricks1[i-1].posy + bricks1[i-1].halfheight + 7 >= tempballposy && bricks1[i-1].numberhits < level)
{
bricks1[i-1].numberhits ++; //Add one to hit count
if (bricks1[i-1].numberhits == level); //Set up to check if brick hits equals level number then a sound effect could be added later
}
score = score+10; //Add 10 to the score each time a brick is deleted
gameball.velx = - gameball.velx; //Reverse the direction of the ball
gameball.posx = tempballposx + (int) ((1.0-t)*(float)gameball.velx); //Calculate the final ball position
gameball.posy = - tempballposy + (int) ((1.0-t)*(float)gameball.vely); //Calculate the final ball position
checklevelcomplete(); //Check if the level has been completed
Meeting with Project Supervisor, Mohammad Ibrahim
Friday 19th June
2.15pm
I demonstrated a version of my work and talked Mohammad through the code. I answered questions relating to the working process and the code itself. We discussed possible directions for the project, such as potentially adding sound. I had not previously considered incorporating sound into the game, as I was not sure exactly how tight time constraints would be (having never programmed a game before), however, Mohammad believes that I am working and progressing well and may have time to add additional aspects to the game.
Collision detection to the top edge of the brick when the ball is travelling from above.
y1 = previous ball posy – bricks1[0].posy – bricks1[0].halfheight – radius – 1
t = y1/vely
x1 = t*velx
tempballposx = previous ball posx – x1
tempballposy = previous ball posy – y1
for (i=0; i<12; i++)
if (tempballposx >= bricks1[i].posx – halfwidth && tempballposx =< bricks1[i].posx + halfwidth)
if (bricks1[i].numberhits < level) //Check brick still exists
bricks1[i].numberhits++ //Add one to hit count
vely = - vely //Reverse ball direction
ballposx = tempballposx + (1-t)* velx //Check final ball position
ballposy = tempballposy + (1-t)* vely //Check final ball position
Collision detection to the base edge of the brick when the ball is travelling from below.
y1=(bricks1[0].posy-bricks1[0].halfheight) – previous ball position – radius – 1
t = y1/vely
x1 = t*velx
tempballposx = previous ball posx + x1
tempballposy = previous ball posy + y1
for (i=0; i<12;i++)
if (tempballposx>= bricks1[i].posx – halfwidth && tempballposx =< bricks1[i].posx + halfwidth)
if (bricks1[i].numberhits < level) //Check brick still exists
bricks1[i].number hits++ //Add one to hit count
vely = - vely //Reverse the ball direction
ballposx = tempballposx + (1-t)* velx //Check final ball position
ballposy = tempballposy + (1-t)* vely //Check final ball position
To create the collision detection on the bricks, I need to calculate a number of collision detection circumstances, these include:
- ball travelling from below, hits base of brick
- ball travelling from above, hits top of brick
- ball travelling from below, hits right side of brick
- ball travelling from below, hits left side of brick
- ball travelling from above, hits right side of brick
- ball travelling from above, hits left side of brick
Each brick needs properties for positioning, collision detection, design information and keeping a record of how many times each brick has been hit.
Each brick has the following properties:
- Position in x
- Position in y
- Width
- Height
- Half width
- Half height
- Number of times brick has been hit
- Main colour
- Darker colour
- Lighter colour
To draw each brick, set it’s position and three colours (to create the pseudo 3D effect), then draw it to the screen if the number of times it has been hit is lower than the level number.
pos x = position in x
pos y = position in y