Mochu Yao explorer game
Dependencies: mbed
item/item.h@28:4a1260ad0346, 2020-04-28 (annotated)
- Committer:
- el17my
- Date:
- Tue Apr 28 18:21:22 2020 +0000
- Revision:
- 28:4a1260ad0346
- Parent:
- 26:4d193529b447
- Child:
- 31:8e92b65e0779
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 | 17:1b4ecc01b79f | 34 | _player_y = 10;//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 | 19:14c5427b30d1 | 39 | if ((_player_x == _item.get_item_x() |
el17my | 19:14c5427b30d1 | 40 | && (_player_y == _item.get_item_y() - 8)) { |
el17my | 10:559487aac60e | 41 | _collision_flag = true; |
el17my | 10:559487aac60e | 42 | _player_score++; |
el17my | 10:559487aac60e | 43 | _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 | 44 | // on a constrained random position. |
el17my | 10:559487aac60e | 45 | gamepad.tone(1000, 0.1);//cause a noise to makesure the coin has collected |
el17my | 10:559487aac60e | 46 | } |
el17my | 10:559487aac60e | 47 | |
el17my | 10:559487aac60e | 48 | // Print the item. |
el17my | 10:559487aac60e | 49 | lcd.drawSprite(_item.get_item_x(),_item.get_item_y(),5,6,(int*)_item.get_item_form()); |
el17my | 10:559487aac60e | 50 | } |
el17my | 10:559487aac60e | 51 | } |
el17my | 23:7be9701fc1b8 | 52 | |
el17my | 28:4a1260ad0346 | 53 | @endcode |
el17my | 10:559487aac60e | 54 | */ |
el17my | 10:559487aac60e | 55 | |
el17my | 1:ed745421d8c4 | 56 | class item |
el17my | 1:ed745421d8c4 | 57 | { |
el17my | 1:ed745421d8c4 | 58 | public: |
el17my | 1:ed745421d8c4 | 59 | // Constructor and Destructor. |
el17my | 28:4a1260ad0346 | 60 | /** |
el17my | 28:4a1260ad0346 | 61 | * @brief Constructor @details Non user specified. |
el17my | 28:4a1260ad0346 | 62 | */ |
el17my | 28:4a1260ad0346 | 63 | item(); |
el17my | 28:4a1260ad0346 | 64 | /** |
el17my | 28:4a1260ad0346 | 65 | * @brief Destructor @details Non user specified. |
el17my | 28:4a1260ad0346 | 66 | */ |
el17my | 28:4a1260ad0346 | 67 | ~item(); |
el17my | 28:4a1260ad0346 | 68 | // Mutators. |
el17my | 28:4a1260ad0346 | 69 | /** |
el17my | 28:4a1260ad0346 | 70 | * @breif Initialises item object. |
el17my | 28:4a1260ad0346 | 71 | */ |
el17my | 28:4a1260ad0346 | 72 | void init(); |
el17my | 28:4a1260ad0346 | 73 | /** |
el17my | 28:4a1260ad0346 | 74 | * @breif Sets the item coordinates. |
el17my | 28:4a1260ad0346 | 75 | * @param rand_x = a random number that determines the x coordinate. |
el17my | 28:4a1260ad0346 | 76 | * @param rand_y = a random number that determines if the item's generated level. |
el17my | 28:4a1260ad0346 | 77 | */ |
el17my | 3:672d4bd8225d | 78 | void set_item(int random_x, int random_y); |
el17my | 28:4a1260ad0346 | 79 | // Accessors. |
el17my | 28:4a1260ad0346 | 80 | /** |
el17my | 28:4a1260ad0346 | 81 | * @breif get the item form. |
el17my | 28:4a1260ad0346 | 82 | * @return and draw the item form. |
el17my | 28:4a1260ad0346 | 83 | */ |
el17my | 28:4a1260ad0346 | 84 | int *get_item_form(); |
el17my | 28:4a1260ad0346 | 85 | /** |
el17my | 28:4a1260ad0346 | 86 | * @breif Gets the x coordinate. |
el17my | 28:4a1260ad0346 | 87 | * @returns The x coordinate of the item. |
el17my | 28:4a1260ad0346 | 88 | */ |
el17my | 1:ed745421d8c4 | 89 | int get_item_x(); |
el17my | 28:4a1260ad0346 | 90 | /** |
el17my | 28:4a1260ad0346 | 91 | * @breif Gets the y coordinate. |
el17my | 28:4a1260ad0346 | 92 | * @returns The y coordinate of the item. |
el17my | 28:4a1260ad0346 | 93 | */ |
el17my | 1:ed745421d8c4 | 94 | int get_item_y(); |
el17my | 1:ed745421d8c4 | 95 | |
el17my | 1:ed745421d8c4 | 96 | |
el17my | 1:ed745421d8c4 | 97 | private: |
el17my | 3:672d4bd8225d | 98 | int _x;//the x coordinate of the item |
el17my | 3:672d4bd8225d | 99 | int _y;//the y coordinate of the item |
el17my | 1:ed745421d8c4 | 100 | }; |
el17my | 1:ed745421d8c4 | 101 | #endif |