Skip to content

Commit

Permalink
Everything works, collision a little messed up
Browse files Browse the repository at this point in the history
  • Loading branch information
kliao2016 committed Nov 13, 2017
1 parent 11e97ba commit 4661d07
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 21 deletions.
Binary file modified BrickBreaker.elf
Binary file not shown.
Binary file modified BrickBreaker.gba
Binary file not shown.
16 changes: 8 additions & 8 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
void drawSlider(Slider *sldptr) {
sldptr->height = SLIDERHEIGHT;
sldptr->width = SLIDERWIDTH;
sldptr->row = SCREENHEIGHT - 1 - sldptr->height;
sldptr->row = SCREENHEIGHT - 15 - sldptr->height;
sldptr->col = (SCREENWIDTH / 2) - 1 - (sldptr->width / 2);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;
Expand Down Expand Up @@ -114,16 +114,16 @@ void handleCollisions(Ball *ballptr, Slider *sldptr) {
ballptr->xDir *= -1;
}

if (ballptr->col + BALLSIZE > SCREENWIDTH) {
ballptr->col = SCREENWIDTH - BALLSIZE;
ballptr->xDir *= -1;
}

if (ballptr->row < 0) {
ballptr->row = 0;
ballptr->yDir *= -1;
}

if (ballptr->col + BALLSIZE > SCREENWIDTH) {
ballptr->col = SCREENWIDTH - BALLSIZE;
ballptr->xDir *= -1;
}

// Collision resolution for when bottom of ball hits top of slider
if ((ballptr->row + BALLSIZE) > sldptr->row
&& ballptr->col >= (sldptr->col - BALLSIZE)
Expand Down Expand Up @@ -184,15 +184,15 @@ void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bric
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->xDir *= -1;
ballptr->col += ballptr->xDir;
ballptr->col += (ballptr->xDir * 3);
} else if ((((ballptr->row + BALLSIZE) == brptr->row)
|| (ballptr->row == (brptr->row + BRICKHEIGHT)))
&& ((ballptr->col >= (cur->col - BALLSIZE))
&& (ballptr->col <= (cur->col + BRICKWIDTH)))) {
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->yDir *= -1;
ballptr->row += ballptr->yDir;
ballptr->row += (ballptr->yDir * 3);
}
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, cur->color);
}
Expand Down
8 changes: 7 additions & 1 deletion game.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ typedef enum {
START_NODRAW,
LEVEL,
LEVEL_NODRAW,
NEXT_LEVEL,
NEXT_LEVEL_NO_DRAW,
LOSE_LIFE,
LOSE_LIFE_NO_DRAW,
GAME_OVER,
GAME_OVER_NO_DRAW
GAME_OVER_NO_DRAW,
WIN_GAME,
WIN_GAME_NO_DRAW,

} GAMEState;

Expand Down
Binary file modified game.o
Binary file not shown.
117 changes: 108 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ Brick bricks[18];
Brick *brptr = bricks;
int numBricks;
int bricksSize;
int level = 1;
int lives = 3;
int level = INITIAL_LEVEL;
int lives = INITIAL_LIVES;
char textBuffer[75];

