Mochu Yao explorer game
Dependencies: mbed
item/item.h@26:4d193529b447, 2020-04-28 (annotated)
- Committer:
- el17my
- Date:
- Tue Apr 28 17:39:26 2020 +0000
- Revision:
- 26:4d193529b447
- Parent:
- 23:7be9701fc1b8
- Child:
- 28:4a1260ad0346
4.29
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17my | 1:ed745421d8c4 | 1 | #ifndef ITEM_H |
el17my | 1:ed745421d8c4 | 2 | #define ITEM_H |
el17my | 1:ed745421d8c4 | 3 | |
el17my | 1:ed745421d8c4 | 4 | #include "mbed.h" |
el17my | 26:4d193529b447 | 5 | |
el17my | 23:7be9701fc1b8 | 6 | /** item Class |
el17my | 23:7be9701fc1b8 | 7 | * @1 generate an item in random position after one disappear |
el17my | 23:7be9701fc1b8 | 8 | * @2 build the item's structer |
el17my | 23:7be9701fc1b8 | 9 | * @3 build a barrier to increase difficulty |
el17my | 23:7be9701fc1b8 | 10 | * @date April 13th 2020 |
el17my | 23:7be9701fc1b8 | 11 | * @author Yaomochu |
el17my | 1:ed745421d8c4 | 12 | |
el17my | 10:559487aac60e | 13 | |
el17my | 10:559487aac60e | 14 | @code |
el17my | 10:559487aac60e | 15 | |
el17my | 10:559487aac60e | 16 | #include "mbed.h" |
el17my | 10:559487aac60e | 17 | #include "N5110.h" |
el17my | 10:559487aac60e | 18 | #include "Gamepad.h" |
el17my | 10:559487aac60e | 19 | #include "item.h" |
el17my | 10:559487aac60e | 20 | #include <cstdlib> //the standard library by which we need the rand() and srand() function |
el17my | 10:559487aac60e | 21 | #include <ctime> //in order to generate random number we need <ctime> |
el17my | 10:559487aac60e | 22 | |
el17my | 10:559487aac60e | 23 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17my | 10:559487aac60e | 24 | Gamepad gamepad; |
el17my | 10:559487aac60e | 25 | item _item; |
el17my | 10:559487aac60e | 26 | //the position of the player is essential |
el17my | 10:559487aac60e | 27 | //the player's position at first can not be same with the item, so I init the player's position and the item's position together |
el17my | 10:559487aac60e | 28 | int _player_x; |
el17my | 10:559487aac60e | 29 | int _player_y; |
el17my | 10:559487aac60e | 30 | int _player_score; |
el17my | 10:559487aac60e | 31 | bool _collision_flag;// to see if the player can get a score |
el17my | 10:559487aac60e | 32 | |
el17my | 10:559487aac60e | 33 | int main() { |
el17my | 10:559487aac60e | 34 | _item.init();// use this to generate the item |
el17my | 10:559487aac60e | 35 | srand(time(NULL));// generate random values |
el17my | 10:559487aac60e | 36 | _player_score = 0;// set player score to 0; |
el17my | 23:7be9701fc1b8 | 37 | _player_x = 40; |
el17my | 17:1b4ecc01b79f | 38 | _player_y = 10;//make sure they are not in the same position at first,but player on the underlevel |
el17my | 10:559487aac60e | 39 | while(1) { |
el17my | 10:559487aac60e | 40 | |
el17my | 10:559487aac60e | 41 | // becasue the module of the player is big so it has to be make sure that |
el17my | 10:559487aac60e | 42 | //the item will be collected when the edge of the module collide with the item |
el17my | 19:14c5427b30d1 | 43 | if ((_player_x == _item.get_item_x() |
el17my | 19:14c5427b30d1 | 44 | && (_player_y == _item.get_item_y() - 8)) { |
el17my | 10:559487aac60e | 45 | _collision_flag = true; |
el17my | 10:559487aac60e | 46 | _player_score++; |
el17my | 10:559487aac60e | 47 | _item.set_item(((rand()%80)+ 5) , ((rand()%80)+ 5)); // use the rand()%m function to generate a number from 80 to 1 |
el17my | 10:559487aac60e | 48 | // on a constrained random position. |
el17my | 10:559487aac60e | 49 | gamepad.tone(1000, 0.1);//cause a noise to makesure the coin has collected |
el17my | 10:559487aac60e | 50 | } |
el17my | 10:559487aac60e | 51 | |
el17my | 10:559487aac60e | 52 | // Print the item. |
el17my | 10:559487aac60e | 53 | lcd.drawSprite(_item.get_item_x(),_item.get_item_y(),5,6,(int*)_item.get_item_form()); |
el17my | 10:559487aac60e | 54 | } |
el17my | 10:559487aac60e | 55 | } |
el17my | 23:7be9701fc1b8 | 56 | |
el17my | 23:7be9701fc1b8 | 57 | * @ endcode |
el17my | 10:559487aac60e | 58 | */ |
el17my | 10:559487aac60e | 59 | |
el17my | 1:ed745421d8c4 | 60 | class item |
el17my | 1:ed745421d8c4 | 61 | { |
el17my | 1:ed745421d8c4 | 62 | public: |
el17my | 1:ed745421d8c4 | 63 | // Constructor and Destructor. |
el17my | 1:ed745421d8c4 | 64 | item(); |
el17my | 1:ed745421d8c4 | 65 | ~item(); |
el17my | 1:ed745421d8c4 | 66 | |
el17my | 2:89f04cd3bf45 | 67 | // Initialises object.*/ |
el17my | 1:ed745421d8c4 | 68 | void init(); |
el17my | 1:ed745421d8c4 | 69 | //Sets the item coordinates. |
el17my | 1:ed745421d8c4 | 70 | //rand_x = a random number that determines the x coordinate |
el17my | 1:ed745421d8c4 | 71 | //rand_y = a random number that determines if the item's generated level |
el17my | 3:672d4bd8225d | 72 | void set_item(int random_x, int random_y); |
el17my | 8:201ef0618b7d | 73 | int *get_item_form();// |
el17my | 1:ed745421d8c4 | 74 | int get_item_x(); |
el17my | 1:ed745421d8c4 | 75 | int get_item_y(); |
el17my | 1:ed745421d8c4 | 76 | |
el17my | 1:ed745421d8c4 | 77 | |
el17my | 1:ed745421d8c4 | 78 | private: |
el17my | 3:672d4bd8225d | 79 | int _x;//the x coordinate of the item |
el17my | 3:672d4bd8225d | 80 | int _y;//the y coordinate of the item |
el17my | 1:ed745421d8c4 | 81 | }; |
el17my | 1:ed745421d8c4 | 82 | #endif |