ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18zc_

Dependencies:   mbed

Revision:
15:7ca2d1b2bd0e
Parent:
9:a8420b353bb0
--- a/Touch/Touch.cpp	Fri May 29 04:25:22 2020 +0000
+++ b/Touch/Touch.cpp	Fri May 29 07:35:45 2020 +0000
@@ -2,17 +2,18 @@
 
 
 //initialisation part
-void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd)
+void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd,int _board_y1,int _board_x1)
 {
     // initialise the game parameters
     _Board_width = Board_width;         //width of board
     _Board_length = Board_length;       //length of board
     _bullet_size = bullet_size;         //bullet size
     _speed = speed;                     //speed
-    _leds=0;//led number
-    
+    _leds=0;                            //led number
+    _board_x=_board_x1;
+    _board_y=_board_y1;
     _board.init(_board_x,_board_y,_Board_length,_Board_width);     // draw board
-    _bullet.init(_board_x,Board_length,_speed,_Board_length);          //draw bullet
+    _bullet.init(_board_x,_bullet_size,_speed,_board_y);          //draw bullet
    
 }
 
@@ -24,31 +25,8 @@
 
 void Touch::draw(N5110 &lcd)
 {
-
-    Vector2D bullet_pos = _bullet.get_pos();                        //bullet position data
- /*   s=0;
-    if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
-    {
-         //s++;
-         Y[s]= bullet_pos.y ;
-         X[s]= bullet_pos.x;
-         s++;
-    }*/
-    for(int r=1;r<84;r++)
-        for(int c=1;c<24;c++)
-        {
-            lcd.setPixel(r,c);
-            if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
-            {  
-                
-                 Y= bullet_pos.y;
-                 X= bullet_pos.x;
-                 lcd.clearPixel(X,Y-1);
-             /*   for(int i=0;i<s;i++)
-                    lcd.clearPixel(X[i],Y[i]-1);*/
-            }
-        }
     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); // draw a platform for game
+    lcd.drawRect(0,20,84,4,FILL_BLACK); // score board
     print_scores(lcd);
     _board.draw(lcd);   // draw a board for game
     _bullet.draw(lcd);  // draw a bullet for game
@@ -101,15 +79,16 @@
 {
     Vector2D bullet_pos = _bullet.get_pos();
     Vector2D bullet_velocity = _bullet.get_velocity();
-    Vector2D p1_pos = _board.get_pos();
+    Vector2D b_pos = _board.get_pos();
     if (
-        (bullet_pos.x >= p1_pos.x) &&                   //left
-        (bullet_pos.x <= p1_pos.x + _Board_length) &&   //right
-        (bullet_pos.y+_bullet_size >=p1_pos.y)&&        //down
-        (bullet_pos.y-_Board_width<=p1_pos.y)           //up
+        (bullet_pos.x >= b_pos.x) &&                   //left
+        (bullet_pos.x <= b_pos.x + _Board_length) &&   //right
+        (bullet_pos.y+_bullet_size >=b_pos.y)&&        //down
+        (bullet_pos.y-_Board_width<=b_pos.y)           //up
     ){
         
         // if it has, fix position and reflect x velocity
+        _board.add_score();
         bullet_velocity.y = -bullet_velocity.y;     //fix position and reflect y velocity
         pad.tone(1000.0,0.1);         // audio effect as reminder
         
@@ -129,8 +108,7 @@
 {
     Vector2D bullet_pos = _bullet.get_pos();
     if (bullet_pos.y + _bullet_size> 47) {
-        _board.add_score();
-        _bullet.init(_board_x,_bullet_size,_speed,_Board_length);
+        _bullet.init(_board_x,_bullet_size,_speed,_board_y);
         _leds++;
         if(_leds>6)
          {  
@@ -147,10 +125,10 @@
 void Touch::print_scores(N5110 &lcd)
 {
     // get scores from Boards
-    int p1_score = _board.get_score();
+    int b_score = _board.get_score();
 
     // print to LCD i
     char buffer1[14];
-    sprintf(buffer1,"%2d",p1_score);
-    lcd.printString(buffer1,WIDTH/2 - 20,1); 
+    sprintf(buffer1,"Score is %2d",b_score);
+    lcd.printString(buffer1,10,1); 
 }
\ No newline at end of file