Snake game snake library
Snake.cpp@5:449858a54971, 2017-05-05 (annotated)
- Committer:
- Nefos
- Date:
- Fri May 05 14:51:27 2017 +0000
- Revision:
- 5:449858a54971
- Parent:
- 4:c74ec3f409f9
- Child:
- 6:cc8d2088f490
needs comments, chekTailCollision is broken;
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:b24ef00836c5 | 22 | void Snake::init(int x, int y, int lenght, int _live){ |
Nefos | 0:2eb4d7e86e2f | 23 | |
Nefos | 0:2eb4d7e86e2f | 24 | |
Nefos | 1:93a4cb86f100 | 25 | |
Nefos | 0:2eb4d7e86e2f | 26 | //initalizing the starting variables |
Nefos | 3:b24ef00836c5 | 27 | //snek._x[0]=x; |
Nefos | 3:b24ef00836c5 | 28 | //snek._y[0]=y; |
Nefos | 3:b24ef00836c5 | 29 | startx=x; |
Nefos | 3:b24ef00836c5 | 30 | starty=y; |
Nefos | 3:b24ef00836c5 | 31 | startl=lenght; |
Nefos | 3:b24ef00836c5 | 32 | live=_live; |
Nefos | 4:c74ec3f409f9 | 33 | /*if (live==0){ |
Nefos | 3:b24ef00836c5 | 34 | live=3; |
Nefos | 4:c74ec3f409f9 | 35 | }*/ |
Nefos | 0:2eb4d7e86e2f | 36 | _length = lenght; |
Nefos | 3:b24ef00836c5 | 37 | |
Nefos | 3:b24ef00836c5 | 38 | _food.init(); |
Nefos | 0:2eb4d7e86e2f | 39 | for (int i=0;_length>i;i++) |
Nefos | 0:2eb4d7e86e2f | 40 | { |
Nefos | 0:2eb4d7e86e2f | 41 | snek._x[i]=x+i; |
Nefos | 0:2eb4d7e86e2f | 42 | snek._y[i]=y; |
Nefos | 0:2eb4d7e86e2f | 43 | snek._dir[i]=1; |
Nefos | 0:2eb4d7e86e2f | 44 | } |
Nefos | 1:93a4cb86f100 | 45 | snek._x[_length]=x+_length; |
Nefos | 1:93a4cb86f100 | 46 | snek._y[_length]=y; |
Nefos | 1:93a4cb86f100 | 47 | |
Nefos | 1:93a4cb86f100 | 48 | /* snek._y[0]=y; |
Nefos | 1:93a4cb86f100 | 49 | snek._y[1]=y+1; |
Nefos | 1:93a4cb86f100 | 50 | snek._y[2]=y+2; |
Nefos | 1:93a4cb86f100 | 51 | snek._y[3]=y+2; |
Nefos | 1:93a4cb86f100 | 52 | snek._y[4]=y+3; |
Nefos | 1:93a4cb86f100 | 53 | snek._y[5]=y+3; |
Nefos | 1:93a4cb86f100 | 54 | snek._y[6]=y+4;*/ |
Nefos | 0:2eb4d7e86e2f | 55 | _direction = 1;//1 is East, 2 is South, 3 is West, 4 is North |
Nefos | 0:2eb4d7e86e2f | 56 | |
Nefos | 0:2eb4d7e86e2f | 57 | |
Nefos | 3:b24ef00836c5 | 58 | |
Nefos | 1:93a4cb86f100 | 59 | printf("xog is %d ", snek._x[_length-1]); |
Nefos | 1:93a4cb86f100 | 60 | //printf("initalized"); |
Nefos | 0:2eb4d7e86e2f | 61 | } |
Nefos | 0:2eb4d7e86e2f | 62 | |
Nefos | 0:2eb4d7e86e2f | 63 | void Snake::draw(N5110 &lcd){ |
Nefos | 3:b24ef00836c5 | 64 | checkWallCollision(lcd); |
Nefos | 5:449858a54971 | 65 | checkTailCollision(lcd); |
Nefos | 3:b24ef00836c5 | 66 | if (live!=0){ |
Nefos | 3:b24ef00836c5 | 67 | lcd.clear(); |
Nefos | 4:c74ec3f409f9 | 68 | waitCount=((float)_length/3)+5; |
Nefos | 4:c74ec3f409f9 | 69 | waitTime=(1/waitCount); |
Nefos | 4:c74ec3f409f9 | 70 | wait(waitTime); |
Nefos | 3:b24ef00836c5 | 71 | char buffer1[14]; |
Nefos | 3:b24ef00836c5 | 72 | char buffer2[14]; |
Nefos | 3:b24ef00836c5 | 73 | sprintf(buffer1,"%2d",live); |
Nefos | 3:b24ef00836c5 | 74 | sprintf(buffer2,"%2d",_length); |
Nefos | 3:b24ef00836c5 | 75 | lcd.printString(buffer1,25,0); |
Nefos | 3:b24ef00836c5 | 76 | lcd.printString(buffer2,70,0); |
Nefos | 3:b24ef00836c5 | 77 | _food.draw(lcd); |
Nefos | 3:b24ef00836c5 | 78 | lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT); |
Nefos | 3:b24ef00836c5 | 79 | lcd.printString("Life:",0,0); |
Nefos | 3:b24ef00836c5 | 80 | lcd.printString("Pts:",40,0); |
Nefos | 3:b24ef00836c5 | 81 | |
Nefos | 3:b24ef00836c5 | 82 | for ( int i=0; _length>i;i++){ |
Nefos | 3:b24ef00836c5 | 83 | if (snek._x!=0) |
Nefos | 3:b24ef00836c5 | 84 | { |
Nefos | 3:b24ef00836c5 | 85 | if (snek._y!=0) |
Nefos | 3:b24ef00836c5 | 86 | { |
Nefos | 3:b24ef00836c5 | 87 | lcd.setPixel(snek._x[i],snek._y[i]); |
Nefos | 3:b24ef00836c5 | 88 | } |
Nefos | 3:b24ef00836c5 | 89 | } |
Nefos | 3:b24ef00836c5 | 90 | |
Nefos | 3:b24ef00836c5 | 91 | |
Nefos | 3:b24ef00836c5 | 92 | |
Nefos | 3:b24ef00836c5 | 93 | lcd.refresh(); |
Nefos | 3:b24ef00836c5 | 94 | |
Nefos | 3:b24ef00836c5 | 95 | //printf("drawn"); |
Nefos | 3:b24ef00836c5 | 96 | } |
Nefos | 3:b24ef00836c5 | 97 | }//live loop |
Nefos | 4:c74ec3f409f9 | 98 | |
Nefos | 0:2eb4d7e86e2f | 99 | } |
Nefos | 0:2eb4d7e86e2f | 100 | void Snake::update(Gamepad &pad){ |
Nefos | 2:5e04e34a3b66 | 101 | |
Nefos | 3:b24ef00836c5 | 102 | if (live!=0){ |
Nefos | 0:2eb4d7e86e2f | 103 | |
Nefos | 3:b24ef00836c5 | 104 | d=pad.get_direction(); |
Nefos | 3:b24ef00836c5 | 105 | startx=snek._x[0]; |
Nefos | 3:b24ef00836c5 | 106 | |
Nefos | 3:b24ef00836c5 | 107 | printf("x+1 is %d", snek._x[_length+1]); |
Nefos | 3:b24ef00836c5 | 108 | printf("y+1 is %d", snek._y[_length+1]); |
Nefos | 3:b24ef00836c5 | 109 | printf("dir+1 is %d", snek._dir[_length+1]); |
Nefos | 3:b24ef00836c5 | 110 | printf("length is %d", _length); |
Nefos | 3:b24ef00836c5 | 111 | |
Nefos | 3:b24ef00836c5 | 112 | /*if ( pad.check_event(Gamepad::A_PRESSED) == true)//testing the add_point manually |
Nefos | 0:2eb4d7e86e2f | 113 | { |
Nefos | 3:b24ef00836c5 | 114 | snek._x[_length+1]=snek._x[_length];//-1 |
Nefos | 3:b24ef00836c5 | 115 | snek._y[_length+1]=snek._y[_length]; |
Nefos | 3:b24ef00836c5 | 116 | snek._dir[_length+1]=snek._dir[_length-1]; |
Nefos | 3:b24ef00836c5 | 117 | _length=_length+1; |
Nefos | 3:b24ef00836c5 | 118 | printf("length+1 is %d", _length); |
Nefos | 0:2eb4d7e86e2f | 119 | |
Nefos | 3:b24ef00836c5 | 120 | } |
Nefos | 3:b24ef00836c5 | 121 | */ |
Nefos | 3:b24ef00836c5 | 122 | |
Nefos | 3:b24ef00836c5 | 123 | |
Nefos | 3:b24ef00836c5 | 124 | //printf("x is %d ", snek._x[_length-1]); |
Nefos | 3:b24ef00836c5 | 125 | if (snek._dir[_length-1]==1) |
Nefos | 3:b24ef00836c5 | 126 | { |
Nefos | 3:b24ef00836c5 | 127 | snek._x[_length]++; |
Nefos | 0:2eb4d7e86e2f | 128 | } |
Nefos | 3:b24ef00836c5 | 129 | |
Nefos | 1:93a4cb86f100 | 130 | if (snek._dir[_length-1]==2) |
Nefos | 3:b24ef00836c5 | 131 | { |
Nefos | 3:b24ef00836c5 | 132 | snek._y[_length]--; |
Nefos | 0:2eb4d7e86e2f | 133 | |
Nefos | 0:2eb4d7e86e2f | 134 | } |
Nefos | 3:b24ef00836c5 | 135 | |
Nefos | 3:b24ef00836c5 | 136 | if (snek._dir[_length-1]==3) |
Nefos | 3:b24ef00836c5 | 137 | { |
Nefos | 3:b24ef00836c5 | 138 | snek._x[_length]--; |
Nefos | 1:93a4cb86f100 | 139 | } |
Nefos | 1:93a4cb86f100 | 140 | |
Nefos | 3:b24ef00836c5 | 141 | if (snek._dir[_length-1]==4) |
Nefos | 3:b24ef00836c5 | 142 | { |
Nefos | 3:b24ef00836c5 | 143 | snek._y[_length]++; |
Nefos | 3:b24ef00836c5 | 144 | } |
Nefos | 1:93a4cb86f100 | 145 | |
Nefos | 3:b24ef00836c5 | 146 | for (int i=0 ;_length<i ;i++) |
Nefos | 3:b24ef00836c5 | 147 | { |
Nefos | 3:b24ef00836c5 | 148 | snek._x[i]=snek._x[i+1]; |
Nefos | 3:b24ef00836c5 | 149 | snek._y[i]=snek._y[i+1]; |
Nefos | 3:b24ef00836c5 | 150 | //printf("done"); |
Nefos | 3:b24ef00836c5 | 151 | } |
Nefos | 3:b24ef00836c5 | 152 | |
Nefos | 3:b24ef00836c5 | 153 | |
Nefos | 0:2eb4d7e86e2f | 154 | //check dpad which way it is pointing |
Nefos | 0:2eb4d7e86e2f | 155 | //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down |
Nefos | 0:2eb4d7e86e2f | 156 | //set a breakpoint at head by snakeX/Y[i] |
Nefos | 0:2eb4d7e86e2f | 157 | //switch case to make sure direction is good |
Nefos | 0:2eb4d7e86e2f | 158 | //if head == food, _length++, and -direction we add 1 length |
Nefos | 0:2eb4d7e86e2f | 159 | // if head==wall game over |
Nefos | 3:b24ef00836c5 | 160 | if (d==N){ |
Nefos | 3:b24ef00836c5 | 161 | |
Nefos | 3:b24ef00836c5 | 162 | if (snek._dir[_length-1]!=4) |
Nefos | 3:b24ef00836c5 | 163 | { |
Nefos | 3:b24ef00836c5 | 164 | snek._dir[_length-1] = 2; |
Nefos | 3:b24ef00836c5 | 165 | } |
Nefos | 3:b24ef00836c5 | 166 | |
Nefos | 3:b24ef00836c5 | 167 | } |
Nefos | 3:b24ef00836c5 | 168 | |
Nefos | 3:b24ef00836c5 | 169 | if (d==E){ |
Nefos | 0:2eb4d7e86e2f | 170 | |
Nefos | 3:b24ef00836c5 | 171 | if (snek._dir[_length-1]!=3) |
Nefos | 0:2eb4d7e86e2f | 172 | { |
Nefos | 3:b24ef00836c5 | 173 | snek._dir[_length-1] = 1; |
Nefos | 3:b24ef00836c5 | 174 | } |
Nefos | 3:b24ef00836c5 | 175 | |
Nefos | 3:b24ef00836c5 | 176 | } |
Nefos | 3:b24ef00836c5 | 177 | if (d==W){ |
Nefos | 3:b24ef00836c5 | 178 | |
Nefos | 3:b24ef00836c5 | 179 | if (snek._dir[_length-1]!=1) |
Nefos | 3:b24ef00836c5 | 180 | { |
Nefos | 3:b24ef00836c5 | 181 | snek._dir[_length-1] = 3; |
Nefos | 0:2eb4d7e86e2f | 182 | } |
Nefos | 0:2eb4d7e86e2f | 183 | |
Nefos | 0:2eb4d7e86e2f | 184 | |
Nefos | 0:2eb4d7e86e2f | 185 | } |
Nefos | 3:b24ef00836c5 | 186 | if (d==S){ |
Nefos | 0:2eb4d7e86e2f | 187 | |
Nefos | 3:b24ef00836c5 | 188 | if (snek._dir[_length-1]!=2) |
Nefos | 1:93a4cb86f100 | 189 | { |
Nefos | 1:93a4cb86f100 | 190 | snek._dir[_length-1] = 4; |
Nefos | 1:93a4cb86f100 | 191 | } |
Nefos | 1:93a4cb86f100 | 192 | |
Nefos | 1:93a4cb86f100 | 193 | |
Nefos | 1:93a4cb86f100 | 194 | } |
Nefos | 3:b24ef00836c5 | 195 | |
Nefos | 3:b24ef00836c5 | 196 | printf("updated "); |
Nefos | 3:b24ef00836c5 | 197 | add_point(); |
Nefos | 3:b24ef00836c5 | 198 | }//live loop |
Nefos | 3:b24ef00836c5 | 199 | } |
Nefos | 3:b24ef00836c5 | 200 | |
Nefos | 3:b24ef00836c5 | 201 | void Snake::add_point(){ |
Nefos | 3:b24ef00836c5 | 202 | |
Nefos | 3:b24ef00836c5 | 203 | posXY foodPos = _food.returnPos(); |
Nefos | 3:b24ef00836c5 | 204 | if(snek._x[_length-1]==foodPos.x && snek._y[_length-1]== foodPos.y) |
Nefos | 3:b24ef00836c5 | 205 | { |
Nefos | 3:b24ef00836c5 | 206 | snek._x[_length+1]=snek._x[_length];//-1 |
Nefos | 3:b24ef00836c5 | 207 | snek._y[_length+1]=snek._y[_length]; |
Nefos | 3:b24ef00836c5 | 208 | snek._dir[_length+1]=snek._dir[_length-1]; |
Nefos | 3:b24ef00836c5 | 209 | _length=_length+1; |
Nefos | 3:b24ef00836c5 | 210 | _food.respawn(); |
Nefos | 3:b24ef00836c5 | 211 | } |
Nefos | 3:b24ef00836c5 | 212 | }//end of add_point |
Nefos | 0:2eb4d7e86e2f | 213 | |
Nefos | 2:5e04e34a3b66 | 214 | void Snake::deadSnake(N5110 &lcd){ |
Nefos | 3:b24ef00836c5 | 215 | |
Nefos | 4:c74ec3f409f9 | 216 | |
Nefos | 4:c74ec3f409f9 | 217 | live--; |
Nefos | 4:c74ec3f409f9 | 218 | while (live==0){ |
Nefos | 3:b24ef00836c5 | 219 | lcd.clear(); |
Nefos | 3:b24ef00836c5 | 220 | lcd.printString("Game Over",0,1); |
Nefos | 3:b24ef00836c5 | 221 | lcd.printString("Press Start",0,2); |
Nefos | 3:b24ef00836c5 | 222 | lcd.printString("To restart",0,3); |
Nefos | 3:b24ef00836c5 | 223 | lcd.refresh(); |
Nefos | 4:c74ec3f409f9 | 224 | |
Nefos | 3:b24ef00836c5 | 225 | } |
Nefos | 4:c74ec3f409f9 | 226 | //lcd.clear(); |
Nefos | 4:c74ec3f409f9 | 227 | //lcd.printString("Game Over",0,1); |
Nefos | 4:c74ec3f409f9 | 228 | //lcd.refresh(); |
Nefos | 2:5e04e34a3b66 | 229 | |
Nefos | 4:c74ec3f409f9 | 230 | init(25,starty,startl,live); |
Nefos | 4:c74ec3f409f9 | 231 | |
Nefos | 4:c74ec3f409f9 | 232 | }//end of deadSnake |
Nefos | 3:b24ef00836c5 | 233 | |
Nefos | 2:5e04e34a3b66 | 234 | void Snake::checkWallCollision(N5110 &lcd){ |
Nefos | 2:5e04e34a3b66 | 235 | |
Nefos | 3:b24ef00836c5 | 236 | if (snek._x[_length]==WIDTH||snek._x[_length]==0)//if snake head hits side walls |
Nefos | 3:b24ef00836c5 | 237 | { |
Nefos | 3:b24ef00836c5 | 238 | deadSnake(lcd); |
Nefos | 3:b24ef00836c5 | 239 | } |
Nefos | 3:b24ef00836c5 | 240 | if (snek._y[_length]==CEILING||snek._y[_length]==FLOOR)//if snake hits top or bottom walls |
Nefos | 3:b24ef00836c5 | 241 | { |
Nefos | 3:b24ef00836c5 | 242 | deadSnake(lcd); |
Nefos | 3:b24ef00836c5 | 243 | } |
Nefos | 2:5e04e34a3b66 | 244 | |
Nefos | 3:b24ef00836c5 | 245 | }//end of checkWallCollision |
Nefos | 5:449858a54971 | 246 | |
Nefos | 5:449858a54971 | 247 | void Snake::checkTailCollision(N5110 &lcd){ |
Nefos | 5:449858a54971 | 248 | for (int i=0 ;_length<i ;i++){ |
Nefos | 5:449858a54971 | 249 | |
Nefos | 5:449858a54971 | 250 | if (snek._x[_length-1]==snek._x[i] && snek._y[_length-1]==snek._y[i]) |
Nefos | 5:449858a54971 | 251 | { |
Nefos | 5:449858a54971 | 252 | deadSnake(lcd); |
Nefos | 5:449858a54971 | 253 | } |
Nefos | 5:449858a54971 | 254 | } |
Nefos | 5:449858a54971 | 255 | |
Nefos | 5:449858a54971 | 256 | |
Nefos | 5:449858a54971 | 257 | } |