Mochu Yao explorer game
Dependencies: mbed
Gameengine/Gameengine.cpp@36:cdfba51a0a44, 2020-05-13 (annotated)
- Committer:
- el17my
- Date:
- Wed May 13 08:49:44 2020 +0000
- Revision:
- 36:cdfba51a0a44
- Parent:
- 35:3d4dd92bc82b
- Child:
- 37:9e6c56fcc738
5.13
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 | 34:66e37a0d59c3 | 57 | _player.set_y_coordinate(X_flag, _jump_height, _y_flag); } |
el17my | 19:14c5427b30d1 | 58 | _f_flag = _player.get_fall_flag(); |
el17my | 19:14c5427b30d1 | 59 | _player_y = _player.get_y(); |
el17my | 36:cdfba51a0a44 | 60 | _jump_height = _player.get_jump_height(); |
el17my | 36:cdfba51a0a44 | 61 | _y_flag = _player.get_y_flag(); } |
el17my | 19:14c5427b30d1 | 62 | |
el17my | 9:e11bb7cef050 | 63 | void Gameengine::set_fall_flag() { |
el17my | 36:cdfba51a0a44 | 64 | if (((set_line_1.right < _player_x) && (_player_x < (set_line_1.left - 5))) && (_y_flag == 0)) { |
el17my | 9:e11bb7cef050 | 65 | _f_flag = true; } |
el17my | 36:cdfba51a0a44 | 66 | else if (((set_line_2.right < _player_x) && (_player_x < (set_line_2.left - 5))) && (_y_flag == 0)) { |
el17my | 9:e11bb7cef050 | 67 | _f_flag = true; } |
el17my | 36:cdfba51a0a44 | 68 | else if (((set_line_3.right < _player_x) && (_player_x < (set_line_3.left - 5))) && (_y_flag == 0)) { |
el17my | 9:e11bb7cef050 | 69 | _f_flag = true; } |
el17my | 33:ea83f08fa466 | 70 | else if ((_player_x <= 0) && (_player_x >= 80)) { |
el17my | 33:ea83f08fa466 | 71 | _f_flag = true; } |
el17my | 9:e11bb7cef050 | 72 | } |
el17my | 9:e11bb7cef050 | 73 | |
el17my | 9:e11bb7cef050 | 74 | void Gameengine::check_collision(Gamepad &gamepad) { |
el17my | 35:3d4dd92bc82b | 75 | if ((_player_x == _item.get_item_x() - 5) && (_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 |