Mochu Yao explorer game
Dependencies: mbed
explorer/explorer.cpp@31:8e92b65e0779, 2020-04-29 (annotated)
- Committer:
- el17my
- Date:
- Wed Apr 29 04:49:10 2020 +0000
- Revision:
- 31:8e92b65e0779
- Parent:
- 20:20e6ba54e15c
- Child:
- 33:ea83f08fa466
4.29
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17my | 2:89f04cd3bf45 | 1 | #include "explorer.h" |
el17my | 2:89f04cd3bf45 | 2 | //the explorer.cpp need to defined the module and movement of the explorer including the restarted point |
el17my | 3:672d4bd8225d | 3 | int move_right_sprite[10][10] = { |
el17my | 2:89f04cd3bf45 | 4 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 5 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 6 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 7 | { 0,0,0,0,1,0,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 8 | { 0,0,1,1,1,1,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 9 | { 0,0,1,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 10 | { 0,0,0,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 11 | { 0,0,0,0,1,1,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 12 | { 0,0,0,1,0,0,0,1,0,0 }, |
el17my | 2:89f04cd3bf45 | 13 | { 1,0,1,0,0,0,0,0,1,0 }, |
el17my | 2:89f04cd3bf45 | 14 | }; |
el17my | 2:89f04cd3bf45 | 15 | |
el17my | 3:672d4bd8225d | 16 | int move_left_sprite[10][10] = { |
el17my | 2:89f04cd3bf45 | 17 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 18 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 19 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 20 | { 0,0,1,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 21 | { 0,0,1,1,1,1,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 22 | { 0,0,0,0,1,0,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 23 | { 0,0,0,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 24 | { 0,0,0,1,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 25 | { 0,0,1,0,0,1,0,0,0,0 }, |
el17my | 3:672d4bd8225d | 26 | { 0,1,0,0,0,0,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 27 | }; |
el17my | 2:89f04cd3bf45 | 28 | |
el17my | 3:672d4bd8225d | 29 | int stand_right_sprite[10][10] = { |
el17my | 2:89f04cd3bf45 | 30 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 31 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 32 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 33 | { 0,0,0,0,1,0,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 34 | { 0,0,1,1,1,1,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 35 | { 0,0,1,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 36 | { 0,0,0,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 37 | { 0,0,0,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 38 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 39 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 40 | }; |
el17my | 2:89f04cd3bf45 | 41 | |
el17my | 3:672d4bd8225d | 42 | int stand_left_sprite[10][10] = { |
el17my | 2:89f04cd3bf45 | 43 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 44 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 45 | { 0,0,0,1,1,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 46 | { 0,0,1,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 47 | { 0,0,1,1,1,1,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 48 | { 0,0,0,0,1,0,1,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 49 | { 0,0,0,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 50 | { 0,0,0,0,1,0,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 51 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 52 | { 0,0,0,1,0,1,0,0,0,0 }, |
el17my | 2:89f04cd3bf45 | 53 | }; |
el17my | 2:89f04cd3bf45 | 54 | |
el17my | 3:672d4bd8225d | 55 | Explorer::Explorer() {} |
el17my | 2:89f04cd3bf45 | 56 | |
el17my | 3:672d4bd8225d | 57 | Explorer::~Explorer() {} |
el17my | 8:201ef0618b7d | 58 | void Explorer::init() { |
el17my | 8:201ef0618b7d | 59 | _f_flag = false; |
el17my | 8:201ef0618b7d | 60 | _player_direction = right; |
el17my | 8:201ef0618b7d | 61 | _r_flag = false; |
el17my | 8:201ef0618b7d | 62 | _speed = 0;//the player is standing at first |
el17my | 19:14c5427b30d1 | 63 | _jump_height = 0; |
el17my | 8:201ef0618b7d | 64 | } |
el17my | 2:89f04cd3bf45 | 65 | |
el17my | 3:672d4bd8225d | 66 | void Explorer::set_x_coordinate(float joy_x, int speed, Player_direction direction) |
el17my | 2:89f04cd3bf45 | 67 | { |
el17my | 2:89f04cd3bf45 | 68 | //this process is to set x and make sure the player can move |
el17my | 2:89f04cd3bf45 | 69 | _player_direction = direction; |
el17my | 3:672d4bd8225d | 70 | _speed = speed; |
el17my | 31:8e92b65e0779 | 71 | if (joy_x < float(-0.05)) { |
el17my | 3:672d4bd8225d | 72 | _speed = _speed--;// |
el17my | 3:672d4bd8225d | 73 | _player_direction = left; |
el17my | 7:88c4ba6bb37b | 74 | _explorer_sprite = Move_left;//makesure the value is the Move_left value in the enum |
el17my | 3:672d4bd8225d | 75 | //now x is moving to the right than add to the X position |
el17my | 31:8e92b65e0779 | 76 | } else if (joy_x > float(0.05)) { |
el17my | 3:672d4bd8225d | 77 | _speed = _speed++; |
el17my | 3:672d4bd8225d | 78 | _player_direction = right; |
el17my | 7:88c4ba6bb37b | 79 | _explorer_sprite = Move_right;//makesure the value is the Move_right value in the enum |
el17my | 3:672d4bd8225d | 80 | } else if (_player_direction == right) { |
el17my | 7:88c4ba6bb37b | 81 | _explorer_sprite = Stand_right;// not moving so equals to enum Stand_right. |
el17my | 3:672d4bd8225d | 82 | } else { |
el17my | 7:88c4ba6bb37b | 83 | _explorer_sprite = Stand_left;//the least situation is equals to enum Stand_right. |
el17my | 3:672d4bd8225d | 84 | } |
el17my | 31:8e92b65e0779 | 85 | _x = _speed + 30;//this equation decides how fast the player move later I will update differnet modes to choose different speed |
el17my | 3:672d4bd8225d | 86 | } |
el17my | 3:672d4bd8225d | 87 | //this function is easy to understand because this is just choosing different sprite according to the direction |
el17my | 8:201ef0618b7d | 88 | int * Explorer::get_form(Explorer_sprite sprite){ |
el17my | 3:672d4bd8225d | 89 | if (sprite == Move_right) { |
el17my | 3:672d4bd8225d | 90 | return *move_right_sprite; |
el17my | 3:672d4bd8225d | 91 | } else if (sprite == Move_left) { |
el17my | 3:672d4bd8225d | 92 | return *move_left_sprite; |
el17my | 3:672d4bd8225d | 93 | } else if (sprite == Stand_left) { |
el17my | 3:672d4bd8225d | 94 | return *stand_right_sprite; |
el17my | 3:672d4bd8225d | 95 | } else { |
el17my | 3:672d4bd8225d | 96 | return *stand_left_sprite; |
el17my | 3:672d4bd8225d | 97 | } |
el17my | 2:89f04cd3bf45 | 98 | } |
el17my | 2:89f04cd3bf45 | 99 | |
el17my | 3:672d4bd8225d | 100 | Player_direction Explorer::get_direction() { |
el17my | 3:672d4bd8225d | 101 | return _player_direction; |
el17my | 3:672d4bd8225d | 102 | } |
el17my | 2:89f04cd3bf45 | 103 | |
el17my | 8:201ef0618b7d | 104 | Explorer_sprite Explorer::get_explorer_sprite() { |
el17my | 7:88c4ba6bb37b | 105 | return _explorer_sprite; |
el17my | 2:89f04cd3bf45 | 106 | } |
el17my | 2:89f04cd3bf45 | 107 | |
el17my | 8:201ef0618b7d | 108 | void Explorer::set_y_coordinate(bool ifjump, int jump_height) { |
el17my | 3:672d4bd8225d | 109 | _jump_height = jump_height; |
el17my | 2:89f04cd3bf45 | 110 | // jump need to have this function |
el17my | 2:89f04cd3bf45 | 111 | // when you jump makesure the module will not be offscreen also if you fall from the surface you can not jump back again |
el17my | 3:672d4bd8225d | 112 | if (ifjump == 1) { |
el17my | 8:201ef0618b7d | 113 | _jump_height = 40; } //the player can jump up to 30 height and then fall down |
el17my | 8:201ef0618b7d | 114 | if (_jump_height !=0) { |
el17my | 8:201ef0618b7d | 115 | _jump_height--; |
el17my | 8:201ef0618b7d | 116 | _y = 20 + _jump_height; |
el17my | 8:201ef0618b7d | 117 | if ((_line_4.left <= _x) && (_line_4.right >= _x) && (_y >= 40)) { |
el17my | 8:201ef0618b7d | 118 | _y = 40; _jump_height = 0;} |
el17my | 8:201ef0618b7d | 119 | else if ((_line_5.left <= _x) && (_line_5.right >= _x) && (_y >= 40)) { |
el17my | 8:201ef0618b7d | 120 | _y = 40; _jump_height = 0;} |
el17my | 8:201ef0618b7d | 121 | else if ((_line_6.left <= _x) && (_line_6.right >= _x) && (_y >= 40)) { |
el17my | 8:201ef0618b7d | 122 | _y = 40; _jump_height = 0;} |
el17my | 8:201ef0618b7d | 123 | else if ((_line_1.left <= _x) && (_line_1.right >= _x) && (_y < 40)) { |
el17my | 8:201ef0618b7d | 124 | _y = 20; _jump_height = 0;} |
el17my | 8:201ef0618b7d | 125 | else if ((_line_2.left <= _x) && (_line_2.right >= _x) && (_y < 40)) { |
el17my | 8:201ef0618b7d | 126 | _y = 20; _jump_height = 0;} |
el17my | 8:201ef0618b7d | 127 | else if ((_line_3.left <= _x) && (_line_3.right >= _x) && (_y < 40)) { |
el17my | 8:201ef0618b7d | 128 | _y = 20; _jump_height = 0;} |
el17my | 8:201ef0618b7d | 129 | else { _f_flag = true; } |
el17my | 2:89f04cd3bf45 | 130 | } |
el17my | 8:201ef0618b7d | 131 | } |
el17my | 8:201ef0618b7d | 132 | |
el17my | 2:89f04cd3bf45 | 133 | int Explorer::get_y() { |
el17my | 2:89f04cd3bf45 | 134 | return _y; |
el17my | 2:89f04cd3bf45 | 135 | } |
el17my | 2:89f04cd3bf45 | 136 | |
el17my | 2:89f04cd3bf45 | 137 | int Explorer::get_x() { |
el17my | 2:89f04cd3bf45 | 138 | return _x; |
el17my | 2:89f04cd3bf45 | 139 | } |
el17my | 2:89f04cd3bf45 | 140 | |
el17my | 3:672d4bd8225d | 141 | int Explorer::get_speed() { |
el17my | 3:672d4bd8225d | 142 | return _speed; |
el17my | 3:672d4bd8225d | 143 | } |
el17my | 2:89f04cd3bf45 | 144 | |
el17my | 8:201ef0618b7d | 145 | int Explorer::get_jump_height() { |
el17my | 8:201ef0618b7d | 146 | return _jump_height; |
el17my | 2:89f04cd3bf45 | 147 | } |
el17my | 2:89f04cd3bf45 | 148 | |
el17my | 20:20e6ba54e15c | 149 | void Explorer::reset_flag(bool flag) { |
el17my | 19:14c5427b30d1 | 150 | if (flag) { |
el17my | 3:672d4bd8225d | 151 | _r_flag = true; |
el17my | 3:672d4bd8225d | 152 | } else { |
el17my | 3:672d4bd8225d | 153 | _r_flag = false; |
el17my | 2:89f04cd3bf45 | 154 | } |
el17my | 19:14c5427b30d1 | 155 | } |
el17my | 20:20e6ba54e15c | 156 | |
el17my | 2:89f04cd3bf45 | 157 | |
el17my | 2:89f04cd3bf45 | 158 | void Explorer::fall(bool f_flag, Gamepad &gamepad) { |
el17my | 2:89f04cd3bf45 | 159 | _f_flag = f_flag; |
el17my | 7:88c4ba6bb37b | 160 | gamepad.tone(int(1500+0.5*_y), 0.05); // decending equation |
el17my | 19:14c5427b30d1 | 161 | _y++; |
el17my | 19:14c5427b30d1 | 162 | if(_y == 80) { // Stop falling function |
el17my | 2:89f04cd3bf45 | 163 | _f_flag = false; |
el17my | 2:89f04cd3bf45 | 164 | _r_flag = true; |
el17my | 8:201ef0618b7d | 165 | gamepad.tone(1000, 0.05); |
el17my | 3:672d4bd8225d | 166 | wait(0.01); |
el17my | 2:89f04cd3bf45 | 167 | } |
el17my | 3:672d4bd8225d | 168 | } |
el17my | 3:672d4bd8225d | 169 | |
el17my | 3:672d4bd8225d | 170 | bool Explorer::get_fall_flag() { |
el17my | 3:672d4bd8225d | 171 | return _f_flag; |
el17my | 3:672d4bd8225d | 172 | } |
el17my | 3:672d4bd8225d | 173 | |
el17my | 3:672d4bd8225d | 174 | bool Explorer::get_reset_flag() { |
el17my | 3:672d4bd8225d | 175 | return _r_flag; |
el17my | 2:89f04cd3bf45 | 176 | } |