ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
snake.cpp
- Committer:
- el17oc
- Date:
- 2020-05-28
- Revision:
- 8:997f90c88246
- Parent:
- 7:0ce806455ef1
- Child:
- 9:a69a6a06dddf
File content as of revision 8:997f90c88246:
#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 get_Snakehead() { Vector2D Snakehead = {_x0, _y0}; return Snakehead } void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Apple &apple, 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); 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.B_pressed()) { _direction = left; } //check these are orrecrt } if(direction != down) { if (pad.X_pressed()) { _direction = up; } } if(direction != up) { if (pad.Y_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; ///// alters Snakehead initial value // Does this code look okay ? returning _direction in previous function and accessing it in this one - _direction should be member function } 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) { lcd.clearPixel(_x3, _y3, 0); } bool Snake::get_gameover() { return _gameover; } int Snake::get_score() { return _score; } void Snake::get_Apple_position(N5110 &lcd) { if(_score++) { int appleposx = rand()%84; int appleposy = rand()%48; //ROB apparently rand() is seeded so will generate same position each time - do you know any other random methods to put here? } if(lcd.getPixel(appleposx, appleposy)==1) { // this pixel is set -- 'if it is already filled on lcd, pick new position' appleposx = rand()%84; appleposy = rand()%48; // making sure the apple doesnt spawn inside the snakes body or wall which would increase the score } _apx = appleposx; //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 = appleposy; //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; } }