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