Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Revision:
10:ee781d18e0f6
Parent:
9:a69a6a06dddf
Child:
11:e7c56013acd9
Child:
12:60c856354406
--- a/snake.cpp	Thu May 28 19:16:52 2020 +0000
+++ b/snake.cpp	Thu May 28 20:58:49 2020 +0000
@@ -46,8 +46,8 @@
 
 
 void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Timer &timer)
-{
-
+{    
+    
     //need to code clear apple and make sure apple isnt spawning every time
     if((_x0 == _apx) && (_y0 == _apy)) {
 
@@ -86,7 +86,7 @@
 {
 
     //apple
-    lcd.setPixel(_apx, _apy,1);
+    lcd.setPixel(_apx, _apy,1);                        //apple
 
     lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);    //game
 
@@ -112,7 +112,7 @@
         }
     }
     if(direction != right) {
-        if (pad.B_pressed()) {
+        if (pad.Y_pressed()) {
 
             _direction = left;
         }                                 //check these are orrecrt
@@ -124,7 +124,7 @@
         }
     }
     if(direction != up) {
-        if (pad.Y_pressed()) {
+        if (pad.B_pressed()) {
 
             _direction = down;
         }
@@ -195,6 +195,7 @@
 
 void Snake::render_clear_tail(N5110 &lcd)
 {
+    _oldscore = _score;
     lcd.clearPixel(_x3, _y3);
 }
 
@@ -206,27 +207,29 @@
 {
     return _score;
 }
-
+int Snake::get_oldscore()
+{
+    return _oldscore;
+}
 
 
 void Snake::get_Apple_position(N5110 &lcd)
 {
 int appleposx;
 int appleposy;
-    if(_score++) {
+    if(_oldscore != _score) {
          appleposx = rand()%84;
-         appleposy = rand()%48;             //ROB apparently rand() is seeded so will generate same position each time - do you know any other random methods to put here?
+         appleposy = rand()%48;  
+         
+         _apx = appleposx;
+         _apy = appleposy;
     }
+    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
 
-    if(lcd.getPixel(appleposx, appleposy)==1) {      // this pixel is set -- 'if it is already filled on lcd, pick new position'
-        appleposx = rand()%84;
-        appleposy = rand()%48;   //  making sure the apple doesnt spawn inside the snakes body or wall which would increase the score
-    }
-
-
-    _apx = appleposx;                 //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 = appleposy;      //alters value of private variable - this can then be accessed by get_Applepos
-
+}
 }
 
 void Snake::get_time(Timer &timer)
@@ -250,4 +253,15 @@
     } else {
         Reset_value = 6;
     }
+}
+
+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];
+    
 }
\ No newline at end of file