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