Skip to content

Commit

Permalink
Added better collision resolution and made velocity better
Browse files Browse the repository at this point in the history
  • Loading branch information
kliao2016 committed Nov 14, 2017
1 parent 0dc3664 commit 3a2bb8e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 13 deletions.
Binary file modified BrickBreaker.elf
Binary file not shown.
Binary file modified BrickBreaker.gba
Binary file not shown.
53 changes: 42 additions & 11 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void drawSlider(Slider *sldptr) {
sldptr->col = (SCREENWIDTH / 2) - 1 - (sldptr->width / 2);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;
sldptr->cdel = 2;
drawImage3(sldptr->row, sldptr->col, sldptr->height, sldptr->width, slider_image);
}

Expand All @@ -25,10 +26,15 @@ void drawSlider(Slider *sldptr) {
*/
void enableSlider(Slider *sldptr) {

int cdel = 2;
int cdel = sldptr->cdel;
const int height = sldptr->height;

if (KEY_DOWN_NOW(BUTTON_RIGHT)) {

if (sldptr->cdel < 0) {
sldptr->cdel *= -1;
}

// Get the width of the slider
int width = sldptr->width;

Expand All @@ -45,6 +51,11 @@ void enableSlider(Slider *sldptr) {
}

if (KEY_DOWN_NOW(BUTTON_LEFT)) {

if (sldptr->cdel > 0) {
sldptr->cdel *= -1;
}

// Get the width of the slider
int width = sldptr->width;

Expand All @@ -53,7 +64,7 @@ void enableSlider(Slider *sldptr) {
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;

sldptr->col = sldptr->col - cdel;
sldptr->col = sldptr->col + cdel;
if (sldptr->col < 0) {
sldptr->col = 0;
}
Expand Down Expand Up @@ -131,10 +142,21 @@ void handleCollisions(Ball *ballptr, Slider *sldptr) {
&& ballptr->col >= (sldptr->col - BALLSIZE)
&& ballptr->col <= (sldptr->col + SLIDER_WIDTH)) {
ballptr->row = sldptr->row - BALLSIZE;
ballptr->yDir = ballptr->yDir / ballptr->yDir * -1;
int dirFactor = (rand() % 3) - 1;
if (dirFactor != 0) {
ballptr->xDir = (ballptr->xDir / abs(ballptr->xDir)) * dirFactor;
ballptr->yDir *= -1;
if (ballptr->xDir > 0 && sldptr->cdel > 0) {
ballptr->xDir += 1;
} else if (ballptr->xDir < 0 && sldptr->cdel > 0) {
ballptr->xDir += 1;
if (ballptr->xDir == 0) {
ballptr->xDir = -1;
}
} else if (ballptr->xDir > 0 && sldptr->cdel < 0) {
ballptr->xDir -= 1;
if (ballptr->xDir == 0) {
ballptr->xDir = 1;
}
} else {
ballptr->xDir -= 1;
}
}

Expand Down Expand Up @@ -186,7 +208,6 @@ void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bric
brptr[j] = brptr[j + 1];
}
bricksSize = *(numBricks);
i--;
} else {
if (((ballptr->col == (cur->col + BRICKWIDTH))
|| ((ballptr->col + BALLSIZE) == cur->col))
Expand All @@ -195,17 +216,27 @@ 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);
ballptr->yDir *= ((rand() % 3) - 1);
if (ballptr->yDir == 0) {
ballptr->yDir = 1;
}
ballptr->col += ballptr->xDir;
ballptr->row += ballptr->yDir;
i = 0;
} 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);
ballptr->xDir *= ((rand() % 3) - 1);
if (ballptr->xDir == 0) {
ballptr->xDir = 1;
}
ballptr->row += ballptr->yDir;
ballptr->col += ballptr->xDir;
i = 0;
}
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, cur->color);
}
Expand Down
1 change: 1 addition & 0 deletions game.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef struct {
int col;
int oldRow;
int oldCol;
int cdel;
unsigned short color;

} Slider;
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 @@ -9,7 +9,7 @@ Slider slider;
Slider *sldptr = &slider;
Ball ball;
Ball *ballptr = &ball;
Brick bricks[21];
Brick bricks[15];
Brick *brptr = bricks;
int numBricks;
int bricksSize;
Expand Down
2 changes: 1 addition & 1 deletion main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Defines */
#define MAXLEVEL 3
#define MAXLEVEL 4
#define INITIAL_LEVEL 1
#define INITIAL_LIVES 3

Expand Down
Binary file modified main.o
Binary file not shown.
Binary file modified myLib.o
Binary file not shown.

0 comments on commit 3a2bb8e

Please sign in to comment.