Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Revision:
12:60c856354406
Parent:
10:ee781d18e0f6
Child:
13:b37dde18bfdc
--- a/snake.cpp	Thu May 28 20:58:49 2020 +0000
+++ b/snake.cpp	Fri May 29 16:00:56 2020 +0000
@@ -17,17 +17,23 @@
     _x1 = 48;
     _x2 = 48;
     _x3 = 48;
+    _x4 = 48;
+    _x5 = 48;
 
     _y0 = 20;
     _y1 = 19;
     _y2 = 18;
     _y3 = 17;
-
+    _y4 = 16;
+    _y5 = 15;
+    
     _apx = 48;
     _apy = 25;
     _gameover = false;
+    _reset_apple = false;
     _score = 0;
     _direction = up;
+    _countdown = 30;
     //  Vector2D *_snakebody = new Vector2D [_length];
 }
 
@@ -45,158 +51,205 @@
 
 
 
-void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Timer &timer)
-{    
-    
+void Snake::apple_collected(N5110 &lcd, Gamepad &pad)
+{
+
     //need to code clear apple and make sure apple isnt spawning every time
     if((_x0 == _apx) && (_y0 == _apy)) {
 
-        _score++;              //causes new apple position to be generated
-        timer.reset();
+        //   _countdown = _reset_value;
+        _score++;
+        _reset_apple = true;
+        _countdown =10;          //causes new apple position to be generated
         pad.tone(1500.0,0.5);
         pad.led(2, 1);
         pad.led(4, 1);
-        wait(0.5);
-        pad.led(2, 0);
-        pad.led(4, 0);
+
+        //pad.led(2, 0);
+        //pad.led(4, 0);
+
+
+
+    } else {
+        _countdown = _countdown - 1;
+        printf("  countdown = %d      score   %d  ", _countdown ,_score);
 
     }
+}
 
+int Snake::get_countdown()
+{
+    return _countdown;
 }
 
 
-void Snake::check_collisions()   //code where it hits itself
+void Snake::check_gameover(N5110 &lcd)   //code where it hits itself
 {
-    if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) {   //how do i access snakehead.headx
+        if (_x0 == 0 ||_x0 == 84 || _y0 == 32 || _y0 == 0) {   //how do i access snakehead.headx
 
-        _gameover = true;
+            _gameover = true;
+        }
+        if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2) || (_x0 == _x3 && _y0 == _y3) || (_x0 == _x4 && _y0 == _y4)|| (_x0 == _x5 && _y0 == _y5)) {
+            _gameover = true;
+        }
+        if(_countdown == 0) {
+            _gameover = true;                                                  //|| (_x0 == _x4 && _y0 == _y4))
+
+        }
     }
-    if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2)) {
-        _gameover = true;
-    }                                                                       //|| (_x0 == _x4 && _y0 == _y4))
-    else {
-        _gameover = _gameover;
+
+void Snake::gameover_true(N5110 &lcd)
+{
+
+    while (_gameover == true) {
+
+        lcd.printString( "  Game Over L ", 0, 2 );    
+    char buffer1[14];
+    sprintf(buffer1,"%2d",_score);
+    lcd.printString(buffer1,0 ,48);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+
+
+        if (_score <= 7) {
+            lcd.printString("    Loser     ", 0,10);
+        }
+        if (7< _score <= 25) {
+            lcd.printString("    Good Job!     ", 0,10);
+        } else {
+            lcd.printString("    SNAKE PRO     ", 0,10);
+            lcd.printString(" ~.~.~.~.<8>~  ", 0, 5);
+
+        }
+
     }
 }
 
+void Snake::render(N5110 &lcd)
+{
+    lcd.refresh();
+
+    //apple
+    lcd.setPixel(_apx, _apy,1);                        //apple
+
+    lcd.drawRect(0, 0, 84, 40, FILL_TRANSPARENT);    //game
+    lcd.setPixel(_x0, _y0,1);                        //snake
+    lcd.setPixel(_x1, _y1,1);
+    lcd.setPixel(_x2, _y2,1);
+    lcd.setPixel(_x3, _y3,1);
+    lcd.setPixel(_x4, _y4,1);
+    lcd.setPixel(_x5, _y5,1);
+    }
+
 
 
 
-void Snake::render(N5110 &lcd)
-{
-
-    //apple
-    lcd.setPixel(_apx, _apy,1);                        //apple
-
-    lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);    //game
-
-
-    lcd.setPixel(_x0, _y0,1);                        //snake
-    lcd.setPixel(_x1, _y1,1);
-    lcd.setPixel(_x2, _y2,1);
-    lcd.setPixel(_x3, _y3,1);
-
-
-    lcd.refresh();
-
-}
-
 
 void Snake::get_direction(Gamepad &pad)
 {
+   // int x;
     Directions direction = _direction;
     if(direction != left) {
         if (pad.A_pressed()) {
 
             _direction = right;
+     //       x=1;
         }
     }
     if(direction != right) {
         if (pad.Y_pressed()) {
 
             _direction = left;
-        }                                 //check these are orrecrt
+      //      x=2;
+        }
+
+        //check these are orrecrt
     }
     if(direction != down) {
-        if (pad.X_pressed()) {
-
+        if (pad.B_pressed()) {
+     //       x=3;
             _direction = up;
         }
     }
     if(direction != up) {
-        if (pad.B_pressed()) {
+        if (pad.X_pressed()) {
 
             _direction = down;
-        }
-    } 
-        else {
-            _direction = direction;
-        }
-    }
-
-
-    void Snake::move_snake() {
-        if (_direction == up) {           //           /-/
-            _x3 = _x2;
-            _y3 = _y2;
-            _x2 = _x1;
-            _y2 = _y1;
-            _x1 = _x0;
-            _y1 = _y0;
-
-            _x0 = _x0;
-            _y0 = _y0 + 1;
+     //       x=4;
         }
-        if (_direction == down) {
-            _x3 = _x2;
-            _y3 = _y2;
-            _x2 = _x1;
-            _y2 = _y1;
-            _x1 = _x0;
-            _y1 = _y0;
-
-            _x0 = _x0;
-            _y0 = _y0 - 1;
+    } else {
+        _direction = _direction;
 
-        }
-        if (_direction == left) {
-
-            _x3 = _x2;
-            _y3 = _y2;
-            _x2 = _x1;
-            _y2 = _y1;
-            _x1 = _x0;
-            _y1 = _y0;
-
-            _x0 = _x0 - 1;
-            _y0 = _y0;
+    }
+    //  printf("direction %d ", x);
+}
 
 
 
-        }
+void Snake::move_snake()
+{
+    if (_direction == up) {
+        _x5 = _x5; 
+        _x4 = _x3;      
+        _x3 = _x2;
+        _y3 = _y2;
+        _x2 = _x1;
+        _y2 = _y1;
+        _x1 = _x0;
+        _y1 = _y0;
 
-        if (_direction == right) {
-            _x3 = _x2;
-            _y3 = _y2;
-            _x2 = _x1;
-            _y2 = _y1;
-            _x1 = _x0;
-            _y1 = _y0;
+        _x0 = _x0;
+        _y0 = _y0 + 1;
+    }
+    if (_direction == down) {
+        _x5 = _x4;
+        _x4 = _x3;
+        _x3 = _x2;
+        _y3 = _y2;
+        _x2 = _x1;
+        _y2 = _y1;
+        _x1 = _x0;
+        _y1 = _y0;
 
-            _x0 = _x0 + 1;
-            _y0 = _y0;
+        _x0 = _x0;
+        _y0 = _y0 - 1;
+
+    }
+    if (_direction == left) {
+
+        _x3 = _x2;
+        _y3 = _y2;
+        _x2 = _x1;
+        _y2 = _y1;
+        _x1 = _x0;
+        _y1 = _y0;
+
+        _x0 = _x0 - 1;
+        _y0 = _y0;
 
 
-        }
 
     }
 
+    if (_direction == right) {
+        _x3 = _x2;
+        _y3 = _y2;
+        _x2 = _x1;
+        _y2 = _y1;
+        _x1 = _x0;
+        _y1 = _y0;
+
+        _x0 = _x0 + 1;
+        _y0 = _y0;
+
+
+    } else
+        _direction = _direction;
+}
 
 
 void Snake::render_clear_tail(N5110 &lcd)
 {
-    _oldscore = _score;
-    lcd.clearPixel(_x3, _y3);
+
+    lcd.setPixel(_x5, _y5, false);
 }
 
 bool Snake::get_gameover()
