#include #include #include #include #include #include #define SCREEN_WIDTH 11 //set the width of the game screen #define SCREEN_HEIGHT 20 //set the height of the game screen #define PADDLE_SIZE 2 //set the paddle size. the blocks are added this amount either size of a central block. #define SLEEP_TIME_MS 100 * 1000 //set the time to sleep between frames #define PADDLE_HEIGHT SCREEN_HEIGHT-4 //the negative number is distance from the bottom #define Y_BLOCK_LAYERS 10 //add the amount of layers of blocks from the top of the screen #define X_BLOCK_LAYERS SCREEN_WIDTH uint8_t screen[SCREEN_WIDTH][SCREEN_HEIGHT]; //this is the array the game lives in enum PIXEL_TYPE { //used to set what kind of pixel is shown on the screen array PIXEL_EMPTY=0u, PIXEL_BALL=1u, PIXEL_BLOCK=2u, PIXEL_PADDLE_MIDDLE=3u, PIXEL_PADDLE_LEFT=4u, PIXEL_PADDLE_RIGHT=5u } PIXEL_TYPE; void clearScreen() { //clears the screen of all pixels to redraw the screen for (int y = 0; y < SCREEN_HEIGHT; y++) { for (int x=0; x"); } else if(screen[x][y]==PIXEL_PADDLE_MIDDLE) { printf("=="); } else if(screen[x][y]==PIXEL_PADDLE_LEFT){ printf("<="); } else if(screen[x][y]==PIXEL_PADDLE_RIGHT){ printf("=>"); } } printf("|\n"); } for (int x =0; x<=SCREEN_WIDTH; x++) { printf("--"); } printf("\n"); //detect when ball hits the edge of the screen if (ball.xPOS==0||ball.xPOS==(SCREEN_WIDTH-1)) { ball.xVEL=ball.xVEL*-1; } if (ball.yPOS==0||ball.yPOS==(SCREEN_HEIGHT-1)) { ball.yVEL=ball.yVEL*-1; } //detect when ball hits a block if (screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_BLOCK) { blockArr[ball.xPOS+ball.xVEL][ball.yPOS].visable=false; ball.xVEL = ball.xVEL *-1; blockFlushHit=true; } if (screen[ball.xPOS][ball.yPOS+ball.yVEL]==PIXEL_BLOCK) { blockArr[ball.xPOS][ball.yPOS+ball.yVEL].visable=false; ball.yVEL = ball.yVEL *-1; blockFlushHit=true; } if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_BLOCK&&blockFlushHit==false) { blockArr[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL].visable=false; if(ballOnXEdge(ball)==false) { ball.xVEL = ball.xVEL *-1; } if (ball.yPOS!=0) { ball.yVEL=ball.yVEL *-1; } } //detect when ball hits paddle if(screen[ball.xPOS+ball.xVEL][ball.yPOS]==PIXEL_PADDLE_MIDDLE) { ball.xVEL = ball.xVEL *-1; } if (screen[ball.xPOS][ball.yPOS+ball.yVEL]== PIXEL_PADDLE_MIDDLE) { //hit detection y direction ball.yVEL= ball.yVEL*-1; } if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE_MIDDLE) { //diagonal hit detection ball.xVEL = ball.xVEL *-1; ball.yVEL=ball.yVEL *-1; } //paddle left if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE_LEFT) { //diagonal hit detection if(ball.xVEL==-1){ ball.xVEL=-1; } else if (ball.xVEL==0){ ball.xVEL=-1; } else { ball.xVEL=-1; } ball.yVEL=ball.yVEL *-1; } //paddle right if (screen[ball.xPOS+ball.xVEL][ball.yPOS+ball.yVEL]==PIXEL_PADDLE_RIGHT) { //diagonal hit detection if (ball.xVEL==-1){ //moving left ball.xVEL=1; } else if (ball.xVEL==0){ //moving straight down ball.xVEL=1; } else { //moving right ball.xVEL=1; } ball.yVEL=ball.yVEL *-1; } //update ball position ball.xPOS=ball.xPOS+ball.xVEL; ball.yPOS= ball.yPOS+ball.yVEL; } }