![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
finished p2-2
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
Revision 2:7abebe259d59, committed 2021-04-26
- Comitter:
- levelsnake3
- Date:
- Mon Apr 26 00:30:47 2021 +0000
- Parent:
- 1:05e3f86df6d5
- Commit message:
- finalAdv;
Changed in this revision
diff -r 05e3f86df6d5 -r 7abebe259d59 compost_pile.cpp --- a/compost_pile.cpp Sat Apr 17 02:34:03 2021 +0000 +++ b/compost_pile.cpp Mon Apr 26 00:30:47 2021 +0000 @@ -30,12 +30,40 @@ void compost_add(int index) { compost_record[index].height = compost_record[index].height+ PILE_WIDTH; + compost_record[index].space = index; set_compost_size(index); if(compost_record[index].height > (TPH*11)){ TPH++; tallestPile = compost_record[index]; } } +void removeTop(void){ + if(TPH > 0){ + boundingBox holderBox; + + COMPOST compIn = compost_record[tallestPile.space]; + + holderBox.topLeft.x = compIn.x; + holderBox.topLeft.y = 120 - compIn.height; + holderBox.bottomRight.x = compIn.x + PILE_WIDTH; + holderBox.bottomRight.y = 120 - compIn.height+11; + + draw_nothing(holderBox); + + compost_record[tallestPile.space].height = compIn.height - PILE_WIDTH; + + set_compost_size(tallestPile.space); + TPH--; + + for(int i =0;i<NUM_PILE;i++){ + if(compost_record[i].height >= (TPH*11)){ + if(compost_record[i].height > (TPH*11)) TPH++; + tallestPile = compost_record[i]; + } + } + + } +} COMPOST getTallestComp(void){ return tallestPile; }
diff -r 05e3f86df6d5 -r 7abebe259d59 compost_pile_public.h --- a/compost_pile_public.h Sat Apr 17 02:34:03 2021 +0000 +++ b/compost_pile_public.h Mon Apr 26 00:30:47 2021 +0000 @@ -15,6 +15,7 @@ typedef struct { int x; ///< Bottom-left corner of the compost. x coordinate on the screen. int height; ///< The height of the compost + int space; boundingBox comBox; } COMPOST; @@ -37,6 +38,8 @@ /** Make a pile of compost one unit taller @param index The index in compost_record. It must be smaller than MAX_NUM_COMPOST_PILES. */ +void removeTop(void); + void compost_add(int index); void maintainComp(void); COMPOST getTallestComp(void);
diff -r 05e3f86df6d5 -r 7abebe259d59 fruit.cpp --- a/fruit.cpp Sat Apr 17 02:34:03 2021 +0000 +++ b/fruit.cpp Mon Apr 26 00:30:47 2021 +0000 @@ -14,6 +14,8 @@ int fruit_tick=0; +int difMod = 0; +int practice = 0; //Create a DLL for fruits DLinkedList* fruitDLL = NULL; @@ -26,12 +28,21 @@ fruitDLL = create_dlinkedlist(); return; } +void difficultyMode(int n){ + if(n == 1) difMod = 3; + else if(n == 2) difMod = -3; + else if(n == 3){ + practice = 1; + difMod = 0; + } + else difMod = 0; +} void fruit_generator(void){ fruit_tick++; // only fire the fruit at certain ticks // must detail interval of different fruits - if((fruit_tick % FRUIT_INTERVAL)==0 || fruit_tick==0){ + if((fruit_tick % (FRUIT_INTERVAL + difMod))==0 || fruit_tick==0){ //printf("fruit_create()"); fruit_create(); } @@ -43,7 +54,8 @@ FRUIT* M = (FRUIT*)malloc(sizeof(FRUIT)); // M->y = 0; //each fruit has its own tick - M->type = rand() % 3; + if(practice == 1) M->type = 5; + else M->type = rand() % 5; switch (M->type) { case 0: @@ -53,7 +65,16 @@ M->draw = draw_realOrange; break; case 2: - M->draw = draw_banana; + M->draw = drawRealBanana; + break; + case 3: + M->draw = draw_Rec; + break; + case 4: + M->draw = drawBigOrange; + break; + case 5: + M->draw = drawPractice; break; default: break; @@ -131,6 +152,12 @@ if(newFruit->type == 1){ draw_nothingOrange(newFruit->box); } + else if((newFruit->type == 0)||(newFruit->type == 3)||(newFruit->type == 5)){ + draw_nothingBomb(newFruit->box); + } + else if(newFruit->type == 4){ + nothingBigOrange(newFruit->box); + } else{ draw_nothing(newFruit->box); } @@ -148,6 +175,12 @@ if(newFruit->type == 1){ draw_nothingOrange(newFruit->box); } + else if((newFruit->type == 0)||(newFruit->type == 3)||(newFruit->type == 5)){ + draw_nothingBomb(newFruit->box); + } + else if(newFruit->type == 4){ + nothingBigOrange(newFruit->box); + } else{ draw_nothing(newFruit->box); }
diff -r 05e3f86df6d5 -r 7abebe259d59 fruit_public.h --- a/fruit_public.h Sat Apr 17 02:34:03 2021 +0000 +++ b/fruit_public.h Mon Apr 26 00:30:47 2021 +0000 @@ -41,6 +41,8 @@ /** This function draw the fruits onto the screen Call fruit_generator() repeatedly in your game-loop. ex: main() */ +void difficultyMode(int n); + void fruit_generator(void); /** This function will return a linked-list of all active fruit structures.
diff -r 05e3f86df6d5 -r 7abebe259d59 globals.h --- a/globals.h Sat Apr 17 02:34:03 2021 +0000 +++ b/globals.h Mon Apr 26 00:30:47 2021 +0000 @@ -22,6 +22,7 @@ extern wave_player waver; extern PwmOut speaker; + typedef struct { int x; int y;
diff -r 05e3f86df6d5 -r 7abebe259d59 graphics.cpp --- a/graphics.cpp Sat Apr 17 02:34:03 2021 +0000 +++ b/graphics.cpp Mon Apr 26 00:30:47 2021 +0000 @@ -9,6 +9,7 @@ #define DIRT BROWN #define RED 0xFF0000 #define ORANGE 0xFFA500 +#define GRAY 0xA0A0A0 void draw_img(int u, int v, const char* img) { int colors[11*11]; @@ -18,8 +19,6 @@ else if (img[i] == 'Y') colors[i] = YELLOW; else if (img[i] == 'G') colors[i] = GREEN; else if (img[i] == 'D') colors[i] = DIRT; - // else if (img[i] == '5') colors[i] = LGREY; - // else if (img[i] == '3') colors[i] = DGREY; else colors[i] = BLACK; } uLCD.BLIT(u, v, 11, 11, colors); @@ -28,14 +27,33 @@ void draw_player(int u, int v, int key) { - uLCD.filled_rectangle(u, v, u+11, v+11, key); + uLCD.filled_rectangle(u, v+2, u+11, v+4, WHITE); + uLCD.filled_rectangle(u, v+4, u+11, v+11, BLUE); + uLCD.filled_rectangle(u, v+7, u+3, v+9, BLACK); + uLCD.filled_rectangle(u+8, v+7, u+11, v+9, BLACK); + if(key == 0x000000) uLCD.filled_rectangle(u, v, u+11, v+11, key); + } void draw_scoreBoard(int score){ - uLCD.rectangle(0, 0, 120, 10, BLACK); uLCD.locate(0, 15); uLCD.color(WHITE); - uLCD.printf("Score: %d",score); + uLCD.printf("S:%d ",score); +} +void draw_scoreMisses(int misses){ + uLCD.locate(5, 15); + uLCD.color(WHITE); + uLCD.printf("M:%d ",misses); +} +void draw_scoreCombo(int combo){ + uLCD.locate(10, 15); + uLCD.color(WHITE); + uLCD.printf("C:%d ",combo); +} +void drawPause(void){ + uLCD.locate(3, 15); + uLCD.color(WHITE); + uLCD.printf("GAME PAUSED"); } void draw_ground(){ @@ -45,21 +63,48 @@ void draw_nothing(boundingBox b) { - // Erase a bounding box uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.bottomRight.x, b.bottomRight.y, BLACK); } +void draw_nothing2(boundingBox b) +{ + uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.bottomRight.x, b.bottomRight.y, GRAY); +} void draw_nothingOrange(boundingBox b) { // Erase a bounding box - uLCD.filled_circle(b.topLeft.x,b.bottomRight.y+2, 6, BLACK); - uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y+4, b.topLeft.x+4, b.topLeft.y+7, BLACK); + uLCD.filled_circle(b.topLeft.x,b.bottomRight.y, 6, BLACK); + uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y+2, b.topLeft.x+3, b.topLeft.y+4, BLACK); } +void drawPractice(boundingBox b){ + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 6, RED); + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 4, WHITE); + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 2, RED); +} void draw_bomb(boundingBox b) { + // Draw an apple using a filled_rectangle or get creative and use a sprite! - uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.bottomRight.x, b.bottomRight.y, RED); + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 6, GRAY); + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 2, RED); + uLCD.filled_rectangle(b.topLeft.x-1, b.topLeft.y-8, b.topLeft.x+1, b.topLeft.y-6, BROWN); + uLCD.filled_rectangle(b.topLeft.x-1, b.topLeft.y-9, b.topLeft.x+1, b.topLeft.y-8, RED); + + } +void draw_nothingBomb(boundingBox b){ + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 6, BLACK); + uLCD.filled_rectangle(b.topLeft.x-1, b.topLeft.y-8, b.topLeft.x+1, b.topLeft.y-6, BLACK); + uLCD.filled_rectangle(b.topLeft.x-1, b.topLeft.y-9, b.topLeft.x+1, b.topLeft.y-8, BLACK); + +} +void draw_Rec(boundingBox b){ + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 6, GRAY); + uLCD.filled_circle(b.topLeft.x,b.topLeft.y, 2, GREEN); + uLCD.filled_rectangle(b.topLeft.x-1, b.topLeft.y-8, b.topLeft.x+1, b.topLeft.y-6, BROWN); + uLCD.filled_rectangle(b.topLeft.x-1, b.topLeft.y-9, b.topLeft.x+1, b.topLeft.y-8, RED); +} + void draw_banana(boundingBox b) { @@ -69,33 +114,29 @@ void draw_realOrange(boundingBox b) { // Draw a banana using a filled_rectangle or get creative and use a sprite! - uLCD.filled_circle(b.topLeft.x,b.bottomRight.y+2, 6, ORANGE); - uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y+4, b.topLeft.x+4, b.topLeft.y+7, GREEN); + uLCD.filled_circle(b.topLeft.x,b.bottomRight.y, 6, ORANGE); + uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y+2, b.topLeft.x+3, b.topLeft.y+4, GREEN); +} +void drawBigOrange(boundingBox b){ + uLCD.filled_circle(b.topLeft.x,b.bottomRight.y, 9, ORANGE); + uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.topLeft.x+4, b.topLeft.y+3, GREEN); + +} +void nothingBigOrange(boundingBox b){ + uLCD.filled_circle(b.topLeft.x,b.bottomRight.y, 9, BLACK); + uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.topLeft.x+4, b.topLeft.y+3, BLACK); + } void drawRealBanana(boundingBox b){ - draw_img(b.topLeft.x, b.topLeft.y, (" GG \n YYY \n YYY \n YYY \n YYY \n YYY ")); - /*({' ',' ',' ','g','g','g',' ',' ',' ',' ',' ', - ' ',' ',' ','g','g',' ',' ',' ',' ',' ',' ', - ' ',' ','y','y','y',' ',' ',' ',' ',' ',' ', - ' ','y','y','y',' ',' ',' ',' ',' ',' ',' ', - ' ','y','y','y',' ',' ',' ',' ',' ',' ',' ', - ' ','y','y','y',' ',' ',' ',' ',' ',' ',' ', - ' ','y','y',' ',' ',' ',' ',' ',' ',' ',' ', - ' ','y','y','y',' ',' ',' ',' ',' ',' ',' ', - ' ',' ','y','y','y',' ',' ',' ',' ',' ',' ', - ' ',' ',' ','y','y','y',' ',' ',' ',' ',' ', - ' ',' ',' ',' ','y','y','y',' ',' ',' ',' ' - }));*/ + uLCD.filled_circle(b.topLeft.x+5,b.topLeft.y+6, 4, YELLOW); + uLCD.filled_circle(b.topLeft.x+9,b.topLeft.y+6, 3, BLACK); + uLCD.filled_rectangle(b.topLeft.x+4, b.topLeft.y, b.topLeft.x+8, b.topLeft.y+1, GREEN); } -void draw_orange(boundingBox b) -{ -// Draw an orange using a filled_rectangle or get creative and use a sprite! - uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.bottomRight.x, b.bottomRight.y, ORANGE); -} void draw_knife(boundingBox b){ - - uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.bottomRight.x, b.bottomRight.y, WHITE); + uLCD.filled_rectangle(b.topLeft.x+2, b.bottomRight.y, b.topLeft.x+6, b.bottomRight.y+2, GRAY); + uLCD.filled_rectangle(b.topLeft.x+2, b.bottomRight.y+2, b.topLeft.x+4, b.bottomRight.y+3, GRAY); + uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y-1, b.topLeft.x+2, b.bottomRight.y, BROWN); } void draw_comp(boundingBox b){ uLCD.filled_rectangle(b.topLeft.x, b.topLeft.y, b.bottomRight.x, b.bottomRight.y, GREEN);
diff -r 05e3f86df6d5 -r 7abebe259d59 graphics.h --- a/graphics.h Sat Apr 17 02:34:03 2021 +0000 +++ b/graphics.h Mon Apr 26 00:30:47 2021 +0000 @@ -32,20 +32,29 @@ */ void draw_img(int u, int v, const char* img); void draw_realOrange(boundingBox b); +void drawPractice(boundingBox b); void draw_nothingOrange(boundingBox b); - void drawRealBanana(boundingBox b); - +void drawPause(void); /** * DrawFunc functions. * These can be used as the MapItem draw functions. */ void draw_scoreBoard(int score); +void draw_scoreMisses(int misses); +void draw_scoreCombo(int combo); + +void draw_nothingBomb(boundingBox b); void draw_nothing(boundingBox b); +void draw_nothing2(boundingBox b); +void drawBigOrange(boundingBox b); +void nothingBigOrange(boundingBox b); + void draw_bomb(boundingBox b); void draw_banana(boundingBox b); void draw_orange(boundingBox b); void draw_knife(boundingBox b); void draw_ground(); +void draw_Rec(boundingBox b); void draw_comp(boundingBox b); #endif // GRAPHICS_H
diff -r 05e3f86df6d5 -r 7abebe259d59 hardware.cpp --- a/hardware.cpp Sat Apr 17 02:34:03 2021 +0000 +++ b/hardware.cpp Mon Apr 26 00:30:47 2021 +0000 @@ -15,6 +15,7 @@ DigitalIn button1(p21); // Pushbuttons (pin) DigitalIn button2(p22); DigitalIn button3(p23); +DigitalIn button4(p24); AnalogOut DACout(p18); // Speaker (pin) PwmOut speaker(p25); wave_player waver(&DACout); @@ -32,6 +33,7 @@ button1.mode(PullUp); button2.mode(PullUp); button3.mode(PullUp); + button4.mode(PullUp); return ERROR_NONE; } @@ -52,6 +54,8 @@ in.b1 = button1; in.b2 = button2; in.b3 = button3; + in.b4 = button4; + pc.printf("%d , %d ,%d %lf %lf %lf\n",in.b1,in.b2,in.b3,in.ax,in.ay,in.az); return in;
diff -r 05e3f86df6d5 -r 7abebe259d59 hardware.h --- a/hardware.h Sat Apr 17 02:34:03 2021 +0000 +++ b/hardware.h Mon Apr 26 00:30:47 2021 +0000 @@ -6,7 +6,7 @@ * If additional hardware is added, new elements should be added to this struct. */ struct GameInputs { - int b1, b2, b3; // Button presses + int b1, b2, b3, b4; // Button presses double ax, ay, az; // Accelerometer readings };
diff -r 05e3f86df6d5 -r 7abebe259d59 main.cpp --- a/main.cpp Sat Apr 17 02:34:03 2021 +0000 +++ b/main.cpp Mon Apr 26 00:30:47 2021 +0000 @@ -19,22 +19,19 @@ #include "player_public.h" //SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, sck, cs -DigitalOut myled1(LED1); -DigitalOut myled2(LED2); -DigitalOut myled3(LED3); -DigitalOut myled4(LED4); // ===User implementations start=== int fruit_contact(void); // iterate through fruits and see if any collided void compost_pile_update(void); void set_random_seed(Timer); int gameOver(int); +void pause(PLAYER ply); +bool paused = false; int end = 0; +int pMode = 0; int main() { - PLAYER ply = player_get_info(); - GameInputs inputs; // First things first: initialize hardware ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); @@ -57,14 +54,21 @@ //initialize functions compost_pile_init(); fruit_init(); - player_init(); + player_init(); + + PLAYER ply = player_get_info(); + pc.printf("Initialization complete\n"); + draw_scoreBoard(ply.score); - + draw_scoreCombo(ply.combo); + draw_scoreMisses(ply.misses); + int getMiss = returnMiss(); while(1) { + t.start(); - pc.printf("score: %d",ply.score); + ply = player_get_info(); // Draw fruits first //pc.printf("fruit_generator\n"); fruit_generator(); @@ -82,9 +86,31 @@ //pc.printf("player motions\n"); if (!inputs.b1 || inputs.ay >=0.4) player_moveUp(); else if (!inputs.b2 || inputs.ay <= -0.4) - { player_moveDown(); + { + player_moveDown(); + } + else if (!inputs.b3) + { + player_throw(); + } + else if((!paused)&&(!inputs.b4)) + { + pause(ply); } - else if (!inputs.b3) player_throw(); + while(paused){ + inputs = read_inputs(); + if(!inputs.b1){ + + pause(ply); + } + } + if(ply.misses != getMiss){ + ply = player_get_info(); + draw_scoreMisses(ply.misses); + draw_scoreCombo(ply.combo); + getMiss = ply.misses; + } + player_knife_draw(); //pc.printf("compost_pile_update\n"); @@ -116,6 +142,22 @@ * with the player's knife. Don't forget to check if the "fruit" is the bomb, since we * generated bombs using the fruit_generator. */ +void pause(PLAYER ply){ + if(paused){ + paused = false; + draw_ground(); + draw_scoreBoard(ply.score); + draw_scoreCombo(ply.combo); + draw_scoreMisses(ply.misses); + } + else{ + paused = true; + draw_ground(); + drawPause(); + + } + +} int fruit_contact(void) { PLAYER ply = player_get_info(); @@ -137,7 +179,12 @@ while(current) { newFruit = (FRUIT*) current->data; - boundingBox knifePos = getKnifeBounding(); + + KNIFE knife1 = getKnifeInfo(0); + KNIFE knife2 = getKnifeInfo(1); + boundingBox knifePos1 = knife1.kbb; + boundingBox knifePos2 = knife2.kbb; + //need to check if each corner is inside the fruit /*pc.printf("\n\n\n\n\n\n\n\n %p %p\n topleft x %d %d\ntopleft y %d %d\n bottem right x %d %d\nbottem right y %d %d\n\n\n\n\n\n\n",current,current->next, @@ -147,59 +194,191 @@ newFruit->box.topLeft.y, knifePos.bottomRight.y );*/ //if(ply.player_has_knife == 0){ - if((newFruit->box.topLeft.x <= knifePos.topLeft.x) && (newFruit->box.bottomRight.x >= knifePos.topLeft.x) && (newFruit->box.bottomRight.y >= knifePos.topLeft.y) && (newFruit->box.topLeft.y <= knifePos.topLeft.y)){ + if((newFruit->box.topLeft.x <= knifePos1.topLeft.x) && (newFruit->box.bottomRight.x >= knifePos1.topLeft.x) && (newFruit->box.bottomRight.y >= knifePos1.topLeft.y) && (newFruit->box.topLeft.y <= knifePos1.topLeft.y)){ newFruit->status = FRUIT_SLICED; if(newFruit->type == 0){ - gameOver(0); + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + + } + else{ + player_score_inc(1); + ply = player_get_info(); + draw_scoreBoard(ply.score); + draw_scoreCombo(ply.combo); + } + doReturnK(0,0); + } + else if((newFruit->box.topLeft.x <= knifePos1.bottomRight.x) && (newFruit->box.bottomRight.x >= knifePos1.bottomRight.x) && (newFruit->box.bottomRight.y >= knifePos1.bottomRight.y) && (newFruit->box.topLeft.y <= knifePos1.bottomRight.y)){ + newFruit->status = FRUIT_SLICED; + if(newFruit->type == 0){ + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + } else{ - player_score_inc(); + + player_score_inc(1); + ply = player_get_info(); + draw_scoreBoard(ply.score); - } + draw_scoreCombo(ply.combo); + } + doReturnK(0,0); } - else if((newFruit->box.topLeft.x <= knifePos.bottomRight.x) && (newFruit->box.bottomRight.x >= knifePos.bottomRight.x) && (newFruit->box.bottomRight.y >= knifePos.bottomRight.y) && (newFruit->box.topLeft.y <= knifePos.bottomRight.y)){ + else if((newFruit->box.topLeft.x <= knifePos1.topLeft.x) && (newFruit->box.bottomRight.x >= knifePos1.topLeft.x) && (newFruit->box.bottomRight.y >= knifePos1.bottomRight.y) && (newFruit->box.topLeft.y <= knifePos1.bottomRight.y)){ newFruit->status = FRUIT_SLICED; - if(newFruit->type == 0){ - gameOver(0); + if(newFruit->type == 0){ + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + + } + else{ + player_score_inc(1); + ply = player_get_info(); + + draw_scoreBoard(ply.score); + draw_scoreCombo(ply.combo); + } + doReturnK(0,0); + } + else if((newFruit->box.topLeft.x <= knifePos1.bottomRight.x) && (newFruit->box.bottomRight.x >= knifePos1.bottomRight.x) && (newFruit->box.bottomRight.y >= knifePos1.topLeft.y) && (newFruit->box.topLeft.y <= knifePos1.topLeft.y)){ + newFruit->status = FRUIT_SLICED; + if(newFruit->type == 0){ + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + } else{ - player_score_inc(); + player_score_inc(1); + ply = player_get_info(); + draw_scoreBoard(ply.score); - } - } - else if((newFruit->box.topLeft.x <= knifePos.topLeft.x) && (newFruit->box.bottomRight.x >= knifePos.topLeft.x) && (newFruit->box.bottomRight.y >= knifePos.bottomRight.y) && (newFruit->box.topLeft.y <= knifePos.bottomRight.y)){ + draw_scoreCombo(ply.combo); + } + doReturnK(0,0); + } + + if((newFruit->box.topLeft.x <= knifePos2.topLeft.x) && (newFruit->box.bottomRight.x >= knifePos2.topLeft.x) && (newFruit->box.bottomRight.y >= knifePos2.topLeft.y) && (newFruit->box.topLeft.y <= knifePos2.topLeft.y)){ newFruit->status = FRUIT_SLICED; if(newFruit->type == 0){ - gameOver(0); + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + + } + else{ + player_score_inc(1); + ply = player_get_info(); + + draw_scoreBoard(ply.score); + draw_scoreCombo(ply.combo); + } + doReturnK(1,0); + } + else if((newFruit->box.topLeft.x <= knifePos2.bottomRight.x) && (newFruit->box.bottomRight.x >= knifePos2.bottomRight.x) && (newFruit->box.bottomRight.y >= knifePos2.bottomRight.y) && (newFruit->box.topLeft.y <= knifePos2.bottomRight.y)){ + newFruit->status = FRUIT_SLICED; + if(newFruit->type == 0){ + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + } else{ - player_score_inc(); + player_score_inc(1); + ply = player_get_info(); + draw_scoreBoard(ply.score); - } + draw_scoreCombo(ply.combo); + } + doReturnK(1,0); } - else if((newFruit->box.topLeft.x <= knifePos.bottomRight.x) && (newFruit->box.bottomRight.x >= knifePos.bottomRight.x) && (newFruit->box.bottomRight.y >= knifePos.topLeft.y) && (newFruit->box.topLeft.y <= knifePos.topLeft.y)){ + else if((newFruit->box.topLeft.x <= knifePos2.topLeft.x) && (newFruit->box.bottomRight.x >= knifePos2.topLeft.x) && (newFruit->box.bottomRight.y >= knifePos2.bottomRight.y) && (newFruit->box.topLeft.y <= knifePos2.bottomRight.y)){ newFruit->status = FRUIT_SLICED; if(newFruit->type == 0){ - gameOver(0); + hurt(); + } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + } else{ - player_score_inc(); + player_score_inc(1); + ply = player_get_info(); + draw_scoreBoard(ply.score); + draw_scoreCombo(ply.combo); + } + doReturnK(1,0); + } + else if((newFruit->box.topLeft.x <= knifePos2.bottomRight.x) && (newFruit->box.bottomRight.x >= knifePos2.bottomRight.x) && (newFruit->box.bottomRight.y >= knifePos2.topLeft.y) && (newFruit->box.topLeft.y <= knifePos2.topLeft.y)){ + newFruit->status = FRUIT_SLICED; + if(newFruit->type == 0){ + hurt(); } - } + else if(newFruit->type ==3){ + removeTop(); + } + else if(newFruit->type ==4){ + player_score_inc(0); + + } + else{ + player_score_inc(1); + draw_scoreBoard(ply.score); + draw_scoreCombo(ply.combo); + } + doReturnK(1,0); + } + if(dead(pMode)){ + gameOver(0); + } + int index = (((newFruit->box.topLeft.x+5)/11)%11); COMPOST compIn = compost_get_info(index); if(newFruit->box.bottomRight.y >= (115 - compIn.height)){ newFruit-> status = FRUIT_SLICED; - if(newFruit->type != 0) { + if((newFruit->type != 0)&&(newFruit->type != 3)&&(newFruit->type != 5)) { compost_add(index); - if(get_compost_tallest_height() >= 4) + if(newFruit->type == 4) compost_add(index); + if(get_compost_tallest_height() >= 6) { gameOver(0); } } } - if(ply.score == 4)gameOver(1); + if(ply.score >= 25 && pMode == 0)gameOver(1); current = current->next; } // } @@ -231,11 +410,37 @@ void set_random_seed(Timer t) { GameInputs inputs; t.start(); - uLCD.printf("Push any button to start.\n"); + uLCD.printf("Choose Difficulty to start\n\nB1.Easy\nB2.Normal\nB3.Hard\nB4.PractceMode\n"); while(1){ - inputs = read_inputs(); - if (inputs.b1 || inputs.b2 || inputs.b3) break; - } + inputs = read_inputs(); + if (!inputs.b1){ + pMode = 0; + + uLCD.baudrate(38400); + difficultyMode(1); + break; + } + else if(!inputs.b2){ + pMode = 0; + + uLCD.baudrate(56000); + difficultyMode(0); + break; + } + else if(!inputs.b3){ + pMode = 0; + + uLCD.baudrate(128000); + difficultyMode(2); + break; + } + else if(!inputs.b4){ + uLCD.baudrate(56000); + pMode = 1; + difficultyMode(3); + break; + } +} uLCD.cls(); t.stop(); int seed = t.read_ms();
diff -r 05e3f86df6d5 -r 7abebe259d59 player.cpp --- a/player.cpp Sat Apr 17 02:34:03 2021 +0000 +++ b/player.cpp Mon Apr 26 00:30:47 2021 +0000 @@ -9,108 +9,189 @@ //================================================================== #include "player_private.h" +extern DigitalOut myled1(LED1); +extern DigitalOut myled2(LED2); +extern DigitalOut myled3(LED3); +extern DigitalOut myled4(LED4); PLAYER player; // structure of player /* -typedef struct { - int y; // The y-coordinate of knife current position - int height; - int score; // Current score - int knife_length; + int id; + int kPos; + bool PHK // The knife status: 0->In the air 1->With Player boundingBox knife_boundingBox; - bool player_has_knife; // The knife status: 0->In the air 1->With Player -} PLAYER;*/ + int active; +}KNIFE; + + int score; // Current score + int lives; + int y; + KNIFE knife[2]; +} PLAYER; +*/ PLAYER player_get_info(void){ return player; } +int returnMiss(void){ + return player.misses; +} +int returnCK(void){ + + if(player.knife[0].PHK == 1) + { + return player.knife[0].id; + } + else if (player.knife[1].PHK == 1) + { + + return player.knife[1].id; + } + return 0; +} +int getLives(void){ + return player.lives; +} +bool dead(int n){ + if((n == 0)&&((player.misses == 7)||(player.lives == 0))) return true; + else return false; +} +void miss(void){ + player.misses++; +} +void hurt(void){ + player.combo = 1; + player.lives--; + if(player.lives == 2) myled3 = 0; + if (player.lives == 1) myled2 = 0; + if(player.lives ==0) myled1= 0; +} void player_init(void) { + player.combo = 1; + player.misses = 0; player.y = 0; player.score = 0; + player.lives = 3; + + myled1 =1; + myled2 = 1; + myled3 = 1; - player.knife_length = 11; - player.knife_boundingBox.topLeft.x = player.knife_length; - player.knife_boundingBox.topLeft.y = 4; + draw_player(0,player.y,0xFFFFFF); - player.knife_boundingBox.bottomRight.x = player.knife_length + KNIFE_LENGTH; - player.knife_boundingBox.bottomRight.y = 0; - player.player_has_knife = 1; - draw_player(0,player.y,0xFFFFFF); - draw_knife(player.knife_boundingBox); + knifeInit(0); + player.knife[1].PHK = 2; +} + +void knifeInit(int n){ + player.knife[n].id = n+1; + player.knife[n].kPos = 11; + player.knife[n].kbb.topLeft.x = 11; + player.knife[n].kbb.topLeft.y = player.y + 4; + player.knife[n].kbb.bottomRight.x = 11 + KNIFE_LENGTH; + player.knife[n].kbb.bottomRight.y = player.y; + player.knife[n].PHK = 1; + draw_knife(player.knife[n].kbb); + } void player_moveUp(void) { draw_player(0,player.y,0x000000); - draw_nothing(player.knife_boundingBox); + if(returnCK() != 0) draw_nothing(player.knife[returnCK()-1].kbb); COMPOST compIn = getTallestComp(); if(player.y < (110 - compIn.height)){//what is the max height player.y = player.y + 10; - if(player.player_has_knife){ - player.knife_boundingBox.topLeft.y = player.y + 4; - player.knife_boundingBox.bottomRight.y = player.y; + if(returnCK() != 0){ + player.knife[returnCK()-1].kbb.topLeft.y = player.y + 4; + player.knife[returnCK()-1].kbb.bottomRight.y = player.y; } } draw_player(0,player.y,0xFFFFFF); - draw_knife(player.knife_boundingBox); + if(returnCK() != 0) draw_knife(player.knife[returnCK()-1].kbb); } void player_moveDown(void) { draw_player(0,player.y,0x000000); - draw_nothing(player.knife_boundingBox); + if(returnCK() != 0) draw_nothing(player.knife[returnCK()-1].kbb); if(player.y > 0){ player.y = player.y - 10; - if(player.player_has_knife){ - player.knife_boundingBox.topLeft.y = player.y + 4; - player.knife_boundingBox.bottomRight.y = player.y; + if(returnCK() != 0){ + player.knife[returnCK()-1].kbb.topLeft.y = player.y + 4; + player.knife[returnCK()-1].kbb.bottomRight.y = player.y; } } draw_player(0,player.y,0xFFFFFF); - draw_knife(player.knife_boundingBox); + if(returnCK() != 0) draw_knife(player.knife[returnCK()-1].kbb); +} - +void doReturnK(int n, int m){ + draw_nothing(player.knife[n].kbb); + player.knife[n].PHK = 2; + player.knife[n].kPos = 0; + if(m == 1){ + miss(); + player.combo = 1; + } + if((player.knife[0].PHK == 2)&&(player.knife[1].PHK == 2)) knifeInit(0); + pc.printf("THHHHHHHHHHHHHHHHHHHISHSIHSIHSIHSIH %d\n\n\n",player.combo); } void player_throw(void) { - if(player.player_has_knife == 1){//what is the max height - player.player_has_knife = 0; + + if(returnCK() != 0){//checkning which knife should be thrown + + player.knife[returnCK()-1].PHK = 0; } } -void player_knife_draw(void) { - if(player.player_has_knife == 0 && player.knife_length+6 < 120){//what is the max height - - draw_nothing(player.knife_boundingBox); +void player_knife_draw(void) { + if(player.knife[0].PHK == 0 && player.knife[0].kPos+6 < 120){//what is the max height + draw_nothing(player.knife[0].kbb); + + player.knife[0].kPos = player.knife[0].kPos + 5 ; + player.knife[0].kbb.topLeft.x = player.knife[0].kPos; + player.knife[0].kbb.bottomRight.x = player.knife[0].kPos + KNIFE_LENGTH; - player.knife_boundingBox.topLeft.x = player.knife_length; - player.knife_boundingBox.bottomRight.x = player.knife_length + KNIFE_LENGTH; + draw_knife(player.knife[0].kbb); + } + else if(player.knife[0].kPos+6 >= 120){ + doReturnK(0,1); + } + if((player.knife[1].PHK == 2)&&(player.knife[0].kPos+6 >= 60)){ + knifeInit(1); + - player.knife_length = player.knife_length + 5 ; - draw_knife(player.knife_boundingBox); - player.player_has_knife = 0; } -else if(player.knife_length+6 >= 120){ - draw_nothing(player.knife_boundingBox); - player_knife_return(); + if(player.knife[1].PHK == 0 && player.knife[1].kPos+6 < 120){//what is the max height + + draw_nothing(player.knife[1].kbb); + + player.knife[1].kPos = player.knife[1].kPos + 5 ; + player.knife[1].kbb.topLeft.x = player.knife[1].kPos; + player.knife[1].kbb.bottomRight.x = player.knife[1].kPos + KNIFE_LENGTH; + + draw_knife(player.knife[1].kbb); + } + else if(player.knife[1].kPos+6 >= 120){ + doReturnK(1,1); + } + if((player.knife[0].PHK == 2)&&(player.knife[1].kPos+6 >= 60)){ + + knifeInit(0); + } } -boundingBox getKnifeBounding(void){ - return player.knife_boundingBox; -} -void player_knife_return(void) { - player.player_has_knife = 1; - player.knife_length = 11; - player.knife_boundingBox.topLeft.x = player.knife_length; - player.knife_boundingBox.bottomRight.x = player.knife_length+ KNIFE_LENGTH; + - player.knife_boundingBox.topLeft.y = player.y + 4; - player.knife_boundingBox.bottomRight.y = player.y; - - draw_knife(player.knife_boundingBox); - +KNIFE getKnifeInfo(int n){ + return player.knife[n]; } // ==== player_private.h implementation ==== -void player_score_inc(void) { - player.score++; +void player_score_inc(int n) { + + pc.printf("THHHHHHHHHHHHfffffffffffffffffffffIHSIHSIH %d\n\n\n",player.combo); + player.score = player.score + player.combo; + if(n == 1) player.combo++; }
diff -r 05e3f86df6d5 -r 7abebe259d59 player_public.h --- a/player_public.h Sat Apr 17 02:34:03 2021 +0000 +++ b/player_public.h Mon Apr 26 00:30:47 2021 +0000 @@ -11,17 +11,27 @@ #define PLAYER_PUBLIC_H #include "doubly_linked_list.h" +typedef struct{ + int id; + int kPos; + int PHK; // The knife status: 0->In the air 1->With Player + boundingBox kbb; +}KNIFE; typedef struct { - int y; // The y-coordinate of knife current position - int score; // Current score - int knife_length; - boundingBox knife_boundingBox; - bool player_has_knife; // The knife status: 0->In the air 1->With Player + int score; // Current score + int lives; + int combo; + int misses; + int y; + KNIFE knife[2]; } PLAYER; typedef void (*DrawFunc)(boundingBox b); +void miss(void); +void hurt(void); +bool dead(int n ); /** getter for user to acquire info without accessing structure */ @@ -35,6 +45,7 @@ * "player.height" pixels up */ void player_moveUp(void); +int returnMiss(void); /** Check if player's current location is valid, then move the player * "player.height" pixels down @@ -54,14 +65,20 @@ */ void player_draw(int color); -boundingBox getKnifeBounding(void); +KNIFE getKnifeInfo(int n); /** Reset knife's coordinates so that it comes back to the player's position, * then redraw the knife */ -void player_knife_return(void); +void player_knife_return(int n); /** Increment the score by 1 */ -void player_score_inc(void); +void player_score_inc(int n); +void knifeInit(int n); +int returnKnife(int n); +int getLives(void); +void knifeInit(int n); +void activeKnife(int n, int a); +void doReturnK(int n, int m); #endif //PLAYER_PUBLIC_H \ No newline at end of file