ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
Diff: snake.cpp
- Revision:
- 9:a69a6a06dddf
- Parent:
- 8:997f90c88246
- Child:
- 10:ee781d18e0f6
--- a/snake.cpp Thu May 28 18:50:56 2020 +0000 +++ b/snake.cpp Thu May 28 19:16:52 2020 +0000 @@ -23,30 +23,37 @@ _y2 = 18; _y3 = 17; - _apx = 48 - _apy = 25 + _apx = 48; + _apy = 25; _gameover = false; _score = 0; _direction = up; // Vector2D *_snakebody = new Vector2D [_length]; } -Vector2D get_Snakehead() { - Vector2D Snakehead = {_x0, _y0}; - return Snakehead - } - +Vector2D Snake::get_Snakehead() +{ + Vector2D Snakehead; + Snakehead.x = _x0; + Snakehead.y = _y0; + + return Snakehead; + +} + -void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Apple &apple, Timer &timer) + + +void Snake::apple_collected(N5110 &lcd, Gamepad &pad, Timer &timer) { - - //need to code clear apple and make sure apple isnt spawning every time + + //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.tone(1500.0,0.5); pad.led(2, 1); pad.led(4, 1); wait(0.5); @@ -65,8 +72,9 @@ _gameover = true; } if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2)) { - _gameover = true; //|| (_x0 == _x4 && _y0 == _y4)) - else { + _gameover = true; + } //|| (_x0 == _x4 && _y0 == _y4)) + else { _gameover = _gameover; } } @@ -77,17 +85,17 @@ void Snake::render(N5110 &lcd) { - //apple + //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(); @@ -119,11 +127,15 @@ if (pad.Y_pressed()) { _direction = down; - } else { + } + } + else { _direction = direction; } -} -void Snake::move_snake() { + } + + + void Snake::move_snake() { if (_direction == up) { // /-/ _x3 = _x2; _y3 = _y2; @@ -134,9 +146,6 @@ _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; @@ -182,11 +191,12 @@ } -} + -void Snake::render_clear_tail(N5110 &lcd) { - lcd.clearPixel(_x3, _y3, 0); - } +void Snake::render_clear_tail(N5110 &lcd) +{ + lcd.clearPixel(_x3, _y3); +} bool Snake::get_gameover() { @@ -200,27 +210,28 @@ void Snake::get_Apple_position(N5110 &lcd) -{ - +{ +int appleposx; +int appleposy; 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? -} + appleposx = rand()%84; + 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 + _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);