Skip to content

Commit

Permalink
Added better random function for ball bounces
Browse files Browse the repository at this point in the history
  • Loading branch information
kliao2016 committed Nov 13, 2017
1 parent 9fa5188 commit ae44e9b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
Binary file modified BrickBreaker.elf
Binary file not shown.
Binary file modified BrickBreaker.gba
Binary file not shown.
9 changes: 6 additions & 3 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ void handleCollisions(Ball *ballptr, Slider *sldptr) {
&& ballptr->col >= (sldptr->col - BALLSIZE)
&& ballptr->col <= (sldptr->col + SLIDER_WIDTH)) {
ballptr->row = sldptr->row - BALLSIZE;
ballptr->yDir *= -1;
ballptr->yDir = ballptr->yDir / ballptr->yDir * -1;
int dirFactor = (rand() % 3) - 1;
if (dirFactor != 0) {
ballptr->xDir = ballptr->xDir * dirFactor;
ballptr->yDir = ballptr->yDir * dirFactor;
ballptr->xDir = (ballptr->xDir / abs(ballptr->xDir)) * dirFactor;
}
}

Expand Down Expand Up @@ -196,13 +195,17 @@ void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bric
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->xDir *= -1;
ballptr->xDir = ((rand() % 2) + 1) * ballptr->xDir / abs(ballptr->xDir);
ballptr->yDir = ((rand() % 2) + 1) * ballptr->yDir / abs(ballptr->yDir);
} 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->xDir = ((rand() % 2) + 1) * ballptr->xDir / abs(ballptr->xDir);
ballptr->yDir = ((rand() % 2) + 1) * ballptr->yDir / abs(ballptr->yDir);
}
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, cur->color);
}
Expand Down
Binary file modified game.o
Binary file not shown.

0 comments on commit ae44e9b

Please sign in to comment.