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