ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
Diff: snake.cpp
- Revision:
- 8:997f90c88246
- Parent:
- 7:0ce806455ef1
- Child:
- 9:a69a6a06dddf
--- a/snake.cpp Thu May 28 15:08:11 2020 +0000 +++ b/snake.cpp Thu May 28 18:50:56 2020 +0000 @@ -23,36 +23,35 @@ _y2 = 18; _y3 = 17; - // Vector2D SK0 {_x0, _y0}; - // Vector2D SK1 {_x1, _y1}; - //Vector2D SK2 {_x2, _y2}; - //Vector2D SK3 {_x3, _y3}; - + _apx = 48 + _apy = 25 _gameover = false; _score = 0; _direction = up; // Vector2D *_snakebody = new Vector2D [_length]; } - -void Snake::check_score(N5110 &lcd, Gamepad &pad, Apple &apple, Timer &timer) -{ +Vector2D get_Snakehead() { + Vector2D Snakehead = {_x0, _y0}; + return Snakehead + } + - Vector2D Apos = apple.get_Applepos(lcd); - // printf("apple coordinate =, %d , %d",Apos.x, Apos.y); //need to code clear apple and make sure apple isnt spawning every time - if((_x0 == Apos.x) && (_y0 == Apos.y)) { - - lcd.setPixel(Apos.x, Apos.y, 0); // Plem:: Wanted to make sure that this didnt clear the pixel of the snake as it passes through it - _score++; +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.leds_on(); + pad.led(2, 1); + pad.led(4, 1); wait(0.5); - pad.leds_off(); - - - lcd.refresh(); + pad.led(2, 0); + pad.led(4, 0); } @@ -64,7 +63,10 @@ if (_x0 == 0 ||_x0 == 84 || _y0 == 0 || _y1 == 42) { //how do i access snakehead.headx _gameover = true; - } else { + } + if ((_x0 == _x1 && _y0 == _y1) || (_x0 == _x2 && _y0 == _x2) || (_x0 == _x2 && _y0 == _y2)) { + _gameover = true; //|| (_x0 == _x4 && _y0 == _y4)) + else { _gameover = _gameover; } } @@ -72,23 +74,27 @@ -void Snake::render(N5110 &lcd, Apple &apple) +void Snake::render(N5110 &lcd) { - Vector2D Apos = apple.get_Applepos(lcd); - lcd.setPixel(Apos.x, Apos.y,1); - lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); + //apple + lcd.setPixel(_apx, _apy,1); - lcd.setPixel(_x0, _y0,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.printString(" &d ",0,0, _display_time); + lcd.refresh(); } -void Snake::get_position(Gamepad &pad) + +void Snake::get_direction(Gamepad &pad) { Directions direction = _direction; if(direction != left) { @@ -116,8 +122,8 @@ } else { _direction = direction; } - - +} +void Snake::move_snake() { if (_direction == up) { // /-/ _x3 = _x2; _y3 = _y2; @@ -178,45 +184,43 @@ } -//MAKE WALL CLASS // MAIN MENU DIFFERENT DIFFUCLTIES CHANGING fps -//DRAW RECTANGLE - SHRINK // CHANE SHAPE AFTER CERTAIN SCORE --- SET UP TIMER +void Snake::render_clear_tail(N5110 &lcd) { + lcd.clearPixel(_x3, _y3, 0); + } -//NOTES - maybe make 2 player - joystick is player 2 - - - +bool Snake::get_gameover() +{ + return _gameover; +} +int Snake::get_score() +{ + return _score; +} -void Snake::get_LEDs(Gamepad &pad) -{ - pad.leds_off(); - - if (_x0 >= 42 && _y0 >= 24) { - // top right led on +void Snake::get_Apple_position(N5110 &lcd) +{ - pad.led(4, 1); - } - // topleft led on - if (_x0 <= 42 && _y0 >=24) { - // top right led on - pad.led(1, 1); - } - //bottom left - if (_x0 <=42 && _y0 <= 24) { - - pad.led(3,1); - } - //bottom right - if (_x0 >= 42 && _y0 <= 24) { - // top right led on - pad.led(6, 1); - } + 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 + } -void Snake::get_timer(Timer &timer) + + _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); @@ -235,37 +239,4 @@ } else { Reset_value = 6; } - - - -} - - -bool Snake::get_gameover() -{ - return _gameover; -} -int Snake::get_score() -{ - return _score; -} - -void Snake::print_scores(N5110 &lcd) -{ - int score = _score; - - char buffer1[14]; - sprintf(buffer1,"%2d",score); - lcd.printString(buffer1,WIDTH/2 - 3,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits - char buffer2[14]; -} - -void Snake::print_display_time(N5110 &lcd) -{ - int countdown = _realtime; - - char buffer1[14]; - sprintf(buffer1,"%2d",countdown); - lcd.printString(buffer1,WIDTH/2,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits - char buffer2[14]; } \ No newline at end of file