Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Tue Apr 28 12:50:40 2020 +0000
Revision:
17:1b4ecc01b79f
Parent:
8:201ef0618b7d
Child:
19:14c5427b30d1
4.28

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 8:201ef0618b7d 62 _speed = 0;//the player is standing at first
el17my 8:201ef0618b7d 63 _jump_height = 15;
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 17:1b4ecc01b79f 71 if (joy_x < float(-0.1)) {
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 17:1b4ecc01b79f 76 } else if (joy_x > float(0.1)) {
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 17:1b4ecc01b79f 85 _x = 0.2*_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 8:201ef0618b7d 149 void Explorer::reset_flag(bool flag, Gamepad &gamepad) {
el17my 8:201ef0618b7d 150 _r_flag = flag;
el17my 3:672d4bd8225d 151 if (flag) {
el17my 3:672d4bd8225d 152 _r_flag = true;
el17my 3:672d4bd8225d 153 } else {
el17my 3:672d4bd8225d 154 _r_flag = false;
el17my 2:89f04cd3bf45 155 }
el17my 8:201ef0618b7d 156 gamepad.tone(600, 0.05);
el17my 2:89f04cd3bf45 157 }
el17my 2:89f04cd3bf45 158
el17my 2:89f04cd3bf45 159 void Explorer::fall(bool f_flag, Gamepad &gamepad) {
el17my 2:89f04cd3bf45 160 _f_flag = f_flag;
el17my 8:201ef0618b7d 161 if (_f_flag == true) {
el17my 7:88c4ba6bb37b 162 gamepad.tone(int(1500+0.5*_y), 0.05); // decending equation
el17my 8:201ef0618b7d 163 _y--; }
el17my 3:672d4bd8225d 164 else if(_y == -80) { // Stop falling function
el17my 2:89f04cd3bf45 165 _f_flag = false;
el17my 2:89f04cd3bf45 166 _r_flag = true;
el17my 8:201ef0618b7d 167 gamepad.tone(1000, 0.05);
el17my 3:672d4bd8225d 168 wait(0.01);
el17my 2:89f04cd3bf45 169 }
el17my 3:672d4bd8225d 170 }
el17my 3:672d4bd8225d 171
el17my 3:672d4bd8225d 172 bool Explorer::get_fall_flag() {
el17my 3:672d4bd8225d 173 return _f_flag;
el17my 3:672d4bd8225d 174 }
el17my 3:672d4bd8225d 175
el17my 3:672d4bd8225d 176 bool Explorer::get_reset_flag() {
el17my 3:672d4bd8225d 177 return _r_flag;
el17my 2:89f04cd3bf45 178 }