Owen Cavender 201159294

Dependencies:   mbed Gamepad2

Revision:
6:bf90044188d0
Parent:
5:d716013c6a18
Child:
7:0ce806455ef1
--- a/snake.cpp	Wed May 27 10:27:34 2020 +0000
+++ b/snake.cpp	Thu May 28 15:02:30 2020 +0000
@@ -7,147 +7,267 @@
 
 Snake::~Snake()
 {
+}
+
+
+
+void Snake::init()
+{
+    _x0 = 48;
+    _x1 = 48;
+    _x2 = 48;
+    _x3 = 48;
+
+    _y0 = 20;
+    _y1 = 19;
+    _y2 = 18;
+    _y3 = 17;
+
+    //  Vector2D SK0 {_x0, _y0};
+    // Vector2D SK1 {_x1, _y1};
+    //Vector2D SK2 {_x2, _y2};
+    //Vector2D SK3 {_x3, _y3};
+
+    _gameover = false;
+    _score = 0;
+    _direction = up;
+                                      //  Vector2D *_snakebody = new Vector2D [_length];
+}
+
+
+void Snake::check_score(N5110 &lcd, Gamepad &pad, Apple &apple, Timer &timer)
+{
+
+
+    Vector2D Apos = apple.get_Applepos(lcd);
+    //   printf("apple coordinate =, %d , %d",Apos.x, Apos.y);               //need to code clear apple and make sure apple isnt spawning every time
+    if((_x0 == Apos.x) && (_y0 == Apos.y)) {
+
+        lcd.setPixel(Apos.x, Apos.y, 0);   // Plem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it
+        _score++;
+        timer.reset();
+               //by waiting unil the snake has passed through before clearing the old apple however the new apple spawn needs to be ASAP after collection
+        //randomises new values for _apx,_apy and draws on lcd -- draws on next loop-- the position is not needed until we compare it to Snakehead
+        pad.tone(1500.0,0.5);          //need to clear apple
+        pad.leds_on();
+        wait(0.5);
+        pad.leds_off();
+
+
+        lcd.refresh();
+
+    }
 
 }
 
 
-void Snake::init (GameEngine &engine)
+void Snake::check_collisions()   //code where it hits itself
 {
-
-    //WIDTH =84 HEIGHT = 42 //snakebody[0] is initalised may have to initilaise the other 2 og snake parts
-    Vector2D SnakeHead = engine.get_Snakehead();
-
-    _length = 2;                                                //  how do i initalise
-    _gameover = false;
-    _score = 0;
-    _direction = up;
-    Vector2D S0 = SnakeHead;
-    
-    // Vector2D S1 = engine.get_oldSnakehead();
-    // Vector2D &Snakehead = {42, 24};
-    // Vector2D &oldSnakehead  = {42, 23};
-}
+    if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) {   //how do i access snakehead.headx
 
-
-
-
-void Snake::gameover_true(N5110 &lcd)      //taking action if crash has occured
-{
-    if (_gameover == true) {
-
-        lcd.clear();
-        lcd.refresh();
-        lcd.printString( "  Game Over L ", 0, 2 );
-        lcd.printString ("score: _score ",15, 15);    //Need to add button to return to main screen / restart  //NEED to return score without ruining structure of lcd.PrintString( "string", x, y)
-
-    }
-
-    else {
-
-
+        _gameover = true;
+    } else {
+        _gameover = _gameover;
     }
 }
 
 
 
 
-void Snake::check_scored(N5110 &lcd, Gamepad &pad, GameEngine &engine)
+void Snake::render(N5110 &lcd, Apple &apple)
 {
-   Vector2D Applepos = engine.get_Applepos();   
-                            //need to code clear apple and make sure apple isnt spawning every time
 
-    if(S0.x == Applepos.x && S0.y == Applepos.y) {
+    Vector2D Apos = apple.get_Applepos(lcd);
+    lcd.setPixel(Apos.x, Apos.y,1);
+    lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT);
 
-        lcd.setPixel(Applepos.x, Applepos.y, 0);   // Plem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it
-        _score++;                                 //by waiting unil the snake has passed through before clearing the old apple however the new apple spawn needs to be ASAP after collection
-        _length = _length++;
-        engine.set_Applepos(lcd);    //randomises new values for _apx,_apy and draws on lcd -- draws on next loop-- the position is not needed until we compare it to Snakehead
-        pad.tone(1500.0,0.5);          //need to clear apple
-        pad.leds_on();
-        wait(0.5);
-        pad.leds_off();
+    lcd.setPixel(_x0, _y0,1);
+    lcd.setPixel(_x1, _y1,1);
+    lcd.setPixel(_x2, _y2,1);
+    lcd.setPixel(_x3, _y3,1);
 
-    }
-    lcd.setPixel(Applepos.x, Applepos.y,1);
+//   lcd.printString("       &d      ",0,0, _display_time);
+
 }
 
