Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Tue Apr 28 16:58:19 2020 +0000
Revision:
22:f8ba0ab7465c
Parent:
20:20e6ba54e15c
Child:
32:47d98959b4ef
4.28

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17my 9:e11bb7cef050 1 #include "Gameengine.h"
el17my 9:e11bb7cef050 2 char buffer[15];
el17my 9:e11bb7cef050 3 // Constructor and destructor.
el17my 9:e11bb7cef050 4 Gameengine::Gameengine() {}
el17my 9:e11bb7cef050 5
el17my 9:e11bb7cef050 6 Gameengine::~Gameengine() {}
el17my 9:e11bb7cef050 7
el17my 9:e11bb7cef050 8 void Gameengine::init() {
el17my 9:e11bb7cef050 9 reset_game_engine();
el17my 9:e11bb7cef050 10 _start_flag = true;
el17my 9:e11bb7cef050 11 srand(time(NULL));
el17my 9:e11bb7cef050 12 }
el17my 9:e11bb7cef050 13
el17my 9:e11bb7cef050 14 void Gameengine::reset_game_engine() {
el17my 9:e11bb7cef050 15 _item.init();
el17my 9:e11bb7cef050 16 _player.init();
el17my 9:e11bb7cef050 17 _surface.init(20,40);
el17my 9:e11bb7cef050 18 _collision_flag = false;
el17my 9:e11bb7cef050 19 _player_score = 0;
el17my 9:e11bb7cef050 20 X_flag = false;
el17my 9:e11bb7cef050 21 _coordinate.coord.x = 0;
el17my 9:e11bb7cef050 22 _coordinate.coord.y = 0;
el17my 9:e11bb7cef050 23 }
el17my 9:e11bb7cef050 24
el17my 9:e11bb7cef050 25 void Gameengine::read_input(Gamepad &gamepad) {
el17my 9:e11bb7cef050 26 _coordinate.coord = gamepad.get_mapped_coord();
el17my 9:e11bb7cef050 27 X_flag = gamepad.check_event(Gamepad::X_PRESSED); }
el17my 9:e11bb7cef050 28
el17my 9:e11bb7cef050 29 void Gameengine::check_reset(N5110 &lcd, Gamepad &gamepad) {
el17my 9:e11bb7cef050 30 if(_player.get_reset_flag()) {
el17my 9:e11bb7cef050 31 gamepad.leds_on();
el17my 9:e11bb7cef050 32 lcd.clear();
el17my 9:e11bb7cef050 33 for (int j = 0; j < 80; j++) {
el17my 9:e11bb7cef050 34 lcd.printString("Try Again",30,2);
el17my 9:e11bb7cef050 35 lcd.printString("Score:",10,4);
el17my 9:e11bb7cef050 36 lcd.refresh();
el17my 9:e11bb7cef050 37 // dependent on counters.
el17my 9:e11bb7cef050 38 wait(0.001); // Control speed of the sequence.
el17my 9:e11bb7cef050 39 sprintf(buffer,"%2d",_player_score);
el17my 9:e11bb7cef050 40 lcd.printString(buffer,30,4); }
el17my 9:e11bb7cef050 41 }
el17my 9:e11bb7cef050 42 }
el17my 9:e11bb7cef050 43
el17my 9:e11bb7cef050 44 void Gameengine::check_start(N5110 &lcd, Gamepad &gamepad) {
el17my 9:e11bb7cef050 45 if(_start_flag == true) {
el17my 9:e11bb7cef050 46 check_reset(lcd, gamepad);}
el17my 9:e11bb7cef050 47 _start_flag = get_start_flag();
el17my 9:e11bb7cef050 48 }
el17my 19:14c5427b30d1 49
el17my 19:14c5427b30d1 50 void Gameengine::get_explorer_y(Gamepad &gamepad) {
el17my 19:14c5427b30d1 51 set_fall_flag();
el17my 19:14c5427b30d1 52 if (_f_flag == true)
el17my 19:14c5427b30d1 53 {
el17my 19:14c5427b30d1 54 _player.fall(_f_flag, gamepad);
el17my 19:14c5427b30d1 55 } else {
el17my 22:f8ba0ab7465c 56 _player.set_y_coordinate(X_flag, _jump_height); }
el17my 19:14c5427b30d1 57 _f_flag = _player.get_fall_flag();
el17my 19:14c5427b30d1 58 _player_y = _player.get_y();
el17my 19:14c5427b30d1 59 _jump_height = _player.get_jump_height(); }
el17my 19:14c5427b30d1 60
el17my 9:e11bb7cef050 61 void Gameengine::set_fall_flag() {
el17my 9:e11bb7cef050 62 if (((set_line_1.right < _player_x) && (_player_x < (set_line_1.left - 5))) && _player_y == 20) {
el17my 9:e11bb7cef050 63 _f_flag = true; }
el17my 9:e11bb7cef050 64 else if (((set_line_2.right < _player_x) && (_player_x < (set_line_2.left - 5))) && _player_y == 20) {
el17my 9:e11bb7cef050 65 _f_flag = true; }
el17my 9:e11bb7cef050 66 else if (((set_line_3.right < _player_x) && (_player_x < (set_line_3.left - 5))) && _player_y == 20) {
el17my 9:e11bb7cef050 67 _f_flag = true; }
el17my 9:e11bb7cef050 68 }
el17my 9:e11bb7cef050 69
el17my 9:e11bb7cef050 70 void Gameengine::check_collision(Gamepad &gamepad) {
el17my 20:20e6ba54e15c 71 if ((_player_x == _item.get_item_x())
el17my 19:14c5427b30d1 72 && (_player_y == _item.get_item_y() - 8)) {
el17my 9:e11bb7cef050 73 _collision_flag = true;
el17my 9:e11bb7cef050 74 _player_score++;
el17my 9:e11bb7cef050 75 _item.set_item(((rand()%80)+ 5) , ((rand()%80)+ 5)); // use the rand()%m function to generate a number from 80 to 1
el17my 9:e11bb7cef050 76 // on a constrained random position.
el17my 9:e11bb7cef050 77 gamepad.tone(1000, 0.1);//cause a noise to makesure the coin has collected
el17my 9:e11bb7cef050 78 }
el17my 9:e11bb7cef050 79 }
el17my 9:e11bb7cef050 80
el17my 9:e11bb7cef050 81 void Gameengine::generate_lines() {
el17my 9:e11bb7cef050 82 _surface.line_1();
el17my 9:e11bb7cef050 83 line_1_value = _surface.getline_1();
el17my 9:e11bb7cef050 84 _surface.line_2();
el17my 9:e11bb7cef050 85 line_2_value = _surface.getline_2();
el17my 9:e11bb7cef050 86 _surface.line_3();
el17my 9:e11bb7cef050 87 line_3_value = _surface.getline_3();
el17my 9:e11bb7cef050 88 _surface.line_4();
el17my 9:e11bb7cef050 89 line_4_value = _surface.getline_4();
el17my 9:e11bb7cef050 90 _surface.line_5();
el17my 9:e11bb7cef050 91 line_5_value = _surface.getline_5();
el17my 9:e11bb7cef050 92 _surface.line_6();
el17my 9:e11bb7cef050 93 line_6_value = _surface.getline_6(); }
el17my 9:e11bb7cef050 94 //the start_flag is the flag to make the surface move
el17my 9:e11bb7cef050 95 //and when the player is playing the start_flag should be false to avoid reset
el17my 9:e11bb7cef050 96 bool Gameengine::get_start_flag() {
el17my 9:e11bb7cef050 97 if(X_flag == true) {
el17my 9:e11bb7cef050 98 _start_flag = false; }
el17my 9:e11bb7cef050 99 else {_start_flag = true;}
el17my 9:e11bb7cef050 100 return _start_flag; }
el17my 9:e11bb7cef050 101
el17my 9:e11bb7cef050 102 int Gameengine::get_score() {
el17my 9:e11bb7cef050 103 return _player_score;
el17my 9:e11bb7cef050 104 }
el17my 9:e11bb7cef050 105
el17my 9:e11bb7cef050 106 void Gameengine::get_explorer_direction() {_player_direction = _player.get_direction(); }
el17my 9:e11bb7cef050 107
el17my 9:e11bb7cef050 108 void Gameengine::get_sprite() {_explorer_sprite = _player.get_explorer_sprite(); }
el17my 19:14c5427b30d1 109
el17my 9:e11bb7cef050 110
el17my 9:e11bb7cef050 111 void Gameengine::get_explorer_x() {
el17my 9:e11bb7cef050 112 _player_x = _player.get_x();
el17my 9:e11bb7cef050 113 _speed = _player.get_speed();
el17my 17:1b4ecc01b79f 114 _player.set_x_coordinate(_coordinate.coord.x, _speed, _player_direction); }
el17my 9:e11bb7cef050 115
el17my 9:e11bb7cef050 116 void Gameengine::update_lcd(N5110 &lcd) {
el17my 9:e11bb7cef050 117 lcd.drawSprite(_player_x,_player_y,10,10,(int *)_player.get_form(_explorer_sprite));
el17my 9:e11bb7cef050 118 lcd.drawSprite(_item.get_item_x(),_item.get_item_y(),5,6,(int*)_item.get_item_form());
el17my 9:e11bb7cef050 119 lcd.drawLine(line_1_value.left,line_1_value.y,line_1_value.right,line_1_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 120 lcd.drawLine(line_2_value.left,line_2_value.y,line_2_value.right,line_2_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 121 lcd.drawLine(line_3_value.left,line_3_value.y,line_3_value.right,line_3_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 122 lcd.drawLine(line_4_value.left,line_4_value.y,line_4_value.right,line_4_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 123 lcd.drawLine(line_5_value.left,line_5_value.y,line_5_value.right,line_5_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 124 lcd.drawLine(line_6_value.left,line_6_value.y,line_6_value.right,line_6_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 125 sprintf(buffer,"%2d",_player_score);
el17my 17:1b4ecc01b79f 126 lcd.printString("score",0,0);
el17my 9:e11bb7cef050 127 lcd.printString(buffer,30,0); }
el17my 9:e11bb7cef050 128
el17my 9:e11bb7cef050 129 void Gameengine::run_engine(N5110 &lcd, Gamepad &gamepad) {
el17my 9:e11bb7cef050 130 check_start(lcd, gamepad);
el17my 9:e11bb7cef050 131 check_reset(lcd, gamepad);
el17my 9:e11bb7cef050 132 read_input(gamepad);
el17my 9:e11bb7cef050 133 get_sprite();
el17my 9:e11bb7cef050 134 get_explorer_direction();
el17my 9:e11bb7cef050 135 get_explorer_y(gamepad);
el17my 9:e11bb7cef050 136 get_explorer_x();
el17my 9:e11bb7cef050 137 generate_lines();
el17my 9:e11bb7cef050 138 check_collision(gamepad);
el17my 9:e11bb7cef050 139 update_lcd(lcd);
el17my 9:e11bb7cef050 140 _player_score = get_score(); }
el17my 9:e11bb7cef050 141
el17my 9:e11bb7cef050 142
el17my 9:e11bb7cef050 143
el17my 9:e11bb7cef050 144
el17my 9:e11bb7cef050 145
el17my 9:e11bb7cef050 146
el17my 9:e11bb7cef050 147
el17my 9:e11bb7cef050 148