This is the code for LCD bouncing ball game.

Dependencies:   4DGL-uLCD-SE LSM9DS0 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // LSM9DS90/uLCD Demo
00002 // ECE 4180 Lab Code Template
00003 
00004 #include "mbed.h"
00005 #include "LSM9DS0.h"
00006 #include "uLCD_4DGL.h"
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <time.h>
00010   
00011 // uncomment this line to enable the uLCD for Part 4 of the lab
00012 #define PART_4
00013 
00014 // SDO_XM and SDO_G are pulled up, so our addresses are:
00015 #define LSM9DS0_XM_ADDR  0x1D // Would be 0x1E if SDO_XM is LOW
00016 #define LSM9DS0_G_ADDR   0x6B // Would be 0x6A if SDO_G is LOW
00017 
00018 // refresh time. set to 500 for part 2 and 50 for part 4
00019 #define REFRESH_TIME_MS 200
00020 
00021 // Verify that the pin assignments below match your breadboard
00022 LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR);
00023 Serial pc(USBTX, USBRX);
00024 
00025 #ifdef PART_4
00026 uLCD_4DGL lcd(p28, p27, p30);
00027 #endif
00028 
00029 //Init Serial port and LSM9DS0 chip
00030 void setup()
00031 {
00032 #ifdef PART_4
00033     lcd.baudrate(3000000);
00034     lcd.background_color(0);
00035     lcd.cls();
00036     
00037     lcd.printf("Initializing...");
00038 #endif
00039 
00040     // Use the begin() function to initialize the LSM9DS0 library.
00041     // You can either call it with no parameters (the easy way):
00042     uint16_t status = imu.begin();
00043 }
00044 
00045 class Brick { 
00046 
00047 public:
00048     int x; 
00049     int y;
00050     int color;
00051     int width;
00052     int height;
00053     bool exist;
00054     
00055     Brick(){
00056         width = 10;
00057         height = 5;
00058         exist = true;
00059     }
00060     
00061     void set(int x, int y){
00062         this->x = x;
00063         this->y = y;    
00064     }
00065 };
00066 
00067 int main()
00068 {
00069     setup();  //Setup sensor and Serial
00070     lcd.cls();
00071 
00072     int score = 0;
00073     Brick* wall[32];
00074     srand(time(0));
00075     for(int i = 0; i < 8; i ++){
00076         for( int j = 0; j < 4; j++){
00077             Brick * brick = new Brick();
00078             brick->set(5 + i * brick->width, 20 + j * brick->height);
00079             int c = 0;
00080             int num = rand() % 8 + 1;
00081             switch (num) {
00082                 case 1:
00083                     c = 0xff0000;
00084                     break;
00085                 case 2:
00086                     c = 0xffff00;
00087                     break;
00088                 case 3:
00089                     c = 0x00FF00;
00090                     break;
00091                 case 4:
00092                     c = 0x0000FF;
00093                     break;
00094                 case 5:
00095                     c = 0xFFC0CB;
00096                     break;
00097                 case 6:
00098                     c = 0xA52A2A;
00099                     break;
00100                 case 7:
00101                     c = 0xd3d3d3;
00102                     break;
00103                 case 8:
00104                     c = 0x800080;
00105                     break; 
00106                 default:
00107                     c = 0x445566;
00108             }
00109             brick->color = c;
00110             wall[i*4 + j] = brick;
00111         }
00112     }
00113     
00114     float player_x = 60.0;
00115     float player_y = 120.0;
00116     float p_width = 30.0;
00117     
00118     float ball_x = 23.0;
00119     float ball_y = 49.0;
00120     float ball_vx = 15.0;
00121     float ball_vy = 15.0;
00122     float ball_r = 2.0;
00123     float width = 110.0;
00124     float height = 120.0;
00125     bool end = false;
00126     bool win = false;
00127     
00128     while (!end && !win)
00129     {
00130         lcd.cls();
00131         lcd.printf("score is %d", score);
00132         imu.readMag();
00133         imu.readAccel();
00134         
00135         for(int i = 0; i < 32; i++ ){
00136             if(wall[i]->exist){
00137                 lcd.filled_rectangle(wall[i]->x, wall[i]->y, wall[i]->x + wall[i]->width, wall[i]->y + wall[i]->height, wall[i]->color);        
00138             }
00139         }
00140         
00141         lcd.circle(ball_x, ball_y, ball_r, RED);
00142         lcd.line(player_x, player_y, player_x + p_width, player_y, BLUE);
00143         
00144         ball_x += ball_vx * 0.7;
00145         ball_y += ball_vy * 0.7;
00146         player_x = player_x - imu.ax_raw * 0.003;
00147         
00148         if(player_x + p_width > width){
00149             player_x = width - p_width;    
00150         }
00151         
00152         if(player_x < 0){
00153             player_x = 0;     
00154         }
00155         
00156         if(ball_x + ball_r > width){
00157             ball_x = width - ball_r;
00158             ball_vx *= -1;
00159         }
00160         
00161         if(ball_x - ball_r < 0){
00162             ball_x = ball_r;
00163             ball_vx *= -1;   
00164         }
00165         
00166         if(ball_y + ball_r > height){
00167             if(ball_x < player_x || ball_x > player_x + p_width){
00168                 end = true;
00169             } else {
00170                 ball_y = height - ball_r;
00171                 ball_vy *= -1;
00172             }
00173         }
00174         
00175         if(ball_y - ball_r < 0){
00176             ball_y = ball_r;
00177             ball_vy *= -1;
00178         }
00179         
00180          for(int i = 0; i < 32; i++ ){
00181             if(wall[i]->exist){
00182                 if (!(ball_x > wall[i]->x + wall[i]->width || 
00183                 ball_x + ball_r < wall[i]->x ||
00184                 ball_y > wall[i]->y + wall[i]->height ||
00185                 ball_y + ball_r < wall[i]->y)){
00186                         wall[i]->exist = false;
00187                         score++;
00188                 }    
00189             }
00190         }
00191         if(score == 32){
00192             win = true;    
00193         }
00194         wait_ms(REFRESH_TIME_MS);
00195     }
00196     lcd.cls();
00197     
00198     if(win){
00199         lcd.printf("You Win!!!");
00200     } else {
00201         lcd.printf("Game Over...");
00202     }
00203 }