![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
baseline features - a little buggy
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
fruit.cpp@2:cf4e61c051a4, 2021-04-10 (annotated)
- Committer:
- robbiehuey
- Date:
- Sat Apr 10 02:08:02 2021 +0000
- Revision:
- 2:cf4e61c051a4
- Parent:
- 1:4421c1e849e9
hi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DCchico |
0:95264f964374 | 1 | //================================================================= |
DCchico |
0:95264f964374 | 2 | // Implementation of fruit module. |
DCchico |
0:95264f964374 | 3 | // |
DCchico |
0:95264f964374 | 4 | // Copyright 2020 Georgia Tech. All rights reserved. |
DCchico |
0:95264f964374 | 5 | // The materials provided by the instructor in this course are for |
DCchico |
0:95264f964374 | 6 | // the use of the students currently enrolled in the course. |
DCchico |
0:95264f964374 | 7 | // Copyrighted course materials may not be further disseminated. |
DCchico |
0:95264f964374 | 8 | // This file must not be made publicly available anywhere. |
DCchico |
0:95264f964374 | 9 | //================================================================== |
DCchico |
0:95264f964374 | 10 | |
DCchico |
0:95264f964374 | 11 | #include "fruit_private.h" |
DCchico |
0:95264f964374 | 12 | #include "doubly_linked_list.h" |
DCchico |
0:95264f964374 | 13 | |
DCchico |
0:95264f964374 | 14 | int fruit_tick=0; |
DCchico |
0:95264f964374 | 15 | |
DCchico |
0:95264f964374 | 16 | //Create a DLL for fruits |
DCchico |
0:95264f964374 | 17 | DLinkedList* fruitDLL = NULL; |
DCchico |
0:95264f964374 | 18 | |
DCchico |
0:95264f964374 | 19 | void fruit_init(void) |
DCchico |
0:95264f964374 | 20 | { |
DCchico |
0:95264f964374 | 21 | //Create a new doubly linked list of fruits |
DCchico |
0:95264f964374 | 22 | fruitDLL = create_dlinkedlist(); |
DCchico |
0:95264f964374 | 23 | } |
DCchico |
0:95264f964374 | 24 | |
DCchico |
0:95264f964374 | 25 | void fruit_generator(void){ |
DCchico |
0:95264f964374 | 26 | fruit_tick++; |
DCchico |
0:95264f964374 | 27 | // only fire the fruit at certain ticks |
robbiehuey | 1:4421c1e849e9 | 28 | if((fruit_tick % 5)==0 || fruit_tick==0){ |
DCchico |
0:95264f964374 | 29 | //printf("fruit_create()"); |
DCchico |
0:95264f964374 | 30 | fruit_create(); |
DCchico |
0:95264f964374 | 31 | } |
DCchico |
0:95264f964374 | 32 | // update the fruits and draw them |
DCchico |
0:95264f964374 | 33 | fruit_update_position(); |
DCchico |
0:95264f964374 | 34 | } |
DCchico |
0:95264f964374 | 35 | |
DCchico |
0:95264f964374 | 36 | void fruit_create(void){ |
DCchico |
0:95264f964374 | 37 | FRUIT* M = (FRUIT*)malloc(sizeof(FRUIT)); |
DCchico |
0:95264f964374 | 38 | // M->y = 0; |
DCchico |
0:95264f964374 | 39 | //each fruit has its own tick |
DCchico |
0:95264f964374 | 40 | M->type = rand() % 3; |
DCchico |
0:95264f964374 | 41 | switch (M->type) |
DCchico |
0:95264f964374 | 42 | { |
DCchico |
0:95264f964374 | 43 | case 0: |
DCchico |
0:95264f964374 | 44 | M->draw = draw_bomb; |
DCchico |
0:95264f964374 | 45 | break; |
DCchico |
0:95264f964374 | 46 | case 1: |
DCchico |
0:95264f964374 | 47 | M->draw = draw_orange; |
DCchico |
0:95264f964374 | 48 | break; |
DCchico |
0:95264f964374 | 49 | case 2: |
DCchico |
0:95264f964374 | 50 | M->draw = draw_banana; |
DCchico |
0:95264f964374 | 51 | break; |
DCchico |
0:95264f964374 | 52 | default: |
DCchico |
0:95264f964374 | 53 | break; |
DCchico |
0:95264f964374 | 54 | } |
DCchico |
0:95264f964374 | 55 | M->tick = 0; |
DCchico |
0:95264f964374 | 56 | //set a random source for the fruit |
robbiehuey | 1:4421c1e849e9 | 57 | M->direction = 0;//rand() % 3; |
DCchico |
0:95264f964374 | 58 | if (M->direction == 0){ |
DCchico |
0:95264f964374 | 59 | M->source = rand() % (SIZE_X - FRUIT_SIZE - PLAYER_SPACE); |
DCchico |
0:95264f964374 | 60 | //set a random target for the fruit |
DCchico |
0:95264f964374 | 61 | M->target = rand() % (SIZE_X - FRUIT_SIZE - PLAYER_SPACE); |
DCchico |
0:95264f964374 | 62 | //the fruit starts at its source |
DCchico |
0:95264f964374 | 63 | M->box.topLeft.x = M->source + PLAYER_SPACE; |
robbiehuey | 1:4421c1e849e9 | 64 | M->box.topLeft.y = 10; // = {M->source + PLAYER_SPACE, 0}; |
DCchico |
0:95264f964374 | 65 | M->box.bottomRight.x = M->source + FRUIT_SIZE + PLAYER_SPACE; |
robbiehuey | 1:4421c1e849e9 | 66 | M->box.bottomRight.y = FRUIT_SIZE+10; |
DCchico |
0:95264f964374 | 67 | //M->box.bottomRight = {M->source + FRUIT_SIZE + PLAYER_SPACE, FRUIT_SIZE}; |
DCchico |
0:95264f964374 | 68 | double diagnal = sqrt((M->source - M->target)*(M->source - M->target) + SIZE_Y*SIZE_Y); |
DCchico |
0:95264f964374 | 69 | M->delta_x = (M->target - M->source) / diagnal; |
DCchico |
0:95264f964374 | 70 | M->delta_y = fabs(SIZE_Y / diagnal); |
DCchico |
0:95264f964374 | 71 | } |
DCchico |
0:95264f964374 | 72 | else if(M->direction == 1){ |
DCchico |
0:95264f964374 | 73 | M->source = rand() % (SIZE_Y - FRUIT_SIZE); |
DCchico |
0:95264f964374 | 74 | //set a random target for the fruit |
DCchico |
0:95264f964374 | 75 | M->target = rand() % (SIZE_Y - FRUIT_SIZE); |
DCchico |
0:95264f964374 | 76 | M->box.topLeft.x = PLAYER_SPACE; |
DCchico |
0:95264f964374 | 77 | M->box.topLeft.y = M->source; |
DCchico |
0:95264f964374 | 78 | //M->box.topLeft = {PLAYER_SPACE, M->source}; |
DCchico |
0:95264f964374 | 79 | M->box.bottomRight.x = PLAYER_SPACE + FRUIT_SIZE; |
DCchico |
0:95264f964374 | 80 | M->box.bottomRight.y = M->source + FRUIT_SIZE; |
DCchico |
0:95264f964374 | 81 | //M->box.bottomRight = {PLAYER_SPACE + FRUIT_SIZE, M->source + FRUIT_SIZE}; |
DCchico |
0:95264f964374 | 82 | double diagnal = sqrt((M->source - M->target)*(M->source - M->target) + (SIZE_X - PLAYER_SPACE)*(SIZE_X - PLAYER_SPACE)); |
DCchico |
0:95264f964374 | 83 | M->delta_x = (SIZE_X - PLAYER_SPACE) / diagnal; |
DCchico |
0:95264f964374 | 84 | M->delta_y = fabs((M->target - M->source) / diagnal); |
DCchico |
0:95264f964374 | 85 | }else{ |
DCchico |
0:95264f964374 | 86 | M->source = rand() % (SIZE_Y - FRUIT_SIZE); |
DCchico |
0:95264f964374 | 87 | //set a random target for the fruit |
DCchico |
0:95264f964374 | 88 | M->target = rand() % (SIZE_Y - FRUIT_SIZE); |
DCchico |
0:95264f964374 | 89 | M->box.topLeft.x = PLAYER_SPACE + SIZE_X - FRUIT_SIZE; |
DCchico |
0:95264f964374 | 90 | M->box.topLeft.y = M->source; |
DCchico |
0:95264f964374 | 91 | //M->box.topLeft = {PLAYER_SPACE + SIZE_X - FRUIT_SIZE, M->source}; |
DCchico |
0:95264f964374 | 92 | M->box.bottomRight.x = PLAYER_SPACE + SIZE_X; |
DCchico |
0:95264f964374 | 93 | M->box.bottomRight.y = M->source + FRUIT_SIZE; |
DCchico |
0:95264f964374 | 94 | //M->box.bottomRight = {PLAYER_SPACE + SIZE_X, M->source + FRUIT_SIZE}; |
DCchico |
0:95264f964374 | 95 | double diagnal = sqrt((M->source - M->target)*(M->source - M->target) + (SIZE_X - PLAYER_SPACE)*(SIZE_X - PLAYER_SPACE)); |
DCchico |
0:95264f964374 | 96 | M->delta_x = (PLAYER_SPACE - SIZE_X) / diagnal; |
DCchico |
0:95264f964374 | 97 | M->delta_y = fabs((M->target - M->source) / diagnal); |
DCchico |
0:95264f964374 | 98 | } |
DCchico |
0:95264f964374 | 99 | |
DCchico |
0:95264f964374 | 100 | |
DCchico |
0:95264f964374 | 101 | M->status = FRUIT_ACTIVE; |
DCchico |
0:95264f964374 | 102 | |
DCchico |
0:95264f964374 | 103 | insertHead(fruitDLL, M); |
DCchico |
0:95264f964374 | 104 | } |
DCchico |
0:95264f964374 | 105 | |
DCchico |
0:95264f964374 | 106 | void fruit_update_position(void){ |
DCchico |
0:95264f964374 | 107 | |
DCchico |
0:95264f964374 | 108 | |
DCchico |
0:95264f964374 | 109 | //controls how fast the fruit will move |
DCchico |
0:95264f964374 | 110 | int rate = FRUIT_SPEED; |
DCchico |
0:95264f964374 | 111 | //delta_x and delta_y account for the slope of the fruit |
DCchico |
0:95264f964374 | 112 | DrawFunc draw = NULL; |
DCchico |
0:95264f964374 | 113 | LLNode* current = fruitDLL->head; |
DCchico |
0:95264f964374 | 114 | FRUIT* newFruit; |
DCchico |
0:95264f964374 | 115 | //iterate over all fruits |
DCchico |
0:95264f964374 | 116 | while(current) |
DCchico |
0:95264f964374 | 117 | { newFruit = (FRUIT*) current->data; |
DCchico |
0:95264f964374 | 118 | if(newFruit->status == FRUIT_SLICED || |
DCchico |
0:95264f964374 | 119 | newFruit->box.topLeft.x > 127 || |
DCchico |
0:95264f964374 | 120 | newFruit->box.bottomRight.x < 0 || |
DCchico |
0:95264f964374 | 121 | newFruit->box.bottomRight.y < 0) |
DCchico |
0:95264f964374 | 122 | { |
DCchico |
0:95264f964374 | 123 | //cover the last fruit location |
DCchico |
0:95264f964374 | 124 | draw_nothing(newFruit->box); |
DCchico |
0:95264f964374 | 125 | // clear the fruit on the screen |
DCchico |
0:95264f964374 | 126 | draw = NULL; |
DCchico |
0:95264f964374 | 127 | // Remove it from the list |
DCchico |
0:95264f964374 | 128 | //pc.printf("deleting fruit node...\n"); |
DCchico |
0:95264f964374 | 129 | deleteNode(fruitDLL, current); |
DCchico |
0:95264f964374 | 130 | //pc.printf("fruit node deleted.\n"); |
DCchico |
0:95264f964374 | 131 | } |
DCchico |
0:95264f964374 | 132 | else |
DCchico |
0:95264f964374 | 133 | { |
DCchico |
0:95264f964374 | 134 | //cover the last fruit location |
DCchico |
0:95264f964374 | 135 | draw_nothing(newFruit->box); |
DCchico |
0:95264f964374 | 136 | |
DCchico |
0:95264f964374 | 137 | // update fruit position |
DCchico |
0:95264f964374 | 138 | |
DCchico |
0:95264f964374 | 139 | //pc.printf("%f, %f\n", newFruit->delta_x, newFruit->delta_y); |
DCchico |
0:95264f964374 | 140 | newFruit->box.topLeft.x += rate*newFruit->delta_x; |
DCchico |
0:95264f964374 | 141 | newFruit->box.topLeft.y += rate*newFruit->delta_y; |
DCchico |
0:95264f964374 | 142 | newFruit->box.bottomRight.x += rate*newFruit->delta_x; |
DCchico |
0:95264f964374 | 143 | newFruit->box.bottomRight.y += rate*newFruit->delta_y; |
DCchico |
0:95264f964374 | 144 | //pc.printf(" %f, %f", newFruit->delta_x, newFruit->delta_y); |
DCchico |
0:95264f964374 | 145 | // draw fruit |
DCchico |
0:95264f964374 | 146 | draw = newFruit->draw; |
DCchico |
0:95264f964374 | 147 | //update fruit's internal tick |
DCchico |
0:95264f964374 | 148 | newFruit->tick++; |
DCchico |
0:95264f964374 | 149 | //current->data = (void*) newFruit; |
DCchico |
0:95264f964374 | 150 | } |
DCchico |
0:95264f964374 | 151 | // Advance the loop |
DCchico |
0:95264f964374 | 152 | if(draw) draw(newFruit->box); |
DCchico |
0:95264f964374 | 153 | current = current->next; |
DCchico |
0:95264f964374 | 154 | } |
DCchico |
0:95264f964374 | 155 | } |
DCchico |
0:95264f964374 | 156 | |
DCchico |
0:95264f964374 | 157 | DLinkedList* get_fruit_list() { |
robbiehuey | 1:4421c1e849e9 | 158 | fruit_update_position(); |
robbiehuey | 1:4421c1e849e9 | 159 | return fruitDLL; |
DCchico |
0:95264f964374 | 160 | } |