Snake game snake library

Committer:
1012754868
Date:
Fri Apr 26 04:46:54 2019 +0000
Revision:
8:721a8dca7a25
Parent:
7:a2f426a37e60
Child:
9:e023c11f7737
Snake library

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