ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18zc_

Dependencies:   mbed

Revision:
7:f61ac963eb07
Parent:
6:b393cfe4e0a7
Child:
8:5f0190b282f7
--- a/Touch/Touch.cpp	Thu May 28 01:11:47 2020 +0000
+++ b/Touch/Touch.cpp	Fri May 29 02:55:37 2020 +0000
@@ -1,67 +1,70 @@
 #include "Touch.h"
 
-Touch::Touch()
-{
 
-}
-
-Touch::~Touch()
-{
-
-}
-
-void Touch::init(int board_width,int board_height,int bullet_size,int speed)
+//initialisation part
+void Touch::init(int Board_width,int Board_length,int bullet_size,int speed,N5110 &lcd)
 {
     // initialise the game parameters
-    _Board_width = board_width;
-    _Board_height = board_height;
-    _bullet_size = bullet_size;
-    _speed = speed;
-
-    // x position on screen - WIDTH is defined in N5110.h
-    _p1x = GAP;
-    _p2x = WIDTH - GAP - _Board_width;
-    _p1y=48-board_height*0.5;
-    // puts boards and ball in middle
-    _p1.init(_p1x,_p1y,_Board_height,_Board_width);
-    //lcd.drawRect(0,0,84,24,FILL_BLACK);
-    _bullet.init(_p1x,board_height,_speed,_Board_height);
+    _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
+    
+    _board.init(_board_x,_board_y,_Board_length,_Board_width);     // draw board
+    _bullet.init(_board_x,Board_length,_speed,_Board_length);          //draw bullet
+   
 }
 
