University project - accelerometer controlled Arkanoid
Dependencies: KS0108 MMA8451Q mbed
Revision 0:9515d58c3408, committed 2015-01-18
- Comitter:
- JSindelar
- Date:
- Sun Jan 18 19:27:16 2015 +0000
- Child:
- 1:d08d63a7c50d
- Commit message:
- University project - accelerometer controlled Arkanoid
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KS0108.lib Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/DimiterK/code/KS0108/#e4b50f4c13a8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA8451Q.lib Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/emilmont/code/MMA8451Q/#c4d879a39775
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backup.cpp Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,22 @@ +/* +unsigned char MovePad(void){ + static unsigned char PaddlePos = (unsigned char)(0.5*(minX+maxX)); + unsigned char PaddleDif; + //static unsigned int PaddlePos = 82; + if ((abs(acc.getAccY()))>AccTres1){ + display.FullRectangle(PaddlePos, PadY, PaddlePos+PadLength ,PadY+2,WHITE); + if ((abs(acc.getAccY()))>AccTres2){ + PaddleDif = 6; + }else{ + PaddleDif = 2; + } + if ((acc.getAccY() > 0)&&((PaddlePos+PadLength) < (minX - 3))){ + PaddlePos += PaddleDif; + } + if ((acc.getAccY() < 0)&&(PaddlePos > (maxX + 1))){ + PaddlePos -= PaddleDif; + } + } + display.FullRectangle(PaddlePos, PadY, PaddlePos+PadLength ,PadY+2,BLACK); + return PaddlePos; +}*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backup2.cpp Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,195 @@ +/* +#include "mbed.h" +#include "KS0108.h" +#include "Arial12.h" +#include "SystemFont5x7.h" +#include "stdbool.h" +#include "MMA8451Q.h" + +#define step (4) +#define minX (125) +#define maxX (39) +#define minY (2) +#define maxY (62) +#define PadLength (10) +#define PadY (59) +#define LowSpeed (2) +#define HighSpeed (4) +#define XPadInit ((unsigned char)(0.5*(minX+maxX-PadLength))) +#define XBallInit ((unsigned char)(XPadInit+0.5*PadLength)) +#define BallD (1) +#define YBallInit ((unsigned char)(PadY-BallD)) + +#define BrickH (4) +#define BrickW (20) + +#define AccTres1 (0.12) +#define AccTres2 (0.25) + +#define MMA8451_I2C_ADDRESS (0x1d<<1) + +KS0108 display (PTE22,PTE23, PTE29, PTE30, PTE20, PTE21, PTC7, PTC0, PTC3, PTC4, PTC5, PTC6, PTC10, PTC11); + +void DrawBorder(); +void DrawBall(unsigned char* X, unsigned char* Y, char* dx, char* dy, unsigned char D, unsigned char PaddlePos, bool* Start, unsigned char* lives); +//unsigned char MovePad(void); +void MovePad(unsigned char* Pad_ptr, bool Start); +void DrawLogo(void); +void DrawTable(char sc, char liv); +void reset(unsigned char* XBall, unsigned char* YBall, char* dxBall, char* dyBall, unsigned char* XPaddle, bool* Start, unsigned char score, unsigned char lives); +void DrawBricks(void); + +MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); + +int main() { + + display.ClearScreen(); + + unsigned char score = 0; + unsigned char lives = 3; + DrawLogo(); + DrawTable(score,lives); + DrawBorder(); + DrawBricks(); + + unsigned char XBall = XBallInit; + unsigned char YBall = YBallInit; + char Balldx = 2; + char Balldy = -2; + + unsigned char PadPos = XPadInit; + bool StartFlag = true; + + + while(1) { + display.TurnOn(); + if (StartFlag){ + reset(&XBall, &YBall, &Balldx, &Balldy, &PadPos, &StartFlag, score, lives); + }else{ + display.FullCircle(2,2,2,WHITE); + } + DrawBall(&XBall, &YBall, &Balldx, &Balldy, BallD, PadPos, &StartFlag, &lives); + MovePad(&PadPos, StartFlag); + wait(0.05); + } + +} + +void DrawLogo(void){ + display.SelectFont(Arial12,BLACK,ReadData); + display.GotoXY(8,8); + display.PrintString("ARM"); + display.GotoXY(4,24); + display.PrintString("kanoid"); +} + +void DrawTable(char sc, char liv){ + static char score[5]; + static char lives[5]; + display.EmptyRectangle(4,38, (maxX-6),56, BLACK); + display.FullRectangle(20,39,(maxX-7), 55, WHITE); + display.SelectFont(System5x7,BLACK,ReadData); + sprintf(score,"S:%2d",sc); + sprintf(lives,"L:%2d",liv); + display.GotoXY(8,40); + display.PrintString(score); + display.GotoXY(8,48); + display.PrintString(lives); +} + +void DrawBorder(void){ + display.HLine(maxX-2, 127,0,BLACK); + display.VLine(maxX-2, 0,63,BLACK); + display.VLine(minX+2, 0,63,BLACK); +} + +void DrawBall(unsigned char* X, unsigned char* Y, char* dx, char* dy, unsigned char D, unsigned char PaddlePos, bool* Start, unsigned char* lives){ + + display.FullCircle(*X, *Y, D, WHITE); + + (*X) += (*dx); + (*Y) += (*dy); + display.FullCircle(*X, *Y, D, BLACK); + //if (!(*Start)){ + if ((((unsigned char)(*X+D)) >= minX)||(((char)(*X-D)) <= maxX)){ + *dx = -(*dx); + } + + if (((unsigned char)(*Y-D)) <= minY){ + *dy = -(*dy); + } + if (((char)(*Y+D)) >= PadY){ + if ((((unsigned char)(*X+D)) <= (PaddlePos+PadLength+2))&&(((unsigned char)(*X-D)) >= (PaddlePos-2))){ + *dy = -(*dy); + }else{ + if (*lives > 1){ + (*lives)--; + *Start = true; + }else{ + //doplnit kod na game over + } + } + } + /*}else{ + display.FullCircle(*X, *Y, D, WHITE); + } +} + +void MovePad(unsigned char* Pad_ptr, bool Start){ + unsigned char PaddleDif; + if ((abs(acc.getAccY()))>AccTres1){ + display.FullRectangle(*Pad_ptr, PadY, *Pad_ptr+PadLength ,PadY+2,WHITE); + if(Start){ + *Pad_ptr = XPadInit; + }else{ + if ((abs(acc.getAccY()))>AccTres2){ + PaddleDif = HighSpeed; + }else{ + PaddleDif = LowSpeed; + } + if ((acc.getAccY() > 0)&&((*Pad_ptr+PadLength) < (minX - 3))){ + *Pad_ptr += PaddleDif; + } + if ((acc.getAccY() < 0)&&(*Pad_ptr > (maxX + 1))){ + *Pad_ptr -= PaddleDif; + } + } + } + display.FullRectangle(*Pad_ptr, PadY, *Pad_ptr+PadLength ,PadY+2,BLACK); +} + +void reset(unsigned char* XBall, unsigned char* YBall, char* dxBall, char* dyBall, unsigned char* XPaddle, bool* Start, unsigned char score, unsigned char lives){ + display.FullCircle(*XBall, *YBall, BallD, WHITE); + *XBall = XBallInit; + *YBall = YBallInit; + display.FullRectangle(*XPaddle, PadY, *XPaddle+PadLength ,PadY+2,WHITE); + *XPaddle = XPadInit; + wait(1); + display.FullCircle(*XBall, *YBall, BallD, BLACK); + display.FullRectangle(*XPaddle, PadY, *XPaddle+PadLength ,PadY+2,BLACK); + DrawTable(score, lives); + *dxBall = 2; + *dyBall = -2; + *Start = false; + wait(1); +} + +void DrawBricks(void){ + struct brick { + unsigned char Xbrick; + unsigned char Ybrick; + bool exists; + }BrickArray[4][3], cihla; + + char i; + char j; + for (j = 0; j < 3; j++){ + for (i = 0; i < 4; i++){ + cihla.Xbrick = maxX + i*(BrickW + 2); + cihla.Ybrick = minY + j*(BrickH + 2); + cihla.exists = true; + BrickArray[i][j] = cihla; + display.EmptyRectangle(cihla.Xbrick,cihla.Ybrick, (cihla.Xbrick + BrickW), (cihla.Ybrick + BrickH) ,((cihla.exists)? BLACK:WHITE)); + } + } +}*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/backup3_non_functional_ptrs2struct_2Darray.cpp Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,242 @@ +/* +#include "mbed.h" +#include "KS0108.h" +#include "Arial12.h" +#include "SystemFont5x7.h" +#include "stdbool.h" +#include "MMA8451Q.h" + +#define step (4) +#define minX (125) +#define maxX (39) +#define minY (2) +#define maxY (62) +#define PadLength (10) +#define PadY (59) +#define LowSpeed (2) +#define HighSpeed (4) +#define XPadInit ((unsigned char)(0.5*(minX+maxX-PadLength))) +#define XBallInit ((unsigned char)(XPadInit+0.5*PadLength)) +#define BallD (1) +#define YBallInit ((unsigned char)(PadY-BallD)) + +#define BrickH (4) +#define BrickW (20) +#define columns (4) +#define rows (3) + +#define AccTres1 (0.12) +#define AccTres2 (0.25) + +#define MMA8451_I2C_ADDRESS (0x1d<<1) + +KS0108 display (PTE22,PTE23, PTE29, PTE30, PTE20, PTE21, PTC7, PTC0, PTC3, PTC4, PTC5, PTC6, PTC10, PTC11); + +//prototypes of functions +void DrawBorder(); +void DrawBall(unsigned char* X, unsigned char* Y, char* dx, char* dy, unsigned char D, unsigned char PaddlePos, bool* Start, unsigned char* lifes); +void MovePad(unsigned char* Pad_ptr, bool Start); +void DrawLogo(void); +void DrawTable(char sc, char liv); +void reset(unsigned char* XBall, unsigned char* YBall, char* dxBall, char* dyBall, unsigned char* XPaddle, bool* Start, unsigned char score, unsigned char lifes); +void CreateGrid(unsigned char* col, unsigned char* ro); +void DrawBricks(unsigned char col[columns], unsigned char ro[rows]); + +MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); + +//main program +int main() { + + display.ClearScreen(); + + unsigned char score = 0; //initial score and number of lifes + unsigned char lifes = 3; + DrawLogo(); //drawing ARM-kanoid title + DrawTable(score,lifes); //drawing table with score and number of lifes + DrawBorder(); //drawing border of the playing area + + unsigned char XBall = XBallInit; + unsigned char YBall = YBallInit; + char Balldx = 2; + char Balldy = -2; + + unsigned char PadPos = XPadInit; //initializing position of the paddle + bool StartFlag = true; + + unsigned char column[columns]; + unsigned char row[rows]; + + struct brick { //structure for arkanoid bricks description (along with constants brickH and brickW) + unsigned char Xbrick; + unsigned char Ybrick; + bool exists; + }BrickArray[rows][columns], cihla; //array of bricks, single brick + + //struct brick (*BrickArr_ptr)[rows][columns] = &BrickArray; //pointer to array of bricks + + struct brick (*BrickArr_ptr)[rows][columns]; + BrickArr_ptr = BrickArray; + CreateGrid(&(column[0]), &(row[0])); //creating grid for bricks + DrawBricks(column, row); //drawing bricks + + while(1) { + display.TurnOn(); + if (StartFlag){ + reset(&XBall, &YBall, &Balldx, &Balldy, &PadPos, &StartFlag, score, lifes); + } + DrawBall(&XBall, &YBall, &Balldx, &Balldy, BallD, PadPos, &StartFlag, &lifes); //function for drawing the ball, computing its movement, bounces etc. + MovePad(&PadPos, StartFlag); //function for paddle drawing and its control by accelerometer + wait(0.05); + } + +} + +void DrawLogo(void){ + display.SelectFont(Arial12,BLACK,ReadData); + display.GotoXY(8,8); + display.PrintString("ARM"); + display.GotoXY(4,24); + display.PrintString("kanoid"); +} + +void DrawTable(char sc, char liv){ + static char score[5]; + static char lifes[5]; + display.EmptyRectangle(4,38, (maxX-6),56, BLACK); + display.FullRectangle(20,39,(maxX-7), 55, WHITE); + display.SelectFont(System5x7,BLACK,ReadData); + sprintf(score,"S:%2d",sc); + sprintf(lifes,"L:%2d",liv); + display.GotoXY(8,40); + display.PrintString(score); + display.GotoXY(8,48); + display.PrintString(lifes); +} + +void DrawBorder(void){ + display.HLine(maxX-2, 127,0,BLACK); + display.VLine(maxX-2, 0,63,BLACK); + display.VLine(minX+2, 0,63,BLACK); +} + +void DrawBall(unsigned char* X, unsigned char* Y, char* dx, char* dy, unsigned char D, unsigned char PaddlePos, bool* Start, unsigned char* lifes){ + + display.FullCircle(*X, *Y, D, WHITE); + + (*X) += (*dx); + (*Y) += (*dy); + display.FullCircle(*X, *Y, D, BLACK); + //if (!(*Start)){ + if ((((unsigned char)(*X+D)) >= minX)||(((char)(*X-D)) <= maxX)){ + *dx = -(*dx); + } + + if (((unsigned char)(*Y-D)) <= minY){ + *dy = -(*dy); + } + if (((char)(*Y+D)) >= PadY){ + if ((((unsigned char)(*X+D)) <= (PaddlePos+PadLength+2))&&(((unsigned char)(*X-D)) >= (PaddlePos-2))){ + *dy = -(*dy); + }else{ + if (*lifes > 1){ + (*lifes)--; + *Start = true; + }else{ + //doplnit kod na game over + } + } + } + +} + +void MovePad(unsigned char* Pad_ptr, bool Start){ + unsigned char PaddleDif; + if ((abs(acc.getAccY()))>AccTres1){ + display.FullRectangle(*Pad_ptr, PadY, *Pad_ptr+PadLength ,PadY+2,WHITE); + if(Start){ + *Pad_ptr = XPadInit; + }else{ + if ((abs(acc.getAccY()))>AccTres2){ + PaddleDif = HighSpeed; + }else{ + PaddleDif = LowSpeed; + } + if ((acc.getAccY() > 0)&&((*Pad_ptr+PadLength) < (minX - 3))){ + *Pad_ptr += PaddleDif; + } + if ((acc.getAccY() < 0)&&(*Pad_ptr > (maxX + 1))){ + *Pad_ptr -= PaddleDif; + } + } + } + display.FullRectangle(*Pad_ptr, PadY, *Pad_ptr+PadLength ,PadY+2,BLACK); +} + +void reset(unsigned char* XBall, unsigned char* YBall, char* dxBall, char* dyBall, unsigned char* XPaddle, bool* Start, unsigned char score, unsigned char lifes){ + display.FullCircle(*XBall, *YBall, BallD, WHITE); + *XBall = XBallInit; + *YBall = YBallInit; + display.FullRectangle(*XPaddle, PadY, *XPaddle+PadLength ,PadY+2,WHITE); + *XPaddle = XPadInit; + wait(1); + display.FullCircle(*XBall, *YBall, BallD, BLACK); + display.FullRectangle(*XPaddle, PadY, *XPaddle+PadLength ,PadY+2,BLACK); + DrawTable(score, lifes); + *dxBall = 2; + *dyBall = -2; + *Start = false; + wait(1); +} + +void CreateGrid(unsigned char* col, unsigned char* ro){ + char i; + for (i = 0; i < columns; i++){ + *(col+i) = maxX + i*(BrickW + 2); + } + for (i = 0; i < columns; i++){ + *(ro+i) = minY + i*(BrickH + 2); + } + } + +void DrawBricks(unsigned char col[columns], unsigned char ro[rows]){ + struct brick { + unsigned char Xbrick; + unsigned char Ybrick; + bool exists; + }BrickArray[rows][columns], cihla; + + char i; + char j; + for (j = 0; j < rows; j++){ + for (i = 0; i < columns; i++){ + cihla.Xbrick = col[i]; + cihla.Ybrick = ro[j]; + cihla.exists = true; + BrickArray[j][i] = cihla; + display.FullRectangle(cihla.Xbrick,cihla.Ybrick, (cihla.Xbrick + BrickW), (cihla.Ybrick + BrickH) ,((cihla.exists)? BLACK:WHITE)); + } + } +} + +void BounceBricks(unsigned char* XBall, unsigned char* YBall, char* dxBall, char* dyBall, struct brick* ArrayOfBricks, struct brick* ExBrick, unsigned char ro[rows], unsigned char col[columns], char* dx, char* dy, unsigned char* score){ + if (*YBall <= ro[rows-1]){ + char i, j; + for (i = columns - 1; i >= 0; i--){ + if (*XBall >= col[i]){ + break; + } + } + for (j = rows - 1; j >= 0; j--){ + if (*YBall >= ro[j]){ + break; + } + } + + if ((*ArrayOfBricks)[i][j].exists){ + (ArrayOfBricks)[i][j].exists = false; + *score++; + *dy = -(*dy); + } + } +} +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,279 @@ +#include "mbed.h" +#include "KS0108.h" +#include "Arial12.h" +#include "SystemFont5x7.h" +#include "stdbool.h" +#include "MMA8451Q.h" +#include <time.h> +#include <stdlib.h> +#include <math.h> +#include <inttypes.h> + +#define minX (125) +#define maxX (39) +#define minY (2) +#define maxY (62) +#define PadLength (10) +#define PadY (59) +#define LowSpeed (2) +#define HighSpeed (4) +#define XPadInit ((uint8_t)(0.5*(minX+maxX-PadLength))) +#define XBallInit ((uint8_t)(XPadInit+0.5*PadLength)) +#define BallD (1) +#define YBallInit ((uint8_t)(PadY-BallD)) + +#define BrickH (4) +#define BrickW (20) +#define columns (4) +#define rows (3) + +#define maxangle (25) +#define velocity (3) +#define pi (3.14) + +#define round(x) ((x)>=0?(int8_t)((x)+0.5):(int8_t)((x)-0.5)) + +#define AccTres1 (0.12) +#define AccTres2 (0.25) + +#define MMA8451_I2C_ADDRESS (0x1d<<1) + +KS0108 display (PTE22,PTE23, PTE29, PTE30, PTE20, PTE21, PTC7, PTC0, PTC3, PTC4, PTC5, PTC6, PTC10, PTC11); + +//prototypes of functions +void DrawBorder(); //function for drawing border around gaming area +void DrawBall(uint8_t* X, uint8_t* Y, int8_t* dx, int8_t* dy, uint8_t D, uint8_t PaddlePos, bool* Start, uint8_t* lives); //function for drawing the ball +void MovePad(uint8_t* Pad_ptr, bool Start); //function for drawing the accelerometer controlled pad +void DrawLogo(void); //function for drawing logo +void DrawTable(int8_t sc, int8_t liv); //function for drawing table with score and number of lives +void reset(uint8_t* XBall, uint8_t* YBall, int8_t* dxBall, int8_t* dyBall, uint8_t* XPaddle, bool* Start, uint8_t *score, uint8_t *lives, uint8_t* col, uint8_t* ro, bool* Exists); //function for reset the game - after the ball was lost, game has been won or lost +void CreateGrid(uint8_t* col, uint8_t* ro, bool* Exists); //function for preparing the grid for array of bricks +void DrawBricks(uint8_t col[columns], uint8_t ro[rows], bool exists[rows*columns]); //drawing the array of bricks +void BounceBricks(uint8_t XBall, uint8_t YBall, int8_t* dxBall, int8_t* dyBall, bool* BrickExists, uint8_t ro[rows], uint8_t col[columns], uint8_t* score, uint8_t lives, bool* Start); //function detecting hits, erasing bricks, counting score etc... + +MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); +//main program +int main() { + srand(time(NULL)); //random seed - will be used for random reflection angle when the game starts/was reset + display.ClearScreen(); + + uint8_t score = 0; //initial score and number of lives + uint8_t lives = 3; + DrawLogo(); //drawing ARM-kanoid title + DrawTable(score,lives); //drawing table with score and number of lives + DrawBorder(); //drawing border of the playing area + + uint8_t XBall = XBallInit; //initial ball position + uint8_t YBall = YBallInit; + int8_t Balldx; + int8_t Balldy; + + uint8_t PadPos = XPadInit; //initializing position of the paddle + bool StartFlag = true; //start flag will be used for reseting the game + + uint8_t column[columns]; //arrays of brick positions on the display - will be filled by function CreateGrid(...) + uint8_t row[rows]; + bool BrickExists[columns*rows]; //array of bool flags for bricks - true = brick exists, false = brick was already destroyed... will be also filled by function CreateGrid(...) + + CreateGrid(&(column[0]), &(row[0]), &(BrickExists[0])); //creating grid for bricks + DrawBricks(column, row, BrickExists); //drawing bricks + + while(1) { + display.TurnOn(); + if (StartFlag){ + reset(&XBall, &YBall, &Balldx, &Balldy, &PadPos, &StartFlag, &score, &lives, &(column[0]), &(row[0]), &(BrickExists[0])); //reset in case lost ball, lost or won game + } + BounceBricks(XBall, YBall, &Balldx, &Balldy, &(BrickExists[0]), row, column, &score, lives, &StartFlag); + DrawBall(&XBall, &YBall, &Balldx, &Balldy, BallD, PadPos, &StartFlag, &lives); //function for drawing the ball, computing its movement, bounces etc. + MovePad(&PadPos, StartFlag); //function for paddle drawing and its control by accelerometer + wait(0.05); //waiting time was set empirically - according to feel about the display refresh rate + } +} + +void DrawLogo(void){ //draws the logo (ARM-kanoid) + display.SelectFont(Arial12,BLACK,ReadData); + display.GotoXY(8,8); + display.PrintString("ARM"); + display.GotoXY(4,24); + display.PrintString("kanoid"); +} + +void DrawTable(int8_t sc, int8_t liv){ //draws table with score and lives + static char score[5]; + static char lives[5]; + display.EmptyRectangle(4,38, (maxX-6),56, BLACK); + display.FullRectangle(20,39,(maxX-7), 55, WHITE); + display.SelectFont(System5x7,BLACK,ReadData); + sprintf(score,"S:%2d",sc); + sprintf(lives,"L:%2d",liv); + display.GotoXY(8,40); + display.PrintString(score); + display.GotoXY(8,48); + display.PrintString(lives); +} + +void DrawBorder(void){ //draws border around gaming area + display.HLine(maxX-2, 127,0,BLACK); + display.VLine(maxX-2, 0,63,BLACK); + display.VLine(minX+2, 0,63,BLACK); +} + +void DrawBall(uint8_t* X, uint8_t* Y, int8_t* dx, int8_t* dy, uint8_t D, uint8_t PaddlePos, bool* Start, uint8_t* lives){ //draws the ball, computes reflections from border of gaming area + + display.FullCircle(*X, *Y, D, WHITE); + + (*X) += (*dx); + (*Y) += (*dy); + display.FullCircle(*X, *Y, D, BLACK); + if ((((uint8_t)(*X+D)) >= minX)||(((int8_t)(*X-D)) <= maxX)){ + *dx = -(*dx); + } + if (((uint8_t)(*Y-D)) <= minY){ + *dy = -(*dy); + } + if (((int8_t)(*Y+D)) >= PadY){ + if ((((uint8_t)(*X+D)) <= (PaddlePos+PadLength+2))&&(((uint8_t)(*X-D)) >= (PaddlePos-2))){ + *dy = -(*dy); + }else{ + *Start = true; + if (*lives > 0){ + (*lives)--; + } + } + } +} + +void MovePad(uint8_t* Pad_ptr, bool Start){ //drawing the accelerometer-controlled pad + uint8_t PaddleDif; + if ((abs(acc.getAccY()))>AccTres1){ + display.FullRectangle(*Pad_ptr, PadY, *Pad_ptr+PadLength ,PadY+2,WHITE); + if(Start){ + *Pad_ptr = XPadInit; + }else{ + if ((abs(acc.getAccY()))>AccTres2){ + PaddleDif = HighSpeed; + }else{ + PaddleDif = LowSpeed; + } + if ((acc.getAccY() > 0)&&((*Pad_ptr+PadLength) < (minX - 3))){ + *Pad_ptr += PaddleDif; + } + if ((acc.getAccY() < 0)&&(*Pad_ptr > (maxX + 1))){ + *Pad_ptr -= PaddleDif; + } + } + } + display.FullRectangle(*Pad_ptr, PadY, *Pad_ptr+PadLength ,PadY+2,BLACK); +} + +void reset(uint8_t* XBall, uint8_t* YBall, int8_t* dxBall, int8_t* dyBall, uint8_t* XPaddle, bool* Start, uint8_t *score, uint8_t *lives, uint8_t* col, uint8_t* ro, bool* Exists){ //resets the game in case of lost ball, lost or won game + display.FullCircle(*XBall, *YBall, BallD, WHITE); + *XBall = XBallInit; + *YBall = YBallInit; + display.FullRectangle(*XPaddle, PadY, *XPaddle+PadLength ,PadY+2,WHITE); + *XPaddle = XPadInit; + wait(1); + if (*score == (rows*columns)){ + display.ClearScreen(); + display.GotoXY(32,32); + display.PrintString("YOU WIN!!!"); + wait(3); + *score = 0; + *lives = 3; + display.ClearScreen(); + DrawLogo(); //drawing ARM-kanoid title + DrawTable(*score,*lives); //drawing table with score and number of lives + DrawBorder(); //drawing border of the playing area + CreateGrid(&(*col), &(*ro), &(*Exists)); + DrawBricks(col, ro, Exists); + }else if (*lives <= 0){ + display.ClearScreen(); + display.GotoXY(32,32); + display.PrintString("GAME OVER!!!"); + wait(3); + *score = 0; + *lives = 3; + display.ClearScreen(); + DrawLogo(); //drawing ARM-kanoid title + DrawTable(*score,*lives); //drawing table with score and number of lives + DrawBorder(); //drawing border of the playing area + CreateGrid(&(*col), &(*ro), &(*Exists)); + DrawBricks(col, ro, Exists); + + }else{ + display.FullCircle(*XBall, *YBall, BallD, BLACK); + display.FullRectangle(*XPaddle, PadY, *XPaddle+PadLength ,PadY+2,BLACK); + DrawTable(*score, *lives); + float angle = (pi/180)*(90+(rand()%(2*maxangle))); + *dxBall = (round(velocity*cos(angle))); + if (*dxBall == 0){ + *dxBall = 1; + } + *dyBall = (-round(velocity*sin(angle))); + display.GotoXY(0,0); + display.PrintNumber(*dxBall); + display.GotoXY(15,0); + display.PrintNumber(*dyBall); + wait(1); + *Start = false; + } +} + +void CreateGrid(uint8_t* col, uint8_t* ro, bool* exists){ //creates grid that will be later used for drawing bricks + int8_t i; + for (i = 0; i < columns; i++){ + *(col+i) = maxX + i*(BrickW + 2); + } + for (i = 0; i < rows; i++){ + *(ro+i) = minY + i*(BrickH + 2); + } + int8_t j; + for (i = 0; i < rows; i++){ + for (j = 0; j < columns; j++){ + *(exists+i*columns+j)=true; + } + } +} + +void DrawBricks(uint8_t col[columns], uint8_t ro[rows], bool exists[rows*columns]){ //draws bricks in prepared grid - these two functions are separated in case of creating another levels + + int8_t i; + int8_t j; + for (j = 0; j < rows; j++){ + for (i = 0; i < columns; i++){ + display.FullRectangle(col[i], ro[j], (col[i] + BrickW), (ro[j] + BrickH) ,((exists[j*columns+i])? BLACK:WHITE)); + } + } +} + +void BounceBricks(uint8_t XBall, uint8_t YBall, int8_t* dxBall, int8_t* dyBall, bool* BrickExists, uint8_t ro[rows], uint8_t col[columns], uint8_t* score, uint8_t lives, bool* Start){ //computes reflections from bricks, destroys bricks, counts score etc... + if (YBall <= (ro[rows-1]+BrickH)){ + int8_t i, j; + for (j = rows - 1; j >= 0; j--){ + if (((YBall-BallD) <= (ro[j]+BrickH))&&((YBall+BallD) > ro[j])){ + break; + } + } + for (i = columns - 1; i >= 0; i--){ + if (((XBall-BallD) <= (col[i]+BrickW))&&((XBall+BallD) > col[i])){ + break; + } + } + if (*(BrickExists+j*columns+i)){ + *(BrickExists+j*columns+i) = false; + display.FullRectangle(col[i], ro[j], (col[i] + BrickW), (ro[j] + BrickH), WHITE); + (*score)++; + if (*score == (rows*columns)){ + *Start = true; + } + DrawTable(*score, lives); + if (!((YBall-*dyBall) <= (minY))){ + *dyBall = -(*dyBall); + } + }else{ + if ((YBall-BallD) <= (minY+1)){ + *dyBall = -(*dyBall); + } + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jan 18 19:27:16 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file