Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Engine/Engine.cpp
- Revision:
- 11:b25874e7efe4
- Parent:
- 8:9d01fd4a63ad
- Child:
- 12:d3eef5ea3f43
--- a/Engine/Engine.cpp Sun May 06 22:42:49 2018 +0000 +++ b/Engine/Engine.cpp Mon May 07 00:10:25 2018 +0000 @@ -13,7 +13,7 @@ void Engine::init() // initialise the game parameters { memset(_grid, 0, sizeof(_grid)); - _solid.init(10,4); + _solid.init(5,15); _noodles.init(10,10); } @@ -25,7 +25,7 @@ void Engine::draw(N5110 &lcd) { lcd.drawRect(0,0,48,48,FILL_TRANSPARENT); - + for (int j = 0; j < 22; j++) { for (int i = 0; i < 22; i++) { if (_grid[i][j] != 0) { @@ -33,25 +33,26 @@ } } } - - lcd.printString("Score",48,0); + + lcd.printString("Score",50,0); int _score = (_solid.getLength() - 3); - char buffer1[14]; - sprintf(buffer1,"%2d",_score); - lcd.printString(buffer1,48,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits - + char buffer1[21]; + sprintf(buffer1,"%3d",_score); + lcd.printString(buffer1,50,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits + if (_gameOver) { - for (int i = 0; i <= 22; i++) { - for (int j = 0; j <= 22; j++) { - lcd.drawRect((2 * i) + 1,(2 * j) + 1,2,2,FILL_BLACK); + for (int i = 0; i < 22; i++) { + for (int j = 0; j < 22; j++) { + lcd.drawRect((2 * i) + 3,(2 * j) + 3,2,2,FILL_BLACK); wait(0.01); lcd.refresh(); } } - } + } + } void Engine::update(Gamepad &pad) @@ -62,9 +63,16 @@ int _l =_solid.getLength(); - for (int i = 0; i < _l; i++) { - if (_grid[_solid.getX(i)][_solid.getY(i)] != 1) { - _grid[_solid.getX(i)][_solid.getY(i)] = 1; + for (int z = 0; z < _l; z++) { + + if (_solid.getX(z) > 21 || _solid.getX(z) < 0) { + _gameOver = true; + } else if (_solid.getY(z) > 21 || _solid.getY(z) < 0) { + _gameOver = true; + } + + if (_grid[_solid.getX(z)][_solid.getY(z)] != 1) { + _grid[_solid.getX(z)][_solid.getY(z)] = 1; } else { _gameOver = true; } @@ -72,8 +80,8 @@ _grid[_noodles.getY()][_noodles.getY()] = 2; - if (_grid[_solid.getX(0)][_solid.getY(0)] == 2) { - if (_solid.getLength()<100) { + if (_grid[_solid.getX(0)][_solid.getY(0)] == 2) { //checks if the head has reached food + if (_solid.getLength()<484) { //checks if the snake can grow any more _solid.grow(); //makes snake longer @@ -89,3 +97,7 @@ } } +int Engine::getScore(){ + return _score; + } +