ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18zc2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Touch.cpp Source File

Touch.cpp

00001 #include "Touch.h"
00002 
00003 
00004 //initialisation part
00005 void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd)
00006 {
00007     // initialise the game parameters
00008     _Board_width = Board_width;         //width of board
00009     _Board_length = Board_length;       //length of board
00010     _bullet_size = bullet_size;         //bullet size
00011     _speed = speed;                     //speed
00012     _leds=0;//led number
00013     
00014     _board.init(_board_x,_board_y,_Board_length,_Board_width);     // draw board
00015     _bullet.init(_board_x,Board_length,_speed,_Board_length);          //draw bullet
00016    
00017 }
00018 
00019 void Touch::reading(Gamepad &pad)
00020 {
00021     _d = pad.get_direction();    // joystick direction
00022     _mag = pad.get_mag();        // joystick direction movement parameter
00023 }
00024 
00025 void Touch::draw(N5110 &lcd)
00026 {
00027 
00028     Vector2D bullet_pos = _bullet.get_pos();                        //bullet position data
00029  /*   s=0;
00030     if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
00031     {
00032          //s++;
00033          Y[s]= bullet_pos.y ;
00034          X[s]= bullet_pos.x;
00035          s++;
00036     }*/
00037     for(int r=1;r<84;r++)
00038         for(int c=1;c<24;c++)
00039         {
00040             lcd.setPixel(r,c);
00041             if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
00042             {  
00043                 
00044                  Y= bullet_pos.y;
00045                  X= bullet_pos.x;
00046                  lcd.clearPixel(X,Y-1);
00047              /*   for(int i=0;i<s;i++)
00048                     lcd.clearPixel(X[i],Y[i]-1);*/
00049             }
00050         }
00051     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw a platform for game
00052     print_scores(lcd);
00053     _board.draw(lcd);   // draw a board for game
00054     _bullet.draw(lcd);  // draw a bullet for game
00055 }
00056 
00057 void Touch::update(Gamepad &pad,N5110 &lcd)
00058 {
00059     drop(pad);
00060     _board.update(_d,_mag);
00061     _bullet.update(lcd);
00062 
00063     Boundary_touch(pad);
00064     Board_touch(pad,lcd);
00065 }
00066 
00067 void Touch::Boundary_touch(Gamepad &pad)
00068 {
00069     // read current ball attributes
00070     Vector2D bullet_pos = _bullet.get_pos();
00071     Vector2D bullet_velocity = _bullet.get_velocity();
00072 
00073     // check if hit top wall
00074     if (bullet_pos.y <= 1) {  //  1 due to 1 pixel boundary
00075         bullet_pos.y = 1;  // bounce off ceiling without going off screen
00076         bullet_velocity.y = -bullet_velocity.y;
00077         // audio feedback
00078         pad.tone(750.0,0.1);
00079     }
00080     // check if hit right wall
00081     else if (bullet_pos.x+_bullet_size >= 83 ) { // right wall is 83
00082         // hit bottom
00083         bullet_pos.x = (WIDTH-1) - _bullet_size;  // stops ball going off screen
00084         bullet_velocity.x = -bullet_velocity.x;
00085         // audio feedback
00086         pad.tone(750.0,0.1);
00087     }// check if hit left wall
00088     else if (bullet_pos.x <= 1 ) { // left wall pixel is 1
00089         // hit bottom
00090         bullet_pos.x = 1;  // stops ball going off screen
00091         bullet_velocity.x = -bullet_velocity.x;
00092         // audio feedback
00093         pad.tone(750.0,0.1);
00094     }
00095     // update ball parameters
00096     _bullet.set_velocity(bullet_velocity);
00097     _bullet.set_pos(bullet_pos);
00098 }
00099 
00100 void Touch::Board_touch(Gamepad &pad,N5110 &lcd)
00101 {
00102     Vector2D bullet_pos = _bullet.get_pos();
00103     Vector2D bullet_velocity = _bullet.get_velocity();
00104     Vector2D p1_pos = _board.get_pos();
00105     if (
00106         (bullet_pos.x >= p1_pos.x) &&                   //left
00107         (bullet_pos.x <= p1_pos.x + _Board_length) &&   //right
00108         (bullet_pos.y+_bullet_size >=p1_pos.y)&&        //down
00109         (bullet_pos.y-_Board_width<=p1_pos.y)           //up
00110     ){
00111         
00112         // if it has, fix position and reflect x velocity
00113         bullet_velocity.y = -bullet_velocity.y;     //fix position and reflect y velocity
00114         pad.tone(1000.0,0.1);         // audio effect as reminder
00115         
00116     }
00117     if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
00118         {
00119             bullet_pos.y = bullet_pos.y + _Board_width;
00120             bullet_velocity.y = -bullet_velocity.y;
00121 
00122         }
00123     // write new datas
00124     _bullet.set_velocity(bullet_velocity);
00125     _bullet.set_pos(bullet_pos);
00126 }
00127 
00128 void Touch::drop(Gamepad &pad)
00129 {
00130     Vector2D bullet_pos = _bullet.get_pos();
00131     if (bullet_pos.y + _bullet_size> 47) {
00132         _board.add_score();
00133         _bullet.init(_board_x,_bullet_size,_speed,_Board_length);
00134         _leds++;
00135         if(_leds>6)
00136          {  
00137             _leds=6;
00138         }
00139         pad.led(_leds,0);
00140         pad.tone(1500.0,0.5);
00141       //  pad.leds_on();
00142         wait(0.5);
00143      //   pad.leds_off();
00144     }
00145 }
00146 
00147 void Touch::print_scores(N5110 &lcd)
00148 {
00149     // get scores from Boards
00150     int p1_score = _board.get_score();
00151 
00152     // print to LCD i
00153     char buffer1[14];
00154     sprintf(buffer1,"%2d",p1_score);
00155     lcd.printString(buffer1,WIDTH/2 - 20,1); 
00156 }