ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp
- Committer:
- el17oc
- Date:
- 2020-05-29
- Revision:
- 13:b37dde18bfdc
- Parent:
- 12:60c856354406
- Child:
- 14:7fb3c93343b6
File content as of revision 13:b37dde18bfdc:
#include "snake.h" Snake::Snake() { } Snake::~Snake() { } void Snake::init() { _x0 = 48; _x1 = 48; _x2 = 48; _x3 = 48; _x4 = 48; _x5 = 48; _y0 = 20; _y1 = 19; _y2 = 18; _y3 = 17; _y4 = 16; _y5 = 15; _apx = 48; _apy = 25; _gameover = false; _reset_apple = false; _score = 0; _direction = up; _countdown = 30; // Vector2D *_snakebody = new Vector2D [_length]; } Vector2D Snake::get_Snakehead() { Vector2D Snakehead; Snakehead.x = _x0; Snakehead.y = _y0; return Snakehead; } void Snake::apple_collected(N5110 &lcd, Gamepad &pad) { //need to code clear apple and make sure apple isnt spawning every time if((_x0 == _apx) && (_y0 == _apy)) { // _countdown = _reset_value; _score++; _reset_apple = true; _countdown = 30; //causes new apple position to be generated pad.tone(1500.0,0.5); pad.led(2, 1); pad.led(4, 1); //pad.led(2, 0); //pad.led(4, 0); } else { _countdown = _countdown - 1; printf(" countdown = %d score %d ", _countdown ,_score); } } int Snake::get_countdown() { return _countdown; } void Snake::check_gameover(N5110 &lcd) //code where it hits itself { if (_x0 == 0 ||_x0 == 84 || _y0 == 32 || _y0 == 0) { //how do i access snakehead.headx _gameover = true; } if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2) || (_x0 == _x3 && _y0 == _y3) || (_x0 == _x4 && _y0 == _y4)|| (_x0 == _x5 && _y0 == _y5)) { _gameover = true; } if(_countdown == 0) { _gameover = true; //|| (_x0 == _x4 && _y0 == _y4)) } } void Snake::gameover_true(N5110 &lcd) { while (_gameover == true) { lcd.printString( " Game Over L ", 0, 2 ); char buffer1[14]; sprintf(buffer1,"%2d",_score); lcd.printString(buffer1,0 ,48); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits if (_score <= 7) { lcd.printString(" Loser ", 0,10); } if (7< _score <= 25) { lcd.printString(" Good Job! ", 0,10); } else { lcd.printString(" SNAKE PRO ", 0,10); lcd.printString(" ~.~.~.~.<8>~ ", 0, 5); } lcd.refresh(); } } void Snake::render(N5110 &lcd) { //apple lcd.setPixel(_apx, _apy,1); //apple lcd.drawRect(0, 0, 84, 40, FILL_TRANSPARENT); //game lcd.setPixel(_x0, _y0,1); //snake lcd.setPixel(_x1, _y1,1); lcd.setPixel(_x2, _y2,1); lcd.setPixel(_x3, _y3,1); lcd.setPixel(_x4, _y4,1); lcd.setPixel(_x5, _y5,1); } void Snake::get_direction(Gamepad &pad) { // int x; Directions direction = _direction; if(direction != left) { if (pad.A_pressed()) { _direction = right; // x=1; } } if(direction != right) { if (pad.Y_pressed()) { _direction = left; // x=2; } //check these are orrecrt } if(direction != down) { if (pad.B_pressed()) { // x=3; _direction = up; } } if(direction != up) { if (pad.X_pressed()) { _direction = down; // x=4; } } else { _direction = _direction; } // printf("direction %d ", x); } void Snake::move_snake() { if (_direction == up) { _x5 = _x4; _x4 = _x3; _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0; _y0 = _y0 + 1; } if (_direction == down) { _x5 = _x4; _x4 = _x3; _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0; _y0 = _y0 - 1; } if (_direction == left) { _x5 = _x4; _y5 = _y4; _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0 - 1; _y0 = _y0; } if (_direction == right) { _x5 = _x4; _y5 = _y4; _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0 + 1; _y0 = _y0; } else _direction = _direction; } void Snake::render_clear_tail(N5110 &lcd) { lcd.setPixel(_x5, _y5, false); } bool Snake::get_gameover() { return _gameover; } int Snake::get_score() { return _score; } int Snake::get_oldscore() { return _oldscore; } int Snake::get_reset_value() { return _reset_value; } void Snake::get_Apple_position(N5110 &lcd) { if(_reset_apple == true) { lcd.setPixel(_apx, _apy,0); _apx = rand()%82+1; _apy = rand()%30+1; _reset_apple = false; } else { _apx = _apx; _apy = _apy; } } /* void Snake::get_time(Timer &timer) { ticker.attach(&flip, 2.0); _realtime = timer.read(); _display_time = (Reset_value - _realtime); int x = _realtime; int y = _display_time; printf("12-T = %d t = %d ", y, x); */ int Snake::set_reset_value() { if (0 <= _score && _score < 10) { _reset_value = 12; } if (10 <= _score && _score < 15) { _reset_value = 10; } if (15 <= _score && _score < 20) { _reset_value = 8; } else { _reset_value = 6; } return _reset_value; } //void Snake::print_display_time(N5110 &lcd) //{ // int countdown = _realtime; // char buffer1[14]; //sprintf(buffer1,"%2d",countdown); //lcd.printString(buffer1,WIDTH/2,30); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits //char buffer2[14]; //}