Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Tue Apr 28 12:50:40 2020 +0000
Revision:
17:1b4ecc01b79f
Parent:
9:e11bb7cef050
Child:
19:14c5427b30d1
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 9:e11bb7cef050 49
el17my 9:e11bb7cef050 50 void Gameengine::set_fall_flag() {
el17my 9:e11bb7cef050 51 if (((set_line_1.right < _player_x) && (_player_x < (set_line_1.left - 5))) && _player_y == 20) {
el17my 9:e11bb7cef050 52 _f_flag = true; }
el17my 9:e11bb7cef050 53 else if (((set_line_2.right < _player_x) && (_player_x < (set_line_2.left - 5))) && _player_y == 20) {
el17my 9:e11bb7cef050 54 _f_flag = true; }
el17my 9:e11bb7cef050 55 else if (((set_line_3.right < _player_x) && (_player_x < (set_line_3.left - 5))) && _player_y == 20) {
el17my 9:e11bb7cef050 56 _f_flag = true; }
el17my 9:e11bb7cef050 57 }
el17my 9:e11bb7cef050 58
el17my 9:e11bb7cef050 59 void Gameengine::check_collision(Gamepad &gamepad) {
el17my 9:e11bb7cef050 60 if (((_player_x - _item.get_item_x())< 7) && ((_player_y - _item.get_item_y()) < 7)) {
el17my 9:e11bb7cef050 61 _collision_flag = true;
el17my 9:e11bb7cef050 62 _player_score++;
el17my 9:e11bb7cef050 63 _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 64 // on a constrained random position.
el17my 9:e11bb7cef050 65 gamepad.tone(1000, 0.1);//cause a noise to makesure the coin has collected
el17my 9:e11bb7cef050 66 }
el17my 9:e11bb7cef050 67 }
el17my 9:e11bb7cef050 68
el17my 9:e11bb7cef050 69 void Gameengine::generate_lines() {
el17my 9:e11bb7cef050 70 _surface.line_1();
el17my 9:e11bb7cef050 71 line_1_value = _surface.getline_1();
el17my 9:e11bb7cef050 72 _surface.line_2();
el17my 9:e11bb7cef050 73 line_2_value = _surface.getline_2();
el17my 9:e11bb7cef050 74 _surface.line_3();
el17my 9:e11bb7cef050 75 line_3_value = _surface.getline_3();
el17my 9:e11bb7cef050 76 _surface.line_4();
el17my 9:e11bb7cef050 77 line_4_value = _surface.getline_4();
el17my 9:e11bb7cef050 78 _surface.line_5();
el17my 9:e11bb7cef050 79 line_5_value = _surface.getline_5();
el17my 9:e11bb7cef050 80 _surface.line_6();
el17my 9:e11bb7cef050 81 line_6_value = _surface.getline_6(); }
el17my 9:e11bb7cef050 82 //the start_flag is the flag to make the surface move
el17my 9:e11bb7cef050 83 //and when the player is playing the start_flag should be false to avoid reset
el17my 9:e11bb7cef050 84 bool Gameengine::get_start_flag() {
el17my 9:e11bb7cef050 85 if(X_flag == true) {
el17my 9:e11bb7cef050 86 _start_flag = false; }
el17my 9:e11bb7cef050 87 else {_start_flag = true;}
el17my 9:e11bb7cef050 88 return _start_flag; }
el17my 9:e11bb7cef050 89
el17my 9:e11bb7cef050 90 int Gameengine::get_score() {
el17my 9:e11bb7cef050 91 return _player_score;
el17my 9:e11bb7cef050 92 }
el17my 9:e11bb7cef050 93
el17my 9:e11bb7cef050 94 void Gameengine::get_explorer_direction() {_player_direction = _player.get_direction(); }
el17my 9:e11bb7cef050 95
el17my 9:e11bb7cef050 96 void Gameengine::get_sprite() {_explorer_sprite = _player.get_explorer_sprite(); }
el17my 9:e11bb7cef050 97
el17my 9:e11bb7cef050 98 void Gameengine::get_explorer_y(Gamepad &gamepad) {
el17my 17:1b4ecc01b79f 99 set_fall_flag();
el17my 17:1b4ecc01b79f 100 if (_f_flag == true)
el17my 9:e11bb7cef050 101 {
el17my 9:e11bb7cef050 102 _player.fall(_f_flag, gamepad);
el17my 9:e11bb7cef050 103 } else {
el17my 9:e11bb7cef050 104 _player.set_y_coordinate(false, _jump_height); }
el17my 9:e11bb7cef050 105 _f_flag = _player.get_fall_flag();
el17my 9:e11bb7cef050 106 _player_y = _player.get_y();
el17my 9:e11bb7cef050 107 _jump_height = _player.get_jump_height(); }
el17my 9:e11bb7cef050 108
el17my 9:e11bb7cef050 109 void Gameengine::get_explorer_x() {
el17my 9:e11bb7cef050 110 _player_x = _player.get_x();
el17my 9:e11bb7cef050 111 _speed = _player.get_speed();
el17my 17:1b4ecc01b79f 112 _player.set_x_coordinate(_coordinate.coord.x, _speed, _player_direction); }
el17my 9:e11bb7cef050 113
el17my 9:e11bb7cef050 114 void Gameengine::update_lcd(N5110 &lcd) {
el17my 9:e11bb7cef050 115 lcd.drawSprite(_player_x,_player_y,10,10,(int *)_player.get_form(_explorer_sprite));
el17my 9:e11bb7cef050 116 lcd.drawSprite(_item.get_item_x(),_item.get_item_y(),5,6,(int*)_item.get_item_form());
el17my 9:e11bb7cef050 117 lcd.drawLine(line_1_value.left,line_1_value.y,line_1_value.right,line_1_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 118 lcd.drawLine(line_2_value.left,line_2_value.y,line_2_value.right,line_2_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 119 lcd.drawLine(line_3_value.left,line_3_value.y,line_3_value.right,line_3_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 120 lcd.drawLine(line_4_value.left,line_4_value.y,line_4_value.right,line_4_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 121 lcd.drawLine(line_5_value.left,line_5_value.y,line_5_value.right,line_5_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 122 lcd.drawLine(line_6_value.left,line_6_value.y,line_6_value.right,line_6_value.y,FILL_BLACK);
el17my 9:e11bb7cef050 123 sprintf(buffer,"%2d",_player_score);
el17my 17:1b4ecc01b79f 124 lcd.printString("score",0,0);
el17my 9:e11bb7cef050 125 lcd.printString(buffer,30,0); }
el17my 9:e11bb7cef050 126
el17my 9:e11bb7cef050 127 void Gameengine::run_engine(N5110 &lcd, Gamepad &gamepad) {
el17my 9:e11bb7cef050 128 check_start(lcd, gamepad);
el17my 9:e11bb7cef050 129 check_reset(lcd, gamepad);
el17my 9:e11bb7cef050 130 read_input(gamepad);
el17my 9:e11bb7cef050 131 get_sprite();
el17my 9:e11bb7cef050 132 get_explorer_direction();
el17my 9:e11bb7cef050 133 get_explorer_y(gamepad);
el17my 9:e11bb7cef050 134 get_explorer_x();
el17my 9:e11bb7cef050 135 generate_lines();
el17my 9:e11bb7cef050 136 check_collision(gamepad);
el17my 9:e11bb7cef050 137 update_lcd(lcd);
el17my 9:e11bb7cef050 138 _player_score = get_score(); }
el17my 9:e11bb7cef050 139
el17my 9:e11bb7cef050 140
el17my 9:e11bb7cef050 141
el17my 9:e11bb7cef050 142
el17my 9:e11bb7cef050 143
el17my 9:e11bb7cef050 144
el17my 9:e11bb7cef050 145
el17my 9:e11bb7cef050 146