Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Fri May 15 09:03:36 2020 +0000
Revision:
39:0debc17bad29
Parent:
38:42ff379fa48c
the final version

Who changed what in which revision?

UserRevisionLine numberNew 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 38:42ff379fa48c 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 34:66e37a0d59c3 85 _x = 0.5*_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 34:66e37a0d59c3 108 void Explorer::set_y_coordinate(bool ifjump, int jump_height, int y_flag) {
el17my 3:672d4bd8225d 109 _jump_height = jump_height;
el17my 36:cdfba51a0a44 110 _y_flag = y_flag;
el17my 34:66e37a0d59c3 111 if (ifjump && _jump_height < 40) {
el17my 34:66e37a0d59c3 112 _jump_height = 30; }
el17my 33:ea83f08fa466 113 if (_jump_height !=0) _speed--; // Keep falling until not jumping.
el17my 33:ea83f08fa466 114 if ((_line_4.left <= _x - 5) && (_line_4.right >= _x)){
el17my 33:ea83f08fa466 115 _y_flag = 1;}
el17my 33:ea83f08fa466 116 else if ((_line_5.left <= _x - 5) && (_line_5.right >= _x)) {
el17my 33:ea83f08fa466 117 _y_flag = 1;}
el17my 33:ea83f08fa466 118 else if ((_line_6.left <= _x - 5) && (_line_6.right >= _x)) {
el17my 33:ea83f08fa466 119 _y_flag = 1;}
el17my 33:ea83f08fa466 120 else if ((_line_1.left <= _x) && (_line_1.right >= _x)){
el17my 33:ea83f08fa466 121 _y_flag = 0; }
el17my 33:ea83f08fa466 122 else if ((_line_2.left <= _x) && (_line_2.right >= _x)){
el17my 33:ea83f08fa466 123 _y_flag = 0;}
el17my 33:ea83f08fa466 124 else if ((_line_3.left <= _x) && (_line_3.right >= _x)){
el17my 34:66e37a0d59c3 125 _y_flag = 0;}
el17my 33:ea83f08fa466 126 if (_y_flag == 1 && ifjump) {
el17my 33:ea83f08fa466 127 _platform = 5;
el17my 33:ea83f08fa466 128 } else if (_y_flag == 0) {
el17my 37:9e6c56fcc738 129 _platform = 25;
el17my 2:89f04cd3bf45 130 }
el17my 33:ea83f08fa466 131 _y = _platform - 0.5*_jump_height;
el17my 8:201ef0618b7d 132 }
el17my 8:201ef0618b7d 133
el17my 2:89f04cd3bf45 134 int Explorer::get_y() {
el17my 2:89f04cd3bf45 135 return _y;
el17my 2:89f04cd3bf45 136 }
el17my 2:89f04cd3bf45 137
el17my 2:89f04cd3bf45 138 int Explorer::get_x() {
el17my 2:89f04cd3bf45 139 return _x;
el17my 2:89f04cd3bf45 140 }
el17my 2:89f04cd3bf45 141
el17my 3:672d4bd8225d 142 int Explorer::get_speed() {
el17my 3:672d4bd8225d 143 return _speed;
el17my 3:672d4bd8225d 144 }
el17my 36:cdfba51a0a44 145
el17my 36:cdfba51a0a44 146 int Explorer::get_y_flag() {
el17my 36:cdfba51a0a44 147 return _y_flag;
el17my 36:cdfba51a0a44 148 }
el17my 2:89f04cd3bf45 149
el17my 8:201ef0618b7d 150 int Explorer::get_jump_height() {
el17my 8:201ef0618b7d 151 return _jump_height;
el17my 2:89f04cd3bf45 152 }
el17my 2:89f04cd3bf45 153
el17my 20:20e6ba54e15c 154 void Explorer::reset_flag(bool flag) {
el17my 19:14c5427b30d1 155 if (flag) {
el17my 3:672d4bd8225d 156 _r_flag = true;
el17my 3:672d4bd8225d 157 } else {
el17my 3:672d4bd8225d 158 _r_flag = false;
el17my 2:89f04cd3bf45 159 }
el17my 19:14c5427b30d1 160 }
el17my 20:20e6ba54e15c 161
el17my 2:89f04cd3bf45 162
el17my 2:89f04cd3bf45 163 void Explorer::fall(bool f_flag, Gamepad &gamepad) {
el17my 33:ea83f08fa466 164 _f_flag = f_flag; // decending equation
el17my 33:ea83f08fa466 165 if (_f_flag == true) {
el17my 33:ea83f08fa466 166 _y++; }
el17my 33:ea83f08fa466 167 else if(_y == 80) { // Stop falling function
el17my 2:89f04cd3bf45 168 _f_flag = false;
el17my 2:89f04cd3bf45 169 _r_flag = true;
el17my 8:201ef0618b7d 170 gamepad.tone(1000, 0.05);
el17my 3:672d4bd8225d 171 wait(0.01);
el17my 2:89f04cd3bf45 172 }
el17my 3:672d4bd8225d 173 }
el17my 3:672d4bd8225d 174
el17my 3:672d4bd8225d 175 bool Explorer::get_fall_flag() {
el17my 3:672d4bd8225d 176 return _f_flag;
el17my 3:672d4bd8225d 177 }
el17my 3:672d4bd8225d 178
el17my 3:672d4bd8225d 179 bool Explorer::get_reset_flag() {
el17my 3:672d4bd8225d 180 return _r_flag;
el17my 2:89f04cd3bf45 181 }