Skip to content

Commit

Permalink
Added randomness to ball bounce, still has some collision issues when…
Browse files Browse the repository at this point in the history
… ball moves fast
  • Loading branch information
kliao2016 committed Nov 13, 2017
1 parent caf746c commit 091a832
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
Binary file modified BrickBreaker.elf
Binary file not shown.
Binary file modified BrickBreaker.gba
Binary file not shown.
21 changes: 18 additions & 3 deletions game.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include "myLib.h"
#include "game.h"

Expand Down Expand Up @@ -109,6 +110,12 @@ void ballMovement(Ball *ballptr, Slider *sldptr, Brick *brptr, int *numBricks, i
*/
void handleCollisions(Ball *ballptr, Slider *sldptr) {

// int seed = 0;
// while (!KEY_DOWN_NOW(BUTTON_RIGHT)) {
// seed++;
// }
// srand(seed);

if (ballptr->col < 0) {
ballptr->col = 0;
ballptr->xDir *= -1;
Expand All @@ -130,6 +137,8 @@ void handleCollisions(Ball *ballptr, Slider *sldptr) {
&& ballptr->col <= (sldptr->col + SLIDERWIDTH)) {
ballptr->row = sldptr->row - BALLSIZE;
ballptr->yDir *= -1;
ballptr->xDir = (rand() % 2 + 1) * ballptr->xDir / abs(ballptr->xDir);
ballptr->yDir = (rand() % 2 + 1) * ballptr->yDir / abs(ballptr->yDir);
}

}
Expand Down Expand Up @@ -172,6 +181,12 @@ void generateBricks(Brick *brptr, int numBricks) {
*/
void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bricksSize) {

// int seed = 0;
// while (!KEY_DOWN_NOW(BUTTON_RIGHT)) {
// seed++;
// }
// srand(seed);

for (int i = 0; i < bricksSize; i++) {
Brick *cur = brptr + i;
if (cur->isHit) {
Expand All @@ -190,16 +205,16 @@ void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bric
cur->isHit = 1;
ballptr->xDir *= -1;
//ballptr->xDir = ballptr->xDir * ((*(numBricks) % 2) + 1);
ballptr->col = ballptr->col + (ballptr->xDir * 2);
} else if ((((ballptr->row + BALLSIZE) == brptr->row)
ballptr->col = ballptr->col + (ballptr->xDir * 1);
} else if ((((ballptr->row + BALLSIZE) == cur->row)
|| (ballptr->row == (cur->row + BRICKHEIGHT)))
&& ((ballptr->col >= (cur->col - BALLSIZE))
&& (ballptr->col <= (cur->col + BRICKWIDTH)))) {
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->yDir *= -1;
//ballptr->yDir = ballptr->yDir * ((*(numBricks) % 2) + 1);
ballptr->row = ballptr->row + (ballptr->yDir * 2);
ballptr->row = ballptr->row + (ballptr->yDir * 1);
}
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, cur->color);
}
Expand Down
Binary file modified game.o
Binary file not shown.
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int main() {

case GAME_OVER_NO_DRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
state = START;
resetGame();
enterPressed = 1;
}
backToMain();
Expand Down
Binary file modified main.o
Binary file not shown.

0 comments on commit 091a832

Please sign in to comment.