Skip to content

Commit

Permalink
Collisions work
Browse files Browse the repository at this point in the history
  • Loading branch information
kliao2016 committed Nov 13, 2017
1 parent 1ba47e3 commit 11e97ba
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 67 deletions.
Binary file modified BrickBreaker.elf
Binary file not shown.
Binary file modified BrickBreaker.gba
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ OFILES = main.o myLib.o font.o game.o graphics.o
# The header files you have created.
# This is necessary to determine when to recompile for files.
# This should be a space (SPACE!) separated list of .h files
HFILES = game.h myLib.h graphics.h
HFILES = game.h myLib.h graphics.h main.h

################################################################################
# These are various settings used to make the GBA toolchain work
Expand Down
109 changes: 72 additions & 37 deletions game.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
* @param sldptr the pointer to the slider to be drawn
*/
void drawSlider(Slider *sldptr) {
sldptr->height = 5;
sldptr->width = 50;
sldptr->row = 159 - sldptr->height;
sldptr->col = 119 - (sldptr->width / 2);
sldptr->height = SLIDERHEIGHT;
sldptr->width = SLIDERWIDTH;
sldptr->row = SCREENHEIGHT - 1 - sldptr->height;
sldptr->col = (SCREENWIDTH / 2) - 1 - (sldptr->width / 2);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;
fillScreen(BLACK);
drawRect(sldptr->row, sldptr->col, sldptr->height, sldptr->width, YELLOW);
}

Expand All @@ -32,15 +31,14 @@ void enableSlider(Slider *sldptr) {
int width = sldptr->width;

// Erase previous slider position
drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BLACK);
drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BGCOLOR);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;

sldptr->col = sldptr->col + cdel;
if (sldptr->col + width > 240) {
sldptr->col = 240 - width;
if (sldptr->col + width > SCREENWIDTH) {
sldptr->col = SCREENWIDTH - width;
}
//drawRect(oldrow, oldcol, height, width, BLACK);
drawRect(sldptr->row, sldptr->col, height, width, YELLOW);
}

Expand All @@ -49,15 +47,14 @@ void enableSlider(Slider *sldptr) {
int width = sldptr->width;

// Erase previous slider position
drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BLACK);
drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BGCOLOR);
sldptr->oldRow = sldptr->row;
sldptr->oldCol = sldptr->col;

