1
Dependencies: mbed Gamepad N5110
Diff: Snake/Snake.cpp
- Revision:
- 7:cafa0b96e8d3
- Parent:
- 3:7b28047013d2
--- a/Snake/Snake.cpp Thu May 02 08:17:22 2019 +0000 +++ b/Snake/Snake.cpp Sun May 05 12:10:48 2019 +0000 @@ -38,34 +38,32 @@ initl=length; snak._x[0] = x; snak._y[0] = y; - snak._dirc[0] = length; + snak._dirc[0] = 1; 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._x[_length]=x+_length-1; 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_WallCollision(lcd,pad); check_TailCollision(lcd,pad); if (live!=0){ lcd.clear(); - waitExpect=((float)_length/3)+5;//set wait so the game is speeding up + waitExpect=((float)_length/4)+6; waitTime=(1/(waitExpect+_speed)); wait(waitTime); - _food.drawfood(lcd);//make first food - lcd.drawRect(0,8,84,48-8,FILL_TRANSPARENT);//draw arena + _food.drawfood(lcd); + lcd.drawRect(0,8,84,48-9,FILL_TRANSPARENT); drawscore(lcd); - for ( int i=0; _length>i;i++){//draw snake + for ( int i=0; i < _length ;i++){ lcd.setPixel(snak._x[i],snak._y[i]); - - lcd.refresh(); } } @@ -74,100 +72,98 @@ 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]++; + d=pad.get_direction(); + if (d==N){ + + if (snak._dirc[_length-1]!=4) + { + snak._dirc[_length-1] = 2; + snak._dirc[_length] = 2; + } + + } + + if (d==E){ + + if (snak._dirc[_length-1]!=3) + { + snak._dirc[_length-1] = 1; + snak._dirc[_length] = 1; + } + + } + + if (d==W){ + + if (snak._dirc[_length-1]!=1) + { + snak._dirc[_length-1] = 3; + snak._dirc[_length] = 3; + } + + + } + + if (d==S){ + + if (snak._dirc[_length-1]!=2) + { + snak._dirc[_length-1] = 4; + snak._dirc[_length] = 4; + } + + + } + + if (snak._dirc[_length-1]==1) + { + snak._x[_length] = snak._x[_length] + 1; + } if (snak._dirc[_length-1]==2) { - snak._y[_length]--; + snak._y[_length] = snak._y[_length] - 1; + } if (snak._dirc[_length-1]==3) { - snak._x[_length]--; + snak._x[_length] = snak._x[_length] - 1; + } if (snak._dirc[_length-1]==4) { - snak._y[_length]++; + snak._y[_length] = snak._y[_length] + 1; } - - for (int i=0 ;_length<i ;i++) + for (int i=0 ;i < _length ;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; - } - + snak._y[i]=snak._y[i+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 + _length=_length+1; + snak._x[_length] = snak._x[_length - 1]; + snak._y[_length] = snak._y[_length - 1]; + _food.createfood(); Tone_1(pad); } -}//end of addPoint +} int Snake::dead(N5110 &lcd, Gamepad &pad){ - live--; //take a life away - //while (live==0){//dead reset game + live--; while(live==0) { lcd.clear(); lcd.printString("Game Over",0,1); @@ -605,16 +601,16 @@ game_music(pad); dead(lcd,pad); } - if (snak._y[_length]== FLOOR ||snak._y[_length]== CEILING )//if snake hits top or bottom walls + if (snak._y[_length]== FLOOR ||snak._y[_length]== CEILING ) { 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++){ +void Snake::check_TailCollision(N5110 &lcd, Gamepad &pad){ + for (int i=0 ;i < (_length - 2) ;i++){ if (snak._x[_length-1]==snak._x[i] && snak._y[_length-1]==snak._y[i]) { @@ -624,7 +620,7 @@ } -}//end of checkTailCollision +} void Snake::game_music(Gamepad &pad){ @@ -637,14 +633,14 @@ } 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); + char buffer_1[15]; + char buffer_2[15]; + sprintf(buffer_1,"%2d",live); + sprintf(buffer_2,"%2d",_length-5); + lcd.printString(buffer_1,20,0); + lcd.printString(buffer_2,70,0); lcd.drawSprite(6,1,7,7,(int *)LIFE); lcd.printString(":",17,0); - lcd.printString("Score:",40,0);//display life and points + lcd.printString("Score:",40,0); -}//end of drawScore \ No newline at end of file +} \ No newline at end of file