@@ -213,55 +266,64 @@
 }
 
 
+int Snake::get_reset_value()
+{
+    return _reset_value;
+}
+
 void Snake::get_Apple_position(N5110 &lcd)
 {
-int appleposx;
-int appleposy;
-    if(_oldscore != _score) {
-         appleposx = rand()%84;
-         appleposy = rand()%48;  
-         
-         _apx = appleposx;
-         _apy = appleposy;
+    if(_reset_apple == true)
+     {
+    lcd.setPixel(_apx, _apy,0);
+        _apx = rand()%82+1;
+        _apy = rand()%30+1;
+
+        _reset_apple = false;
+    } else {
+        _apx = _apx;
+        _apy = _apy;
     }
-    else {
- //   if(lcd.getPixel(appleposx, appleposy)==1) {      // this pixel is set -- 'if it is already filled on lcd, pick new position'
-    _apx = _apx;                 //i and j are fed into applepos.x/y -- those values are then fed into set_applepos which assigngs that value to _appleposx/y which then is fed into get_applepos where it is stored and returned
-    _apy = _apy;     //alters value of private variable - this can then be accessed by get_Applepos
 
 }
-}
-
+/*
 void Snake::get_time(Timer &timer)
 {
-
+    ticker.attach(&flip, 2.0);
     _realtime = timer.read();
     _display_time = (Reset_value - _realtime);
+    int x = _realtime;
+    int y = _display_time;
 
-    if(_realtime == Reset_value) {
-        _gameover = true;
-    }
+    printf("12-T = %d     t =  %d   ", y, x);
+*/
+
+int Snake::set_reset_value()
+{
 
     if (0 <= _score && _score < 10) {
-        Reset_value = 12;
+        _reset_value = 12;
     }
     if (10 <= _score && _score < 15) {
-        Reset_value = 10;
+        _reset_value = 10;
     }
     if (15 <= _score && _score < 20) {
-        Reset_value = 8;
+        _reset_value = 8;
     } else {
-        Reset_value = 6;
+        _reset_value = 6;
     }
+
+    return _reset_value;
 }
 
-void Snake::print_display_time(N5110 &lcd)
-{
-    int countdown = _realtime;
+
+//void Snake::print_display_time(N5110 &lcd)
+//{
+  //  int countdown = _realtime;
 
-    char buffer1[14];
-    sprintf(buffer1,"%2d",countdown);
-    lcd.printString(buffer1,WIDTH/2,30);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
-    char buffer2[14];
+  //  char buffer1[14];
+    //sprintf(buffer1,"%2d",countdown);
+    //lcd.printString(buffer1,WIDTH/2,30);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    //char buffer2[14];
     
-}
\ No newline at end of file
+//}
\ No newline at end of file