Snake game snake library
Snake.cpp
- Committer:
- 1012754868
- Date:
- 2019-04-28
- Revision:
- 10:f63560a7e7ed
- Parent:
- 9:e023c11f7737
File content as of revision 10:f63560a7e7ed:
#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, Gamepad &pad){ check_WallCollision(lcd,pad);// if head==wall game over check_TailCollision(lcd,pad); 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(pad); }//live loop }//end of update void Snake::eat(Gamepad &pad){ 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 Tone_1(pad); } }//end of addPoint int Snake::dead(N5110 &lcd, Gamepad &pad){ 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 Back",0,4); lcd.refresh(); pad.tone(0.0,0.5); //gap wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(784.0,0.75); //5- wait(0.75); pad.tone(659.0,0.25); //3. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.5); //2 wait(0.5); pad.tone(784.0,0.5); //5 wait(0.5); pad.tone(587.0,0.5); //2 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(440.0,0.25); //l6. wait(0.25); pad.tone(659.0,0.75); //3- wait(0.75); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(494.0,0.5); //l7 wait(0.5); pad.tone(0.0,0.5); //gap wait(0.5); pad.tone(494.0,0.5); //l7 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(440.0,0.5); //l6 wait(0.5); pad.tone(494.0,0.5); //l7 wait(0.5); pad.tone(523.0,0.25); //1. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(392.0,0.5); //l5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.5); //1 wait(0.5); pad.tone(587.0,0.25); //2. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(698.0,0.5); //4 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(698.0,0.25); //4. wait(0.25); pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(587.0,0.25); //2. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(587.0,0.5); //2 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(0.0,5); //gap wait(0.5); pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(587.0,0.25); //2. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(784.0,0.75); //5- wait(0.75); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(587.0,0.5); //2 wait(0.5); pad.tone(784.0,0.5); //5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.5); //2 wait(0.5); pad.tone(523.0,0.25); //1. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(440.0,0.25); //l6. wait(0.25); pad.tone(440.0,0.5); //l6 wait(0.5); pad.tone(494.0,0.25); //l7. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(392.0,0.5); //l5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(0.0,0.5); //gap wait(0.5); pad.tone(0.0,0.25); //gap. wait(0.25); pad.tone(494.0,0.5); //l7 wait(0.5); pad.tone(523.0,0.25); //1. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(392.0,0.5); //l5 wait(0.5); pad.tone(523.0,0.5); //1 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(698.0,0.5); //4 wait(0.5); pad.tone(698.0,0.25); //4. wait(0.25); pad.tone(659.0,0.25); //3. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(523.0,0.5); //1 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(0.0,0.5); //gap wait(0.5); pad.tone(0.0,0.5); //gap wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(698.0,0.25); //4. wait(0.25); pad.tone(784.0,0.5); //5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(784.0,0.5); //5 wait(0.5); pad.tone(784.0,0.5); //5 wait(0.5); pad.tone(784.0,0.5); //5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(784.0,0.25); //5. wait(0.25); pad.tone(880.0,0.25); //6. wait(0.25); pad.tone(784.0,0.25); //5. wait(0.25); pad.tone(698.0,0.25); //4. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.5); //3 wait(0.5); pad.tone(659.0,0.5); //3 wait(0.5); pad.tone(659.0,0.5); //3 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.5); //3 wait(0.5); pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(698.0,0.25); //4. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(523.0,0.5); //1 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.5); //1 wait(0.5); pad.tone(523.0,0.5); //1. wait(0.5); pad.tone(494.0,0.25); //l7. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(440.0,0.5); //l6 wait(0.5); pad.tone(494.0,0.5); //l7 wait(0.5); pad.tone(494.0,0.25); //l7. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(587.0,0.5); //2 wait(0.5); pad.tone(587.0,0.25); //2. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(659.0,0.25); //3. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.5); //2 wait(0.5); pad.tone(0.0,0.5); //gap wait(0.5); pad.tone(659.0,0.25); //3. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(698.0,0.25); //4. wait(0.25); pad.tone(784.0,0.5); //5 wait(0.5); pad.tone(784.0,0.5); //5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(784.0,0.5); //5 wait(0.5); pad.tone(784.0,0.5); //5 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(784.0,0.25); //5. wait(0.25); pad.tone(880.0,0.25); //6. wait(0.25); pad.tone(784.0,0.25); //5. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(698.0,0.25); //4. wait(0.25); pad.tone(659.0,0.5); //3 wait(0.5); pad.tone(659.0,0.5); //3 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.5); //3 wait(0.5); pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(698.0,0.25); //4. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(587.0,0.25); //2. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.25); //1. wait(0.25); pad.tone(494.0,0.25); //l7. wait(0.25); pad.tone(440.0,0.5); //l6 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(440.0,0.25); //l6. wait(0.25); pad.tone(494.0,0.25); //l7. wait(0.25); pad.tone(523.0,0.25); //1. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(392.0,0.5); //l5 wait(0.5); pad.tone(523.0,0.5); //1 wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(659.0,0.25); //3. wait(0.25); pad.tone(587.0,0.75); //2- wait(0.75); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(587.0,0.25); //2. wait(0.25); pad.tone(523.0,0.25); //1. wait(0.25); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } pad.tone(523.0,0.5); //1 wait(0.5); pad.tone(0.0,0.5); //gap wait(0.5); pad.tone(0.0,0.5); //gap wait(0.5); if(pad.check_event(Gamepad::BACK_PRESSED) == true){ return 0; } } if(Highest_score < (_length - 5)){ Highest_score = _length - 5; } init(initx,inity,initl,live); //end of deadSnake return 0; } void Snake::Tone_1(Gamepad &pad){ pad.tone(750.0,0.1); } void Snake::check_WallCollision(N5110 &lcd, Gamepad &pad){ if (snak._x[_length] == 0 || snak._x[_length] == WIDTH)//if snake head hits side walls { game_music(pad); dead(lcd,pad); } if (snak._y[_length]== FLOOR ||snak._y[_length]== CEILING )//if snake hits top or bottom walls { game_music(pad); dead(lcd,pad); } }//end of checkWallCollision void Snake::check_TailCollision(N5110 &lcd, Gamepad &pad){//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]) { game_music(pad); dead(lcd,pad); } } }//end of checkTailCollision void Snake::game_music(Gamepad &pad){ pad.tone(1046.0,0.125); wait(0.125); pad.tone(1075.0,0.125); wait(0.125); pad.tone(1318.0,0.125); wait(0.125); } 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