ELEC2645 (2018/19) / Mbed 2 deprecated el17kz

Dependencies:   mbed

Revision:
5:df0bf821b4cc
Parent:
4:7fca66882a00
Child:
8:5fde4e54a2f4
--- a/GameEngine/GameEngine.cpp	Thu May 09 06:17:43 2019 +0000
+++ b/GameEngine/GameEngine.cpp	Thu May 09 13:49:02 2019 +0000
@@ -20,7 +20,7 @@
     _obstacle_height = obstacle_height;
 
     // x position on screen - WIDTH is defined in N5110.h
-    _rx = 20;
+    _rx = START;
     
     _o1y = 32;
    // _o2y = 25;
@@ -33,7 +33,7 @@
 void GameEngine::read_input(Gamepad &pad)
 {
     _j = pad.check_event(Gamepad::Y_PRESSED);
-    _ran = pad.check_event(Gamepad::X_PRESSED);
+    _ran = true;
 }
 
 void GameEngine::draw(N5110 &lcd)
@@ -41,6 +41,8 @@
     // draw the elements in the LCD buffer
     lcd.drawLine(0,FLOOR,WIDTH,FLOOR,1);
     
+     print_scores(lcd);
+    
     _r.draw(lcd);
     
     _o1.draw(lcd);
@@ -48,7 +50,71 @@
 
 void GameEngine::update(Gamepad &pad)
 {
+    check_pass(pad);
+    
     _r.update(_j);
     
     _o1.update(_ran);
+    
+    check_obstacle_collisions(pad);
+    
 }
+
+void GameEngine::check_obstacle_collisions(Gamepad &pad)
+{
+    Vector2D o1_pos = _o1.get_pos();
+    //Vector2D ball_velocity = _ball.get_velocity();
+
+    // check p1 first
+    Vector2D r_pos = _r.get_pos();
+
+    // see if obstacle has hit the runner by checking for overlaps
+    if (
+        (o1_pos.y >= r_pos.y) && //top
+        (o1_pos.y <= r_pos.y + _runner_height) && //bottom
+        (o1_pos.x >= _rx) && //left
+        (o1_pos.x <= _rx + _runner_width)  //right
+    ) {
+        // if it has, fix position and reflect x velocity
+       // ball_pos.x = _p1x + _paddle_width;
+       // ball_velocity.x = -ball_velocity.x;
+        pad.tone(1200.0,1.0);
+        while(1) {
+            pad.leds_on();
+            }
+        // audio feedback
+       
+    }
+
+
+    // write new attributes
+    //_ball.set_velocity(ball_velocity);
+   // _ball.set_pos(ball_pos);
+}
+
+void GameEngine::check_pass(Gamepad &pad)
+{
+    Vector2D o1_pos = _o1.get_pos();
+    // P2 has scored
+    if (o1_pos.x == START - 5) {
+        _r.add_score();
+        //_ball.init(_ball_size,_speed);
+        pad.tone(1500.0,0.3); 
+    }
+
+}
+
+void GameEngine::print_scores(N5110 &lcd)
+{
+    // get score
+    int r_score = _r.get_score();
+    //int p2_score = _p2.get_score();
+
+    // print to LCD i
+    char buffer1[14];
+    sprintf(buffer1,"%2d",r_score);
+    lcd.printString(buffer1,WIDTH/2 - 20,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    //char buffer2[14];
+   // sprintf(buffer2,"%2d",p2_score);
+   // lcd.printString(buffer2,WIDTH/2 + 4,1);
+} 
\ No newline at end of file