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