ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
Diff: snake.cpp
- Revision:
- 1:897160a1a3ae
- Child:
- 2:44e4a6ecdbef
diff -r b7f1f47bb26a -r 897160a1a3ae snake.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/snake.cpp Tue May 26 12:17:59 2020 +0000 @@ -0,0 +1,157 @@ +#include "snake.h" + +Snake::Snake() +{ + +} + +Snake::~Snake() +{ + +} + + +void Snake::init () +{ + + //WIDTH =84 HEIGHT = 42 //snakebody[0] is initialised may have to initilaise the other 2 og snake parts + _length = 1; + _gameover = false; + _score = 0; + _direction = up; + +} + + + +int Snake::set_direction(Gamepad &pad) //int type as Directions is an enum +{ + + Directions _direction; + + + if (pad.A_pressed()) { + + _direction = right; + + + } + + if (pad.B_pressed()) { + + _direction = left; //check these are orrecrt + + } + + if (pad.X_pressed()) { + + _direction = up; + + } + + if (pad.Y_pressed()) { + + + _direction = down; + + } + + else { + + _direction = _direction; + } + return _direction; +} + + +void Snake::move_and_draw_snake(N5110 &lcd) +{ + Vector2D Snakehead = _engine.get_Snakehead(); //initialises Snakehead value + Vector2D *snakebody = new Vector2D [_length]; + + snakebody[0].x = Snakehead.x; + snakebody[0].y = Snakehead.y; + + while(1) { + if (_direction == up) { + Snakehead.y++; // alters Snakehead initial value + } + if (_direction == down) { //this part is needed to keep the snake running continually + Snakehead.y--; + } + if (_direction == left) { + Snakehead.x--; + } + if (_direction == right) { + Snakehead.x++; //updates the head position before rendering so when it draws i=1 the 0 position will be new + } + + for(int i=1; i<=_length; i++) { //0 being head of snake so snakepos[1]=snakepos[0] which is the head - moving up one place + snakebody[i].x = snakebody[i-1].x; + snakebody[i].y = snakebody[i-1].y; + lcd.setPixel(snakebody[i].x, snakebody[i].y, 1); + if(snakebody[i].x == Snakehead.x && snakebody[i].y == Snakehead.y) { //is snakebody[0] being plotted + _gameover = true; + + } + } + } + + +} + + +void Snake::gameover_true(N5110 &lcd) //taking action if crash has occured +{ + if (_gameover == true) { + + lcd.clear(); + lcd.refresh(); + lcd.printString( " Game Over L ", 0, 2 ); + lcd.printString ("score: _score ",15, 15); //Need to add button to return to main screen / restart + + } + + else { + + + } +} + + + +void Snake::check_if_scored(N5110 &lcd, Gamepad &pad) +{ + Vector2D Snakehead = _engine.get_Snakehead(); + Vector2D Applepos = _engine.get_Applepos(); //need to code clear apple and make sure apple isnt spawning every time + + if(Snakehead.x == Applepos.x && Snakehead.y == Applepos.y) { + + + _score++; + _length = _length++; + _engine.set_Applepos(lcd); //randomises new values for _apx,_apy and draws on lcd -- the position is not needed until we compare it to Snakehead + pad.tone(1500.0,0.5); //need to clear apple + pad.leds_on(); + wait(0.5); + pad.leds_off(); + + } +} + +void Snake::check_wall_collisions() // Vector2D *&snakebody hopefully this points to values stored in snakebody then can compare to snakehead +{ + Vector2D Snakehead = _engine.get_Snakehead(); + + + if (Snakehead.x == 0 || Snakehead.x == 84 || Snakehead.y == 0 || Snakehead.y == 42) { //how do i access snakehead.headx + + _gameover = true; + } else { + _gameover = false; + + } +} + + +