ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18ac

Dependencies:   mbed

Revision:
2:7fa08670b1fc
Child:
3:c61d0c70eda4
diff -r a084ee340d74 -r 7fa08670b1fc Game/Game.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Game/Game.cpp	Tue May 26 16:26:12 2020 +0000
@@ -0,0 +1,201 @@
+#include "Game.h"
+#include "mbed.h"
+#include "Gamepad.h"
+#include "N5110.h"
+#include "Menu.h"
+
+
+Game::Game()
+{
+    x = 4;
+    y = 4;
+    for (int i = 0; i < 16; i++) {
+        fruitX[i] = 3 + i*5;
+    }
+    for (int i = 0; i < 9; i++) {
+        fruitY[i] = 3 + i*5;
+    }
+    score = 0;
+    a = 0;                              //used to select the direction based ont what button is pressed
+    ntail = 0;                          //used to increase lenght of the tail
+    k = 0;
+    rx = rand() % 16;
+    ry = rand() % 9;
+    fruitX1 = fruitX[rx]+1;
+    fruitY1 = fruitY[ry]+1;
+    x_pos.push_back(4);
+    y_pos.push_back(4);
+}
+
+void Game::movement(Gamepad &pad)
+{
+    if (pad.Y_held() && !(a == 2)) {
+        a = 1;
+    }
+    if (pad.A_held() && !(a == 1)) {
+        a = 2;
+    }
+    if (pad.X_held() && !(a == 4)) {
+        a = 3;
+    }
+    if (pad.B_held() && !(a == 3)) {
+        a = 4;
+    }
+    if (a == 1) {
+        x += -5;
+        //wait(0.25);
+    }
+    if (a == 2) {
+        x += 5;
+        //wait(0.25);
+    }
+    if (a == 3) {
+        y += -5;
+        //wait(0.25);
+    }
+    if (a == 4) {
+        y += 5;
+        //wait(0.25);
+    }
+
+    int prevX = x_pos[0];
+    int prevY = y_pos[0];
+
+    x_pos[0] = x;
+    y_pos[0] = y;
+
+    if (!(ntail + 1 == x_pos.size())) {
+        x_pos.push_back(x_pos[ntail - 1]);
+        y_pos.push_back(y_pos[ntail - 1]);
+    }
+
+    if (x_pos.size() > 0) {
+        for (int i = 0; i < x_pos.size() - 1; i++) {
+            x_pos[x_pos.size() - i - 1] = x_pos[x_pos.size() - i - 2];
+            y_pos[y_pos.size() - i - 1] = y_pos[y_pos.size() - i - 2];
+        }
+        x_pos[1] = prevX;
+        y_pos[1] = prevY;
+    }
+}
+
+bool Game::death(N5110 &lcd,Gamepad &pad)
+{
+    if (x < 1 || x > WIDTH-2) {                                 //if you hit the side walls you die
+        while (1) {
+            gameover(lcd, pad);
+            if (pad.B_held()) {
+                return true;
+            }
+            if (pad.start_held()) {
+                return false;
+            }
+        }
+    }
+
+    if (y < 1 || y > HEIGHT) {                                  //if you hit the top or bottom walls you die
+        while (1) {
+            gameover(lcd, pad);
+            if (pad.B_held()) {
+                return true;
+            }
+            if (pad.start_held()) {
+                return false;
+            }
+        }
+    }
+    for (int i = 1; i < x_pos.size(); i++) {                    //if you hit your tail you die
+        if (x_pos[0] == x_pos[i] && y_pos[0] == y_pos[i]) {
+            while (1) {
+                gameover(lcd, pad);
+                if (pad.B_held()) {
+                    return true;
+                }
+                if (pad.start_held()) {
+                    return false;
+                }
+            }
+        }
+    }
+    return false;
+}
+
+void Game::draw(N5110 &lcd,Gamepad &pad)
+{
+    lcd.clear();
+    lcd.drawRect(1,1,WIDTH-2,HEIGHT-1,FILL_TRANSPARENT);    //rectangle around border of field
+    lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK);     //fruit on the field
+    lcd.drawCircle(x,y,2,FILL_TRANSPARENT);                 //initial snake design
+    //printf("size of vector %d\n", x_pos.size());
+    for (k = 1; k <= ntail; k++) {
+        lcd.drawCircle(x_pos[k],y_pos[k],2,FILL_TRANSPARENT);
+        //printf("draw \n");
+    }
+    //printf("/////////////// \n");
+    //printf("Fruit x coordinate: %d\n", fruitX1);
+    //printf("Fruit y coordinate: %d\n", fruitY1);
+    //printf("Snake x: %d\n", x);
+    //printf("Snake y: %d\n", y);
+    //printf("seg1 x: %d\n", x_pos[1]);
+    //printf("seg1 y: %d\n", y_pos[1]);
+    lcd.refresh();
+}
+
+void Game::gameover(N5110 &lcd,Gamepad &pad)
+{
+    lcd.clear();
+    //while (1) {
+    lcd.printString("GAME OVER",WIDTH/2-25,0);
+    sprintf (buffer, "Score: %d",score);
+    lcd.printString(buffer,WIDTH/2-26,2);
+    lcd.printString("TO PLAY AGAIN",3,4);
+    lcd.printString("PRESS B",17,5);
+    pad.leds_on();
+    wait(0.1);
+    pad.leds_off();
+    wait(0.1);
+    pad.tone(500.0,1);
+    lcd.refresh();
+    wait(1/6);
+    // if ( _pad.B_held()) {
+    //break;
+    //}
+    //}
+    /*wait(1/6);
+    _lcd.clear();
+    //_menu.menu_screen();
+    _lcd.refresh();
+    wait(1/6);*/
+}
+
+void Game::point(N5110 &lcd,Gamepad &pad)
+{
+    if (x == fruitX1 && y == fruitY1) {             //if central coordinate of the snake head is equal to the central coordinate of the food:
+        //printf("Snake x: %d\n", x);
+        //printf("Snake y: %d\n", y);
+        //printf("seg1 x: %d\n", x_pos[1]);
+        //printf("seg1 y: %d\n", y_pos[1]);
+        //printf("Fruit x coordinate: %d\n", fruitX1);
+        //printf("Fruit y coordinate: %d\n", fruitY1);
+        score = score + 10;
+        pad.tone(750.0,1);
+        rx = rand() % 16;                           //generates a new random coordinate for the food
+        ry = rand() % 9;
+        //printf("rx: %d\n", rx);
+        //printf("ry: %d\n", ry);
+        fruitX1 = fruitX[rx]+1;
+        fruitY1 = fruitY[ry]+1;
+        for (int i = 0; i < x_pos.size(); i++) {
+            if (fruitX1 == x_pos[i] && fruitY1 == y_pos[i]) {
+                rx = rand() % 16;
+                ry = rand() % 9;
+                fruitX1 = fruitX[rx]+1;
+                fruitY1 = fruitY[ry]+1;
+            }
+        }
+        ntail++;
+        lcd.drawRect(fruitX[rx],fruitY[ry],3,3,FILL_BLACK);
+        //_lcd.refresh();
+        //wait(1/6);
+    }
+}
\ No newline at end of file