sldptr->col = sldptr->col - cdel;
if (sldptr->col < 0) {
sldptr->col = 0;
}
//drawRect(sldptr->oldRow, sldptr->oldCol, height, width, BLACK);
drawRect(sldptr->row, sldptr->col, height, width, YELLOW);
}
}
Expand Down Expand Up @@ -86,17 +83,22 @@ void createBall(int row, int col, int size, Ball *ballptr) {
*
* @param ballptr the pointer to the ball to move
* @param sldptr the pointer to the slider of the level
* @param brptr the pointer to the array of bricks in the game
* @param numBricks the pointer to the number of bricks in the game
* @param bricksSize the size of the array of bricks
*/
void startLevel(Ball *ballptr, Slider *sldptr) {
drawRect(ballptr->oldRow, ballptr->oldCol, ballptr->size, ballptr->size, BLACK);
void ballMovement(Ball *ballptr, Slider *sldptr, Brick *brptr, int *numBricks, int bricksSize) {
drawRect(ballptr->oldRow, ballptr->oldCol, ballptr->size, ballptr->size, BGCOLOR);
ballptr->col += ballptr->xDir;
ballptr->row += ballptr->yDir;

handleCollisions(ballptr, sldptr);
handleBrickCollisions(brptr, ballptr, numBricks, bricksSize);

ballptr->oldRow = ballptr->row;
ballptr->oldCol = ballptr->col;
drawRect(ballptr->row, ballptr->col, ballptr->size, ballptr->size, WHITE);

}

/**
Expand All @@ -106,15 +108,14 @@ void startLevel(Ball *ballptr, Slider *sldptr) {
* @param sldptr the pointer to the slider of the game
*/
void handleCollisions(Ball *ballptr, Slider *sldptr) {
int size = ballptr->size;

if (ballptr->col < 0) {
ballptr->col = 0;
ballptr->xDir *= -1;
}

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

Expand All @@ -124,10 +125,10 @@ void handleCollisions(Ball *ballptr, Slider *sldptr) {
}

// Collision resolution for when bottom of ball hits top of slider
if ((ballptr->row + size) > sldptr->row
&& ballptr->col >= sldptr->col
&& (ballptr->col + size) <= (sldptr->col + sldptr->width)) {
ballptr->row = sldptr->row - ballptr->size;
if ((ballptr->row + BALLSIZE) > sldptr->row
&& ballptr->col >= (sldptr->col - BALLSIZE)
&& ballptr->col <= (sldptr->col + SLIDERWIDTH)) {
ballptr->row = sldptr->row - BALLSIZE;
ballptr->yDir *= -1;
}

Expand All @@ -136,31 +137,65 @@ void handleCollisions(Ball *ballptr, Slider *sldptr) {
/**
* Function to generate bricks on the level
*
* @param bricks the list of bricks to generate
* @param brptr the pointer to the first brick of the array of bricks
* @param numBricks the number of bricks on the screen
*/
void generateBricks(int numBricks) {
void generateBricks(Brick *brptr, int numBricks) {
u16 colors[] = {RED, GREEN, BLUE, YELLOW, WHITE};
int numcolors = sizeof(colors)/sizeof(colors[0]);
Brick brick;
Brick *brptr = &brick;
int row = 30;
int row = 25;
int col = 45;

for (int i = 0; i < numBricks; i++) {
Brick *cur = brptr + i;
u16 color = colors[i % numcolors];
brptr->row = row;
brptr->col = col;
brptr->height = 10;
brptr->width = 30;
brptr->color = color;
cur->row = row;
cur->col = col;
cur->height = BRICKHEIGHT;
cur->width = BRICKWIDTH;
cur->color = color;
cur->isHit = 0;
col = col + 30 + (cur->width);
if ((i + 1) % 3 == 0) {
row += brptr->height;
row += BRICKHEIGHT + 3;
col = 45;
}
drawRect(brptr->row, brptr->col,
brptr->height,
brptr->width,
color);
col = col + 30 + (brptr->width);
}
}

/**
* Function to handle ball collision with a brick
*
* @param brptr the pointer to the brick
* @param ballptr the pointer to the ball
* @param numBricks the pointer to the number of bricks to generate
* @param bricksSize the number of bricks in the array
*/
void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bricksSize) {

for (int i = 0; i < bricksSize; i++) {
Brick *cur = brptr + i;
if (cur->isHit) {
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, BGCOLOR);
} else {
if (((ballptr->col == (cur->col + BRICKWIDTH))
|| ((ballptr->col + BALLSIZE) == cur->col))
&& ((ballptr->row <= (cur->row + BRICKHEIGHT))
&& (ballptr->row >= (cur->row - BALLSIZE)))) {
*(numBricks) -= 1;
cur->isHit = 1;
ballptr->xDir *= -1;
ballptr->col += ballptr->xDir;
} 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;
}
drawRect(cur->row, cur->col, BRICKHEIGHT, BRICKWIDTH, cur->color);
}
}

}
10 changes: 6 additions & 4 deletions game.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ typedef struct {
int width;
int row;
int col;
int isHit;
unsigned short color;

} Brick;

typedef enum {
START,
START_NODRAW,
LEVEL1,
LEVEL1_NODRAW,
LEVEL,
LEVEL_NODRAW,
GAME_OVER,
GAME_OVER_NO_DRAW

Expand Down Expand Up @@ -44,6 +45,7 @@ typedef struct {
void drawSlider(Slider *sldptr);
void enableSlider(Slider *sldptr);
void createBall(int row, int col, int size, Ball *ballptr);
void startLevel(Ball *ballptr, Slider *sldptr);
void ballMovement(Ball *ballptr, Slider *sldptr, Brick *brptr, int *numBricks, int bricksSize);
void handleCollisions(Ball *ballptr, Slider *sldptr);
void generateBricks(int numBricks);
void generateBricks(Brick *brptr, int numBricks);
void handleBrickCollisions(Brick *brptr, Ball *ballptr, int *numBricks, int bricksSize);
Binary file modified game.o
Binary file not shown.
78 changes: 54 additions & 24 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,66 @@
#include "myLib.h"
#include "game.h"
#include "graphics.h"
#include "main.h"

GAMEState state = START;
Slider slider;
Slider *sldptr = &slider;
Ball ball;
Ball *ballptr = &ball;
Brick bricks[18];
Brick *brptr = bricks;
int numBricks;
int bricksSize;
int level = 1;
int lives = 3;

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

GAMEState state = START;
Slider slider;
Slider *sldptr = &slider;
Ball ball;
Ball *ballptr = &ball;
int level;
//int lives = 3;

int enterPressed = 0;

// Game Loop
while(1) {
waitForVblank();
switch (state) {
case START:
startScreen();
state = START_NODRAW;
break;

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

case LEVEL1:
level = 1;
drawSlider(sldptr);
createBall(sldptr->row - 5, sldptr->col + (sldptr->width / 2), 5, ballptr);
generateBricks(level * 3);
drawString3(5, 5, "Level 1", GREEN);
state = LEVEL1_NODRAW;
case LEVEL:
setGameStage();
state = LEVEL_NODRAW;
break;

case LEVEL1_NODRAW:
enableSlider(sldptr);
startLevel(ballptr, sldptr);

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

case GAME_OVER:
fillScreen(BLACK);
fillScreen(BGCOLOR);
drawString3(79, 90, "GAME OVER", YELLOW);
state = GAME_OVER_NO_DRAW;
break;
Expand All @@ -78,7 +87,28 @@ int main() {
* Function to setup the start screen
*/
void startScreen() {
fillScreen(BLACK);
fillScreen(BGCOLOR);
drawImage3(0, 0, START_SCREEN_HEIGHT, START_SCREEN_WIDTH, start_screen);
drawString3(105, 50, "Press ENTER to Start Game", YELLOW);
}

/**
* Function to generate basic game stage
*/
void setGameStage() {
fillScreen(BGCOLOR);
drawString3(5, 5, "Level 1", GREEN);
drawSlider(sldptr);
createBall(sldptr->row - 5, sldptr->col + (SLIDERWIDTH / 2), BALLSIZE, ballptr);
numBricks = level * 3;
bricksSize = level * 3;
generateBricks(brptr, bricksSize);
}

/**
* Function to begin the level
*/
void startLevel() {
enableSlider(sldptr);
ballMovement(ballptr, sldptr, brptr, &numBricks, bricksSize);
}
5 changes: 5 additions & 0 deletions main.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/* Prototypes */
void startScreen();
void setGameStage();
void startLevel();

/* Defines */
#define MAXLEVEL 2
Binary file modified main.o
Binary file not shown.
9 changes: 8 additions & 1 deletion myLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ typedef unsigned short u16;

#define SCANLINECOUNTER *(volatile unsigned short *)0x4000006

#define SCREENHEIGHT 149
#define SCREENHEIGHT 160
#define SCREENWIDTH 240
#define SLIDERHEIGHT 5
#define SLIDERWIDTH 45
#define BRICKHEIGHT 10
#define BRICKWIDTH 30
#define BALLSIZE 5

#define BG2_ENABLE (1<<10)
#define COLOR(r, g, b) ((r) | (g)<<5 | (b)<<10)
Expand All @@ -18,6 +24,7 @@ typedef unsigned short u16;
#define WHITE COLOR(31,31,31)
#define BLACK 0
#define DKGRAY COLOR(15, 15, 15)
#define BGCOLOR 0


#define OFFSET(row, col, rowlen) ((row)*(rowlen)+(col))
Expand Down

0 comments on commit 11e97ba

Please sign in to comment.