/* Author : Gaurav Sharma h2016075@pilani.bits-pilani.ac.in Himanshu Shekhar h2016071@pilani.bits-pilani.ac.in. */ #include #include #include #include #include #include #include "lcd.h" #include "glcd_graphics.h" #include "timer.h" #include "character_map.h" #include "images.h" #include "ext_int.h" signed int dx, dy; #define DELAY_PER_FRAME 30 //delay in us #define NUM_BRICK_COLS 10 #define NUM_BRICK_ROWS 5 #define BALL_RADIUS 2 #define BRICK_WIDTH 10 #define BRICK_HEIGHT 4 #define BRICK_OFFSET 2 #define DEFAULT_X_SPEED 1 #define DEFAULT_Y_SPEED 1 #define PADDLE_WIDTH 20 #define PADDLE_HEIGHT 3 typedef struct BALL { uint8_t radius; uint8_t initial_x, initial_y; uint8_t speed_x, speed_y; uint8_t is_visible; int8_t curr_x, curr_y; //include pattern }ball; typedef struct BRICK { uint8_t pattern; uint8_t x1, y1; uint8_t is_visible; uint8_t offset; uint8_t width; uint8_t height; }brick; ball m_ball; brick paddle; brick brick_array[NUM_BRICK_ROWS][NUM_BRICK_COLS]; /*Score parameters. */ unsigned long long int level_time; unsigned int level_score; unsigned int brick_count = 0; char status[50]; unsigned int lives = 5; /*Function definitions. */ void draw_stats(void); void show_welcome_screen(void); void display_score_screen(void); void draw_game_over(void); void player_won(void); void play_buzzer(unsigned long buzzer_time_us) ; void init_game(void); int left_button_down(void); int right_button_down(void); void wait_for_button(void); void draw_stats(void); void populate_frame(void); void show_welcome_screen() { //show original atari logo uint16_t i; for(i=0; i<1024; i++) back_buffer[i] = atari_logo[i]; draw_frame(); //wait for button click wait_for_button(); disable_signal(); clear_frame(); draw_string(1, " BREAKOUT "); draw_string(3, " Embedded System design"); draw_string(5, "**Press start button**"); draw_frame(); wait_for_button(); disable_signal(); } void display_score_screen() { char message[100]; clear_frame(); draw_frame(); sprintf(message, " Your score..."); draw_string(1, message); sprintf(message, " 000%u", level_score); draw_string(3, message); draw_frame(); sprintf(message, " congratulations!!!"); draw_string(5, message); draw_frame(); //wait_infinitely while(1); } void draw_game_over() { uint16_t i; char score_str[100]; for(i=0; i<1024; i++) back_buffer[i] = game_over[i]; sprintf(score_str, " Score : %u", level_score); draw_string(6, score_str); draw_frame(); play_buzzer(200*1000); _delay_ms(200); play_buzzer(200*1000); _delay_ms(200); play_buzzer(200*1000); while(1); } void player_won() { uint16_t i; char score_str[100]; for(i=0; i<1024; i++) back_buffer[i] = you_win[i]; sprintf(score_str, " Score : %u", level_score*lives); draw_string(6, score_str); draw_frame(); while(1); } void play_buzzer(unsigned long buzzer_time_us) { IO1DIR |= (1<<25); IO1CLR |= (1<<25); _delay_us(buzzer_time_us); IO1SET |= (1<<25); } void init_game() { uint8_t i,j,k; srand(i*j/k); m_ball.radius = BALL_RADIUS; m_ball.initial_x = 128/2; m_ball.initial_y = 64 - 8; m_ball.is_visible = 1; m_ball.curr_x = m_ball.initial_x; m_ball.curr_y = m_ball.initial_y; m_ball.speed_x = DEFAULT_X_SPEED; m_ball.speed_y = DEFAULT_Y_SPEED; paddle.pattern = 1; paddle.is_visible = 1; paddle.offset = 1; paddle.width = PADDLE_WIDTH; paddle.height = PADDLE_HEIGHT; paddle.x1 = 128/2 - paddle.width/2; paddle.y1 = 64 - 3; for(i=0; i= 2) paddle.x1 -= 2; } if(right_button_down()) { if(paddle.x1 + paddle.width <= 126) paddle.x1 += 2; } /*Draw the paddle. */ fill_rect(paddle.x1, paddle.y1, paddle.x1 + paddle.width, 64); /* Draw the ball. */ draw_circle(m_ball.curr_x, m_ball.curr_y, m_ball.radius); //draw_circle(x1, y1, 3); m_ball.curr_x += dx; m_ball.curr_y += dy; //x1 += dx; //y1 += dy; if(m_ball.curr_x > (128 - edge) || m_ball.curr_x < (0 + edge)) {dx *= -1; play_buzzer(800); } if(m_ball.curr_y < (0+edge)) {dy = abs(dy); play_buzzer(800);} /*Check for the collision with the paddle. */ if((m_ball.curr_x+edge >= (paddle.x1)) && (m_ball.curr_x-edge <= (paddle.x1 + paddle.width)) && (m_ball.curr_y == paddle.y1)) {// && m_ball.curr_y <= paddle.y1 + paddle.height) { if(m_ball.curr_x <= paddle.x1 + paddle.width/4) dx = -1 * DEFAULT_X_SPEED; else if(m_ball.curr_x >= paddle.x1 + paddle.width/2 +paddle.width/4 ) dx = DEFAULT_X_SPEED; else dx = 0; dy = -1 * abs(dy); m_ball.curr_y = paddle.y1; play_buzzer(600); } /* Check if ball has touched the floor.*/ if(m_ball.curr_y >= 62) { play_buzzer(1000000); lives--; dy = -1 * abs(dy); level_score = level_score/2; if(lives == 0) { draw_game_over(); _delay_ms(3000); display_score_screen(); } } /*Now check if the ball has collided with any of the visible bricks .*/ for(i=0; i= brick_array[i][j].x1 && m_ball.curr_x - (edge-2) <= brick_array[i][j].x1 + brick_array[i][j].width) && (m_ball.curr_y >= brick_array[i][j].y1 && m_ball.curr_y <= brick_array[i][j].y1 + brick_array[i][j].height) ) { /* make the ball disappear */ brick_array[i][j].is_visible = 0; /*Revert the direction of the ball .*/ dy *= -1; /*Make a sound. */ play_buzzer(800); level_score += 2; if(brick_array[i][j].pattern == 0) { //it is a bomb level_score += 10; if(i != 0) {brick_array[i-1][j].is_visible = 0; brick_count -= 1;play_buzzer(800); level_score += 2;} //vanish left brick if(i != NUM_BRICK_ROWS -1 ) {brick_array[i+1][j].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} //vanish right brick if(j != 0) {brick_array[i][j-1].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} if(j != NUM_BRICK_COLS -1 ) {brick_array[i][j+1].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} if(i != 0 && j && 0) {brick_array[i-1][j-1].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} if(i != NUM_BRICK_ROWS && j != NUM_BRICK_COLS) {brick_array[i+1][j+1].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} if(i != NUM_BRICK_ROWS && j != 0) {brick_array[i+1][j-1].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} if(i != 0 && j != NUM_BRICK_COLS) {brick_array[i-1][j+1].is_visible = 0; brick_count -= 1;play_buzzer(800);level_score += 2;} } brick_count--; if(brick_count == 0) player_won(); } } //draw the bricks for(i=0; i