ZIYI CHEN ml17z4c 201214999

Dependencies:   mbed

Revision:
9:a8b2086a46e5
Child:
11:1812f04382fa
diff -r 52e0506e98b8 -r a8b2086a46e5 Eng/Eng.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Eng/Eng.cpp	Wed May 08 20:48:33 2019 +0000
@@ -0,0 +1,142 @@
+#include "Eng.h"
+
+Eng::Eng()
+{
+
+}
+
+Eng::~Eng()
+{
+
+}
+
+void Eng::init()     
+{
+    dSnake.init(5,10);    //she de chu shi zuo biao
+    _noodles.init(20,20);
+    _gameOver = false;
+
+}
+
+void Eng::loadMap()
+{
+
+    memset(snake1, 0, sizeof(snake1));
+
+}
+
+void Eng::userinput(Gamepad &pad)
+{
+    _d = pad.get_direction();
+}
+
+void Eng::draw(N5110 &lcd)
+{
+    lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);
+//
+    for (int i = 0; i < 84; i++) {
+        for (int j = 0; j < 48; j++) {
+            if (snake1[j][i] != 0) {
+                lcd.drawRect((2 * j) + 2,(2 * i) + 2,2,2,FILL_BLACK);
+            }
+        }
+    }
+}
+
+void Eng::score(N5110 &lcd)
+{
+
+    lcd.clear();
+    lcd.printString("Score ",0,0);
+    char buffer[14];
+    int _score = (dSnake.getLength() - 5);
+
+    sprintf(buffer,"%3d",_score);
+    lcd.printString(buffer,0,1);
+
+
+
+    lcd.refresh();
+
+    wait(5.0);
+}
+
+void Eng::update(Gamepad &pad)
+{
+    loadMap();
+
+    dSnake.update(_d);
+
+    dead();
+
+    snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] = 2;
+
+    if (checkFood()) {
+
+        pad.tone(750.0,0.1);
+
+        growSnake();
+
+        bool empty = false;
+
+        while (!empty) {
+
+            _noodles.random();
+
+            if ( snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] == 0) {
+                
+                empty = true;
+            }
+        }
+    }
+}
+
+void Eng::dead()
+{
+
+    int _l =dSnake.getLength();                               
+
+    for (int z = 0; z < _l; z++) {                             
+
+        if (dSnake.xcoordinate(z) > 44 || dSnake.xcoordinate(z) < 0) {        
+            _gameOver = true;
+        } else if (dSnake.ycoordinate(z) > 22 || dSnake.ycoordinate(z) < 0) { 
+            _gameOver = true;
+        }
+
+        if (snake1[dSnake.xcoordinate(z)][dSnake.ycoordinate(z)] != 1) {    
+            snake1[dSnake.xcoordinate(z)][dSnake.ycoordinate(z)] = 1;        
+        } else {
+            _gameOver = true;                                   
+        }
+    }
+}
+
+bool Eng::checkFood()
+{
+    if (snake1[dSnake.xcoordinate(0)][dSnake.ycoordinate(0)] == 2) { 
+        return true;
+    } else {
+        return false;
+    }
+}
+
+void Eng::growSnake()
+{
+    if (dSnake.getLength()<4032) { 
+
+        dSnake.grow();  
+
+    }
+}
+
+float Eng::getScore()
+{
+    return _score;
+}
+
+
+bool Eng::getGameOver()
+{
+    return _gameOver;
+}