Snake game snake library

Committer:
Nefos
Date:
Fri May 05 15:20:17 2017 +0000
Revision:
7:a2f426a37e60
Parent:
6:cc8d2088f490
Child:
8:721a8dca7a25
Final code for releasing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nefos 0:2eb4d7e86e2f 1 #include "Snake.h"
Nefos 0:2eb4d7e86e2f 2
Nefos 0:2eb4d7e86e2f 3
Nefos 2:5e04e34a3b66 4 #define WIDTH 84
Nefos 2:5e04e34a3b66 5 #define HEIGHT 48
Nefos 2:5e04e34a3b66 6 #define CEILING 8
Nefos 2:5e04e34a3b66 7 #define FLOOR 48
Nefos 2:5e04e34a3b66 8
Nefos 0:2eb4d7e86e2f 9 snakePart snek;
Nefos 0:2eb4d7e86e2f 10
Nefos 1:93a4cb86f100 11
Nefos 1:93a4cb86f100 12
Nefos 0:2eb4d7e86e2f 13 Snake::Snake()
Nefos 0:2eb4d7e86e2f 14 {
Nefos 0:2eb4d7e86e2f 15
Nefos 0:2eb4d7e86e2f 16 }
Nefos 0:2eb4d7e86e2f 17 Snake::~Snake()
Nefos 0:2eb4d7e86e2f 18 {
Nefos 0:2eb4d7e86e2f 19
Nefos 0:2eb4d7e86e2f 20 }
Nefos 0:2eb4d7e86e2f 21
Nefos 6:cc8d2088f490 22 /************************Functions************************/
Nefos 6:cc8d2088f490 23
Nefos 7:a2f426a37e60 24 void Snake::init(int x, int y, int lenght, int _live){//initalizing the starting variables
Nefos 7:a2f426a37e60 25
Nefos 6:cc8d2088f490 26 startx=x;//saving these variables for later init
Nefos 3:b24ef00836c5 27 starty=y;
Nefos 3:b24ef00836c5 28 startl=lenght;
Nefos 3:b24ef00836c5 29 live=_live;
Nefos 6:cc8d2088f490 30 _length = lenght;
Nefos 6:cc8d2088f490 31 _food.init();//init food
Nefos 6:cc8d2088f490 32 for (int i=0;_length>i;i++)//create start snake
Nefos 0:2eb4d7e86e2f 33 {
Nefos 0:2eb4d7e86e2f 34 snek._x[i]=x+i;
Nefos 0:2eb4d7e86e2f 35 snek._y[i]=y;
Nefos 0:2eb4d7e86e2f 36 snek._dir[i]=1;
Nefos 0:2eb4d7e86e2f 37 }
Nefos 1:93a4cb86f100 38 snek._x[_length]=x+_length;
Nefos 7:a2f426a37e60 39 snek._x[_length+1]=x+_length+1;
Nefos 1:93a4cb86f100 40 snek._y[_length]=y;
Nefos 7:a2f426a37e60 41 snek._dir[_length]=1;
Nefos 1:93a4cb86f100 42 printf("xog is %d ", snek._x[_length-1]);
Nefos 1:93a4cb86f100 43 //printf("initalized");
Nefos 6:cc8d2088f490 44 }//end of init
Nefos 0:2eb4d7e86e2f 45
Nefos 0:2eb4d7e86e2f 46 void Snake::draw(N5110 &lcd){
Nefos 6:cc8d2088f490 47 checkWallCollision(lcd);// if head==wall game over
Nefos 5:449858a54971 48 checkTailCollision(lcd);
Nefos 3:b24ef00836c5 49 if (live!=0){
Nefos 3:b24ef00836c5 50 lcd.clear();
Nefos 6:cc8d2088f490 51 waitCount=((float)_length/3)+5;//set wait so the game is speeding up
Nefos 4:c74ec3f409f9 52 waitTime=(1/waitCount);
Nefos 7:a2f426a37e60 53 wait(waitTime);
Nefos 6:cc8d2088f490 54 _food.draw(lcd);//make first food
Nefos 7:a2f426a37e60 55 lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//draw arena
Nefos 7:a2f426a37e60 56 drawScore(lcd);
Nefos 7:a2f426a37e60 57 for ( int i=0; _length>i;i++){//draw snake
Nefos 3:b24ef00836c5 58 if (snek._x!=0)
Nefos 3:b24ef00836c5 59 {
Nefos 3:b24ef00836c5 60 if (snek._y!=0)
Nefos 3:b24ef00836c5 61 {
Nefos 3:b24ef00836c5 62 lcd.setPixel(snek._x[i],snek._y[i]);
Nefos 3:b24ef00836c5 63 }
Nefos 3:b24ef00836c5 64 }
Nefos 3:b24ef00836c5 65
Nefos 3:b24ef00836c5 66 lcd.refresh();
Nefos 3:b24ef00836c5 67
Nefos 3:b24ef00836c5 68 //printf("drawn");
Nefos 3:b24ef00836c5 69 }
Nefos 3:b24ef00836c5 70 }//live loop
Nefos 4:c74ec3f409f9 71
Nefos 6:cc8d2088f490 72 }//end of draw
Nefos 0:2eb4d7e86e2f 73 void Snake::update(Gamepad &pad){
Nefos 2:5e04e34a3b66 74
Nefos 3:b24ef00836c5 75 if (live!=0){
Nefos 0:2eb4d7e86e2f 76
Nefos 3:b24ef00836c5 77 d=pad.get_direction();
Nefos 3:b24ef00836c5 78 printf("x+1 is %d", snek._x[_length+1]);
Nefos 3:b24ef00836c5 79 printf("y+1 is %d", snek._y[_length+1]);
Nefos 3:b24ef00836c5 80 printf("dir+1 is %d", snek._dir[_length+1]);
Nefos 3:b24ef00836c5 81 printf("length is %d", _length);
Nefos 3:b24ef00836c5 82
Nefos 6:cc8d2088f490 83 /*if ( pad.check_event(Gamepad::A_PRESSED) == true)//testing the addPoint manually
Nefos 0:2eb4d7e86e2f 84 {
Nefos 3:b24ef00836c5 85 snek._x[_length+1]=snek._x[_length];//-1
Nefos 3:b24ef00836c5 86 snek._y[_length+1]=snek._y[_length];
Nefos 3:b24ef00836c5 87 snek._dir[_length+1]=snek._dir[_length-1];
Nefos 3:b24ef00836c5 88 _length=_length+1;
Nefos 3:b24ef00836c5 89 printf("length+1 is %d", _length);
Nefos 0:2eb4d7e86e2f 90
Nefos 3:b24ef00836c5 91 }
Nefos 3:b24ef00836c5 92 */
Nefos 6:cc8d2088f490 93 //printf("x is %d ", snek._x[_length-1]);
Nefos 7:a2f426a37e60 94 if (snek._dir[_length-1]==1)//set direction according _dir
Nefos 3:b24ef00836c5 95 {
Nefos 3:b24ef00836c5 96 snek._x[_length]++;
Nefos 0:2eb4d7e86e2f 97 }
Nefos 3:b24ef00836c5 98
Nefos 1:93a4cb86f100 99 if (snek._dir[_length-1]==2)
Nefos 3:b24ef00836c5 100 {
Nefos 3:b24ef00836c5 101 snek._y[_length]--;
Nefos 0:2eb4d7e86e2f 102
Nefos 0:2eb4d7e86e2f 103 }
Nefos 3:b24ef00836c5 104
Nefos 3:b24ef00836c5 105 if (snek._dir[_length-1]==3)
Nefos 3:b24ef00836c5 106 {
Nefos 3:b24ef00836c5 107 snek._x[_length]--;
Nefos 1:93a4cb86f100 108 }
Nefos 1:93a4cb86f100 109
Nefos 3:b24ef00836c5 110 if (snek._dir[_length-1]==4)
Nefos 3:b24ef00836c5 111 {
Nefos 3:b24ef00836c5 112 snek._y[_length]++;
Nefos 3:b24ef00836c5 113 }
Nefos 1:93a4cb86f100 114
Nefos 3:b24ef00836c5 115 for (int i=0 ;_length<i ;i++)
Nefos 3:b24ef00836c5 116 {
Nefos 3:b24ef00836c5 117 snek._x[i]=snek._x[i+1];
Nefos 3:b24ef00836c5 118 snek._y[i]=snek._y[i+1];
Nefos 3:b24ef00836c5 119 //printf("done");
Nefos 3:b24ef00836c5 120 }
Nefos 6:cc8d2088f490 121 //check dpad which way it is pointing
Nefos 6:cc8d2088f490 122 //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down
Nefos 6:cc8d2088f490 123 if (d==N){// if stick points up, go up
Nefos 3:b24ef00836c5 124
Nefos 3:b24ef00836c5 125 if (snek._dir[_length-1]!=4)
Nefos 3:b24ef00836c5 126 {
Nefos 3:b24ef00836c5 127 snek._dir[_length-1] = 2;
Nefos 3:b24ef00836c5 128 }
Nefos 3:b24ef00836c5 129
Nefos 3:b24ef00836c5 130 }
Nefos 3:b24ef00836c5 131
Nefos 6:cc8d2088f490 132 if (d==E){// if stick points right, go right
Nefos 0:2eb4d7e86e2f 133
Nefos 3:b24ef00836c5 134 if (snek._dir[_length-1]!=3)
Nefos 0:2eb4d7e86e2f 135 {
Nefos 3:b24ef00836c5 136 snek._dir[_length-1] = 1;
Nefos 3:b24ef00836c5 137 }
Nefos 3:b24ef00836c5 138
Nefos 3:b24ef00836c5 139 }
Nefos 6:cc8d2088f490 140
Nefos 6:cc8d2088f490 141 if (d==W){// if stick points left, go left
Nefos 3:b24ef00836c5 142
Nefos 3:b24ef00836c5 143 if (snek._dir[_length-1]!=1)
Nefos 3:b24ef00836c5 144 {
Nefos 3:b24ef00836c5 145 snek._dir[_length-1] = 3;
Nefos 0:2eb4d7e86e2f 146 }
Nefos 0:2eb4d7e86e2f 147
Nefos 0:2eb4d7e86e2f 148
Nefos 0:2eb4d7e86e2f 149 }
Nefos 6:cc8d2088f490 150
Nefos 6:cc8d2088f490 151 if (d==S){// if stick points down, go down
Nefos 0:2eb4d7e86e2f 152
Nefos 3:b24ef00836c5 153 if (snek._dir[_length-1]!=2)
Nefos 1:93a4cb86f100 154 {
Nefos 1:93a4cb86f100 155 snek._dir[_length-1] = 4;
Nefos 1:93a4cb86f100 156 }
Nefos 1:93a4cb86f100 157
Nefos 1:93a4cb86f100 158
Nefos 1:93a4cb86f100 159 }
Nefos 3:b24ef00836c5 160
Nefos 3:b24ef00836c5 161 printf("updated ");
Nefos 6:cc8d2088f490 162 addPoint();
Nefos 3:b24ef00836c5 163 }//live loop
Nefos 6:cc8d2088f490 164 }//end of update
Nefos 3:b24ef00836c5 165
Nefos 6:cc8d2088f490 166 void Snake::addPoint(){
Nefos 3:b24ef00836c5 167
Nefos 3:b24ef00836c5 168 posXY foodPos = _food.returnPos();
Nefos 3:b24ef00836c5 169 if(snek._x[_length-1]==foodPos.x && snek._y[_length-1]== foodPos.y)
Nefos 3:b24ef00836c5 170 {
Nefos 3:b24ef00836c5 171 snek._x[_length+1]=snek._x[_length];//-1
Nefos 3:b24ef00836c5 172 snek._y[_length+1]=snek._y[_length];
Nefos 3:b24ef00836c5 173 snek._dir[_length+1]=snek._dir[_length-1];
Nefos 6:cc8d2088f490 174 _length=_length+1;//if head == food, _length++
Nefos 6:cc8d2088f490 175 _food.respawn();//spawn new food
Nefos 3:b24ef00836c5 176 }
Nefos 6:cc8d2088f490 177 }//end of addPoint
Nefos 0:2eb4d7e86e2f 178
Nefos 2:5e04e34a3b66 179 void Snake::deadSnake(N5110 &lcd){
Nefos 3:b24ef00836c5 180
Nefos 4:c74ec3f409f9 181
Nefos 7:a2f426a37e60 182 live--; //take a life away
Nefos 7:a2f426a37e60 183 while (live==0){//dead reset game
Nefos 3:b24ef00836c5 184 lcd.clear();
Nefos 3:b24ef00836c5 185 lcd.printString("Game Over",0,1);
Nefos 7:a2f426a37e60 186 lcd.printString("Press Reset",0,2);
Nefos 3:b24ef00836c5 187 lcd.printString("To restart",0,3);
Nefos 3:b24ef00836c5 188 lcd.refresh();
Nefos 4:c74ec3f409f9 189
Nefos 3:b24ef00836c5 190 }
Nefos 4:c74ec3f409f9 191 //lcd.clear();
Nefos 4:c74ec3f409f9 192 //lcd.printString("Game Over",0,1);
Nefos 4:c74ec3f409f9 193 //lcd.refresh();
Nefos 2:5e04e34a3b66 194
Nefos 6:cc8d2088f490 195 init(startx,starty,startl,live);
Nefos 4:c74ec3f409f9 196
Nefos 4:c74ec3f409f9 197 }//end of deadSnake
Nefos 3:b24ef00836c5 198
Nefos 2:5e04e34a3b66 199 void Snake::checkWallCollision(N5110 &lcd){
Nefos 2:5e04e34a3b66 200
Nefos 3:b24ef00836c5 201 if (snek._x[_length]==WIDTH||snek._x[_length]==0)//if snake head hits side walls
Nefos 3:b24ef00836c5 202 {
Nefos 3:b24ef00836c5 203 deadSnake(lcd);
Nefos 3:b24ef00836c5 204 }
Nefos 3:b24ef00836c5 205 if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR)//if snake hits top or bottom walls
Nefos 3:b24ef00836c5 206 {
Nefos 3:b24ef00836c5 207 deadSnake(lcd);
Nefos 3:b24ef00836c5 208 }
Nefos 2:5e04e34a3b66 209
Nefos 3:b24ef00836c5 210 }//end of checkWallCollision
Nefos 5:449858a54971 211
Nefos 7:a2f426a37e60 212 void Snake::checkTailCollision(N5110 &lcd){//if snake eats itself
Nefos 5:449858a54971 213 for (int i=0 ;_length<i ;i++){
Nefos 5:449858a54971 214
Nefos 5:449858a54971 215 if (snek._x[_length-1]==snek._x[i] && snek._y[_length-1]==snek._y[i])
Nefos 5:449858a54971 216 {
Nefos 5:449858a54971 217 deadSnake(lcd);
Nefos 5:449858a54971 218 }
Nefos 5:449858a54971 219 }
Nefos 5:449858a54971 220
Nefos 5:449858a54971 221
Nefos 7:a2f426a37e60 222 }//end of checkTailCollision
Nefos 7:a2f426a37e60 223
Nefos 7:a2f426a37e60 224 void Snake::drawScore(N5110 &lcd){
Nefos 7:a2f426a37e60 225 char buffer1[14];
Nefos 7:a2f426a37e60 226 char buffer2[14];
Nefos 7:a2f426a37e60 227 sprintf(buffer1,"%2d",live);
Nefos 7:a2f426a37e60 228 sprintf(buffer2,"%2d",_length-5);
Nefos 7:a2f426a37e60 229 lcd.printString(buffer1,25,0);
Nefos 7:a2f426a37e60 230 lcd.printString(buffer2,70,0);
Nefos 7:a2f426a37e60 231 lcd.printString("Life:",0,0);
Nefos 7:a2f426a37e60 232 lcd.printString("Pts:",40,0);//display life and points
Nefos 7:a2f426a37e60 233
Nefos 7:a2f426a37e60 234 }//end of drawScore