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.
Engine/Engine.cpp
- Committer:
- Andrew_M
- Date:
- 2018-05-06
- Revision:
- 8:9d01fd4a63ad
- Parent:
- 7:c1e0593bfc99
- Child:
- 11:b25874e7efe4
File content as of revision 8:9d01fd4a63ad:
#include "Engine.h" Engine::Engine() { } Engine::~Engine() { } void Engine::init() // initialise the game parameters { memset(_grid, 0, sizeof(_grid)); _solid.init(10,4); _noodles.init(10,10); } void Engine::read_input(Gamepad &pad) { _d = pad.get_direction(); } 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) { lcd.drawRect((2 * i) + 2,(2 * j) + 2,2,2,FILL_BLACK); } } } lcd.printString("Score",48,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 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); wait(0.01); lcd.refresh(); } } } } void Engine::update(Gamepad &pad) { memset(_grid, 0, sizeof(_grid)); _solid.update(_d); 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; } else { _gameOver = true; } } _grid[_noodles.getY()][_noodles.getY()] = 2; if (_grid[_solid.getX(0)][_solid.getY(0)] == 2) { if (_solid.getLength()<100) { _solid.grow(); //makes snake longer bool empty = false; //makes a varible for finding an empty square while (!empty) { //loops until empty square found _noodles.random(); //randomise position of noodles if ( _grid[_noodles.getY()][_noodles.getY()] == 0) { empty = true; //stops looping when free space is found // no need to set the grid as it will put the noodles in the new location the next time 'update' is called } } } } }