// initialize round // place paddles in the middle and initialize ball void start(Paddle * RedPaddle, Paddle * BluePaddle, Ball * ActiveBall) { tft.fillScreen(BLACK); RedPaddle->horzPosition = srcWidth /2 - RedPaddle->size/2; BluePaddle->horzPosition = srcWidth /2 - RedPaddle->size/2; drawPaddle(RedPaddle); drawPaddle(BluePaddle); initializeBall(ActiveBall); }
// This function does boundary checking on the ball, also determines where // it hits the paddle, and changes the behavior of the ball depending. void checkBallCollisions(int paddlePos) { // first check for any brick collisions char detected = drawBricks(&ball); // if no bricks hit, continue with normal collision detection if(detected == 'n') { // right wall if(ball.x >= SCREEN_WIDTH - BALL_RADIUS) { ball.x = SCREEN_WIDTH - (BALL_RADIUS + 1); xdir = -xdir; playTone(500, 50); return; } // left wall if(ball.x <= BALL_RADIUS) { ball.x = BALL_RADIUS + 1; xdir = -xdir; playTone(500, 50); return; } // ceiling if(ball.y >= SCREEN_HEIGHT - BALL_RADIUS) { ydir = -1.0; playTone(500, 50); return; } // middle of paddle if(ball.y <= 19 && (ball.x <= paddlePos + (PADDLE_WIDTH / 2) + (BALL_RADIUS + 1) && ball.x >= paddlePos + (PADDLE_WIDTH / 2) - (BALL_RADIUS + 1))) { paddleHits++; // increase ball speed slightly with every 6 paddle hits if((paddleHits % 18 == 0) && (paddleHits <= 36)) speed += 1.0; ball.y = 20; ydir = 1.0; playTone(500,50); return; } // right side of paddle else if(ball.y <= 19 && ((ball.x <= paddlePos + PADDLE_WIDTH) && (ball.x > paddlePos + (PADDLE_WIDTH / 2) + (BALL_RADIUS + 1)))) { paddleHits++; // increase ball speed slightly with every 6 paddle hits if((paddleHits % 18 == 0) && (paddleHits <= 36)) speed += 1.0; ball.y = 20; ydir = 1.0; xdir += 1.0; playTone(500, 50); return; } // left side of paddle else if(ball.y <= 19 && ((ball.x >= paddlePos) && (ball.x < paddlePos + (PADDLE_WIDTH / 2) - (BALL_RADIUS + 1)))) { paddleHits++; // increase ball speed slightly with every 6 paddle hits if((paddleHits % 18 == 0) && (paddleHits <= 36)) speed += 1.0; ball.y = 20; ydir = 1.0; xdir -= 1.0; playTone(500, 50); return; } // ball hits floor, lives lost, score deducted if(ball.y <= 10 && ((ball.x < paddlePos) || (ball.x > paddlePos + PADDLE_WIDTH))) { playTone(50,500); tft.fillCircle(ball.y, ball.x, BALL_RADIUS, ST7735_BLACK); drawPaddle(); initializeBall(getDifficulty()); decreaseLives(); displayStats(); delay(20); } } // brick collision detected, adjust ball accordingly else { // corner of brick hit if(detected == 'c') { xdir = -xdir; ydir = -ydir; playTone(200,50); return; } else { // left/right side of brick hit if(detected == 'x') { xdir = -xdir; playTone(200,50); return; } // top/bottom of brick hit else if(detected == 'y') { ydir = -ydir; playTone(200,50); return; } } } }
/* gestisce i tasti carattere */ void keyHandler(GLubyte key, GLint x, GLint y){ static int sCount=0; if(key== 'q'){ fprintf(stderr, "Exit\n"); programData.exit = 1; }else{ if(key == 'p'){ /* menu */ programData.menu = !programData.menu; }else{ if(key == 'm'){ /* multisampling */ programData.multisample = !programData.multisample; if(programData.multisample){ fprintf(stderr, "MULTISAMPING ON\n"); glEnable(GL_MULTISAMPLE); } else{ fprintf(stderr, "MULTISAMPING OFF\n"); glDisable(GL_MULTISAMPLE); } }else{ if(key == 'f'){ /* nebbia */ programData.fog= !programData.fog; if(programData.fog){ fprintf(stderr, "FOG ON\n"); fog(1); } else{ fprintf(stderr, "FOG OFF\n"); fog(0); } }else{ if(key == 'n'){ /* nuova partita */ programData.gameStatus = 1; programData.menu = 0; programData.points= 0; /* Inizializzo la posizione della telecamera */ worldData.angleX = 0.0f; worldData.angleY = 0.0f; worldData.angleMX = 0.0f; worldData.angleMY = 0.0f; worldData.nextAngleX = 0.0f; worldData.nextAngleY = 0.0f; worldData.nextAngleMX = 0.0f; worldData.nextAngleMY = 0.0f; /* Inizializzo il verme */ initializeWorm(0.0f, 0.0f, 83.5f, 0); srand(time(NULL)); /* Inizializzo la palla */ initializeBall(); /* Inizializzo i dati utilizzati per la telecamera */ worldData.kup = 0; worldData.kdown = 0; worldData.kleft = 0; worldData.kright = 0; worldData.xStatus = 0; worldData.yStatus = 0; worldData.nextXstatus = 0; worldData.nextYstatus = 0; programData.velocity = VELOCITY; programData.timeFrame = 1250/programData.velocity; programData.velAngolare = programData.velocity*0.2; programData.timerender = programData.time; programData.timebase = programData.time; programData.frame = 0; }else{ if(key == 's'){/* screenshot */ char sname[20]; sprintf(sname, "screenshot%d.tga",sCount); gltWriteTGA(sname); sCount++; } } } } } } }