+void Snake::get_position(Gamepad &pad)
+{
+    Directions direction = _direction;
+    if(direction != left) {
+        if (pad.A_pressed()) {
+
+            _direction = right;
+        }
+    }
+    if(direction != right) {
+        if (pad.B_pressed()) {
+
+            _direction = left;
+        }                                 //check these are orrecrt
+    }
+    if(direction != down) {
+        if (pad.X_pressed()) {
+
+            _direction = up;
+        }
+    }
+    if(direction != up) {
+        if (pad.Y_pressed()) {
+
+            _direction = down;
+        } else {
+            _direction = direction;
+        }
 
 
-int Snake::set_direction(Gamepad &pad)     //int type as Directions is an enum
-{
-
-    Directions _direction;
+        if (_direction == up) {           //           /-/
+            _x3 = _x2;
+            _y3 = _y2;
+            _x2 = _x1;
+            _y2 = _y1;
+            _x1 = _x0;
+            _y1 = _y0;
 
-
-    if (pad.A_pressed()) {
-        _direction = right;
-    }
+            _x0 = _x0;
+            _y0 = _y0 + 1;
 
-    if (pad.B_pressed()) {
-        _direction = left;     //check these are orrecrt
-    }
+            ///// alters Snakehead initial value
+            //    Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function
+        }
+        if (_direction == down) {
+            _x3 = _x2;
+            _y3 = _y2;
+            _x2 = _x1;
+            _y2 = _y1;
+            _x1 = _x0;
+            _y1 = _y0;
 
-    if (pad.X_pressed()) {
-        _direction = up;
-    }
-    if (pad.Y_pressed()) {
-        _direction = down;
-    } else {
-        _direction = _direction;
-    }
-    return _direction;
-}
+            _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;
 
 
 
-void Snake::move_snakebody(GameEngine &engine, N5110 &lcd)
-{
-    lcd.refresh();
-    
-    Vector2D *_snakebody = new Vector2D [_length];
-    _snakebody[0].x = S0.x;           //assign _sb0[0] before changing value
-    _snakebody[0].y = S0.y;           //engine.get_oldSnakehead;                                           // _engine.get_oldSnakehead();
-
-
-    while(1) {
-        if (_direction == up) {           //           /-/
-            S0.y++;                           ///// alters Snakehead initial value
-            //    Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function
-        }
-        if (_direction == down) {             // Direction should remain  after button is pressed - should not just change the snakehead coordinate once
-            S0.y--;
-        }
-        if (_direction == left) {
-            S0.x--;
         }
 
         if (_direction == right) {
-            S0.x++;
-        }
-        lcd.setPixel(S0.x, S0.y, 1);   
-                                                            //updates the head position before rendering so when it draws i=1 the 0 position will be new
-        for(int i=1; i<=_length; i++) {                     //0 being head of snake  so snakepos[1]=snakepos[0] which is the head - moving up one place
-            _snakebody[i].x = _snakebody[i-1].x;
-            _snakebody[i].y = _snakebody[i-1].y;             //plotting of snake
-        
-            lcd.setPixel(_snakebody->x, _snakebody->y, 1);  //pointer used - need to point to x and y
-                                                                    //Snake needs to follow itself - low numbers lead the way
+            _x3 = _x2;
+            _y3 = _y2;
+            _x2 = _x1;
+            _y2 = _y1;
+            _x1 = _x0;
+            _y1 = _y0;
 
-            if((_snakebody[i].x == _snakebody[0].x) && (_snakebody[i].x == _snakebody[0].x)) { //  Unsure how to initially define the snake with (3 bits, when score =0) starting position  *without* redeclaring Vector2D *snakebody = new Vector2D [_length];
-                _gameover = true;
-            }
+            _x0 = _x0 + 1;
+            _y0 = _y0;
+
+
         }
 
     }
+
 }
-void Snake::check_wall_collisions()     // Vector2D *&snakebody hopefully this points to values stored in snakebody then can compare to snakehead
+
+//MAKE WALL CLASS // MAIN  MENU DIFFERENT DIFFUCLTIES CHANGING fps
+//DRAW RECTANGLE - SHRINK // CHANE SHAPE AFTER CERTAIN SCORE --- SET UP TIMER
+
+//NOTES - maybe make 2 player - joystick is player 2
+
+
+
+
+
+
+void Snake::get_LEDs(Gamepad &pad)
 {
+    pad.leds_off();
 
-    if (S0.x == 0 || S0.x == 84 || S0.y == 0 || S0.y == 42) {   //how do i access snakehead.headx
+    if (_x0 >= 42 && _y0 >= 24) {
+        // top right led on
+
+        pad.led(4, 1);
+    }
+    // topleft led on
+    if (_x0 <= 42 && _y0 >=24) {
+        // top right led on
+        pad.led(1, 1);
+    }
+    //bottom left
+    if (_x0 <=42 && _y0 <= 24) {
+
+        pad.led(3,1);
+    }
+    //bottom right
+    if (_x0 >= 42 && _y0 <= 24) {
+        // top right led on
+        pad.led(6, 1);
+    }
+}
+
+
+void Snake::get_timer(Timer &timer)
+{
+    _realtime = timer.read();
+    _display_time = (Reset_value - _realtime);
 
+    if(_realtime == Reset_value) {
         _gameover = true;
+    }
+
+    if (0 <= _score && _score < 10) {
+        Reset_value = 12;
+    }
+    if (10 <= _score && _score < 15) {
+        Reset_value = 10;
+    }
+    if (15 <= _score && _score < 20) {
+        Reset_value = 8;
     } else {
-    
-    _gameover = _gameover;
+        Reset_value = 6;
+    }
+
+
+
+}
+
+
+bool Snake::get_gameover()
+{
+    return _gameover;
+}
+int Snake::get_score()
+{
+    return _score;
 }
+
+void Snake::print_scores(N5110 &lcd)
+{
+    int score = _score;
+
+    char buffer1[14];
+    sprintf(buffer1,"%2d",score);
+    lcd.printString(buffer1,WIDTH/2 - 3,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    char buffer2[14];
 }
+
+void Snake::print_display_time(N5110 &lcd)
+{
+        int countdown = _realtime;
+
+    char buffer1[14];
+    sprintf(buffer1,"%2d",countdown);
+    lcd.printString(buffer1,WIDTH/2,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    char buffer2[14];
+}
\ No newline at end of file