ZIYI CHEN ml17z4c 201214999
Dependencies: mbed
Diff: Eng/Eng.cpp
- Revision:
- 11:1812f04382fa
- Parent:
- 9:a8b2086a46e5
--- a/Eng/Eng.cpp Wed May 08 22:20:00 2019 +0000 +++ b/Eng/Eng.cpp Thu May 09 11:04:43 2019 +0000 @@ -10,7 +10,7 @@ } -void Eng::init() +void Eng::init() { dSnake.init(5,10); //she de chu shi zuo biao _noodles.init(20,20); @@ -18,12 +18,7 @@ } -void Eng::loadMap() -{ - memset(snake1, 0, sizeof(snake1)); - -} void Eng::userinput(Gamepad &pad) { @@ -63,8 +58,8 @@ void Eng::update(Gamepad &pad) { - loadMap(); + memset(snake1, 0, sizeof(snake1)); dSnake.update(_d); dead(); @@ -72,9 +67,9 @@ snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] = 2; if (checkFood()) { - - pad.tone(750.0,0.1); - + + pad.tone(1000.0,0.1); + growSnake(); bool empty = false; @@ -84,7 +79,7 @@ _noodles.random(); if ( snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] == 0) { - + empty = true; } } @@ -94,27 +89,31 @@ void Eng::dead() { - int _l =dSnake.getLength(); + int _l =dSnake.getLength(); - for (int z = 0; z < _l; z++) { + for (int i = 0; i < _l; i++) { - if (dSnake.xcoordinate(z) > 44 || dSnake.xcoordinate(z) < 0) { + if (dSnake.xcoordinate(i) > 40) { + _gameOver = true; + } else if( dSnake.xcoordinate(i) < 0) { _gameOver = true; - } else if (dSnake.ycoordinate(z) > 22 || dSnake.ycoordinate(z) < 0) { + } else if (dSnake.ycoordinate(i) > 22 ) { + _gameOver = true; + } else if(dSnake.ycoordinate(i) < 0) { _gameOver = true; } - if (snake1[dSnake.xcoordinate(z)][dSnake.ycoordinate(z)] != 1) { - snake1[dSnake.xcoordinate(z)][dSnake.ycoordinate(z)] = 1; + if (snake1[dSnake.xcoordinate(i)][dSnake.ycoordinate(i)] != 1) { + snake1[dSnake.xcoordinate(i)][dSnake.ycoordinate(i)] = 1; } else { - _gameOver = true; + _gameOver = true; } } } bool Eng::checkFood() { - if (snake1[dSnake.xcoordinate(0)][dSnake.ycoordinate(0)] == 2) { + if (snake1[dSnake.xcoordinate(0)][dSnake.ycoordinate(0)] == 2) { return true; } else { return false; @@ -123,9 +122,9 @@ void Eng::growSnake() { - if (dSnake.getLength()<4032) { + if (dSnake.getLength()<4032) { - dSnake.grow(); + dSnake.grow(); } }