4180-L4

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "uLCD_4DGL.h"
00004 
00005 uLCD_4DGL uLCD(p9, p10, p11);
00006 DigitalIn flap_button(p30);
00007 AnalogIn ain(p20);
00008 
00009 int bird_lo_addr = 0x7801;
00010 int y_pos = 63, old_ypos = 63;
00011 int vel = 1;
00012 int state = 0;
00013 int score = 0;
00014 
00015 // wall_1
00016 int wall_x_pos1 = 128, opening_y_pos1;
00017 
00018 // wall 2
00019 int wall_x_pos2, opening_y_pos2;
00020 
00021 // wall 3
00022 int wall_x_pos3, opening_y_pos3; 
00023 
00024 void draw_frame() {
00025     
00026     switch (state) {
00027         case 0:
00028             uLCD.locate(0, 6);
00029             uLCD.printf("PRESS 2 START");
00030             break;
00031         
00032         case 1:
00033             // Draw blue rectangle over old bird position 
00034             uLCD.set_sector_address(0x001D, 0x7803);
00035             uLCD.display_image(20, old_ypos);
00036             
00037             if(wall_x_pos1 > -21 && wall_x_pos1 < 128){
00038                 // draw wall 1
00039                 if(wall_x_pos1 < 0){
00040                     //uLCD.filled_rectangle(0, 0, wall_x_pos1 + 19, 127, RED);
00041                     //uLCD.filled_rectangle(0, opening_y_pos1, wall_x_pos1 + 19, opening_y_pos1 + 39, BLUE);
00042                     uLCD.filled_rectangle(wall_x_pos1 + 20, 0, wall_x_pos1 + 21, 127, BLUE);
00043                 } else if (wall_x_pos1 > 119){
00044                     uLCD.filled_rectangle(wall_x_pos1, 0, wall_x_pos1+1, opening_y_pos1-1, RED);
00045                     uLCD.filled_rectangle(wall_x_pos1, opening_y_pos1+40, wall_x_pos1+1, 127, RED);
00046                 } else {
00047                     uLCD.filled_rectangle(wall_x_pos1, 0, wall_x_pos1+1, opening_y_pos1-1, RED);   
00048                     uLCD.filled_rectangle(wall_x_pos1, opening_y_pos1+40, wall_x_pos1+1, 127, RED);   
00049                     uLCD.filled_rectangle(wall_x_pos1 + 20, 0, wall_x_pos1 + 21, 127, BLUE);
00050                 }
00051             }
00052             
00053             if(wall_x_pos2 > -21 && wall_x_pos2 < 128){
00054                 // draw wall 1
00055                 if(wall_x_pos2 < 0){
00056                     //uLCD.filled_rectangle(0, 0, wall_x_pos2 + 19, 127, RED);
00057                     //uLCD.filled_rectangle(0, opening_y_pos2, wall_x_pos2 + 19, opening_y_pos2 + 39, BLUE);
00058                     uLCD.filled_rectangle(wall_x_pos2 + 20, 0, wall_x_pos2 + 21, 127, BLUE);
00059                 } else if (wall_x_pos2 > 119){
00060                     uLCD.filled_rectangle(wall_x_pos2, 0, wall_x_pos2+1, opening_y_pos2-1, RED);
00061                     uLCD.filled_rectangle(wall_x_pos2, opening_y_pos2+40, wall_x_pos2+1, 127, RED);
00062                 } else {
00063                     uLCD.filled_rectangle(wall_x_pos2, 0, wall_x_pos2+1, opening_y_pos2-1, RED);   
00064                     uLCD.filled_rectangle(wall_x_pos2, opening_y_pos2+40, wall_x_pos2+1, 127, RED);   
00065                     uLCD.filled_rectangle(wall_x_pos2 + 20, 0, wall_x_pos2 + 21, 127, BLUE);
00066                 }
00067             }
00068             
00069             if(wall_x_pos3 > -21 && wall_x_pos3 < 128){
00070                 // draw wall 3
00071                 if(wall_x_pos3 < 0){
00072                     uLCD.filled_rectangle(wall_x_pos3 + 20, 0, wall_x_pos3 + 21, 127, BLUE);
00073                 } else if (wall_x_pos3 > 119){
00074                     uLCD.filled_rectangle(wall_x_pos3, 0, wall_x_pos3+1, opening_y_pos3-1, RED);
00075                     uLCD.filled_rectangle(wall_x_pos3, opening_y_pos3+40, wall_x_pos3+1, 127, RED);
00076                 } else {
00077                     uLCD.filled_rectangle(wall_x_pos3, 0, wall_x_pos3+1, opening_y_pos3-1, RED);   
00078                     uLCD.filled_rectangle(wall_x_pos3, opening_y_pos3+40, wall_x_pos3+1, 127, RED);   
00079                     uLCD.filled_rectangle(wall_x_pos3 + 20, 0, wall_x_pos3 + 21, 127, BLUE);
00080                 }
00081             }
00082             
00083             // Draw bird at current position
00084             uLCD.set_sector_address(0x001D, bird_lo_addr);
00085             uLCD.display_image(20, y_pos);
00086             
00087             break;
00088             
00089         case 2:
00090             uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
00091             uLCD.locate(0, 6);
00092             uLCD.printf("GAME OVER");
00093             uLCD.locate(0, 7);
00094             uLCD.printf("SCORE: %d", score);
00095             break;
00096     }
00097 }
00098 
00099 void update_frame() {
00100     // Update bird position
00101     old_ypos = y_pos;
00102     y_pos += vel;
00103     
00104     if (y_pos > 122) {
00105         y_pos = 122;
00106     } else if (y_pos < 0) {
00107         y_pos = 0;
00108     }
00109     
00110     if (wall_x_pos1 <= 26 && wall_x_pos1 > 0) {
00111         if (y_pos < opening_y_pos1 || y_pos+5 > opening_y_pos1+39) {
00112             state = 2;
00113             wait(3);
00114         }
00115     }
00116     
00117     if (wall_x_pos2 <= 26 && wall_x_pos2 > 0) {
00118         if (y_pos < opening_y_pos2 || y_pos+5 > opening_y_pos2+39) {
00119             state = 2;
00120             wait(3);
00121         }
00122     }
00123     
00124     if (wall_x_pos3 <= 26 && wall_x_pos3 > 0) {
00125         if (y_pos < opening_y_pos3 || y_pos+5 > opening_y_pos3+39) {
00126             state = 2;
00127             wait(3);
00128         }
00129     }
00130     
00131     // Update bird velocity
00132     if (!flap_button) {
00133         // Toggle bird flap
00134         bird_lo_addr ^= 0x3;
00135     
00136         vel--;
00137         if (vel < -5) vel = -5;
00138     } else {
00139         vel++;
00140         if (vel > 5) vel = 5;
00141     }
00142     
00143     // Update wall positions
00144     if(wall_x_pos1 > -21){
00145         wall_x_pos1 -= 2;
00146     } else {
00147         wall_x_pos1 = wall_x_pos3 + 80 - (rand() % score);
00148         opening_y_pos1 = (int)(ain*1000.0) % 46 + 20;
00149         score++;
00150     }
00151     
00152     if(wall_x_pos2 > -21){
00153         wall_x_pos2 -= 2;
00154     } else {
00155         wall_x_pos2 = wall_x_pos1 + 80 - (rand() % score);
00156         opening_y_pos2 = (int)(ain*1000.0) % 46 + 20;
00157         score++;
00158     }
00159     
00160     if(wall_x_pos3 > -21){
00161         wall_x_pos3 -= 2;
00162     } else {
00163         wall_x_pos3 = wall_x_pos2 + 80 - (rand() % score);
00164         opening_y_pos3 = (int)(ain*1000.0) % 46 + 20;
00165         score++;
00166     }
00167 }
00168 
00169 int main() {    
00170     flap_button.mode(PullUp);
00171     
00172     uLCD.baudrate(3000000);
00173     uLCD.cls();
00174     uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
00175     uLCD.media_init();
00176     
00177     opening_y_pos1 = rand() % 46 + 20;
00178     opening_y_pos2 = rand() % 46 + 20;
00179     opening_y_pos3 = rand() % 46 + 20;
00180     
00181     wall_x_pos2 = wall_x_pos1 + 80 - (rand() % 5);
00182     wall_x_pos3 = wall_x_pos2 + 80 - (rand() % 5);
00183     
00184     // Bird is 6 rows, 7 cols
00185     
00186     while (1) {
00187         draw_frame();
00188         
00189         if (state == 1) {
00190             update_frame();
00191         } else if (state == 0 && !flap_button) {
00192             uLCD.filled_rectangle(0, 0, 127, 127, BLUE);
00193             state = 1;   
00194         }
00195         
00196         wait_ms(50);
00197     }
00198 }