Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PinDetect PowerControl mbed
Revision 8:7889867308d3, committed 2015-05-07
- Comitter:
- el13cj
- Date:
- Thu May 07 17:20:11 2015 +0000
- Parent:
- 7:61423369f83c
- Child:
- 9:8447ce6f51ae
- Commit message:
- Game functions mostly working and code comments pretty much complete.; Still getting errors such that ball goes through/doesn't break bricks.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BrickBreaker.cpp Thu May 07 17:20:11 2015 +0000
@@ -0,0 +1,959 @@
+/**
+@file BrickBreaker.cpp
+
+@brief Function implementations
+
+*/
+
+
+#include "BrickBreaker.h"
+#include "mbed.h"
+
+
+
+// Initialise the N5110 screen
+// VCC,SCE,RST,D/C,MOSI,SCLK,LED
+// \ \ | | / / /
+N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
+
+int main() {
+
+
+ PHY_PowerDown();
+ int result = semihost_powerdown();
+
+ lcd.init();
+ calibrateJoystick();
+ initBricks(0);
+
+ readHighScores();
+
+ pollJoystick.attach(&updateJoystick,1.0/10); // read joystick 10 times per second
+ select.attach(&getMenuSelect,0.15);
+
+ movePaddle.attach(&paddle,0.025);
+
+ initBricks(0);
+
+ button.attach_asserted(&newScreen);
+ button.setSampleFrequency();
+
+ playPause.mode(PullUp);
+ playPause.attach_asserted(&pause);
+ playPause.setSampleFrequency();
+
+ while(1) {
+
+ while(displayMenu == 1) {
+ menu();
+ }
+ while(displayGame == 1) {
+ game(startStop);
+ if (lives < 0) {
+ displayGame = 0;
+ displayGameOver = 1;
+ }
+ if (getNumBricks() == 0) {
+ newLevel();
+ }
+ }
+ while(displayHelp == 1) {
+ help();
+ }
+ while(displayHighScores == 1) {
+ highScores();
+ }
+ while(displayGameOver == 1) {
+ gameOver();
+ }
+ lcd.refresh();
+
+ }
+}
+
+void readHighScores() {
+ FILE *fp = fopen("/local/scores.txt","r");
+ fscanf(fp,"%i",&highscore1);
+ fscanf(fp,"%i",&highscore2);
+ fscanf(fp,"%i",&highscore3);
+ fclose(fp);
+}
+
+void writeHighScores() {
+ FILE *fp = fopen("/local/scores.txt","w");
+ fprintf(fp,"%i\r\n",highscore1);
+ fprintf(fp,"%i\r\n",highscore2);
+ fprintf(fp,"%i\r\n",highscore3);
+ fclose(fp);
+}
+
+void newLevel()
+{
+ lcd.clear();
+ level++;
+ initBricks(level);
+ by = 30;
+ bx = 42;
+ px = 38;
+ d = 8;
+ lcd.refresh();
+
+
+}
+
+void pause()
+{
+ if (displayGame == 1) {
+ startStop = !startStop;
+ }
+}
+
+int getNumBricks() {
+ int sum = 0;
+ for (int i = 0; i < 8; i++) {
+ for (int j = 0; j < 4; j++) {
+
+ sum+=bricks[j][i];
+ }
+ }
+ return sum;
+}
+
+int lifeLost() {
+ if (by >= 48 && by <= 50) {
+ lives-=1;
+ by = 30;
+ bx = 42;
+ px = 38;
+ d = 8;
+ startStop = 0;
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+void game(int g) {
+ if (g == 1) {
+ lifeLost();
+ borderInit();
+ dispScore();
+ dispLives();
+ ball();
+ doBricks();
+ } else if (g == 0) {
+ }
+}
+
+void highScores() {
+ readHighScores();
+ lcd.clear();
+ lcd.printString("HIGH SCORES",10,0);
+ lcd.printString("1/",10,1);
+ lcd.printNum(highscore1,30,1);
+ lcd.printString("2/",10,2);
+ lcd.printNum(highscore2,30,2);
+ lcd.printString("3/",10,3);
+ lcd.printNum(highscore3,30,3);
+ lcd.printString("menu",10,5);
+ lcd.printString(">",5,5);
+ lcd.refresh();
+ Sleep();
+}
+
+void help()
+{
+ //menuSelect = 35;
+ if (helpScreen == 0) {
+ lcd.clear();
+ lcd.printString("HELP",10,0);
+ lcd.printString("Destroy bricks",0,1);
+ lcd.printString("to reach the",0,2);
+ lcd.printString("next level",0,3);
+ lcd.printString("more",40,5);
+ lcd.printString("menu",10,5);
+ lcd.printString(">",helpSelect,5);
+ lcd.refresh();
+ Sleep();
+ } else if (helpScreen == 1) {
+ lcd.clear();
+ lcd.printString("HELP",10,0);
+ lcd.printString("Use joystick",0,1);
+ lcd.printString("to control",0,2);
+ lcd.printString("the paddle",0,3);
+ lcd.printString("more",40,5);
+ lcd.printString("menu",10,5);
+ lcd.printString(">",helpSelect,5);
+ lcd.refresh();
+ Sleep();
+ } else if (helpScreen == 2) {
+ lcd.clear();
+ lcd.printString("HELP",10,0);
+ lcd.printString("Pause the game",0,1);
+ lcd.printString("using the >/||",0,2);
+ lcd.printString("button",0,3);
+ lcd.printString("more",40,5);
+ lcd.printString("menu",10,5);
+ lcd.printString(">",helpSelect,5);
+ lcd.refresh();
+ Sleep();
+ } else if (helpScreen == 3) {
+ lcd.clear();
+ lcd.printString("HELP",10,0);
+ lcd.printString("Control volume",0,1);
+ lcd.printString("using the blue",0,2);
+ lcd.printString("potentiometer",0,3);
+ lcd.printString("more",40,5);
+ lcd.printString("menu",10,5);
+ lcd.printString(">",helpSelect,5);
+ lcd.refresh();
+ Sleep();
+ }
+}
+
+void gameOver() {
+
+
+ lcd.clear();
+ lcd.printString("GAME OVER",10,0);
+ lcd.printString("score",10,1);
+ lcd.printNum(score,42,1);
+ lcd.printString(">menu",10,5);
+ if ((score >= highscore1) && scoreFlag) {
+ highscore3 = highscore2;
+ highscore2 = highscore1;
+ highscore1 = score;
+ writeHighScores();
+ scoreFlag = 0;
+ } else if ((score >= highscore2) && scoreFlag) {
+ highscore3 = highscore2;
+ highscore2 = score;
+ writeHighScores();
+ scoreFlag = 0;
+ } else if ((score >= highscore3) && scoreFlag) {
+ highscore3 = score;
+ writeHighScores();
+ scoreFlag = 0;
+ }
+ if (score >= highscore3) {
+ lcd.printString("NEW HIGH SCORE",0,3);
+ }
+ lcd.refresh();
+ Sleep();
+}
+
+void newScreen()
+{
+ if (displayMenu) {
+ switch (menuSelect) {
+ case 1:
+ lcd.clear();
+ displayMenu = 0;
+ displayGame = 1;
+ break;
+ case 2:
+ displayMenu = 0;
+ displayHelp = 1;
+ break;
+ case 3:
+ displayMenu = 0;
+ displayHighScores = 1;
+ break;
+ }
+ } else if (displayHelp) {
+ switch (helpSelect) {
+ case 5:
+ displayMenu = 1;
+ displayHelp = 0;
+ break;
+ case 35:
+ helpScreen++;
+ if (helpScreen > 3) {
+ helpScreen = 0;
+ }
+ break;
+ }
+ } else if (displayHighScores) {
+ displayMenu = 1;
+ displayHighScores = 0;
+ } else if (displayGame) {
+ startStop = !startStop;
+ } else if (displayGameOver) {
+ displayMenu = 1;
+ displayGameOver = 0;
+ }
+}
+
+void getMenuSelect()
+{
+ if (displayMenu) {
+
+
+ if (joystick.direction == DOWN) {
+ lcd.clearRect(5,(menuSelect*8),5,8);
+ if (menuSelect < 3) {
+ menuSelect++;
+ } else if (menuSelect == 3) {
+ menuSelect = 1;
+ }
+ } else if (joystick.direction == UP) {
+ lcd.clearRect(5,(menuSelect*8),5,8);
+ if (menuSelect > 1) {
+ menuSelect-=1;
+ } else if (menuSelect == 1) {
+ menuSelect = 3;
+ }
+ }
+ } else if (displayHelp) {
+
+
+ if (joystick.direction == RIGHT) {
+ lcd.clearRect(helpSelect,40,5,8);
+
+ if (helpSelect < 35) {
+ helpSelect+=30;
+ } else if (menuSelect == 35) {
+ helpSelect = 5;
+ }
+ } else if (joystick.direction == LEFT) {
+ lcd.clearRect(helpSelect,40,5,8);
+
+ if (helpSelect > 5) {
+ helpSelect-=30;
+ } else if (menuSelect == 5) {
+ helpSelect = 35;
+ }
+ }
+ }
+}
+
+void menu() {
+ lcd.clear();
+ lcd.printString("BRICK BREAKER",0,0);
+ lcd.printString("Start",10,1);
+ lcd.printString("Help",10,2);
+ lcd.printString("Scores",10,3);
+ lcd.printString(">",5,menuSelect);
+ lives = 3;
+ score = 0;
+ initBricks(0);
+ startStop = 1;
+ level = 0;
+ scoreFlag = 1;
+ lcd.refresh();
+ Sleep();
+}
+
+void dispLives() {
+
+ lcd.printNum(lives,77,5);
+ lcd.printChar(0x80,77,4);
+}
+
+void dispScore() {
+
+
+ if (score<10) {
+ lcd.printNum(score,77,0);
+ } else if (score<100) {
+
+ lcd.printNum(((score/10)%10),77,0);
+ lcd.printNum((score%10),77,1);
+
+ } else if (score<1000) {
+
+ lcd.printNum(((score/100)%10),77,0);
+ lcd.printNum(((score/10)%10),77,1);
+ lcd.printNum((score%10),77,2);
+
+ }
+}
+
+void doBricks() {
+
+ for (int x = 0; x<8; x++) {
+ for (int y = 0; y<4; y++) {
+ getBrickTouch(x,y);
+ }
+ }
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 8; j++) {
+ if (bricks[i][j] && brickDrawFlag[i][j]) {
+ lcd.drawRect(bricksx[j],bricksy[i], 6, 2, 1);
+ brickDrawFlag[i][j] = 0;
+ }
+ }
+ }
+
+ clearBricks();
+}
+
+void initBricks(int l)
+{
+
+ int bricksTemp[4][8];
+
+ if (l == 0) {
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 8; j++) {
+ bricks[i][j] = 1;
+ brickDrawFlag[i][j] = 1;
+ }
+ }
+ } else if (l == 1) {
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 8; j+=2) {
+ bricks[i][j] = 1;
+ brickDrawFlag[i][j] = 1;
+ }
+ }
+ } else if (l > 1) {
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 8; j++) {
+ int mint = ceil((noise.read() * 100000));
+ bricksTemp[i][j] = mint%100;
+ if (bricksTemp[i][j] > ((l*57)%73)) {
+ bricks[i][j] = 1;
+ brickDrawFlag[i][j] = 1;
+ }
+ }
+ }
+ }
+}
+
+void clearBricks() {
+ for (int i = 0; i < 8; i++) {
+ for (int j = 0; j < 4; j++) {
+ if (clearFlag[j][i]) {
+ lcd.clearRect(bricksx[i],bricksy[j],7,3);
+ clearFlag[j][i] = 0;
+ score++;
+
+ }
+ }
+ }
+
+}
+
+
+void getBrickTouch(int x, int y)
+{
+
+ if (bricks[y][x]) {
+ for (int a = -1; a < 7; a++) {
+ if (lcd.getPixel(bricksx[x]+a,bricksy[y]-1)) {
+ clearFlag[y][x] = 1;
+ bricks[y][x] = 0;
+ } else if (lcd.getPixel(bricksx[x]+a,bricksy[y]+3)) {
+ clearFlag[y][x] = 1;
+ bricks[y][x] = 0;
+ }
+ }
+ for (int b = 0; b < 3; b++) {
+ if (lcd.getPixel(bricksx[x]-1,bricksy[y]+b)) {
+ clearFlag[y][x] = 1;
+ bricks[y][x] = 0;
+ } else if (lcd.getPixel(bricksx[x]+7,bricksy[y]+b)) {
+ clearFlag[y][x] = 1;
+ bricks[y][x] = 0;
+ }
+ }
+ }
+}
+
+
+// read default positions of the joystick to calibrate later readings
+void calibrateJoystick()
+{
+ button.mode(PullUp);
+ // must not move during calibration
+ joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
+ joystick.y0 = yPot;
+}
+
+
+void updateJoystick()
+{
+ if (startStop == 1) {
+
+
+ // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
+ joystick.x = xPot - joystick.x0;
+ joystick.y = yPot - joystick.y0;
+ // read button state
+
+ // calculate direction depending on x,y values
+ // tolerance allows a little lee-way in case joystick not exactly in the stated direction
+ if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+ joystick.direction = CENTRE;
+ } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+ joystick.direction = UP;
+ } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+ joystick.direction = DOWN;
+ } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+ joystick.direction = RIGHT;
+ } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+ joystick.direction = LEFT;
+ } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
+ joystick.direction = UPRIGHT;
+ } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) {
+ joystick.direction = DOWNRIGHT;
+ } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x < -1*DIRECTION_TOLERANCE) {
+ joystick.direction = UPLEFT;
+ } else if ( joystick.x < -1*DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) {
+ joystick.direction = DOWNLEFT;
+ } else {
+ joystick.direction = UNKNOWN;
+ }
+ } else if (startStop == 0) {
+ joystick.direction = CENTRE;
+ }
+
+}
+
+
+//function to move the game paddle left and right with the joystick
+void paddle()
+{
+ if (!displayGame) return;
+
+ lcd.clearRect(1,py,74,3);
+
+ if ((joystick.direction == RIGHT || joystick.direction == UPRIGHT || joystick.direction == DOWNRIGHT) && (px < 67)) {
+ px = px+1;
+ lcd.drawRect(px,py,pw,ph,1);
+ } else if ((joystick.direction == LEFT || joystick.direction == UPLEFT || joystick.direction == DOWNLEFT) && (px > 0)) {
+ px = px-1;
+ lcd.drawRect(px,py,pw,ph,1);
+ } else {
+ lcd.drawRect(px,py,pw,ph,1);
+ }
+
+}
+
+
+
+//set the touchFlag if any of the pixels touching the ball are set
+//store which pixels are set in an array
+void getTouchFlag()
+{
+ if (lcd.getPixel(bx-1,by)) {//11
+ touchFlag = 1;
+ surround[11] = 1;
+ } if (lcd.getPixel(bx-1,by-1)) {//0
+ touchFlag = 1;
+ surround[0] = 1;
+ } if (lcd.getPixel(bx,by-1)) {//1
+ touchFlag = 1;
+ surround[1] = 1;
+ } if (lcd.getPixel(bx+1,by-1)) {//2
+ touchFlag = 1;
+ surround[2] = 1;
+ } if (lcd.getPixel(bx+2,by-1)) {//3
+ touchFlag = 1;
+ surround[3] = 1;
+ } if (lcd.getPixel(bx+2,by)) {//4
+ touchFlag = 1;
+ surround[4] = 1;
+ } if (lcd.getPixel(bx+2,by+1)) {//5
+ touchFlag = 1;
+ surround[5] = 1;
+ } if (lcd.getPixel(bx+2,by+2)) {//6
+ touchFlag = 1;
+ surround[6] = 1;
+ } if (lcd.getPixel(bx+1,by+2)) {//7
+ touchFlag = 1;
+ surround[7] = 1;
+ } if (lcd.getPixel(bx,by+2)) {//8
+ touchFlag = 1;
+ surround[8] = 1;
+ } if (lcd.getPixel(bx-1,by+2)) {//9
+ touchFlag = 1;
+ surround[9] = 1;
+ } if (lcd.getPixel(bx-1,by+1)) {//10
+ touchFlag = 1;
+ surround[10] = 1;
+ }
+}
+
+//work out what the ball has hit and calculate/set the new angle accordingly
+int setAngle()
+{
+ getTouchFlag();
+
+ if (touchFlag) {
+
+ //FOR 3 CORNER SQUARES
+ if (surround[11] && surround[0] && surround[1]) { //top right corner
+ d = 6;
+ } else if (surround[2] && surround[3] && surround[4]) { //bottom right corner
+ d = 10;
+ } else if (surround[5] && surround[6] && surround[7]) { //bottom left corner
+ d = 14;
+ } else if (surround[8] && surround[9] && surround[10]) { //top left corner
+ d = 2;
+ }
+ //FOR 3 TOUCHING OFFCENTRE EDGE SQUARES
+ else if (surround[0] && surround[1] && surround[2] && !surround[3]) { //top edge left
+ d = 5;
+ } else if (surround[3] && surround[1] && surround[2] && !surround[0]) { //top edge right
+ d = 11;
+ } else if (surround[6] && surround[7] && surround[8] && !surround[9]) { // low edge right
+ d = 13;
+ } else if (surround[7] && surround[8] && surround[9] && !surround[6]) { //low edge left
+ d = 3;
+ } else if (surround[0] && surround[11] && surround[10] && !surround[9]) { //left edge top
+ d = 5;
+ } else if (surround[3] && surround[4] && surround[5] && !surround[6]) { //right edge top
+ d = 11;
+ } else if (surround[11] && surround[10] && surround[9] && !surround[0]) { //left edge low
+ d = 3;
+ } else if (surround[4] && surround[5] && surround[6] && !surround[3]) { //right edge low
+ d = 13;
+ }
+ //FOR CeNTRE SQUARES
+ else if (surround[1] && surround[2]) { //top edge
+ switch (d) {
+ case 3:
+ d = 5;
+ break;
+ case 2:
+ d = 6;
+ break;
+ case 1:
+ d = 7;
+ break;
+ case 0:
+ d = 8;
+ break;
+ case 15:
+ d = 9;
+ break;
+ case 14:
+ d = 10;
+ break;
+ case 13:
+ d = 11;
+ break;
+ }
+
+ } else if (surround[4] && surround[5]) { //RHS edge
+ switch (d) {
+ case 1:
+ d = 15;
+ break;
+ case 2:
+ d = 14;
+ break;
+ case 3:
+ d = 13;
+ break;
+ case 4:
+ d = 12;
+ break;
+ case 5:
+ d = 11;
+ break;
+ case 6:
+ d = 10;
+ break;
+ case 7:
+ d = 9;
+ break;
+ }
+
+ } else if (surround[10] && surround[11]) { //LHS edge
+ switch (d) {
+ case 9:
+ d = 7;
+ break;
+ case 10:
+ d = 6;
+ break;
+ case 11:
+ d = 5;
+ break;
+ case 12:
+ d = 4;
+ break;
+ case 13:
+ d = 3;
+ break;
+ case 14:
+ d = 2;
+ break;
+ case 15:
+ d = 1;
+ break;
+ }
+ } else if (surround[7] && surround[8]) { //bottom edge
+ switch (d) {
+ case 5:
+ d = 3;
+ break;
+ case 6:
+ d = 2;
+ break;
+ case 7:
+ d = 1;
+ break;
+ case 8:
+ d = 0;
+ break;
+ case 9:
+ d = 15;
+ break;
+ case 10:
+ d = 14;
+ break;
+ case 11:
+ d = 13;
+ break;
+ }
+ }
+
+ //FOR 2 TOUCHING OFFCENTRE EDGE SQUARES
+ else if (surround[0] && surround[1]) { //top edge left
+ d = 9;
+ } else if (surround[2] && surround[3]) { //top edge right
+ d = 7;
+ } else if (surround[9] && surround[8]) { // low edge right
+ d = 1;
+ } else if (surround[7] && surround[6]) { //low edge left
+ d = 15;
+ } else if (surround[0] && surround[11]) { //left edge top
+ d =7;
+ } else if (surround[3] && surround[4]) { //right edge top
+ d = 9;
+ } else if (surround[10] && surround[9]) { //left edge low
+ d = 1;
+ } else if (surround[5] && surround[6]) { //right edge low
+ d = 15;
+ }
+ // FOR 1 TOUCHING CORNER SQUARE
+ else if (surround[3]) { //top right
+ d = 10;
+ } else if (surround[6]) { // bottom right
+ d = 14;
+ } else if (surround[9]) { //bottom left
+ d = 2;
+ } else if (surround[0]) { //top left
+ d = 6;
+ }
+
+
+
+ touchFlag = 0; //clear the touch flag
+
+ for (int i = 0; i<12; i++) { //clear the set points
+ surround[i] = 0;
+ }
+ }
+
+ return d;
+
+}
+
+void ball()
+
+{
+ int e = d;
+
+ setAngle();
+
+ doBricks();
+ moveBall1(d);
+ wait_ms(25);
+
+ if (d != e) return;
+
+ setAngle();
+ doBricks();
+
+ moveBall2(d);
+ wait_ms(25);
+
+}
+
+void moveBall1(int d) //move the ball, quantised into 16 directions of motion
+{
+ lcd.clearRect(bx,by,bw+1,bh+1);
+
+ if (d == 0) { //0deg (vertical up)
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 1) { //22.5deg
+ by-=1;
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 2) { //45deg
+ by-=1;
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 3) { //62.5deg
+ by-=1;
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 4) { //90deg (right)
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 5) { //112.5deg
+ bx+=1;
+ by+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 6) { //135deg
+ bx+=1;
+ by+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 7) { //157.5deg
+ bx+=1;
+ by+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 8) { //180deg (vertical down)
+ by++;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 9) { //202.5deg
+ by++;
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 10) { //225deg
+ by+=1;
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 11) { //247.5deg
+ by+=1;
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 12) { //270deg (left)
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 13) { //292.5deg
+ bx-=1;
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 14) { //315deg
+ bx-=1;
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 15) { //337.5deg
+ bx-=1;
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ }
+}
+
+
+void moveBall2(int d) //move the ball, quantised into 16 directions of motion
+{
+ lcd.clearRect(bx,by,bw+1,bh+1);
+
+ if (d == 0) { //0deg (vertical up)
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 1) { //22.5deg
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 2) { //45deg
+ by-=1;
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 3) { //62.5deg
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 4) { //90deg (right)
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 5) { //112.5deg
+ bx+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 6) { //135deg
+ bx+=1;
+ by+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 7) { //157.5deg
+ by+=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 8) { //180deg (vertical down)
+ by++;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 9) { //202.5deg
+ by++;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 10) { //225deg
+ by+=1;
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 11) { //247.5deg
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 12) { //270deg (left)
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 13) { //292.5deg
+ bx-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 14) { //315deg
+ bx-=1;
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ } else if (d == 15) { //337.5deg
+ by-=1;
+ lcd.drawRect(bx,by,bw,bh,bf);
+ lcd.refresh();
+ }
+}
+
+
+//set pixels around the edge of the board
+void borderInit()
+{
+
+ if (!displayGame) return;
+
+
+ for (int i =0; i<=75; i++) {
+ lcd.setPixel(i,0);
+ }
+ for (int i = 0; i<=48; i++) {
+ lcd.setPixel(0,i);
+ lcd.setPixel(75,i);
+ lcd.setPixel(83,i);
+ }
+ for (int i =75; i<84; i++) {
+ lcd.setPixel(i,31);
+ }
+ borderFlag = 1;
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/BrickBreaker.h Thu May 07 17:20:11 2015 +0000
@@ -0,0 +1,545 @@
+/**
+@file BrickBreaker.h
+
+@brief Header file containg function prototypes and variables
+
+*/
+
+
+#include "mbed.h"
+#include "N5110.h"
+#include "PinDetect.h"
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
+
+#define USR_POWERDOWN (0x104)
+
+// change this to alter tolerance of joystick direction
+#define DIRECTION_TOLERANCE 0.05
+
+//included for power control
+int semihost_powerdown() {
+
+ uint32_t arg;
+ return __semihost(USR_POWERDOWN, &arg);
+}
+
+
+
+
+// connections for joystick
+
+/** Joystick Button
+*
+* Button for menu navigation and other controls. PinDetect was used to overcome the inherent debounce of the buttons
+*/
+PinDetect button(p17);
+
+/** Joystick x-axis
+*
+* Horizontal motion of the joystick, read as a potentiometer.
+*/
+AnalogIn xPot(p15);
+
+/** Joystick y-axis
+*
+* Veritcal motion of the joystick, read as a potentiometer.
+*/
+AnalogIn yPot(p16);
+
+//connections for other controls
+
+/** Play/Pause
+*
+* Button to pause and resume the game. PinDetect was used to overcome the inherent debounce of the buttons
+*/
+PinDetect playPause(p12);
+
+/** Volume
+*
+* Potentiometer to control the volume of the buzzer
+*/
+AnalogIn volPot(p20);
+
+/** Noise Pin
+*
+* Pin to create noise for the randomisation of the levels
+*/
+AnalogIn noise(p19);
+
+
+/** Local File System
+*
+* Create Local filesystem to store the highscores
+*/
+
+LocalFileSystem local("local");
+
+/** Direction Name
+*
+* Enum type to store the names of the joystick directions
+*/
+enum DirectionName {
+ UP,
+ DOWN,
+ LEFT,
+ RIGHT,
+ CENTRE,
+ UPLEFT,
+ UPRIGHT,
+ DOWNLEFT,
+ DOWNRIGHT,
+ UNKNOWN
+};
+
+/** Joystick
+*
+* Struct for the Joystick
+*/
+typedef struct JoyStick Joystick;
+struct JoyStick {
+ float x; // current x value
+ float x0; // 'centred' x value
+ float y; // current y value
+ float y0; // 'centred' y value
+ int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
+ DirectionName direction; // current direction
+};
+
+Joystick joystick;
+
+/** High Score 1
+*
+* Variable to store the highscores from the memory while the program is running.
+* This is compared with the player's score at the end of the game and overwritten if necessary.
+*/
+int highscore1;
+
+/** High Score 2
+*
+* Variable to store the highscores from the memory while the program is running.
+* This is compared with the player's score at the end of the game and overwritten if necessary.
+*/
+int highscore2;
+
+/** High Score 3
+*
+* Variable to store the highscores from the memory while the program is running.
+* This is compared with the player's score at the end of the game and overwritten if necessary.
+*/
+int highscore3;
+
+
+/** Score Flag
+*
+* Set to 1 on initial conditions, set to 0 if a score overwrites a high score.
+* This is to stop the same score from overwriting multiple entries in the high scores.
+*/
+int scoreFlag = 1;
+
+/** Ball x-Position
+*
+* Stores the x-Position of the ball for passing to lcd.drawRect()
+*/
+int bx = 42;
+
+/** Ball y-Position
+*
+* Stores the y-position of the ball for passing to lcd.drawRect()
+*/
+int by = 30;
+
+/** Ball Width
+*
+* Stores the width of the ball for passing to lcd.drawRect()
+*/
+int bw = 1;
+
+/** Ball Height
+*
+* Stores the Height of the ball for passing to lcd.drawRect()
+*/
+int bh = 1;
+
+/** Ball Fill
+*
+* Stores the Fill State of the ball for passing to lcd.drawRect()
+*/
+int bf = 1;
+
+/** Ball Direction
+*
+* Direction of the ball, quantised into 16 directions of motion
+*/
+int d = 0;
+
+/** Paddle x-Position
+*
+* Stores the x-Position of the paddle for passing to lcd.drawRect()
+*/
+int px = 38;
+
+/** Paddle y-Position
+*
+* Stores the y-position of the paddle for passing to lcd.drawRect()
+*/
+int py = 40;
+
+/** Paddle Width
+*
+* Stores the width of the paddle for passing to lcd.drawRect()
+*/
+int pw = 8;
+
+/** Paddle Height
+*
+* Stores the Height of the paddle for passing to lcd.drawRect()
+*/
+int ph = 2;
+
+
+/** Ball Surround
+*
+* Array to store the active pixels around the ball
+*/
+int surround[12] = {};
+
+/** Touch Flag
+*
+* Set when the ball touches any set pixel on the screen to flag it for setAngle()
+*/
+int touchFlag = 0;
+
+/** Bricks x-Coordinates
+*
+* Array to index the x-positions of the bricks
+*/
+int bricksx[8] = {3, 12, 21, 30, 39, 48, 57, 66};
+
+/** Bricks y-Coordinates
+*
+* Array to index the y-positions of the bricks
+*/
+int bricksy[4] = {3, 8, 13, 18};
+
+/** State of Bricks
+*
+* Array to index the state of the bricks: 1 is set, 0 is cleared
+*/
+int bricks[4][8];
+
+/** Display Menu
+*
+* Display the menu - for passing to the main() function
+* One of several variables to control what is displayed on the screen
+*/
+int displayMenu = 1;
+
+/** Display Help
+*
+* Display the Help - for passing to the main() function
+* One of several variables to control what is displayed on the screen
+*/
+int displayHelp = 0;
+
+/** Display High Scores
+*
+* Display the High Scores - for passing to the main() function
+* One of several variables to control what is displayed on the screen
+*/
+int displayHighScores = 0;
+
+/** Display Game
+*
+* Display the Game - for passing to the main() function
+* One of several variables to control what is displayed on the screen
+*/
+int displayGame = 0;
+
+/** Display Game Over
+*
+* Display the Game Over - for passing to the main() function
+* One of several variables to control what is displayed on the screen
+*/
+int displayGameOver = 0;
+
+/** Menu Select
+*
+* Used to control the position of the cursor on the screen
+*/
+int menuSelect = 1;
+
+/** Help Screen
+*
+* Used to select which of the help pages to load
+*/
+int helpScreen = 0;
+
+/** Help Select
+*
+* Used to control the position of the cursor on the help screen
+*/
+int helpSelect = 35;
+
+/** Level
+*
+* Used to set the level - for passing to initBricks()
+*/
+int level = 0;
+
+/** Start/Stop
+*
+* Variable to determine whether the game is paused - 1 is start, 0 is stop.
+*/
+int startStop = 1;
+
+/** Lives
+*
+* Variable to store the remaining number of player lives
+*/
+int lives = 3;
+
+/** Score
+*
+* Variable to store the current player score
+*/
+int score = 0;
+
+/** Border Flag
+*
+* Set when the borders have been drawn to stop multiple attempts
+*/
+int borderFlag = 0;
+
+/** Brick Clear Flag
+*
+* Set to 1 for the bricks to be cleared, set to 0 once cleared to stop blank space from being repeatedly cleared
+*/
+int clearFlag[4][8];
+
+/** Brick Draw Flag
+*
+* Set to 1 for the bricks to be drawn, set to 0 once drawn to stop unnecessary overwriting
+*/
+int brickDrawFlag[4][8];
+
+/** Poll Joystick
+*
+* Timer to regularly update the game joystick values
+*/
+Ticker pollJoystick;
+
+/** Move Paddle
+*
+* Timer to regularly move the paddle
+*/
+Ticker movePaddle;
+
+/** Select
+*
+* Timer to update the cursor throughout the game menus
+*/
+Ticker select;
+
+/** Read High Scores
+*
+* Reads the current values of the high scores from the internal memory
+*/
+void readHighScores();
+
+/** Write High Scores
+*
+* Writes the updated values of the high scores to the internal memory
+*/
+void writeHighScores();
+
+
+/** Initialise the Bricks
+*
+* Populate the array storing the state of the bricks for drawing
+*
+* @param l - The game level to be initialised. Predefined when l<2 and random afterwards.
+*/
+void initBricks(int l);
+
+/** Do the Bricks
+*
+* Does getBrickTouch() on all bricks, draws any that are set to be drawn and clears any that are set to be cleared
+*/
+void doBricks();
+
+/** Get Brick Touch
+*
+* Checks to see if any of the pixels surrounding a given brick are set - i.e. if something is touching one
+*
+* @param x - the horizontal index of the brick check
+* @param y - the vertical index of the brick to check
+*/
+void getBrickTouch(int x, int y);
+
+/** Clear Bricks
+*
+* Loops through all bricks and clears any that are set to be cleared
+*/
+void clearBricks();
+
+/** Calibrate Joystick
+*
+* Initialises the built in button in the joystick in PullUp mode. Also reads the initial values of the
+* joystick for reference.
+*/
+void calibrateJoystick();
+
+/** Update Joystick
+*
+* Checks the x and y values of the potentiometers and assigns the joystick a direction based on that.
+*/
+void updateJoystick();
+
+/** Menu
+*
+* Displays the menu screen and moves the cursor to the current position
+*/
+void menu();
+
+/** Get Menu Select
+*
+* Checks the joystick direction and assigns the cursor a new position based on that
+*/
+void getMenuSelect();
+
+/** New Screen
+*
+* Called on a click of the joystick from the menu, changes the information displayed on the screen.
+* Depends on the position of the cursor
+*/
+void newScreen();
+
+/** Help
+*
+* Displays the various pages of help available, moves the cursor to the current position
+*/
+void help();
+
+/** High Scores
+*
+* Pulls the high score data from the internal memory and displays them on the screen
+*/
+void highScores();
+
+/** Game
+*
+* Pulls together various other functions to run the game
+*
+* @param g - If true, game runs. If false, game stops. Used to pause the game
+*/
+void game(int g);
+
+/** Ball
+*
+* @brief Move the ball
+*
+* 2-step movement for the ball - combines moveBall1() and moveBall2() with setAngle() and doBricks() to ensure game updates properly.
+*
+* NB: wait() functions were included to slow down the motion of the ball to a reasonable speed. A timer would have been preferable but the game
+* ceased to function if this implementation was used.
+*/
+void ball();
+
+/** Paddle
+*
+* @brief Move the paddle left and right
+*
+* Checks the position of the joystick and updates the postion of the paddle based on that.
+*
+* NB: The entire row is cleared when in theory only the prior position should need to be. In practise this does not work and the row does not
+* properly cleared.
+*/
+void paddle();
+
+/** Get Number of Bricks
+*
+* @return - Number of remaining bricks
+*
+* Finds the number of bricks remaining on the screen
+*/
+int getNumBricks();
+
+/** New Level
+*
+* @brief Updates the level and redraws the bricks
+*
+* Called when getNumBricks() returns 0
+*/
+void newLevel();
+
+/** Pause
+*
+* Reverses the value of StartStop to Pause/Unpause the game
+*/
+void pause();
+
+/** Move Ball (1)
+*
+* First iteration of ball motion
+*
+* @param d - The direction to move the ball in - quantised into 16 separate directions
+*/
+void moveBall1(int d);
+
+/** Move Ball (2)
+*
+* Second iteration of ball motion
+*
+* @param d - The direction to move the ball in - quantised into 16 separate directions
+*/
+void moveBall2(int d);
+
+/** Initialise the Borders
+*
+* Draws the boundaries so that the ball cannot leave the screen through the sides or top.
+* Also draws some graphics for containing the scores and remaining lives.
+*/
+void borderInit();
+
+/** Get Ball Touch Flag
+*
+* Check to see if the ball is touching anything
+*/
+void getTouchFlag();
+
+/** Set Ball Angle
+*
+* @return d The direction that the ball is to move in
+*
+* Set the new direction of the ball based on what the edges of the ball are touching and the current angle of the ball
+*/
+int setAngle();
+
+/** Displays Score
+*
+* Displays the current score on the screen. The score is displayed vertical but the score is often multiple
+* digits so modulus was used to split up the digits.
+*/
+void dispScore();
+
+
+/** Display Lives
+*
+* Displays the current number of remaining lives on the screen
+*/
+void dispLives();
+
+/** Lost Life
+*
+* @return 1 - If a life is lost. I think this was for debugging CHECK THIS LATER
+*/
+int lifeLost();
+
+/** Game Over
+*
+* Displays the dreaded Game Over screen. If the score is greater than any of the high scores they will be updated accordingly and the player informed.
+*/
+void gameOver();
+
+
--- a/main.cpp Thu May 07 11:48:50 2015 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,1194 +0,0 @@
-
-
-
-#include "mbed.h"
-#include "N5110.h"
-#include "PinDetect.h"
-#include "PowerControl/PowerControl.h"
-#include "PowerControl/EthernetPowerControl.h"
-
-#define USR_POWERDOWN (0x104)
-#define PI 3.14159265359
-
-// change this to alter tolerance of joystick direction
-#define DIRECTION_TOLERANCE 0.05
-
-//included for power control
-int semihost_powerdown() {
-
- uint32_t arg;
- return __semihost(USR_POWERDOWN, &arg);
-}
-
-//included for debugging
-DigitalOut myled(LED1);
-DigitalOut debug(LED2);
-
-
-//initialise the N5110 screen
-// VCC,SCE,RST,D/C,MOSI,SCLK,LED
-// \ \ | | / / /
-N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
-
-// connections for joystick
-PinDetect button(p17);
-AnalogIn xPot(p15);
-AnalogIn yPot(p16);
-
-//create filesystem to store the highscores
-LocalFileSystem local("local");
-
-//connections for other controls
-PinDetect playPause(p12);
-AnalogIn volPot(p20);
-AnalogIn noise(p19);
-
-//enum type to store the joystick directions
-enum DirectionName {
- UP,
- DOWN,
- LEFT,
- RIGHT,
- CENTRE,
- UPLEFT,
- UPRIGHT,
- DOWNLEFT,
- DOWNRIGHT,
- UNKNOWN
-};
-
-
-// struct for Joystick
-typedef struct JoyStick Joystick;
-struct JoyStick {
- float x; // current x value
- float x0; // 'centred' x value
- float y; // current y value
- float y0; // 'centred' y value
- int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
- DirectionName direction; // current direction
-};
-
-// create struct variable
-Joystick joystick;
-
-//flag for printing joystick value - no longer used
-int printFlag = 0;
-
-//variables to store the highscores
-int highscore1;
-int highscore2;
-int highscore3;
-
-//flag to stop the score from updating more than once
-int scoreFlag = 1;
-
-//ball position and parameters
-int bx = 42, by = 30, bw = 1,bh = 1, bf = 1;
-
-//paddle position and parameters
-int px = 38, py = 40, pw = 8, ph = 2;
-
-//ball angle
-int d = 0;
-
-//arrau to store the squares touching the ball
-int surround[12] = {};
-
-//set when ball is in contact with a set pixel
-int touchFlag = 0;
-
-//arrays to index the position of the bricks
-int bricksx[8] = {3, 12, 21, 30, 39, 48, 57, 66};
-int bricksy[4] = {3, 8, 13, 18};
-
-//array to store the state of the bricks
-int bricks[4][8];
-
-//select to set the screen to display
-int displayMenu = 1;
-int displayHelp = 0;
-int displayHighScores = 0;
-int displayGame = 0;
-int displayGameOver = 0;
-
-//variable to set the position of the cursor on the menu screen
-int menuSelect = 1;
-
-int helpScreen = 0;
-int helpSelect = 35;
-
-int level = 0;
-
-//determine whether the game is paused
-int startStop = 1;
-
-//store the remaining number of player lives
-int lives = 3;
-
-// store the current player score
-int score = 0;
-
-//set when the borders have been drawn
-int borderFlag = 0;
-
-//set when an object touches the bricks
-int clearFlag[4][8];
-
-//set when the bricks are initialised, clear when destroyed
-int brickDrawFlag[4][8];
-
-
-// timer to regularly read the joystick
-Ticker pollJoystick;
-
-//timer to move the game paddle
-Ticker movePaddle;
-
-//timer to draw the bricks
-Ticker drawBricks;
-
-//timer to move the cursor throughout the menus
-Ticker select;
-
-//function to read the saved values of the high scores from the memore
-void readHighScores();
-
-//function to write the new values for the high scores to the device memory
-void writeHighScores();
-
-//function to populate the ^array^
-void initBricks(int l);
-
-//function to draw the bricks
-void doBricks();
-
-//function to check if an object is touching the bricks
-void getBrickTouch(int x, int y);
-
-//function to clear the brick
-void clearBricks();
-
-// function prototypes for calibrating joystick
-void calibrateJoystick();
-
-//for updating joystick
-void updateJoystick();
-
-//function to display the menu screen
-void menu();
-
-//function to update the position of the menu cursor
-void getMenuSelect();
-
-//function to move to the new screen
-void newScreen();
-
-//function to display the help screens
-void help();
-
-//display the high score list
-void highScores();
-
-//run the game
-void game(int g);
-
-//move the ball
-void ball();
-
-//clear the paddle space for redrawing
-void clearPaddle();
-
-//move the paddle
-void paddle();
-
-//find the number of bricks still remaining on the screen
-int getNumBricks();
-
-//update the level
-void newLevel();
-
-//function to pause the game
-void pause();
-
-//first and second iterations of ball motion
-void moveBall1(int d);
-void moveBall2(int d);
-
-//draw the game borders
-void borderInit();
-
-//check to see if the ball is touching anything
-void getTouchFlag();
-
-//check what the ball has hit and set the new angle
-int setAngle();
-
-//display the current score on the screen in game
-void dispScore();
-
-
-//display the current number of lives remaining
-void dispLives();
-
-//handle what happens in the event of an in-game failure
-int lifeLost();
-
-//to show the game end screen
-void gameOver();
-
-
-
-int main() {
-
-
- PHY_PowerDown();
- int result = semihost_powerdown();
-
- lcd.init();
- calibrateJoystick();
- initBricks(0);
-
- readHighScores();
-
- pollJoystick.attach(&updateJoystick,1.0/10); // read joystick 10 times per second
- select.attach(&getMenuSelect,0.15);
-
- movePaddle.attach(&paddle,0.025);
-
- initBricks(0);
-
- button.attach_asserted(&newScreen);
- button.setSampleFrequency();
-
- playPause.mode(PullUp);
- playPause.attach_asserted(&pause);
- playPause.setSampleFrequency();
-
- while(1) {
-
- while(displayMenu == 1) {
- menu();
- }
- while(displayGame == 1) {
- game(startStop);
- if (lives < 0) {
- displayGame = 0;
- displayGameOver = 1;
- }
- if (getNumBricks() == 0) {
- newLevel();
- }
- }
- while(displayHelp == 1) {
- help();
- }
- while(displayHighScores == 1) {
- highScores();
- }
- while(displayGameOver == 1) {
- gameOver();
- }
- lcd.refresh();
-
- }
-}
-
-void readHighScores() {
- FILE *fp = fopen("/local/scores.txt","r");
- fscanf(fp,"%i",&highscore1);
- fscanf(fp,"%i",&highscore2);
- fscanf(fp,"%i",&highscore3);
- fclose(fp);
-}
-
-void writeHighScores() {
- FILE *fp = fopen("/local/scores.txt","w");
- fprintf(fp,"%i\r\n",highscore1);
- fprintf(fp,"%i\r\n",highscore2);
- fprintf(fp,"%i\r\n",highscore3);
- fclose(fp);
-}
-
-void newLevel()
-{
- lcd.clear();
- level++;
- initBricks(level);
- by = 30;
- bx = 42;
- px = 38;
- d = 8;
- lcd.refresh();
-
-
-}
-
-void pause()
-{
- if (displayGame == 1) {
- startStop = !startStop;
- myled = !myled;
- }
-}
-
-int getNumBricks() {
- int sum = 0;
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 4; j++) {
-
- sum+=bricks[j][i];
- }
- }
- return sum;
-}
-
-int lifeLost() {
- if (by >= 48 && by <= 50) {
- lives-=1;
- by = 30;
- bx = 42;
- px = 38;
- d = 8;
- startStop = 0;
- return 1;
- } else {
- return 0;
- }
-}
-
-void game(int g) {
- if (g == 1) {
- lifeLost();
- //myled = lifeLost();
- borderInit();
- dispScore();
- dispLives();
- ball();
- doBricks();
- } else if (g == 0) {
- debug = !debug;
- wait(0.2);
- }
-}
-
-void highScores() {
- readHighScores();
- lcd.clear();
- lcd.printString("HIGH SCORES",10,0);
- lcd.printString("1/",10,1);
- lcd.printNum(highscore1,30,1);
- lcd.printString("2/",10,2);
- lcd.printNum(highscore2,30,2);
- lcd.printString("3/",10,3);
- lcd.printNum(highscore3,30,3);
- lcd.printString("menu",10,5);
- lcd.printString(">",5,5);
- lcd.refresh();
- Sleep();
-}
-
-void help()
-{
- //menuSelect = 35;
- if (helpScreen == 0) {
- lcd.clear();
- lcd.printString("HELP",10,0);
- lcd.printString("Destroy bricks",0,1);
- lcd.printString("to reach the",0,2);
- lcd.printString("next level",0,3);
- lcd.printString("more",40,5);
- lcd.printString("menu",10,5);
- lcd.printString(">",helpSelect,5);
- lcd.refresh();
- Sleep();
- } else if (helpScreen == 1) {
- lcd.clear();
- lcd.printString("HELP",10,0);
- lcd.printString("Use joystick",0,1);
- lcd.printString("to control",0,2);
- lcd.printString("the paddle",0,3);
- lcd.printString("more",40,5);
- lcd.printString("menu",10,5);
- lcd.printString(">",helpSelect,5);
- lcd.refresh();
- Sleep();
- } else if (helpScreen == 2) {
- lcd.clear();
- lcd.printString("HELP",10,0);
- lcd.printString("Pause the game",0,1);
- lcd.printString("using the >/||",0,2);
- lcd.printString("button",0,3);
- lcd.printString("more",40,5);
- lcd.printString("menu",10,5);
- lcd.printString(">",helpSelect,5);
- lcd.refresh();
- Sleep();
- } else if (helpScreen == 3) {
- lcd.clear();
- lcd.printString("HELP",10,0);
- lcd.printString("Control volume",0,1);
- lcd.printString("using the blue",0,2);
- lcd.printString("potentiometer",0,3);
- lcd.printString("more",40,5);
- lcd.printString("menu",10,5);
- lcd.printString(">",helpSelect,5);
- lcd.refresh();
- Sleep();
- }
-}
-
-void gameOver() {
-
-
- lcd.clear();
- lcd.printString("GAME OVER",10,0);
- lcd.printString("score",10,1);
- lcd.printNum(score,42,1);
- lcd.printString(">menu",10,5);
- if ((score >= highscore1) && scoreFlag) {
- highscore3 = highscore2;
- highscore2 = highscore1;
- highscore1 = score;
- writeHighScores();
- scoreFlag = 0;
- } else if ((score >= highscore2) && scoreFlag) {
- highscore3 = highscore2;
- highscore2 = score;
- writeHighScores();
- scoreFlag = 0;
- } else if ((score >= highscore3) && scoreFlag) {
- highscore3 = score;
- writeHighScores();
- scoreFlag = 0;
- }
- if (score >= highscore3) {
- lcd.printString("NEW HIGH SCORE",0,3);
- }
- lcd.refresh();
- Sleep();
-}
-
-void newScreen()
-{
- if (displayMenu) {
- switch (menuSelect) {
- case 1:
- lcd.clear();
- displayMenu = 0;
- displayGame = 1;
- break;
- case 2:
- displayMenu = 0;
- displayHelp = 1;
- break;
- case 3:
- displayMenu = 0;
- displayHighScores = 1;
- break;
- }
- } else if (displayHelp) {
- switch (helpSelect) {
- case 5:
- displayMenu = 1;
- displayHelp = 0;
- break;
- case 35:
- helpScreen++;
- if (helpScreen > 3) {
- helpScreen = 0;
- }
- break;
- }
- } else if (displayHighScores) {
- displayMenu = 1;
- displayHighScores = 0;
- } else if (displayGameOver) {
- displayMenu = 1;
- displayGameOver = 0;
- }
-}
-
-void getMenuSelect()
-{
- if (displayMenu) {
-
-
- if (joystick.direction == DOWN) {
- lcd.clearRect(5,(menuSelect*8),5,8);
- if (menuSelect < 3) {
- menuSelect++;
- } else if (menuSelect == 3) {
- menuSelect = 1;
- }
- } else if (joystick.direction == UP) {
- lcd.clearRect(5,(menuSelect*8),5,8);
- if (menuSelect > 1) {
- menuSelect-=1;
- } else if (menuSelect == 1) {
- menuSelect = 3;
- }
- }
- } else if (displayHelp) {
-
-
- if (joystick.direction == RIGHT) {
- lcd.clearRect(helpSelect,40,5,8);
-
- if (helpSelect < 35) {
- helpSelect+=30;
- } else if (menuSelect == 35) {
- helpSelect = 5;
- }
- } else if (joystick.direction == LEFT) {
- lcd.clearRect(helpSelect,40,5,8);
-
- if (helpSelect > 5) {
- helpSelect-=30;
- } else if (menuSelect == 5) {
- helpSelect = 35;
- }
- }
- }
-}
-
-void menu() {
- lcd.clear();
- lcd.printString("BRICK BREAKER",0,0);
- lcd.printString("Start",10,1);
- lcd.printString("Help",10,2);
- lcd.printString("Scores",10,3);
- lcd.printString(">",5,menuSelect);
- lives = 3;
- score = 0;
- initBricks(0);
- startStop = 1;
- level = 0;
- lcd.refresh();
- Sleep();
-}
-
-void dispLives() {
-
- lcd.printNum(lives,77,5);
- lcd.printChar(0x80,77,4);
-}
-
-void dispScore() {
-
-
- if (score<10) {
- lcd.printNum(score,77,0);
- } else if (score<100) {
-
- lcd.printNum(((score/10)%10),77,0);
- lcd.printNum((score%10),77,1);
-
- } else if (score<1000) {
-
- lcd.printNum(((score/100)%10),77,0);
- lcd.printNum(((score/10)%10),77,1);
- lcd.printNum((score%10),77,2);
-
- }
-}
-
-void doBricks() {
-
- for (int x = 0; x<8; x++) {
- for (int y = 0; y<4; y++) {
- getBrickTouch(x,y);
- }
- }
-
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 8; j++) {
- if (bricks[i][j] && brickDrawFlag[i][j]) {
- lcd.drawRect(bricksx[j],bricksy[i], 6, 2, 1);
- brickDrawFlag[i][j] = 0;
- }
- }
- }
-
- clearBricks();
-}
-
-void initBricks(int l)
-{
-
- int bricksTemp[4][8];
-
- if (l == 0) {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 8; j++) {
- bricks[i][j] = 1;
- brickDrawFlag[i][j] = 1;
- }
- }
- } else if (l == 1) {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 8; j+=2) {
- bricks[i][j] = 1;
- brickDrawFlag[i][j] = 1;
- }
- }
- } else if (l > 1) {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 8; j++) {
- int mint = ceil((noise.read() * 100000));
- bricksTemp[i][j] = mint%100;
- if (bricksTemp[i][j] > ((l*57)%73)) {
- bricks[i][j] = 1;
- brickDrawFlag[i][j] = 1;
- }
- }
- }
- }
-}
-
-void clearBricks() {
- for (int i = 0; i < 8; i++) {
- for (int j = 0; j < 4; j++) {
- if (clearFlag[j][i]) {
- lcd.clearRect(bricksx[i],bricksy[j],7,3);
- clearFlag[j][i] = 0;
- score++;
-
- }
- }
- }
-
-}
-
-
-void getBrickTouch(int x, int y)
-{
-
- if (bricks[y][x]) {
- for (int a = -1; a < 7; a++) {
- if (lcd.getPixel(bricksx[x]+a,bricksy[y]-1)) {
- clearFlag[y][x] = 1;
- bricks[y][x] = 0;
- } else if (lcd.getPixel(bricksx[x]+a,bricksy[y]+3)) {
- clearFlag[y][x] = 1;
- bricks[y][x] = 0;
- }
- }
- for (int b = 0; b < 3; b++) {
- if (lcd.getPixel(bricksx[x]-1,bricksy[y]+b)) {
- clearFlag[y][x] = 1;
- bricks[y][x] = 0;
- } else if (lcd.getPixel(bricksx[x]+7,bricksy[y]+b)) {
- clearFlag[y][x] = 1;
- bricks[y][x] = 0;
- }
- }
- }
-}
-
-
-// read default positions of the joystick to calibrate later readings
-void calibrateJoystick()
-{
- button.mode(PullUp);
- // must not move during calibration
- joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
- joystick.y0 = yPot;
-}
-
-
-void updateJoystick()
-{
- if (startStop == 1) {
-
-
- // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
- joystick.x = xPot - joystick.x0;
- joystick.y = yPot - joystick.y0;
- // read button state
- //joystick.button = button;
-
- // calculate direction depending on x,y values
- // tolerance allows a little lee-way in case joystick not exactly in the stated direction
- if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
- joystick.direction = CENTRE;
- } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
- joystick.direction = UP;
- } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
- joystick.direction = DOWN;
- } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
- joystick.direction = RIGHT;
- } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
- joystick.direction = LEFT;
- } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) {
- joystick.direction = UPRIGHT;
- } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) {
- joystick.direction = DOWNRIGHT;
- } else if ( joystick.y < DIRECTION_TOLERANCE && joystick.x < -1*DIRECTION_TOLERANCE) {
- joystick.direction = UPLEFT;
- } else if ( joystick.x < -1*DIRECTION_TOLERANCE && joystick.y > -1*DIRECTION_TOLERANCE) {
- joystick.direction = DOWNLEFT;
- } else {
- joystick.direction = UNKNOWN;
- }
- } else if (startStop == 0) {
- joystick.direction = CENTRE;
- }
-
- // set flag for printing
- printFlag = 1;
-}
-
-//clear the paddle row: TODO clear only the paddle cells
-void clearPaddle()
-{
-
- for (int i = 0; i < 84; i++) {
- for (int j = py; j < py+3; j++) {
- lcd.clearPixel(i,j);
- lcd.clearPixel(i,j);
- lcd.clearPixel(i,j);
- }
- }
-}
-
-
-//function to move the game paddle left and right with the joystick
-void paddle()
-{
- if (!displayGame) return;
-
- lcd.clearRect(1,py,74,3);
-
- if ((joystick.direction == RIGHT || joystick.direction == UPRIGHT || joystick.direction == DOWNRIGHT) && (px < 67)) {
- px = px+1;
- lcd.drawRect(px,py,pw,ph,1);
- } else if ((joystick.direction == LEFT || joystick.direction == UPLEFT || joystick.direction == DOWNLEFT) && (px > 0)) {
- px = px-1;
- lcd.drawRect(px,py,pw,ph,1);
- } else {
- lcd.drawRect(px,py,pw,ph,1);
- }
-
-}
-
-
-
-//set the touchFlag if any of the pixels touching the ball are set
-//store which pixels are set in an array
-void getTouchFlag()
-{
- if (lcd.getPixel(bx-1,by)) {//11
- touchFlag = 1;
- surround[11] = 1;
- } if (lcd.getPixel(bx-1,by-1)) {//0
- touchFlag = 1;
- surround[0] = 1;
- } if (lcd.getPixel(bx,by-1)) {//1
- touchFlag = 1;
- surround[1] = 1;
- } if (lcd.getPixel(bx+1,by-1)) {//2
- touchFlag = 1;
- surround[2] = 1;
- } if (lcd.getPixel(bx+2,by-1)) {//3
- touchFlag = 1;
- surround[3] = 1;
- } if (lcd.getPixel(bx+2,by)) {//4
- touchFlag = 1;
- surround[4] = 1;
- } if (lcd.getPixel(bx+2,by+1)) {//5
- touchFlag = 1;
- surround[5] = 1;
- } if (lcd.getPixel(bx+2,by+2)) {//6
- touchFlag = 1;
- surround[6] = 1;
- } if (lcd.getPixel(bx+1,by+2)) {//7
- touchFlag = 1;
- surround[7] = 1;
- } if (lcd.getPixel(bx,by+2)) {//8
- touchFlag = 1;
- surround[8] = 1;
- } if (lcd.getPixel(bx-1,by+2)) {//9
- touchFlag = 1;
- surround[9] = 1;
- } if (lcd.getPixel(bx-1,by+1)) {//10
- touchFlag = 1;
- surround[10] = 1;
- }
-}
-
-//work out what the ball has hit and calculate/set the new angle accordingly
-int setAngle()
-{
- getTouchFlag();
-
- if (touchFlag) {
-
- //FOR 3 CORNER SQUARES
- if (surround[11] && surround[0] && surround[1]) { //top right corner
- d = 6;
- } else if (surround[2] && surround[3] && surround[4]) { //bottom right corner
- d = 10;
- } else if (surround[5] && surround[6] && surround[7]) { //bottom left corner
- d = 14;
- } else if (surround[8] && surround[9] && surround[10]) { //top left corner
- d = 2;
- }
- //FOR 3 TOUCHING OFFCENTRE EDGE SQUARES
- else if (surround[0] && surround[1] && surround[2] && !surround[3]) { //top edge left
- d = 5;
- } else if (surround[3] && surround[1] && surround[2] && !surround[0]) { //top edge right
- d = 11;
- } else if (surround[6] && surround[7] && surround[8] && !surround[9]) { // low edge right
- d = 13;
- } else if (surround[7] && surround[8] && surround[9] && !surround[6]) { //low edge left
- d = 3;
- } else if (surround[0] && surround[11] && surround[10] && !surround[9]) { //left edge top
- d = 5;
- } else if (surround[3] && surround[4] && surround[5] && !surround[6]) { //right edge top
- d = 11;
- } else if (surround[11] && surround[10] && surround[9] && !surround[0]) { //left edge low
- d = 3;
- } else if (surround[4] && surround[5] && surround[6] && !surround[3]) { //right edge low
- d = 13;
- }
- //FOR CeNTRE SQUARES
- else if (surround[1] && surround[2]) { //top edge
- switch (d) {
- case 3:
- d = 5;
- break;
- case 2:
- d = 6;
- break;
- case 1:
- d = 7;
- break;
- case 0:
- d = 8;
- break;
- case 15:
- d = 9;
- break;
- case 14:
- d = 10;
- break;
- case 13:
- d = 11;
- break;
- }
-
- } else if (surround[4] && surround[5]) { //RHS edge
- switch (d) {
- case 1:
- d = 15;
- break;
- case 2:
- d = 14;
- break;
- case 3:
- d = 13;
- break;
- case 4:
- d = 12;
- break;
- case 5:
- d = 11;
- break;
- case 6:
- d = 10;
- break;
- case 7:
- d = 9;
- break;
- }
-
- } else if (surround[10] && surround[11]) { //LHS edge
- switch (d) {
- case 9:
- d = 7;
- break;
- case 10:
- d = 6;
- break;
- case 11:
- d = 5;
- break;
- case 12:
- d = 4;
- break;
- case 13:
- d = 3;
- break;
- case 14:
- d = 2;
- break;
- case 15:
- d = 1;
- break;
- }
- } else if (surround[7] && surround[8]) { //bottom edge
- switch (d) {
- case 5:
- d = 3;
- break;
- case 6:
- d = 2;
- break;
- case 7:
- d = 1;
- break;
- case 8:
- d = 0;
- break;
- case 9:
- d = 15;
- break;
- case 10:
- d = 14;
- break;
- case 11:
- d = 13;
- break;
- }
- }
-
- //FOR 2 TOUCHING OFFCENTRE EDGE SQUARES
- else if (surround[0] && surround[1]) { //top edge left
- d = 9;
- } else if (surround[2] && surround[3]) { //top edge right
- d = 7;
- } else if (surround[9] && surround[8]) { // low edge right
- d = 1;
- } else if (surround[7] && surround[6]) { //low edge left
- d = 15;
- } else if (surround[0] && surround[11]) { //left edge top
- d =7;
- } else if (surround[3] && surround[4]) { //right edge top
- d = 9;
- } else if (surround[10] && surround[9]) { //left edge low
- d = 1;
- } else if (surround[5] && surround[6]) { //right edge low
- d = 15;
- }
- // FOR 1 TOUCHING CORNER SQUARE
- else if (surround[3]) { //top right
- d = 10;
- } else if (surround[6]) { // bottom right
- d = 14;
- } else if (surround[9]) { //bottom left
- d = 2;
- } else if (surround[0]) { //top left
- d = 6;
- }
-
-
-
- touchFlag = 0; //clear the touch flag
-
- for (int i = 0; i<12; i++) { //clear the set points
- surround[i] = 0;
- }
- }
-
- return d;
-
-}
-
-void ball()
-
-{
-
- setAngle();
- doBricks();
- moveBall1(d);
- wait_ms(25);
-
- setAngle();
- doBricks();
- moveBall2(d);
- wait_ms(25);
-
-}
-
-void moveBall1(int d) //move the ball, quantised into 16 directions of motion
-{
- lcd.clearRect(bx,by,bw+1,bh+1);
-
- if (d == 0) { //0deg (vertical up)
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 1) { //22.5deg
- by-=1;
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 2) { //45deg
- by-=1;
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 3) { //62.5deg
- by-=1;
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 4) { //90deg (right)
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 5) { //112.5deg
- bx+=1;
- by+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 6) { //135deg
- bx+=1;
- by+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 7) { //157.5deg
- bx+=1;
- by+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 8) { //180deg (vertical down)
- by++;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 9) { //202.5deg
- by++;
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 10) { //225deg
- by+=1;
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 11) { //247.5deg
- by+=1;
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 12) { //270deg (left)
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 13) { //292.5deg
- bx-=1;
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 14) { //315deg
- bx-=1;
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 15) { //337.5deg
- bx-=1;
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- }
-}
-
-
-void moveBall2(int d) //move the ball, quantised into 16 directions of motion
-{
- lcd.clearRect(bx,by,bw+1,bh+1);
-
- if (d == 0) { //0deg (vertical up)
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 1) { //22.5deg
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 2) { //45deg
- by-=1;
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 3) { //62.5deg
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 4) { //90deg (right)
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 5) { //112.5deg
- bx+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 6) { //135deg
- bx+=1;
- by+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 7) { //157.5deg
- by+=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 8) { //180deg (vertical down)
- by++;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 9) { //202.5deg
- by++;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 10) { //225deg
- by+=1;
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 11) { //247.5deg
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 12) { //270deg (left)
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 13) { //292.5deg
- bx-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 14) { //315deg
- bx-=1;
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- } else if (d == 15) { //337.5deg
- by-=1;
- lcd.drawRect(bx,by,bw,bh,bf);
- lcd.refresh();
- }
-}
-
-
-//set pixels around the edge of the board
-void borderInit()
-{
-
- if (!displayGame) return;
-
-
- for (int i =0; i<=75; i++) {
- lcd.setPixel(i,0);
- }
- for (int i = 0; i<=48; i++) {
- lcd.setPixel(0,i);
- lcd.setPixel(75,i);
- lcd.setPixel(83,i);
- }
- for (int i =75; i<84; i++) {
- lcd.setPixel(i,31);
- }
- borderFlag = 1;
-
-}