int main() {
// Enable GBA Mode3
REG_DISPCTL = MODE3 | BG2_ENABLE;

int enterPressed = 0;
int aPressed = 0;

// Game Loop
while(1) {
Expand All @@ -44,25 +46,52 @@ int main() {
break;

case LEVEL_NODRAW:
startLevel();
// Collision resolution for when ball hits bottom of screen
if ((ballptr->row + BALLSIZE) >= SCREENHEIGHT) {
if (ballptr->row + BALLSIZE >= SCREENHEIGHT) {
lives -= 1;
if (lives == 0) {
if (lives > 0) {
state = LOSE_LIFE;
} else {
state = GAME_OVER;
}
}
startLevel();
if (numBricks <= 0) {
if (level < MAXLEVEL) {
level++;
state = LEVEL;
state = NEXT_LEVEL;
} else {
state = WIN_GAME;
}
}
break;

case NEXT_LEVEL:
setUpNextLevel();
state = NEXT_LEVEL_NO_DRAW;
break;

case NEXT_LEVEL_NO_DRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
state = LEVEL;
enterPressed = 1;
}
break;

case LOSE_LIFE:
tryAgain();
state = LOSE_LIFE_NO_DRAW;
break;

case LOSE_LIFE_NO_DRAW:
if (!aPressed && KEY_DOWN_NOW(BUTTON_A)) {
state = LEVEL;
enterPressed = 1;
}
break;

case GAME_OVER:
fillScreen(BGCOLOR);
drawString3(79, 90, "GAME OVER", YELLOW);
gameOver();
state = GAME_OVER_NO_DRAW;
break;

Expand All @@ -72,11 +101,26 @@ int main() {
enterPressed = 1;
}
break;

case WIN_GAME:
gameWon();
state = WIN_GAME_NO_DRAW;
break;

case WIN_GAME_NO_DRAW:
if (!enterPressed && KEY_DOWN_NOW(BUTTON_START)) {
resetGame();
enterPressed = 1;
}
break;
}

if (!KEY_DOWN_NOW(BUTTON_START)) {
enterPressed = 0;
}
if (!KEY_DOWN_NOW(BUTTON_A)) {
aPressed = 0;
}
}

return 0;
Expand All @@ -97,7 +141,7 @@ void startScreen() {
*/
void setGameStage() {
fillScreen(BGCOLOR);
drawString3(5, 5, "Level 1", GREEN);
updateScreenText();
drawSlider(sldptr);
createBall(sldptr->row - 5, sldptr->col + (SLIDERWIDTH / 2), BALLSIZE, ballptr);
numBricks = level * 3;
Expand All @@ -112,3 +156,58 @@ void startLevel() {
enableSlider(sldptr);
ballMovement(ballptr, sldptr, brptr, &numBricks, bricksSize);
}

/**
* Function to notify player that the game has ended and they lost
*/
void gameOver() {
fillScreen(BGCOLOR);
drawString3(70, 99, "GAME OVER.", RED);
drawString3(79, 50, "PRESS ENTER TO PLAY AGAIN", RED);
}

/**
* Function to notify that the player has lost a life
*/
void tryAgain() {
fillScreen(BGCOLOR);
drawString3(70, 67, "YOU LOST A LIFE.", RED);
drawString3(79, 60, "HIT Z TO TRY AGAIN", RED);
}

/**
* Function to notify that the player has won the game
*/
void gameWon() {
fillScreen(BGCOLOR);
drawString3(70, 56, "CONGRATULATIONS, YOU WON!", GREEN);
drawString3(79, 53, "PRESS ENTER TO PLAY AGAIN", GREEN);
}

/**
* Function to reset the game
*/
void resetGame() {
level = INITIAL_LEVEL;
lives = INITIAL_LIVES;
state = START;
}

/**
* Function to notify player that they made it to the next level
*/
void setUpNextLevel() {
fillScreen(BGCOLOR);
sprintf(textBuffer, "LEVEL %d. PRESS ENTER TO START", level);
drawString3(79, 36, textBuffer, YELLOW);
}

/**
* Function to update text on the screen
*/
void updateScreenText() {
sprintf(textBuffer, "LEVEL %d", level);
drawString3(SCREENHEIGHT - 10, 5, textBuffer, GREEN);
sprintf(textBuffer, "LIVES: %d", lives);
drawString3(SCREENHEIGHT - 10, SCREENWIDTH - sizeof(textBuffer) + 20, textBuffer, GREEN);
}
14 changes: 11 additions & 3 deletions main.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/* Defines */
#define MAXLEVEL 3
#define INITIAL_LEVEL 1
#define INITIAL_LIVES 3

/* Prototypes */
void startScreen();
void setGameStage();
void startLevel();

/* Defines */
#define MAXLEVEL 2
void gameOver();
void tryAgain();
void gameWon();
void resetGame();
void setUpNextLevel();
void updateScreenText();
Binary file modified main.o
Binary file not shown.

0 comments on commit 4661d07

Please sign in to comment.