ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp
- Committer:
- el17oc
- Date:
- 2020-05-28
- Revision:
- 10:ee781d18e0f6
- Parent:
- 9:a69a6a06dddf
- Child:
- 11:e7c56013acd9
- Child:
- 12:60c856354406
File content as of revision 10:ee781d18e0f6:
#include "snake.h" Snake::Snake() { } Snake::~Snake() { } void Snake::init() { _x0 = 48; _x1 = 48; _x2 = 48; _x3 = 48; _y0 = 20; _y1 = 19; _y2 = 18; _y3 = 17; _apx = 48; _apy = 25; _gameover = false; _score = 0; _direction = up; // 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, Timer &timer) { //need to code clear apple and make sure apple isnt spawning every time if((_x0 == _apx) && (_y0 == _apy)) { _score++; //causes new apple position to be generated timer.reset(); pad.tone(1500.0,0.5); pad.led(2, 1); pad.led(4, 1); wait(0.5); pad.led(2, 0); pad.led(4, 0); } } void Snake::check_collisions() //code where it hits itself { if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) { //how do i access snakehead.headx _gameover = true; } if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2)) { _gameover = true; } //|| (_x0 == _x4 && _y0 == _y4)) else { _gameover = _gameover; } } void Snake::render(N5110 &lcd) { //apple lcd.setPixel(_apx, _apy,1); //apple lcd.drawRect(0, 0, 84, 42, 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.refresh(); } void Snake::get_direction(Gamepad &pad) { Directions direction = _direction; if(direction != left) { if (pad.A_pressed()) { _direction = right; } } if(direction != right) { if (pad.Y_pressed()) { _direction = left; } //check these are orrecrt } if(direction != down) { if (pad.X_pressed()) { _direction = up; } } if(direction != up) { if (pad.B_pressed()) { _direction = down; } } else { _direction = direction; } } void Snake::move_snake() { if (_direction == up) { // /-/ _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0; _y0 = _y0 + 1; } if (_direction == down) { _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0; _y0 = _y0 - 1; } if (_direction == left) { _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0 - 1; _y0 = _y0; } if (_direction == right) { _x3 = _x2; _y3 = _y2; _x2 = _x1; _y2 = _y1; _x1 = _x0; _y1 = _y0; _x0 = _x0 + 1; _y0 = _y0; } } void Snake::render_clear_tail(N5110 &lcd) { _oldscore = _score; lcd.clearPixel(_x3, _y3); } bool Snake::get_gameover() { return _gameover; } int Snake::get_score() { return _score; } int Snake::get_oldscore() { return _oldscore; } void Snake::get_Apple_position(N5110 &lcd) { int appleposx; int appleposy; if(_oldscore != _score) { appleposx = rand()%84; appleposy = rand()%48; _apx = appleposx; _apy = appleposy; } else { // if(lcd.getPixel(appleposx, appleposy)==1) { // this pixel is set -- 'if it is already filled on lcd, pick new position' _apx = _apx; //i and j are fed into applepos.x/y -- those values are then fed into set_applepos which assigngs that value to _appleposx/y which then is fed into get_applepos where it is stored and returned _apy = _apy; //alters value of private variable - this can then be accessed by get_Applepos } } void Snake::get_time(Timer &timer) { _realtime = timer.read(); _display_time = (Reset_value - _realtime); if(_realtime == Reset_value) { _gameover = true; } 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; } } 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]; }