Li Ruofan 201199450
Dependencies: mbed
Diff: Snake/Snake.cpp
- Revision:
- 1:bd7c99a5bd10
- Parent:
- 0:80a59a49d504
--- a/Snake/Snake.cpp Sat May 23 19:01:15 2020 +0000 +++ b/Snake/Snake.cpp Sun May 24 08:16:35 2020 +0000 @@ -26,38 +26,37 @@ } pos Snake::getPos(){ - return apple; //set apple's pos + return apple; } //end producing -//***Functions***// -void Snake::death(N5110 &lcd){ +void Snake::gameover(N5110 &lcd){ - Life--; + Life--; for (;Life==0;){ lcd.clear(); - lcd.printString(" Dead ",0,1); - lcd.printString(" Score: ",0,2); + lcd.printString(" You dead ",0,1); + lcd.printString(" Score: ",0,3); char buffer[2]; sprintf(buffer,"%2d",L-6); - lcd.printString(buffer,60,2); + lcd.printString(buffer,60,3); - lcd.printString(" Reset to New",0,4); - + lcd.printString(" Reset the pad",0,4); + lcd.printString(" to restart ",0,5); lcd.refresh(); - } - - init(beginX,beginY,beginL,Life); - -} + } + init(beginX,beginY,beginL,Life); + + } //Reset the pad to restart the game. + void Snake::WallMeet(N5110 &lcd){ if (greedy.bodyX[L]==84 || greedy.bodyX[L]==0 //snake is also called greedy in this game || greedy.bodyY[L]==0 || greedy.bodyY[L]==48 ) //if greedy touch the wall { - death(lcd); + gameover(lcd); } }//end WallMeet @@ -67,7 +66,7 @@ while (i < L - 1){ if (greedy.bodyY[L-1]==greedy.bodyY[i] && greedy.bodyX[L-1]==greedy.bodyX[i] ) { - death(lcd); + gameover(lcd); } i++; } @@ -101,13 +100,13 @@ }//end of init void Snake::draw(N5110 &lcd){ - WallMeet(lcd); // if head==wall game over - checkBodyMeet(lcd); //checking body whether meet wall + WallMeet(lcd); + checkBodyMeet(lcd); //checke if greedy touch the wall if (Life != 0 ){ lcd.clear(); wait(0.15-L*0.001); - drawApple(lcd);//make first apple - lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);//draw battlefield + drawApple(lcd);//produce apple + lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);//create battlefield int i = 0; while (i < L){ lcd.setPixel(greedy.bodyX[i],greedy.bodyY[i]); @@ -173,6 +172,7 @@ printf("updated "); + if(eatApple()){ greedy.bodyX[L] = greedy.bodyX[L+1]; greedy.bodyY[L] = greedy.bodyY[L+1];