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