Skip to content

Instantly share code, notes, and snippets.

@Gi133
Created April 13, 2012 09:37
Show Gist options
  • Select an option

  • Save Gi133/2375441 to your computer and use it in GitHub Desktop.

Select an option

Save Gi133/2375441 to your computer and use it in GitHub Desktop.
GBA Breakout!
uint8_t ballTile[64]=
{
0,0,0,0,0,0,0,0,
0,0,5,5,5,5,0,0,
0,5,6,6,6,6,5,0,
0,5,6,7,7,6,5,0,
0,5,6,7,7,6,5,0,
0,5,6,6,6,6,5,0,
0,0,5,5,5,5,0,0,
0,0,0,0,0,0,0,0,
};
// Ball|Game class (named Ballie for some odd and unfathomable reason)
class Ballie
{
private:
public:
//Variables:
int pos_x;
int pos_y;
int speed_x;
int speed_y;
int paddle_pos;
int Blocks[5][21]; //First 4 rows are the edge coords, 5th is active orint Blocks[5][21] so if 1 it's active, if 0 it's not.
bool launched;
//Functions:
Ballie(int);
void Update(int);
void checkLaunch();
void checkBounds();
void checkPaddle();
void checkBlocks();
void updateBlocks();
void resetBlocks();
void resetBall();
void resetGame();
bool checkReset();
};
Ballie::Ballie(int paddle_x)
{
pos_x = paddle_x + 12;
pos_y = 142;
speed_x = 0;
speed_y = 0;
launched = false;
}
void Ballie::Update(int paddle_x)
{
paddle_pos = paddle_x;
if (launched == false)
{
pos_x = paddle_x+12;
checkLaunch();
}
if (launched == true)
{
//Detect Collision
checkPaddle();
checkBounds();
checkBlocks();
updateBlocks();
//Keep moving.
pos_y += speed_y;
pos_x += speed_x;
}
}
void Ballie::checkLaunch()
{
if ((REG_P1 & KEY_A) == 0)
{
speed_y = -1;
speed_x = 1;
launched = true;
}
}
void Ballie::checkBounds()
{
if (pos_y <= (1 - speed_y))
{
pos_y = 0;
speed_y = -speed_y;
}
if (pos_x <= (1 - speed_x))
{
pos_x = 1 - speed_x;
speed_x = -speed_x;
}
if (pos_x >= (231 - speed_x))
{
pos_x = 231-speed_x;
speed_x = -speed_x;
}
}
void Ballie::checkPaddle()
{
if ((speed_y > 0)&&(pos_y >= 142+speed_y))
{
if ((paddle_pos <= pos_x) && (pos_x <= (paddle_pos+32)))
{
speed_y = -speed_y;
}
}
}
void Ballie::checkBlocks()
{
for (int i = 0; i < 21; i++)
{
int blockY = Blocks[0][i];
blockY -= 8; //Offset the ball pixels so you don't check origin.
int blockY1 = Blocks[1][i];
int blockX = Blocks[2][i];
blockX -= 8;
int blockX1 = Blocks[3][i];
bool blockV = Blocks[4][i];
if (((pos_y <= blockY1) && (pos_y > blockY)) && ((pos_x <= blockX1) && (pos_x >= blockX)) && (blockV == true))
{
if ((pos_y == blockY1) || (pos_y == blockY))
{
speed_y = -speed_y;
}
if ((pos_x == blockX1) || (pos_x == blockX))
{
speed_x = -speed_x;
}
Blocks[4][i] = 0;
}
}
}
void Ballie::updateBlocks()
{
for (int i = 0; i < 21; i++)
{
if (Blocks[4][i] == 0)
{
SetObjectX (2+i, 300);
}
}
}
void Ballie::resetBlocks()
{
for (int i = 0; i < 21; i++)
{
int blockX = Blocks[2][i];
SetObjectX (2+i, blockX);
}
}
void Ballie::resetBall()
{
pos_x = paddle_pos + 12;
pos_y = 142;
speed_x = 0;
speed_y = 0;
launched = false;
}
void Ballie::resetGame()
{
resetBall();
resetBlocks();
}
bool Ballie::checkReset()
{
bool end = false;
for (int i = 0; i < 21; i++)
{
if (Blocks[4][i] == 1)
{
end = true;
}
}
return !end;
}
const uint8_t blockTile1[64] =
{
8,8,8,8,8,8,8,8,
8,9,9,9,9,9,9,9,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
};
const uint8_t blockTile2[64] =
{
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
};
const uint8_t blockTile3[64] =
{
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
};
const uint8_t blockTile4[64] =
{
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
};
const uint8_t blockTile5[64] =
{
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,9,10,10,10,10,10,10,
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,
};
const uint8_t blockTile6[64] =
{
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,
};
const uint8_t blockTile7[64] =
{
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,
};
const uint8_t blockTile8[64] =
{
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
10,10,10,10,10,10,9,8,
8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,8,
};
#include <stdint.h>
#include <stdlib.h>
#include "gba.h"
#include "font.h"
#include "Player.h"
#include "Ballie.h"
#include "ball.c"
#include "block.c"
#include "paddle.c"
int main()
{
//Game Variables
Player paddle;
Ballie ball(paddle.pos_x);
//Block Edges, X and Y being origin coords, X1 and Y1 being the coords of the edge opposite the origin.
int EdgeY = 0;
int EdgeY1 = 0;
int EdgeX = 0;
int EdgeX1 = 0;
// Set display options.
REG_DISPCNT = DCNT_MODE0 | DCNT_OBJ | DCNT_OBJ_1D | DCNT_BG0| DCNT_BG1 | DCNT_BG2 | DCNT_BG3;
// Set background 0 options. (Game Background)
REG_BG0CNT = BG_CBB(0) | BG_SBB(30) | BG_8BPP | BG_REG_32x32;
REG_BG0HOFS = 0;
REG_BG0VOFS = 0;
// Set background 1 options. (Game Foreground)
REG_BG1CNT = BG_CBB(1) | BG_SBB(29) | BG_8BPP | BG_REG_32x32;
REG_BG1HOFS = 0;
REG_BG1VOFS = 0;
// Set background 2 options.
REG_BG2CNT = BG_CBB(1) | BG_SBB(28) | BG_8BPP | BG_REG_32x32;
REG_BG2HOFS = 0;
REG_BG2VOFS = 0;
// Set background 3 options.
REG_BG3CNT = BG_CBB(1) | BG_SBB(27) | BG_8BPP | BG_REG_32x32;
REG_BG3HOFS = 0;
REG_BG3VOFS = 0;
// Set up the object palette.
SetPaletteObj(0, RGB(0, 0, 0)); // transparent
SetPaletteObj(1, RGB(0, 0, 0)); // black
//Paddle Palette
SetPaletteObj(2, RGB(20, 20, 31)); // light blue
SetPaletteObj(3, RGB(10, 10, 31)); // blue
SetPaletteObj(4, RGB(0, 0, 31)); // dark blue
//Ball Palette
SetPaletteObj(5, RGB(16, 16, 16)); // Grey
SetPaletteObj(6, RGB(25, 25, 25)); // Light Grey
SetPaletteObj(7, RGB(31, 31, 31)); // White
//Block Palette
SetPaletteObj(8, RGB(31, 16, 16)); // Light red
SetPaletteObj(9, RGB(31, 8, 8)); // Red
SetPaletteObj(10, RGB(31, 0, 0)); // Darker Red
//Load paddle tiles
LoadTile8(4, 1, paddleTile1);
LoadTile8(4, 2, paddleTile2);
LoadTile8(4, 3, paddleTile3);
LoadTile8(4, 4, paddleTile4);
//Load ball tile
LoadTile8(4, 5, ballTile);
//Load block tiles
LoadTile8(4, 6, blockTile1);
LoadTile8(4, 7, blockTile2);
LoadTile8(4, 8, blockTile3);
LoadTile8(4, 9, blockTile4);
LoadTile8(4, 10, blockTile5);
LoadTile8(4, 11, blockTile6);
LoadTile8(4, 12, blockTile7);
LoadTile8(4, 13, blockTile8);
ClearObjects();
// Set up Player paddle.
SetObject(0,
ATTR0_SHAPE(1) | ATTR0_8BPP | ATTR0_REG | ATTR0_Y(paddle.pos_y),
ATTR1_SIZE(1) | ATTR1_X(paddle.pos_x),
ATTR2_ID8(1));
// Set up the ball.
SetObject(1,
ATTR0_SHAPE(0) | ATTR0_8BPP | ATTR0_REG | ATTR0_Y(ball.pos_y),
ATTR1_SIZE(0) | ATTR1_X(ball.pos_x),
ATTR2_ID8(5));
// Set up the blocks.
for (int z=0; z < 3; z++)
{
for (int i=0; i < 7; i++)
{
//Current Block number is i+(7*z).
EdgeY = 16+(16*z);
EdgeY1 = 32+(16*z);
EdgeX = 8+(32*i);
EdgeX1 = 40+(32*i);
ball.Blocks[0][i+(7*z)] = EdgeY;
ball.Blocks[1][i+(7*z)] = EdgeY1;
ball.Blocks[2][i+(7*z)] = EdgeX;
ball.Blocks[3][i+(7*z)] = EdgeX1;
ball.Blocks[4][i+(7*z)] = 1;
SetObject(2+i+(z*7),
ATTR0_SHAPE(1) | ATTR0_8BPP | ATTR0_REG | ATTR0_Y(EdgeY),
ATTR1_SIZE(2) | ATTR1_X(EdgeX),
ATTR2_ID8(6));
}
}
UpdateObjects();
WaitVSync();
while (true)
{
paddle.checkMove();
SetObjectX (0, paddle.pos_x);
ball.Update(paddle.pos_x);
SetObjectX (1, ball.pos_x);
SetObjectY (1, ball.pos_y);
UpdateObjects();
WaitVSync();
if (ball.checkReset() == true)
{
ball.resetGame();
}
}
return 0;
}
const uint8_t paddleTile1[64] =
{
0,0,2,2,2,2,2,2,
0,2,3,3,3,3,3,3,
2,3,4,4,4,4,4,4,
2,3,4,4,4,4,4,4,
2,3,4,4,4,4,4,4,
2,3,4,4,4,4,4,4,
0,2,3,3,3,3,3,3,
0,0,2,2,2,2,2,2,
};
const uint8_t paddleTile2[64] =
{
2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,
3,3,3,3,3,3,3,3,
2,2,2,2,2,2,2,2,
};
const uint8_t paddleTile3[64] =
{
2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,
3,3,3,3,3,3,3,3,
2,2,2,2,2,2,2,2,
};
const uint8_t paddleTile4[64] =
{
2,2,2,2,2,2,0,0,
3,3,3,3,3,3,2,0,
4,4,4,4,4,4,3,2,
4,4,4,4,4,4,3,2,
4,4,4,4,4,4,3,2,
4,4,4,4,4,4,3,2,
3,3,3,3,3,3,2,0,
2,2,2,2,2,2,0,0,
};
class Player
{
private:
public:
//Variables:
int pos_x;
int pos_y;
int score;
int speed;
//Functions:
Player();
void checkMove();
};
Player::Player()
{
pos_x = 0;
pos_y = 150;
score = 0;
speed = 2;
}
void Player::checkMove()
{
if (((REG_P1 & KEY_RIGHT) == 0) && (pos_x < 208))
{
pos_x += speed;
}
if (((REG_P1 & KEY_LEFT) == 0) && (pos_x > 0))
{
pos_x -= speed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment