Mochu Yao explorer game
Dependencies: mbed
item/item.h@31:8e92b65e0779, 2020-04-29 (annotated)
- Committer:
- el17my
- Date:
- Wed Apr 29 04:49:10 2020 +0000
- Revision:
- 31:8e92b65e0779
- Parent:
- 28:4a1260ad0346
- Child:
- 32:47d98959b4ef
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 | * @date April 13th 2020 |
el17my | 23:7be9701fc1b8 | 8 | * @author Yaomochu |
el17my | 1:ed745421d8c4 | 9 | |
el17my | 10:559487aac60e | 10 | @code |
el17my | 10:559487aac60e | 11 | |
el17my | 10:559487aac60e | 12 | #include "mbed.h" |
el17my | 10:559487aac60e | 13 | #include "N5110.h" |
el17my | 10:559487aac60e | 14 | #include "Gamepad.h" |
el17my | 10:559487aac60e | 15 | #include "item.h" |
el17my | 28:4a1260ad0346 | 16 | #include <cstdlib> |
el17my | 28:4a1260ad0346 | 17 | #include <ctime> |
el17my | 10:559487aac60e | 18 | |
el17my | 10:559487aac60e | 19 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17my | 10:559487aac60e | 20 | Gamepad gamepad; |
el17my | 10:559487aac60e | 21 | item _item; |
el17my | 10:559487aac60e | 22 | //the position of the player is essential |
el17my | 10:559487aac60e | 23 | //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 | 24 | int _player_x; |
el17my | 10:559487aac60e | 25 | int _player_y; |
el17my | 10:559487aac60e | 26 | int _player_score; |
el17my | 10:559487aac60e | 27 | bool _collision_flag;// to see if the player can get a score |
el17my | 10:559487aac60e | 28 | |
el17my | 10:559487aac60e | 29 | int main() { |
el17my | 10:559487aac60e | 30 | _item.init();// use this to generate the item |
el17my | 10:559487aac60e | 31 | srand(time(NULL));// generate random values |
el17my | 10:559487aac60e | 32 | _player_score = 0;// set player score to 0; |
el17my | 23:7be9701fc1b8 | 33 | _player_x = 40; |
el17my | 31:8e92b65e0779 | 34 | _player_y = 25;//make sure they are not in the same position at first,but player on the underlevel |
el17my | 10:559487aac60e | 35 | while(1) { |
el17my | 10:559487aac60e | 36 | |
el17my | 10:559487aac60e | 37 | // becasue the module of the player is big so it has to be make sure that |
el17my | 10:559487aac60e | 38 | //the item will be collected when the edge of the module collide with the item |
el17my | 31:8e92b65e0779 | 39 | if (abs((_player_x - _item.get_item_x())= 5 && abs((_player_y - _item.get_item_y() = 5))) { |
el17my | 10:559487aac60e | 40 | _collision_flag = true; |
el17my | 10:559487aac60e | 41 | _player_score++; |
el17my | 10:559487aac60e | 42 | _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 | 43 | // on a constrained random position. |
el17my | 10:559487aac60e | 44 | gamepad.tone(1000, 0.1);//cause a noise to makesure the coin has collected |
el17my | 10:559487aac60e | 45 | } |
el17my | 10:559487aac60e | 46 | |
el17my | 10:559487aac60e | 47 | // Print the item. |
el17my | 10:559487aac60e | 48 | lcd.drawSprite(_item.get_item_x(),_item.get_item_y(),5,6,(int*)_item.get_item_form()); |
el17my | 10:559487aac60e | 49 | } |
el17my | 10:559487aac60e | 50 | } |
el17my | 23:7be9701fc1b8 | 51 | |
el17my | 28:4a1260ad0346 | 52 | @endcode |
el17my | 10:559487aac60e | 53 | */ |
el17my | 10:559487aac60e | 54 | |
el17my | 1:ed745421d8c4 | 55 | class item |
el17my | 1:ed745421d8c4 | 56 | { |
el17my | 1:ed745421d8c4 | 57 | public: |
el17my | 1:ed745421d8c4 | 58 | // Constructor and Destructor. |
el17my | 28:4a1260ad0346 | 59 | /** |
el17my | 28:4a1260ad0346 | 60 | * @brief Constructor @details Non user specified. |
el17my | 28:4a1260ad0346 | 61 | */ |
el17my | 28:4a1260ad0346 | 62 | item(); |
el17my | 28:4a1260ad0346 | 63 | /** |
el17my | 28:4a1260ad0346 | 64 | * @brief Destructor @details Non user specified. |
el17my | 28:4a1260ad0346 | 65 | */ |
el17my | 28:4a1260ad0346 | 66 | ~item(); |
el17my | 28:4a1260ad0346 | 67 | // Mutators. |
el17my | 28:4a1260ad0346 | 68 | /** |
el17my | 28:4a1260ad0346 | 69 | * @breif Initialises item object. |
el17my | 28:4a1260ad0346 | 70 | */ |
el17my | 28:4a1260ad0346 | 71 | void init(); |
el17my | 28:4a1260ad0346 | 72 | /** |
el17my | 28:4a1260ad0346 | 73 | * @breif Sets the item coordinates. |
el17my | 28:4a1260ad0346 | 74 | * @param rand_x = a random number that determines the x coordinate. |
el17my | 28:4a1260ad0346 | 75 | * @param rand_y = a random number that determines if the item's generated level. |
el17my | 28:4a1260ad0346 | 76 | */ |
el17my | 3:672d4bd8225d | 77 | void set_item(int random_x, int random_y); |
el17my | 28:4a1260ad0346 | 78 | // Accessors. |
el17my | 28:4a1260ad0346 | 79 | /** |
el17my | 28:4a1260ad0346 | 80 | * @breif get the item form. |
el17my | 28:4a1260ad0346 | 81 | * @return and draw the item form. |
el17my | 28:4a1260ad0346 | 82 | */ |
el17my | 28:4a1260ad0346 | 83 | int *get_item_form(); |
el17my | 28:4a1260ad0346 | 84 | /** |
el17my | 28:4a1260ad0346 | 85 | * @breif Gets the x coordinate. |
el17my | 28:4a1260ad0346 | 86 | * @returns The x coordinate of the item. |
el17my | 28:4a1260ad0346 | 87 | */ |
el17my | 1:ed745421d8c4 | 88 | int get_item_x(); |
el17my | 28:4a1260ad0346 | 89 | /** |
el17my | 28:4a1260ad0346 | 90 | * @breif Gets the y coordinate. |
el17my | 28:4a1260ad0346 | 91 | * @returns The y coordinate of the item. |
el17my | 28:4a1260ad0346 | 92 | */ |
el17my | 1:ed745421d8c4 | 93 | int get_item_y(); |
el17my | 1:ed745421d8c4 | 94 | |
el17my | 1:ed745421d8c4 | 95 | |
el17my | 1:ed745421d8c4 | 96 | private: |
el17my | 3:672d4bd8225d | 97 | int _x;//the x coordinate of the item |
el17my | 3:672d4bd8225d | 98 | int _y;//the y coordinate of the item |
el17my | 1:ed745421d8c4 | 99 | }; |
el17my | 1:ed745421d8c4 | 100 | #endif |