Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Wed Apr 15 08:17:18 2020 +0000
Revision:
1:ed745421d8c4
Child:
2:89f04cd3bf45
second part of the code 4/13

Who changed what in which revision?

UserRevisionLine numberNew 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 1:ed745421d8c4 6 /** item Class
el17my 1:ed745421d8c4 7 * @the item class has three functions
el17my 1:ed745421d8c4 8 1 generate an item in random position after one disappear
el17my 1:ed745421d8c4 9 2 build the item's structer
el17my 1:ed745421d8c4 10 3 build a barrier to increase difficulty
el17my 1:ed745421d8c4 11 * @date April 13th 2020
el17my 1:ed745421d8c4 12 * @author Yaomochu
el17my 1:ed745421d8c4 13
el17my 1:ed745421d8c4 14 @code
el17my 1:ed745421d8c4 15
el17my 1:ed745421d8c4 16 #include "mbed.h"
el17my 1:ed745421d8c4 17 #include "item.h"
el17my 1:ed745421d8c4 18 #include "Gamepad.h"
el17my 1:ed745421d8c4 19 #include "N5110.h"
el17my 1:ed745421d8c4 20 #include <cstdlib>
el17my 1:ed745421d8c4 21 #include <ctime>
el17my 1:ed745421d8c4 22
el17my 1:ed745421d8c4 23 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
el17my 1:ed745421d8c4 24 Gamepad gamepad;
el17my 1:ed745421d8c4 25 item _item;
el17my 1:ed745421d8c4 26
el17my 1:ed745421d8c4 27 int _explorer_x;
el17my 1:ed745421d8c4 28 int _explorer_y;
el17my 1:ed745421d8c4 29 int _score;
el17my 1:ed745421d8c4 30 bool _collision_flag;
el17my 1:ed745421d8c4 31
el17my 1:ed745421d8c4 32 int main()
el17my 1:ed745421d8c4 33 {
el17my 1:ed745421d8c4 34 _item.init();
el17my 1:ed745421d8c4 35 _explorer_x = 40;
el17my 1:ed745421d8c4 36 _explorer_y = 15;
el17my 1:ed745421d8c4 37 _player_score = 0;
el17my 1:ed745421d8c4 38 srand(time(NULL));%this srand comment reqire the coin to get a random location value
el17my 1:ed745421d8c4 39
el17my 1:ed745421d8c4 40 while(1){
el17my 1:ed745421d8c4 41 // If the player collides with the item,a flag will be set to run the interruprtion. player's score will be +1
el17my 1:ed745421d8c4 42 if (_explorer_x == _item.get_x()&&(_explorer_y == _item.get_y() - 5))
el17my 1:ed745421d8c4 43 {
el17my 1:ed745421d8c4 44 _collision_flag = true;
el17my 1:ed745421d8c4 45 _score++;
el17my 1:ed745421d8c4 46 // the item will have a new random position and make a noise.
el17my 1:ed745421d8c4 47 _item.set_item((rand()%100),(abs(rand()%100 - 10)));
el17my 1:ed745421d8c4 48 // Place item on a constrained random position.
el17my 1:ed745421d8c4 49 gamepad.tone(2000, 0.05); // Make collection noise on buzzer.
el17my 1:ed745421d8c4 50
el17my 1:ed745421d8c4 51 // If the player collides with the barrier,a flag will be set to run the interruprtion. player's score will be -1
el17my 1:ed745421d8c4 52 if (_explorer_x == _barrier.get_x()&&(_explorer_y == _barrier.get_y() - 5))
el17my 1:ed745421d8c4 53 {
el17my 1:ed745421d8c4 54 _collision_flag = true;
el17my 1:ed745421d8c4 55 _score--;
el17my 1:ed745421d8c4 56 // the barrier will have a new random position and make a noise.
el17my 1:ed745421d8c4 57 _item.set_barrier((rand()%100),(abs(rand()%100 - 10)));
el17my 1:ed745421d8c4 58 // Place barrier on a constrained random position.
el17my 1:ed745421d8c4 59 gamepad.tone(3000, 0.05); // Make collection noise on buzzer.
el17my 1:ed745421d8c4 60 }
el17my 1:ed745421d8c4 61
el17my 1:ed745421d8c4 62 // than draw the next item on the lcd.
el17my 1:ed745421d8c4 63 lcd.drawSprite(_item.get_x(),_item.get_y(),6,5,(int*)_item.get_item_sprite());
el17my 1:ed745421d8c4 64 lcd.drawSprite(_barrier.get_x(),_barrier.get_y(),7,7,(int*)_barrier.get_barrier_sprite());
el17my 1:ed745421d8c4 65 }
el17my 1:ed745421d8c4 66 }
el17my 1:ed745421d8c4 67
el17my 1:ed745421d8c4 68 @endcode
el17my 1:ed745421d8c4 69 */
el17my 1:ed745421d8c4 70
el17my 1:ed745421d8c4 71 class item
el17my 1:ed745421d8c4 72 {
el17my 1:ed745421d8c4 73 public:
el17my 1:ed745421d8c4 74 // Constructor and Destructor.
el17my 1:ed745421d8c4 75 item();
el17my 1:ed745421d8c4 76 ~item();
el17my 1:ed745421d8c4 77
el17my 1:ed745421d8c4 78 // Initialises Coin object.*/
el17my 1:ed745421d8c4 79 void init();
el17my 1:ed745421d8c4 80 //Generates the item and barrier.
el17my 1:ed745421d8c4 81 void generate_item();
el17my 1:ed745421d8c4 82 //Sets the item coordinates.
el17my 1:ed745421d8c4 83 //rand_x = a random number that determines the x coordinate
el17my 1:ed745421d8c4 84 //rand_y = a random number that determines if the item's generated level
el17my 1:ed745421d8c4 85 void set_item(int rand_x, int rand_y);
el17my 1:ed745421d8c4 86 void set_barrier(int rand_dx, int rand_dy);
el17my 1:ed745421d8c4 87 int *get_item_sprite();
el17my 1:ed745421d8c4 88 int get_item_x();
el17my 1:ed745421d8c4 89 int get_item_y();
el17my 1:ed745421d8c4 90 int *get_barrier_sprite();
el17my 1:ed745421d8c4 91 int get_barrier_bx();
el17my 1:ed745421d8c4 92 int get_barrier_by();
el17my 1:ed745421d8c4 93
el17my 1:ed745421d8c4 94
el17my 1:ed745421d8c4 95 private:
el17my 1:ed745421d8c4 96 int _x;
el17my 1:ed745421d8c4 97 int _y;
el17my 1:ed745421d8c4 98 int _bx;
el17my 1:ed745421d8c4 99 int _by;
el17my 1:ed745421d8c4 100 int _loop;
el17my 1:ed745421d8c4 101 bool _rotate_item;
el17my 1:ed745421d8c4 102 };
el17my 1:ed745421d8c4 103 #endif