int refresh_game() { char key; fd_set set; FD_ZERO(&set); FD_SET(0, &set); struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec= 4* 100*1000; if (select(1, &set, NULL, NULL, &timeout) < 0) return -1; if(FD_ISSET(0,&set)){ while((key=getch())==-1); if(key==' '||key== 'w'||key == 'W') bird.y -=1; else if(key=='q'||key=='Q') return -1; } else bird.y +=1; move_wall(); draw_bird(); if(wether_crash()==1) return -2; return 1; }
// Main loop. void main_app_loop() { if (current_screen == SCREEN_GAMEPLAY) { // Find out how long the processor has been active (since boot or since sleep) time_now_ms = pulse_get_millis(); // Check if it's been at least 10 ms since the box was last drawn if (time_now_ms - time_last_box_drawn_ms >= game_speed) { score++; // Change the color and speed up the walls when certain scores are reached switch(score) { case 800: wall_color = teal; game_speed-=2; break; case 1600: wall_color = blue; game_speed-=2; break; case 2400: wall_color = purple; game_speed-=2; break; case 3200: wall_color = pink; game_speed-=2; break; case 4000: wall_color = orange; game_speed-=2; break; case 4800: wall_color = red; game_speed-=2; break; } // Erase the old ball and walls by drawing over them draw_ball(ball_x,ball_y, COLOR_BLACK24); for (int i = 0; i < NUM_WALLS; i++) draw_wall(wall_y[i],wall_gap_x[i], COLOR_BLACK24); // Move the ball based on user input if(press && ball_x < SCREEN_WIDTH - BALL_SIZE) ball_x++; else if (!press && ball_x > 0) ball_x--; // Move the walls up for (int i = 0; i < NUM_WALLS; i++) move_wall(i); // Check for collisions between the ball and the wall bool collision = false; for (int i = 0; i < NUM_WALLS; i++) { if ((ball_y == wall_y[i] - BALL_SIZE + 1 || ball_y == wall_y[i] - BALL_SIZE)&& (ball_x < wall_gap_x[i] || ball_x > wall_gap_x[i] + GAP_WIDTH - BALL_SIZE)) { ball_y = wall_y[i] - BALL_SIZE; collision = true; } } // check if ball reached the top if (ball_y == start_y) game_over(); else { if (!collision && ball_y < SCREEN_HEIGHT-BALL_SIZE-1) ball_y = (ball_y + 1); // Draw the new box and walls draw_game_elements(); // update the time_last_box_draw_ms variable time_last_box_drawn_ms = time_now_ms; } } } else if (current_screen == SCREEN_CLOCK) pulse_get_time_date(¤t_time); }