Balint Bogdan 2645 project, 200966741

Dependents:   2645Game_el15bb

Committer:
Nefos
Date:
Fri May 05 15:06:54 2017 +0000
Revision:
6:cc8d2088f490
Parent:
5:449858a54971
Child:
7:a2f426a37e60
working code, needs formatting;

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