Snake game snake library
Diff: Snake.cpp
- Revision:
- 2:5e04e34a3b66
- Parent:
- 1:93a4cb86f100
- Child:
- 3:b24ef00836c5
diff -r 93a4cb86f100 -r 5e04e34a3b66 Snake.cpp --- a/Snake.cpp Fri May 05 10:36:54 2017 +0000 +++ b/Snake.cpp Fri May 05 11:55:34 2017 +0000 @@ -1,6 +1,11 @@ #include "Snake.h" +#define WIDTH 84 +#define HEIGHT 48 +#define CEILING 8 +#define FLOOR 48 + //Snake snakePart; snakePart snek; @@ -24,6 +29,7 @@ //initalizing the starting variables snek._x[0]=x; snek._y[0]=y; + live=1; _length = lenght; counter=0; for (int i=0;_length>i;i++) @@ -51,8 +57,11 @@ } void Snake::draw(N5110 &lcd){ + checkWallCollision(lcd); + if (live==1){ lcd.clear(); - lcd.drawRect(0,4,84,48-8,FILL_TRANSPARENT); + + lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT); for ( int i=0; _length>i;i++) { if (snek._x!=0) @@ -67,11 +76,16 @@ //draw lines from breakpoint to breakpoint to head //draw food //lcd.setPixel(48,28); + lcd.refresh(); + //update(pad); //printf("drAwn"); } + }//live loop } void Snake::update(Gamepad &pad){ + + if (live==1){ d=pad.get_direction(); startx=snek._x[0]; @@ -157,7 +171,7 @@ if (d==N) { - if (snek._dir[_length]!=4) + if (snek._dir[_length-1]!=4) { snek._dir[_length-1] = 2; @@ -171,7 +185,7 @@ if (d==E) { - if (snek._dir[_length]!=3) + if (snek._dir[_length-1]!=3) { snek._dir[_length-1] = 1; @@ -185,7 +199,7 @@ if (d==W) { - if (snek._dir[_length]!=1) + if (snek._dir[_length-1]!=1) { snek._dir[_length-1] = 3; @@ -199,7 +213,7 @@ if (d==S) { - if (snek._dir[_length]!=2) + if (snek._dir[_length-1]!=2) { snek._dir[_length-1] = 4; @@ -230,7 +244,7 @@ // } //counter=0; printf("updated "); - +}//live loop } snakePart Snake::add_point(int x, int y){ @@ -247,8 +261,28 @@ //_length++; } -void Snake::get_length(){ +void Snake::deadSnake(N5110 &lcd){ + lcd.clear(); + lcd.printString("Game Over",0,1); + lcd.refresh(); +live=0; + + } +void Snake::checkWallCollision(N5110 &lcd){ + + if (snek._x[_length]==WIDTH||snek._x[_length]==0) + { + + deadSnake(lcd); + } + if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR) + { + + deadSnake(lcd); + } + //return _length; - } \ No newline at end of file +} + \ No newline at end of file