-void Touch::read_input(Gamepad &pad)
+void Touch::reading(Gamepad &pad)
 {
-    _d = pad.get_direction();
-    _mag = pad.get_mag();
+    _d = pad.get_direction();    // joystick direction
+    _mag = pad.get_mag();        // joystick direction movement parameter
 }
 
 void Touch::draw(N5110 &lcd)
 {
-    // draw the elements in the LCD buffer
-    // pitch
-    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
-    lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2);
-    lcd.drawRect(0,0,84,24,FILL_BLACK);
-    //lcd.drawRect(0,0,84,24,FILL_BLACK);
-    //score
+
+    Vector2D bullet_pos = _bullet.get_pos(); //bullet position
+ /*   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
     print_scores(lcd);
-    // boards
-    _p1.draw(lcd);
-    _p2.draw(lcd);
-    // ball
+    _board.draw(lcd);
     _bullet.draw(lcd);
 }
 
-void Touch::update(Gamepad &pad)
+void Touch::update(Gamepad &pad,N5110 &lcd)
 {
     check_goal(pad);
-    // important to update boards and ball before checking collisions so can
+    // important to update Boards and ball before checking collisions so can
     // correct for it before updating the display
-    _p1.update(_d,_mag);
+    _board.update(_d,_mag);
     //_p2.update(_d,_mag);
-    _bullet.update();
+    _bullet.update(lcd);
 
     check_wall_collision(pad);
-    check_Board_collisions(pad);
+    check_Board_collisions(pad,lcd);
 }
 
 void Touch::check_wall_collision(Gamepad &pad)
@@ -77,68 +80,55 @@
         // audio feedback
         pad.tone(750.0,0.1);
     }
-    // check if hit bottom wall
-    else if (bullet_pos.y + _bullet_size >= (HEIGHT-1) ) { // bottom pixel is 47
+    // check if hit right wall
+    else if (bullet_pos.x+_bullet_size >= 83 ) { // right wall is 83
         // hit bottom
-        bullet_pos.y = (HEIGHT-1) - _bullet_size;  // stops ball going off screen
-        bullet_velocity.y = -bullet_velocity.y;
+        bullet_pos.x = (WIDTH-1) - _bullet_size;  // stops ball going off screen
+        bullet_velocity.x = -bullet_velocity.x;
+        // audio feedback
+        pad.tone(750.0,0.1);
+    }// check if hit left wall
+    else if (bullet_pos.x <= 1 ) { // left wall pixel is 1
+        // hit bottom
+        bullet_pos.x = 1;  // stops ball going off screen
+        bullet_velocity.x = -bullet_velocity.x;
         // audio feedback
         pad.tone(750.0,0.1);
     }
-
     // update ball parameters
     _bullet.set_velocity(bullet_velocity);
     _bullet.set_pos(bullet_pos);
 }
 
-void Touch::check_Board_collisions(Gamepad &pad)
+void Touch::check_Board_collisions(Gamepad &pad,N5110 &lcd)
 {
-    // read current ball attributes
     Vector2D bullet_pos = _bullet.get_pos();
     Vector2D bullet_velocity = _bullet.get_velocity();
-
-    // check p1 first
-    Vector2D p1_pos = _p1.get_pos();
-    Vector2D p2_pos = _p2.get_pos();
-    // see if ball has hit the board by checking for overlaps
-    //lcd.drawRect(0,0,84,24,FILL_BLACK);
+    Vector2D p1_pos = _board.get_pos();
     if (
-        (bullet_pos.y >= p1_pos.y) && //top
-        (bullet_pos.y <= p1_pos.y + _Board_height) && //bottom
-        (bullet_pos.x >= _p1x) && //left
-        (bullet_pos.x <= _p1x + _Board_width)  //right
+        (bullet_pos.x >= p1_pos.x) && //top
+        (bullet_pos.x <= p1_pos.x + _Board_length) && //bottom
+        (bullet_pos.y+_bullet_size >=p1_pos.y) //left
+          //right
     ){
         
         // if it has, fix position and reflect x velocity
-        bullet_pos.x = _p1x + _Board_width;
-        bullet_velocity.x = -bullet_velocity.x;
+      //  bullet_pos.y = _boardy - _bullet_size;
+        bullet_velocity.y = -bullet_velocity.y;
         // audio feedback
         pad.tone(1000.0,0.1);
         
     }
     if((bullet_pos.y >= 0)&&(bullet_pos.y <= 24))
         {
-         // lcd.drawRect(bullet_pos.x,bullet_pos.y-4,4,4,FILL_TRANSPARENT);
+           // Y= bullet_pos.y ;
+          //  X= bullet_pos.x;
+           // lcd.clearPixel(X,_Y-1);
             bullet_pos.y = bullet_pos.y + _Board_width;
             bullet_velocity.y = -bullet_velocity.y;
-            }
-    // check p2 next
-   /* Vector2D p2_pos = _p2.get_pos();
-
-    // see if ball has hit the board by checking for overlaps
-    if (
-        (ball_pos.y >= p2_pos.y) && //top
-        (ball_pos.y <= p2_pos.y + _Board_height) && //bottom
-        (ball_pos.x + _bullet_size >= _p2x) && //left
-        (ball_pos.x + _bullet_size <= _p2x + _Board_width)  //right
-    ) {
-        // if it has, fix position and reflect x velocity
-        ball_pos.x = _p2x - _bullet_size;
-        ball_velocity.x = -ball_velocity.x;
-        // audio feedback
-        pad.tone(1000.0,0.1);
-    }*/
-
+          //  lcd.clearPixel(_x,_y-1);
+        }
+    // lcd.clearPixel(X,Y-1);*/
     // write new attributes
     _bullet.set_velocity(bullet_velocity);
     _bullet.set_pos(bullet_pos);
@@ -147,32 +137,30 @@
 void Touch::check_goal(Gamepad &pad)
 {
     Vector2D bullet_pos = _bullet.get_pos();
-    // P2 has scored
-    if (bullet_pos.x + _bullet_size < 0) {
-        _p2.add_score();
-        _bullet.init(_p1x,_bullet_size,_speed,_Board_height);
-        pad.tone(1500.0,0.5);
-        pad.leds_on();
-        wait(0.5);
-        pad.leds_off();
-    }
+    
+    
 
     // P1 has scored
-    if (bullet_pos.x > WIDTH) {
-        _p1.add_score();
-        _bullet.init(_p1x,_bullet_size,_speed,_Board_height);
+    if (bullet_pos.y + _bullet_size> 47) {
+        _board.add_score();
+        _bullet.init(_board_x,_bullet_size,_speed,_Board_length);
+        _leds++;
+        if(_leds>6)
+         {  
+            _leds=6;
+        }
+        pad.led(_leds,0);
         pad.tone(1500.0,0.5);
-        pad.leds_on();
+      //  pad.leds_on();
         wait(0.5);
-        pad.leds_off();
+     //   pad.leds_off();
     }
 }
 
 void Touch::print_scores(N5110 &lcd)
 {
-    // get scores from boards
-    int p1_score = _p1.get_score();
-    int p2_score = _p2.get_score();
+    // get scores from Boards
+    int p1_score = _board.get_score();
 
     // print to LCD i
     char buffer1[14];