Snake game snake library
Snake.cpp
- Committer:
- 1012754868
- Date:
- 2019-04-26
- Revision:
- 8:721a8dca7a25
- Parent:
- 7:a2f426a37e60
- Child:
- 9:e023c11f7737
File content as of revision 8:721a8dca7a25:
#include "Snake.h" #define WIDTH 84 #define HEIGHT 48 #define CEILING 8 #define FLOOR 48 snakePart snak; int LIFE[7][7] = { {0,1,0,0,0,1,0}, {1,1,1,0,1,1,1}, {1,1,1,1,1,1,1}, {0,1,1,1,1,1,0}, {0,0,1,1,1,0,0}, {0,0,0,1,0,0,0}, }; Snake::Snake() { } Snake::~Snake() { } /************************Functions************************/ void Snake::init(int x, int y, int length, int _live){//initalizing the starting variables live=_live; _length = length; _food.init(); initx=x; inity=y; initl=length; snak._x[0] = x; snak._y[0] = y; snak._dirc[0] = length; for (int i=1; i < _length ;i++) { snak._x[i]=snak._x[0]+i; snak._y[i]=snak._y[0]; snak._dirc[i]=1; } snak._x[_length]=x+_length; snak._y[_length]=y; snak._dirc[_length]=1; } void Snake::drawsnake(N5110 &lcd){ check_WallCollision(lcd);// if head==wall game over check_TailCollision(lcd); if (live!=0){ lcd.clear(); waitExpect=((float)_length/3)+5;//set wait so the game is speeding up waitTime=(1/(waitExpect+_speed)); wait(waitTime); _food.drawfood(lcd);//make first food lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//draw arena drawscore(lcd); for ( int i=0; _length>i;i++){//draw snake lcd.setPixel(snak._x[i],snak._y[i]); lcd.refresh(); } } } void Snake::snakemov(Gamepad &pad){ if (live!=0){ d=pad.get_direction(); if (snak._dirc[_length-1]==1)//set direction according _dir { snak._x[_length]++; } if (snak._dirc[_length-1]==2) { snak._y[_length]--; } if (snak._dirc[_length-1]==3) { snak._x[_length]--; } if (snak._dirc[_length-1]==4) { snak._y[_length]++; } for (int i=0 ;_length<i ;i++) { snak._x[i]=snak._x[i+1]; snak._y[i]=snak._y[i+1]; //printf("done"); } //check dpad which way it is pointing //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down if (d==N){// if stick points up, go up if (snak._dirc[_length-1]!=4) { snak._dirc[_length-1] = 2; } } if (d==E){// if stick points right, go right if (snak._dirc[_length-1]!=3) { snak._dirc[_length-1] = 1; } } if (d==W){// if stick points left, go left if (snak._dirc[_length-1]!=1) { snak._dirc[_length-1] = 3; } } if (d==S){// if stick points down, go down if (snak._dirc[_length-1]!=2) { snak._dirc[_length-1] = 4; } } printf("updated "); eat(); }//live loop }//end of update void Snake::eat(){ Foodpos foodPos = _food.returnPos(); if(snak._x[_length-1]==foodPos.x && snak._y[_length-1]== foodPos.y) { snak._x[_length+1]=snak._x[_length];//-1 snak._y[_length+1]=snak._y[_length]; snak._dirc[_length+1]=snak._dirc[_length-1]; _length=_length+1;//if head == food, _length++ _food.createfood();//spawn new food } }//end of addPoint void Snake::dead(N5110 &lcd){ live--; //take a life away //while (live==0){//dead reset game while(live==0) { lcd.clear(); lcd.printString("Game Over",0,1); lcd.printString("Press Reset",0,2); lcd.printString("To restart",0,3); lcd.refresh(); } init(initx,inity,initl,live); //end of deadSnake } void Snake::check_WallCollision(N5110 &lcd){ if (snak._x[_length] == 0 || snak._x[_length] == WIDTH)//if snake head hits side walls { dead(lcd); } if (snak._y[_length]== FLOOR ||snak._y[_length]== CEILING )//if snake hits top or bottom walls { dead(lcd); } }//end of checkWallCollision void Snake::check_TailCollision(N5110 &lcd){//if snake eats itself for (int i=0 ;_length<i ;i++){ if (snak._x[_length-1]==snak._x[i] && snak._y[_length-1]==snak._y[i]) { dead(lcd); } } }//end of checkTailCollision void Snake::drawscore(N5110 &lcd){ char buffer1[14]; char buffer2[14]; sprintf(buffer1,"%2d",live); sprintf(buffer2,"%2d",_length-5); lcd.printString(buffer1,20,0); lcd.printString(buffer2,70,0); lcd.drawSprite(6,1,7,7,(int *)LIFE); lcd.printString(":",17,0); lcd.printString("Score:",40,0);//display life and points }//end of drawScore