void initializeGame(ball *aBall, paddle *p1, paddle *p2){ uint32_t blue = 0x000099; //black = 0x0; uint32_t gold = 0xffff00; //white = 0xffffff; aBall->x = START_BALL_X; aBall->y = START_BALL_Y; aBall->vX = -VEL_X; aBall->vY = 0; p1->x = START_PAD_1_X; p1->y = START_PAD_1_Y; p1->direction = IDLE; p2->x = START_PAD_2_X; p2->y = START_PAD_2_Y; p2->direction = IDLE; if ( (p1->score == 4) || (p2->score == 4) ){ p1->score = 0; p2->score = 0; } fill(blue); drawHalf(gold); drawScoreboard(p1,p2); drawPaddle(gold,START_PAD_1_Y,START_PAD_1_X); drawPaddle(gold,START_PAD_2_Y,START_PAD_2_X); drawBall(gold,aBall); startGP(); }
void updateFrame(uint32_t pY1, uint32_t pY2, uint32_t ballX, uint32_t ballY, ball *aBall, paddle *p1, paddle *p2){ uint32_t gold = 0xffff00; //white = 0xffffff; uint32_t blue = 0x000099; //black = 0x0; drawHalf(gold); drawScoreboard(p1,p2); drawPaddle(gold,pY1,START_PAD_1_X); drawPaddle(gold,pY2,START_PAD_2_X); drawBall(gold,aBall); startGP(); }
// 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); }
void Lines::paintEvent(QPaintEvent *e){ Q_UNUSED(e); QPainter qp(this); /*Create serial object */ try { // Make a SimpleSerial object with the parameter for your Wunderboard/OS this->tmp = this->wunder->readLine(); /*handles odd SimpleSerial Problems*/ if(this->tmp.length() != 3) adcval = MADC; //send it back to the middle else adcval = atoi(this->tmp.c_str()) - MADC ; } catch(boost::system::system_error& e) { std::cerr<<tmp<<": Error: "<<std::endl;//<<e.what()<<std::endl; } qp.setWindow(0,0, 800, 600); drawBoundary(&qp); drawPaddle(&qp); drawBall(&qp); drawBlocks(&qp); check_collision(); }
void render() { // Clear frame buffer and depth buffer glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glColor3f(1.0,1.0,1.0); setOrtho(); glPushMatrix(); drawMiddleLine(); drawPaddle(100,startX); drawPaddle(1280-100,startY); drawBall(ballX,ballY); glPopMatrix(); glFlush(); glutSwapBuffers(); }
void breakoutDraw() { clear(); drawPaddle(paddle); drawBall(ball); u8 row; u8 col; for(row=0; row < FIELD_HEIGHT; ++row) { for(col=0; col < FIELD_WIDTH; ++col) { if(bricks[(row*FIELD_WIDTH) + col].alive) drawBrick(row, col, bricks[(row*FIELD_WIDTH) + col]); } } render(); }
// 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; } } } }
void display() { /* If we are using DGR, send or receive data to keep multiple * processes/computers synchronized. */ dgr_update(); /* Syncronize the DGR objects */ dgr_setget("paddleA", &paddleA, sizeof(Paddle)); dgr_setget("paddleB", &paddleB, sizeof(Paddle)); dgr_setget("ball", &ball, sizeof(Ball)); dgr_setget("planet", planet, sizeof(float)*3); dgr_setget("state", &gameState, sizeof(int)); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor3f(1,1,1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float frustum[6]; projmat_get_frustum(frustum, -1, -1); glOrtho(frustum[0], frustum[1], frustum[2], frustum[3], frustum[4], frustum[5]); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Pick a depth that is between the near and far planes. float depth = -(frustum[4] + frustum[5])/2.0; // Move the light source GLfloat position[] = { 1.0f, -1.0f, depth+5.5f, 1.0f }; glLightfv(GL_LIGHT0, GL_POSITION, position); // Draw the background stars float masterfrust[6]; projmat_get_master_frustum(masterfrust); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texIdStars); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Draw the background quad with the scrolling star texture float tickmod = ticks / 200.0f; glBegin(GL_QUADS); glTexCoord2f(tickmod+1.0f, -tickmod); glVertex3f(masterfrust[1], masterfrust[3], depth-3.0); glTexCoord2f(tickmod, -tickmod); glVertex3f(masterfrust[0], masterfrust[3], depth-3.0); glTexCoord2f(tickmod, 1.0f-tickmod); glVertex3f(masterfrust[0], masterfrust[2], depth-3.0); glTexCoord2f(tickmod+1.0f, 1.0f-tickmod); glVertex3f(masterfrust[1], masterfrust[2], depth-3.0); glEnd(); //Draw the earth glMatrixMode(GL_MODELVIEW); glPushMatrix(); glBindTexture(GL_TEXTURE_2D, texIdEarth); glTranslatef(planet[0], planet[1], depth-3.0); glRotatef(25.0f, 0.0f, 0.0f, 1.0f); glRotatef(-90, 1.0f, 0.0f, 0.0f); glRotatef(ticks, 0.0f, 0.0f, 1.0f); ticks += .005; if(ticks > 360.0f)ticks = 0.0f; gluSphere(earth, planet[2]*1.65f, 200, 200); glPopMatrix(); //Draw the clouds glEnable(GL_BLEND); glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR); glPushMatrix(); glBindTexture(GL_TEXTURE_2D, texIdClouds); glLoadIdentity(); glTranslatef(planet[0], planet[1], depth-3.0); glRotatef(25.0f, 0.0f, 0.0f, 1.0f); glRotatef(-90, 1.0f, 0.0f, 0.0f); glRotatef(ticks, 1.0f, 0.0f, 1.0f); gluSphere(clouds, planet[2]*1.652f, 200, 200); glPopMatrix(); // Reset somethings for the rest of the scene glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // top player (player 1) paddle drawPaddle(paddleA, depth+5.0f); // bottom player (player 2) paddle drawPaddle(paddleB, depth+5.0f); glDisable(GL_BLEND); // ball glEnable(GL_LIGHTING); glColor3fv(ball.color); glPushMatrix(); glTranslatef(ball.xpos, ball.ypos, depth+4.0f); glutSolidSphere(ball.radius, 100, 100); glPopMatrix(); /* If DGR is enabled, only do this in the master*/ if(dgr_is_enabled() == 0 || dgr_is_master()) { // Run the game code game(); } glFlush(); glutSwapBuffers(); glutPostRedisplay(); // call display() repeatedly }
void main() { unsigned char x, y, button_press; signed char xVel, yVel; unsigned long timeWaster,timeWaster2; unsigned char paddleRow; unsigned char playGame; // === Initialize system ================================================ IFG1=0; /* clear interrupt flag1 */ WDTCTL=WDTPW+WDTHOLD; /* stop WD */ button_press = FALSE; //sets up the screen init(); initNokia(); clearDisplay(); //initializes the position of the block x=1; y=1; //initializes the velocity of the block xVel = 1; yVel = 1; //wastes time to make the block move slower. timeWaster = 0; timeWaster2 = 0; //intializes the location of the paddle. paddleRow = 4; //sets the color to be black initialy :) to omoimasu drawBlock(y,x); //first we need to draw the paddle. drawPaddle(paddleRow,PADDLECOL); playGame = 1; while(playGame) { //delays the code to make the ball move slower. for(timeWaster = 0; timeWaster<350; timeWaster++){ for(timeWaster2 = 0; timeWaster2<350; timeWaster2++){ //want to give the user a chance to hit the button before the block is moved. //therefore, check this every time you run through the for loop!!!! //might have to alter how long the loop delays for after this. } } //check if the up or down buttons have been hit!! if (UP_BUTTON == 0) { for(timeWaster = 0; timeWaster<60;timeWaster++){ } if (paddleRow>=1) paddleRow = paddleRow-1; button_press = TRUE; } else if (DOWN_BUTTON == 0) { for(timeWaster = 0; timeWaster<60;timeWaster++){ } if (paddleRow<=6) paddleRow=paddleRow+1; button_press = TRUE; } //check if too far left if(y<=0||y>=HEIGHT){ yVel *= -1; } //check if in bounds in x direction if(x<=0){ xVel *= -1; } //did it hit the paddle? if(x>=LENGTH&&y==paddleRow){ xVel *= -1; }else if (x==LENGTH){ //if it hit the right wall instead. //make the game end!!! playGame = 0; } //moves it!!! x +=xVel; y +=yVel; //resets screen with new location of block clearDisplay(); drawBlock(y,x); drawPaddle(paddleRow,PADDLECOL); } while(1){ clearDisplay(); } }
int playLevel(int levelNo, int pause) { lcdClear(); lcdFill(GLOBAL(nickbg)); // load level int bricks[FIELD_HEIGHT][FIELD_WIDTH]; memcpy(bricks, levels[levelNo], sizeof(bricks)); // initialisation int ball[2] = {SCREEN_WIDTH / 2, FIELD_HEIGHT * (BRICK_HEIGHT + BRICK_SPACING) + 1}; int direction[2] = {1,1}; int paddleX = SCREEN_WIDTH / 2 - PADDLE_WIDTH / 2; int lives = LIVES_INITIAL; drawBricks(bricks); while ( 1 ) { // ball drawBall(ball, GLOBAL(nickbg)); ball[0] += direction[0]; ball[1] += direction[1]; // paddle / user input drawPaddle(paddleX, GLOBAL(nickbg)); int key = getInputRaw(); switch (key) { case BTN_ENTER: // exit return 0; case BTN_LEFT: paddleX -= PADDLE_SPEED; if (paddleX < 0) paddleX = 0; break; case BTN_RIGHT: paddleX += PADDLE_SPEED; if (paddleX + PADDLE_WIDTH > SCREEN_WIDTH) paddleX = SCREEN_WIDTH - PADDLE_WIDTH; break; } drawPaddle(paddleX, GLOBAL(nickfg)); // collisions // bricks int x = ball[0] / ((BRICK_WIDTH + BRICK_SPACING)); int y = ball[1] / ((BRICK_HEIGHT + BRICK_SPACING)); if (0 <= x && x < FIELD_WIDTH && 0 <= y && y < FIELD_HEIGHT) { if (bricks[y][x] == 1) { // collision with brick int xRel = ball[0] - x * (BRICK_WIDTH + BRICK_SPACING); int yRel = ball[1] - y * (BRICK_HEIGHT + BRICK_SPACING); if (xRel == 0 || xRel == BRICK_WIDTH) direction[0] *= -1; // hit top or bottom if (yRel == 0 || yRel == BRICK_HEIGHT) direction[1] *= -1; // hit left or right bricks[y][x] = 0; if (fieldIsCleared(bricks)) return 1; // next level } } // paddle / bottom if (direction[1] > 0) { // moving to the bottom if (ball[1] >= PADDLE_Y) { if (paddleX <= ball[0] && ball[0] <= paddleX + PADDLE_WIDTH) { // collision with paddle direction[1] = - abs(direction[1]); if (key == BTN_LEFT) direction[0] = -2; else if (key == BTN_RIGHT) direction[0] = 2; else direction[0] = (direction[0] > 0) ? 1 : -1; } else { // ball lost lives--; if (lives == 0) { lcdClear(); lcdPrintln(""); lcdPrintln(""); lcdPrintln(""); lcdPrintln(""); lcdPrintln(" GAME OVER"); lcdDisplay(); delayms(2000); return 0; } ball[0] = SCREEN_WIDTH / 2; ball[1] = FIELD_HEIGHT * (BRICK_HEIGHT + BRICK_SPACING) + 1; direction[0] = (paddleX + PADDLE_WIDTH / 2 < ball[0]) ? -1 : 1; direction[1] = 1; } } } // walls if (ball[1] <= 0) direction[1] = abs(direction[1]); if (ball[0] <= 0) direction[0] = abs(direction[0]); else if (ball[0] >= SCREEN_WIDTH - 1) direction[0] = - abs(direction[0]); drawBricks(bricks); drawBall(ball, GLOBAL(nickfg)); lcdDisplay(); delayms(pause); } return 0; }
void resetDisplay(){ clearScreen(); drawBall(); drawBorder(); drawPaddle(); }
int main() { init(); double ballVelX = BALL_VEL; double ballVelY = BALL_VEL; int topPaddleX; int bottomPaddleX; double ballX; double ballY; unsigned int topPlayerScore = 0; unsigned int bottomPlayerScore = 0; reset(topPaddleX, bottomPaddleX, ballX, ballY); bool quit = false; int key; while(!quit) { preFrame(); key = getKey(); if(key == 's') --topPaddleX; else if(key == 'd') ++topPaddleX; else if(key == 'k') --bottomPaddleX; else if(key == 'l') ++bottomPaddleX; else if(key == '`') quit = true; ballX += ballVelX; ballY += ballVelY; if(ballX >= XMAX) ballVelX = -ballVelX; else if(ballX <= 0) ballVelX = -ballVelX; if(ballY >= YMAX) { ballVelY = -ballVelY; if(ballX < (bottomPaddleX - PADDLE_HALF_SIZE) || ballX > (bottomPaddleX + PADDLE_HALF_SIZE)) { reset(topPaddleX, bottomPaddleX, ballX, ballY); ++topPlayerScore; } } else if(ballY <= 0) { ballVelY = -ballVelY; if(ballX < (topPaddleX - PADDLE_HALF_SIZE) || ballX > (topPaddleX + PADDLE_HALF_SIZE)) { ++bottomPlayerScore; reset(topPaddleX, bottomPaddleX, ballX, ballY); } } postFrame(); drawPaddle(topPaddleX, 0); drawPaddle(bottomPaddleX, YMAX); drawBall((int)ballX, (int)ballY); drawScore(topPlayerScore, bottomPlayerScore); } cleanUp(); return 0; }
/* * main.c */ void main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer unsigned char black, notBlack, button_press, y; // === Initialize system ================================================ IFG1=0; /* clear interrupt flag1 */ WDTCTL=WDTPW+WDTHOLD; /* stop WD */ button_press = FALSE; black = TRUE; notBlack = FALSE; init(); initNokia(); clearDisplay(0,0,notBlack); vector2d pos = {2,2}; vector2d vel = {1,1}; unsigned char rad = 2; y=1; ball myBall; myBall.position = pos; myBall.velocity = vel; myBall.radius = rad; while(1){ if (UP_BUTTON == 0) { while(UP_BUTTON == 0); if (y>=1) y=y-1; button_press = TRUE; } else if (DOWN_BUTTON == 0) { while(DOWN_BUTTON == 0); if (y<=6) y=y+1; button_press = TRUE; } else if(AUX_BUTTON == 0){ while(AUX_BUTTON == 0); black = !black; notBlack = !black; clearDisplay(0,0,notBlack); drawBlock(myBall.position.y, myBall.position.x, black); drawPaddle(y,0,black); } if (button_press) { button_press = FALSE; //clearDisplay(); drawBlock(myBall.position.y, myBall.position.x, black); drawPaddle(y,0,black); } moveBall(&myBall,y); clearDisplay(0,0,notBlack); drawBlock(myBall.position.y, myBall.position.x, black); drawPaddle(y,0,black); _delay_cycles(5333333); } }
// Run the user's paddle void *moveme(void* vp) { int ch; // get the extents of the screen int maxx; int maxy; getmaxyx(win, maxy, maxx); int vpos = maxy / 2; // draw the default paddle drawPaddle(vpos); while (!quit) { (void) noecho(); ch = getch(); switch (ch) { case KEY_UP: // The user has pressed the up arrow. Move the paddle one pixel upwards if possible. if (!pauseGame) { undrawPaddle(vpos); vpos--; if (vpos < 0 + PADDLE_SIZE / 2) { vpos = PADDLE_SIZE / 2; } drawPaddle(vpos); } break; case KEY_DOWN: // The user has pressed the down arrow. Move the paddle one pixel downward if possible. if (!pauseGame) { undrawPaddle(vpos); vpos++; if (vpos > (maxy - (PADDLE_SIZE / 2))) { vpos = (maxy - (PADDLE_SIZE / 2)); } drawPaddle(vpos); } break; case 'q': // The user has pressed the quiz button. Exit the game. quit = true; break; case 'p': pauseGame = !pauseGame; break; case '+': // The user pressed the plus key to increase the speed of ball pthread_mutex_lock(&mutex); int temp = ballMovementDelay / 2; if(temp > 0) ballMovementDelay = temp; pthread_mutex_unlock(&mutex); break; case '-': // The user pressed the minus key to decrease the speed of ball pthread_mutex_lock(&mutex); ballMovementDelay *= 2; pthread_mutex_unlock(&mutex); break; default: break; } } return NULL; }