Joe Shotton / Mbed 2 deprecated ELEC2645_Project_ll16j23s

Dependencies:   mbed ll16j23s_test_docs

Revision:
9:0571880085cc
Parent:
8:bcc3403d7e79
Child:
10:a2d643b3c782
diff -r bcc3403d7e79 -r 0571880085cc SnakeEngine/SnakeEngine.cpp
--- a/SnakeEngine/SnakeEngine.cpp	Mon May 25 14:45:32 2020 +0000
+++ b/SnakeEngine/SnakeEngine.cpp	Mon May 25 20:31:52 2020 +0000
@@ -3,10 +3,12 @@
 SnakeEngine::SnakeEngine() {
     //constructor
     score = 0;
-    _menu_select = 1;
-    _map = 1;
+    _menu_select = 0;
+    _map_select = 0;
     _game_state = 1;
     _pot2 = 0.5;
+    _death = false;
+
 }
 
 SnakeEngine::~SnakeEngine()
@@ -15,51 +17,48 @@
 }
  
 void SnakeEngine::game_init(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag){
+    transition_black(lcd);
+    transition_white(lcd);
+    map_run(lcd);
     _food.init(mag);
     _body.init();
     _food.rand_pos(pad, lcd);
-    transition_black(lcd);
-    transition_white(lcd);
+    menu_flash(pad, 3);
 } 
 
 
 void SnakeEngine::game_run(Gamepad &pad, N5110 &lcd){
     //input???
     map_run(lcd);
-    move_body(pad, lcd);
+    _body.run(pad, lcd, _death);
+    _food.run(lcd);
     snake_food_collision(pad, lcd, _body._length);  
-    if (_body.snake_snake_collision(pad) == true) {
-        //death_init(lcd);
-        //_game_state = 4;
+    if (_death == true) {
+        printf("Dead!");
+        _game_state = 4;   
+        death_init(pad, lcd);
     }
     //snake-wall collision 
 } 
 
-
-void SnakeEngine::move_body(Gamepad &pad, N5110 &lcd){
-    _body.snake_movement(pad);
-    _body.draw_body(lcd);
-    //printf("Moving!");
-    _food.do_food(lcd);
-    snake_food_collision(pad, lcd, _body._length);
-} 
-
 void SnakeEngine::snake_food_collision(Gamepad &pad, N5110 &lcd, int &_length) {
     if (_food._x == _body._x_head && _food._y == _body._y_head){
         //printf("FOOD!");
-    while (_food.rand_pos(pad, lcd) == false){
-        _food.rand_pos(pad, lcd);
-        printf("Reselected food position\n");
-    }
-    pad.led(3,0.9);
-    _length += 5;
-    score++;
+        pad.led(3,0.9);
+        pad.led(6,0.9);
+        while (_food.rand_pos(pad, lcd) == false){
+            _food.rand_pos(pad, lcd);
+            printf("Reselected food position\n");
+        }
+        _body.add_length(5);
+        score++;
+        pad.led(3,0.0);
+        pad.led(6,0.0);
     }
 }
 
 void SnakeEngine::map_run(N5110 &lcd) {
-    
-    switch(_map) {
+    switch(_map_select+1) {
         case 2:
             map2_draw(lcd);
             snake_map2_collision();    
@@ -75,21 +74,23 @@
     }
 }
 
-bool SnakeEngine::snake_map2_collision() {
-    if (_body._x_head == 0 || _body._x_head == 82 || _body._y_head == 0 || _body._y_head == 46){ //if snakehead coords exceed the ring coords
-        return true;    
+void SnakeEngine::snake_map2_collision() {
+    if (_body._x_head < 2 || _body._x_head > 81 || _body._y_head < 2 || _body._y_head > 45){ //if snakehead coords exceed the ring coords
+        _death = true; 
     }
 }
 
-bool SnakeEngine::snake_map3_collision() {
-    /*
-    if (_body._x_head == 0 || _body._x_head == 82 || _body._y_head == 0 || _body._y_head == 46){
-        return true;
+void SnakeEngine::snake_map3_collision() {
+    if ((_body._x_head == 40 || _body._x_head == 42) && (_body._y_head < 16 || _body._y_head > 30)){ 
+    //if head is at the either of the N/S walls' x coords, and above (y=18) or below (y=30), trigger death 
+        _death = true; 
+    } else if ((_body._x_head < 16 || _body._x_head > 66) && (_body._y_head == 22 || _body._y_head == 24)){
+    //if head is at west of x=18 or east of x=66, and at either of the W/E wall's y coords, trigger death 
+        _death = true; 
     }
-    */
 }
 
-bool SnakeEngine::snake_map4_collision() {
+void SnakeEngine::snake_map4_collision() {
     /*
     for(int i = 0; i < 48; i++){
         if(_body._x_head == MAP 4 COORDS && _body._y_head == MAP 4 COORDS) {
@@ -116,17 +117,10 @@
         lcd.drawRect(68, 8, 4, 4,FILL_BLACK); //NE square
         lcd.drawRect(68, 36, 4, 4,FILL_BLACK);//SE square
         
-        lcd.drawRect(40, 6, 4, 8,FILL_BLACK); //N spot, vertical
-        lcd.drawRect(38, 8, 8, 4,FILL_BLACK); //N spot, horizontal
-        
-        lcd.drawRect(26, 20, 4, 8,FILL_BLACK);//E spot, vertical
-        lcd.drawRect(24, 22, 8, 4,FILL_BLACK);//E spot, horizontal
-        
-        lcd.drawRect(40, 34, 4, 8,FILL_BLACK);//S spot, vertical
-        lcd.drawRect(38, 36, 8, 4,FILL_BLACK);//S spot, horizontal
-        
-        lcd.drawRect(54, 20, 4, 8,FILL_BLACK);//W spot, vertical
-        lcd.drawRect(52, 22, 8, 4,FILL_BLACK);//W spot, horizontal
+        lcd.drawRect(38, 6, 8, 8,FILL_BLACK); //N large square
+        lcd.drawRect(24, 20, 8, 8,FILL_BLACK);//E large square
+        lcd.drawRect(38, 34, 8, 8,FILL_BLACK);//S large square
+        lcd.drawRect(52, 20, 8, 8,FILL_BLACK);//W large square
 }
 
 
@@ -170,7 +164,8 @@
     //wait_ms(500);      
 }
 
-void SnakeEngine::menu1_init(N5110 &lcd){
+void SnakeEngine::menu1_init(Gamepad &pad, N5110 &lcd){
+         contrast(pad, lcd);
          transition_black(lcd);
          transition_white(lcd);
          lcd.clear();
@@ -182,32 +177,32 @@
          lcd.printString("Maps",30,3);
          lcd.drawCircle(24,27,3,FILL_TRANSPARENT);
          lcd.refresh();   
+         menu_flash(pad, 2);
 }
 
-void SnakeEngine::menu1_select(N5110 &lcd, Gamepad &pad){    
+void SnakeEngine::menu1_select(N5110 &lcd, Gamepad &pad, FXOS8700CQ &mag){    
     //printf("Menu 1\n");
     if (pad.X_pressed() == true){ //detect if 'up' selection
         _menu_select--;
     } else if (pad.B_pressed() == true){ //detect if 'down' selection
         _menu_select++;
     }
-    //printf("Option: %d\n", _menu_select);
-    if (pad.A_pressed() == true && _menu_select == 1){ //if option 1 selected and 'A' pressed
-        //game_init();      //Initialise game
-        _game_state = 3;    //switch state
-    } else if (pad.A_pressed() == true && _menu_select == 2){ //if option 2 selected and 'A' pressed
-        menu2_init(lcd);    //Initialise map menu 
-        _game_state = 2;    //switch state
+    _menu_select = ((_menu_select % 2) + 2) % 2; //wrap around numbers, ie down on 2 goes to 1 and up on 1 goes to 2
+    select_circles(lcd, _menu_select + 1); //draw black circle in selected option
+    //printf("Option: %d\n", _menu_select + 1);
+    
+    if (pad.A_pressed() == true){ //if option 1 selected and 'A' pressed
+        if (_menu_select == 0){
+            game_init(pad, lcd, mag);      //Initialise game
+            _game_state = 3;    //switch state
+        } else {
+            menu2_init(pad, lcd);    //Initialise menu 2
+            _game_state = 2;    //switch state
+        }
     }
-    if (_menu_select > 2) { //bottom - top wrap around
-        _menu_select = 1;
-    } else if (_menu_select < 1) { ////top - bottom wrap around
-        _menu_select = 2;
-    }
-    select_circles(lcd, _menu_select); //draw black circle in selected option
 }
 
-void SnakeEngine::menu2_init(N5110 &lcd){
+void SnakeEngine::menu2_init(Gamepad &pad, N5110 &lcd){
          //transition_black(lcd);
          //transition_white(lcd);
          //printf("Menu 1\n");
@@ -222,56 +217,69 @@
          lcd.printString("Spots",30,5);
          lcd.drawCircle(24,43,3,FILL_TRANSPARENT);
          lcd.refresh();   
+         menu_flash(pad, 2);
 }
 
 void SnakeEngine::menu2_select(N5110 &lcd, Gamepad &pad){   
     
     //printf("Menu 2\n");
     if (pad.X_pressed() == true){
-        _map--;
+        _map_select--;
     } else if (pad.B_pressed() == true){
-        _map++;
+        _map_select++;
     } else if (pad.Y_pressed() == true){
-        preview(lcd, _map);
+        preview(pad, lcd, _map_select+1);
     } else if (pad.A_pressed() == true){
         //game_init();
         _game_state = 3;
     }
     
-    //printf("Map: %d\n", _map);
-    if (_map > 4) {
-        _map = 1;
-    } else if (_map < 1) {
-        _map = 4;
-    }
-    select_circles(lcd, _map);
+    //printf("Map: %d\n", _map_select);
+    _map_select = ((_map_select % 4) + 4) % 4;
+    select_circles(lcd, _map_select+1);
 }
 
 
-void SnakeEngine::death_init(N5110 &lcd){
-         wait_ms(500);
+void SnakeEngine::death_init(Gamepad &pad, N5110 &lcd){
+         _death = 0;
+         _menu_select = 0;
          transition_black(lcd);
          transition_white(lcd);
          //printf("Game over\n");
-         lcd.printString("  GAME  OVER  ",0,0);
-         lcd.printString("   Score =",3,1);
+         lcd.printString("  GAME OVER",3,0);
+         lcd.printString("    Score:",0,1);
          char buffer[14];
-         if (score < 10){
-            sprintf(buffer,"      0%2d",score);
-         } else {
-            sprintf(buffer,"       %2d",score);   
-         }
+         sprintf(buffer,"      %2d",score);   
          lcd.printString(buffer,0,2);
-         lcd.printString("Again!",24,4);
+         lcd.printString("Again!",30,4);
          lcd.drawCircle(24,35,3,FILL_TRANSPARENT);
-         lcd.printString("Menu",24,5);
-         lcd.drawCircle(24,44,3,FILL_TRANSPARENT);
-         lcd.refresh();   
+         lcd.printString("Maps",30,5);
+         lcd.drawCircle(24,43,3,FILL_TRANSPARENT);
+         lcd.refresh();
+         menu_flash(pad, 1);   
 } 
 
-void SnakeEngine::death_select(N5110 &lcd, Gamepad &pad){
+void SnakeEngine::death_select(N5110 &lcd, Gamepad &pad, FXOS8700CQ &mag){
+    //printf("Menu 1\n");
+    if (pad.X_pressed() == true){ //detect if 'up' selection
+        _menu_select--;
+    } else if (pad.B_pressed() == true){ //detect if 'down' selection
+        _menu_select++;
+    }
+    _menu_select = ((_menu_select % 2) + 2) % 2; //wrap around numbers, ie down on 2 goes to 1 and up on 1 goes to 2
+    select_circles(lcd, _menu_select + 3); //draw black circle in selected option
+    //printf("Option: %d\n", _menu_select + 1);
     
-} 
+    if (pad.A_pressed() == true){ //if option 1 selected and 'A' pressed
+        if (_menu_select == 0){
+            game_init(pad, lcd, mag); //Initialise game
+            _game_state = 3;    //switch state
+        } else {
+            menu2_init(pad, lcd);    //Initialise menu 2
+            _game_state = 2;    //switch state
+        }
+    }
+}  
 
 void SnakeEngine::select_circles(N5110 &lcd, int line) {
     for(int i = 19; i < 52; i +=8) {
@@ -288,9 +296,9 @@
     //wait_ms(200);
 }
 
-void SnakeEngine::preview(N5110 &lcd, int _map){
+void SnakeEngine::preview(Gamepad &pad, N5110 &lcd, int _map_select){
     lcd.clear(); 
-    switch(_map) {
+    switch(_map_select) {
         case 1:
             lcd.clear(); 
             lcd.printString("(Empty)",21,2);
@@ -311,11 +319,19 @@
     lcd.refresh(); 
     wait_ms(1000);
     lcd.clear();
-    menu2_init(lcd);
+    menu2_init(pad, lcd);
 }
 
 void SnakeEngine::contrast(Gamepad &pad, N5110 &lcd){
     _pot2 = pad.read_pot2();
     lcd.setContrast(0.25 + _pot2 * 0.5); 
     //printf("Contrast: %f\n", 0.25 + _pot2 * 0.5);  
+}
+
+void SnakeEngine::menu_flash(Gamepad &pad, int led){
+    for(int i = 1; i < 7; i++){
+        pad.led(led, i % 2);  
+        pad.led(led + 3, i % 2);    
+        wait_ms(100);
+    }   
 }
\ No newline at end of file