// au départ c'est le joueur de gauche qui a le service // il doit déplacer sa raquette pour lancer la balle. // la balle part avec un angle pointant vers le haut // ou le bas en fonction de la direction du // déplacement de la raquette. void play_pong(){ wait_service(); running=1; while (running){ move_paddle(LEFT_PLAYER); move_paddle(RIGHT_PLAYER); move_ball(); frame_sync(); frame_delay(1); } }//f()
// attend que le joueur déplace sa raquette void wait_service(){ int y; paddle[server]=read_pot(server)+TOP_BORDER; while (1){ y=read_pot(server)+TOP_BORDER; if (y<paddle[server]-1 || y>paddle[server]+1) break; move_paddle(1-server);// l'autre joueur peut se déplacer en attendant le service. } ball.dy=rand()%3 - 1; if (server==LEFT_PLAYER){ ball.dx=1; }else{ ball.dx=-1; } ball.skip=rand()%2; }//f()
void step_rat(void) { // The keepout is used to know where to -not- put the paddle // the 'bouncepos' is where we expect the ball's y-coord to be when // it intersects with the paddle area static uint8_t right_keepout_top, right_keepout_bot; static uint8_t left_keepout_top, left_keepout_bot; static uint16_t dest_paddle_pos; static uint16_t right_dest, left_dest; // Save old ball location so we can do some vector stuff oldball_x = ball_x; oldball_y = ball_y; // move ball according to the vector ball_x += ball_dx; ball_y += ball_dy; /************************************* TOP & BOTTOM WALLS */ // bouncing off bottom wall, reverse direction // if (ball_y > (SCREEN_H_FIXED - BALL_RADIUS*2*FIXED_MATH - BOTBAR_H_FIXED)) { if (ball_y > (SCREEN_H - BOTBAR_H - BALL_RADIUS*2)*FIXED_MATH) { //DEBUG(putstring_nl("bottom wall bounce")); ball_y = (SCREEN_H - BOTBAR_H - BALL_RADIUS*2)*FIXED_MATH; ball_dy = -ball_dy; } // bouncing off top wall, reverse direction if (ball_y < TOPBAR_H_FIXED) { //DEBUG(putstring_nl("top wall bounce")); ball_y = TOPBAR_H_FIXED; ball_dy = -ball_dy; } /************************************* LEFT & RIGHT WALLS */ // the ball hits either wall, the ball resets location & angle if ( ((INT_MSB(ball_x)) > (SCREEN_W - BALL_RADIUS*2)) || ((int8_t)(INT_MSB(ball_x)) <= 0) || ((ball_dx ==0) && (ball_dy==0)) ) { if(DEBUGGING) { if ((int8_t)(INT_MSB(ball_x)) <= 0) { putstring("Left wall collide"); if (! minute_changed) { putstring_nl("...on accident"); } else { putstring_nl("...on purpose"); } } else { putstring("Right wall collide"); if (! hour_changed) { putstring_nl("...on accident"); } else { putstring_nl("...on purpose"); } } } // place ball in the middle of the screen ball_x = (SCREEN_W/2 - BALL_RADIUS)*FIXED_MATH; ball_y = (SCREEN_H/2 - BALL_RADIUS)*FIXED_MATH; // TODO JMM: don't use cosine/sine... pick one randomly and calc the other one. int8_t angle = random_angle(); ball_dx = (int16_t) (((int32_t) MAX_BALL_SPEED * cosine(angle)) / 0x7FFF); ball_dy = (int16_t) (((int32_t) MAX_BALL_SPEED * sine(angle)) / 0x7FFF); glcdFillRectangle(LEFTPADDLE_X, left_keepout_top, PADDLE_W, left_keepout_bot - left_keepout_top, 0); glcdFillRectangle(RIGHTPADDLE_X, right_keepout_top, PADDLE_W, right_keepout_bot - right_keepout_top, 0); right_keepout_top = right_keepout_bot = 0; left_keepout_top = left_keepout_bot = 0; redraw_time_rat = 1; minute_changed = hour_changed = 0; ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, ball_dx > 0); //left_score = time_h; //right_score = time_m; setscore_rat(); } // save old paddle position oldleftpaddle_y = leftpaddle_y; oldrightpaddle_y = rightpaddle_y; /* if(ball_dx > 0) { // For debugging, print the ball location DEBUG(putstring("ball @ (")); DEBUG(uart_putw_dec(ball_x)); DEBUG(putstring(", ")); DEBUG(uart_putw_dec(ball_y)); DEBUG(putstring(")")); DEBUG(putstring(" ball_dx @ (")); DEBUG(uart_putw_dec(ball_dx)); DEBUG(putstring(")")); DEBUG(putstring(" ball_dy @ (")); DEBUG(uart_putw_dec(ball_dy)); DEBUG(putstring(")")); DEBUG(putstring(" ball_dy @ (")); DEBUG(uart_putw_dec(ball_dy)); DEBUG(putstring(")")); }*/ /*if(!minute_changed) { if((ball_dx < 0) && (ball_x < (SCREEN_W/2)*FIXED_MATH)) { move_paddle(&leftpaddle_y, ball_y); } } else { //Minute changed. We now have to miss the ball on purpose, if at all possible. //If we don't succeed this time around, we will try again next time around. if((ball_dx < 0) && (ball_x < (SCREEN_W/2)*FIXED_MATH) ) { move_paddle(&leftpaddle_y, dest_paddle_pos); } }*/ //ticksremaining--; if((ball_dx < 0) && (ball_x < (SCREEN_W/2)*FIXED_MATH) ) { move_paddle(&leftpaddle_y, minute_changed?dest_paddle_pos:(ball_y-(PADDLE_H_FIXED/3))); } else if((ball_dx > 0) && (ball_x > (SCREEN_W/2)*FIXED_MATH) ) { move_paddle(&rightpaddle_y, hour_changed?dest_paddle_pos:(ball_y-(PADDLE_H_FIXED/3))); } else { if(ball_dx < 0) ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 1); else ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 0); } // make sure the paddles dont hit the top or bottom if (leftpaddle_y < TOPBAR_H_FIXED +1) leftpaddle_y = TOPBAR_H_FIXED + 1; if (rightpaddle_y < TOPBAR_H_FIXED + 1) rightpaddle_y = TOPBAR_H_FIXED + 1; if (leftpaddle_y > ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1)) leftpaddle_y = ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1); if (rightpaddle_y> ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1)) rightpaddle_y = ((SCREEN_H - PADDLE_H - BOTBAR_H)*FIXED_MATH - 1); if ((ball_dx > 0) && intersectrect(INT_MSB(ball_x), INT_MSB(ball_y), BALL_RADIUS*2, BALL_RADIUS*2, RIGHTPADDLE_X, INT_MSB(rightpaddle_y), PADDLE_W, PADDLE_H)) { ball_dx = -ball_dx; ball_x = RIGHTPADDLE_X_FIXED - (BALL_RADIUS*2*FIXED_MATH); //ball_y = right_dest; //ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 1); } if ((ball_dx < 0) && intersectrect(INT_MSB(ball_x), INT_MSB(ball_y), BALL_RADIUS*2, BALL_RADIUS*2, LEFTPADDLE_X, INT_MSB(leftpaddle_y), PADDLE_W, PADDLE_H)) { ball_dx = -ball_dx; ball_x = LEFTPADDLE_X_FIXED + PADDLE_W_FIXED; //ball_y = left_dest; //ticksremaining = calculate_dest_pos(&left_dest, &right_dest, &dest_paddle_pos, 0); } }
int main() { SDL_Surface *temp; /* Initialize SDL’s video system and check for errors */ if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } /* Make sure SDL_Quit gets called when the program exits! */ atexit(SDL_Quit); /* Attempt to set a 640x480 8 bit color video mode */ screen = SDL_SetVideoMode(640, 480, 8,SDL_DOUBLEBUF); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("numbermap.bmp"); if (temp == NULL) { printf("Unable to load numbermap.bmp.\n"); return 1; } /* Set the numbermaps colorkey. */ Uint32 colorkey = SDL_MapRGB(temp->format, 255, 0, 255); SDL_SetColorKey(temp, SDL_SRCCOLORKEY, colorkey); //convert the numbermaps surface to the same type as the screen numbermap = SDL_DisplayFormat(temp); if (numbermap == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("title.bmp"); if (temp == NULL) { printf("Unable to load numbermap.bmp.\n"); return 1; } /* Set the numbermaps colorkey. */ SDL_SetColorKey(temp, SDL_SRCCOLORKEY, colorkey); //convert the numbermaps surface to the same type as the screen title = SDL_DisplayFormat(temp); if (numbermap == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); //load the numbermap image strip of 10 number 64px * 64px temp = SDL_LoadBMP("gameover.bmp"); if (temp == NULL) { printf("Unable to load gameover.bmp.\n"); return 1; } //convert the end surface to the same type as the screen end = SDL_DisplayFormat(temp); if (end == NULL) { printf("Unable to convert bitmap.\n"); return 1; } SDL_FreeSurface(temp); /* Initialize the ball position data. */ init_ball(); int quit = 0; int state = 0; Uint8 *keystate = 0; Uint32 next_game_tick = SDL_GetTicks(); int sleep = 0; int r = 0; /* Animate */ while (quit == 0) { /* Update SDL's internal input state information. */ SDL_PumpEvents(); /* Grab a snapshot of the keyboard. */ keystate = SDL_GetKeyState(NULL); /* Respond to input. */ if (keystate[SDLK_ESCAPE]) { quit = 1; } if (keystate[SDLK_DOWN]) { move_paddle(0); } if (keystate[SDLK_UP]) { move_paddle(1); } //draw the background draw_background(); //display main menu if (state == 0 ) { if (keystate[SDLK_SPACE]) { state = 1; } //draw menu draw_menu(); //display gameover } else if (state == 2) { if (keystate[SDLK_SPACE]) { state = 0; //delay for a little bit so the space bar press dosnt get triggered twice //while the main menu is showing SDL_Delay(500); } if (r == 1) { //if player 1 is AI if player 1 was human display the return value of r not 3 draw_game_over(3); } else { //display gameover draw_game_over(r); } //display the game } else if (state == 1){ //check score r = check_score(); if (r == 1) { state = 2; } else if (r == 2){ state = 2; } //paddle ai movement move_paddle_ai(); /* Move the balls for the next frame. */ move_ball(); //draw net draw_net(); //draw paddles draw_paddle(); /* Put the ball on the screen. */ draw_ball(); //draw the score draw_player_0_score(); //draw the score draw_player_1_score(); } /* Ask SDL to update the entire screen. */ SDL_Flip(screen); next_game_tick += 1000 / 60; sleep = next_game_tick - SDL_GetTicks(); if( sleep >= 0 ) { SDL_Delay(sleep); } } return 0; }