Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: Engine/Engine.cpp
- Revision:
- 17:f377df4ea7b1
- Parent:
- 16:331be5c7ed80
- Child:
- 18:304700b5d8f8
diff -r 331be5c7ed80 -r f377df4ea7b1 Engine/Engine.cpp --- a/Engine/Engine.cpp Wed Apr 10 13:40:58 2019 +0000 +++ b/Engine/Engine.cpp Thu Apr 11 11:37:49 2019 +0000 @@ -10,27 +10,19 @@ void Engine::init() { // Initialisation of the game. 40 and 22 refer to inital line lengths. reset_skater(); - _lower_map.init(40); - _upper_map.init(22); - _skater.set_reset_flag(false); - _coin.init(); - _spikes.init(); - _coin_collision_flag = false; - _player_score = 0; + reset_engine(); srand(time(NULL)); } -void Engine::check_reset() { +void Engine::check_reset(N5110 &lcd) { // Reset the game if the skater has fallen (i.e. reset flag = TRUE). // 40 and 22 refer to the inital lengths of lower and upper platfroms respectivly. if (_skater.get_reset_flag()) { reset_skater(); - _lower_map.init(40); - _upper_map.init(22); - _skater.set_reset_flag(false); - _coin.init(); - _spikes.init(); - _player_score = 0; + reset_engine(); + wait(1); + lcd.clear(); + lcd.refresh(); wait(1); } } @@ -41,8 +33,19 @@ _direction = Left; _level_condition = 0; _start_platform = true; - _x = 40; - _y = 23; + _skater.set_reset_flag(false); +} + +void Engine::reset_engine() { + _input.coord.x = 0; + _input.coord.y = 0; + _input.A_flag = false; + _lower_map.init(40); + _upper_map.init(22); + _coin.init(); + _spikes.init(); + _coin_collision_flag = false; + _player_score = 0; } void Engine::read_input(Gamepad &gamepad) { @@ -75,22 +78,20 @@ } } -void Engine::process_x(int t) { - // Reset the game if the skater goes off the screen, and update x coordinate. +void Engine::process_x(int game_counter) { + // Update x coordinate. // Will move the skater to the left along with the platforms (if he is not moving - // left already). - if ( _x < -10 || _x > 84 ) { - _skater.set_reset_flag(true); - } else { - _skater.set_x_position_and_sprite(_input.coord.x, - _moving_counter, - _direction, - _input.coord.y); - _x = _skater.get_x_position(); - _moving_counter = _skater.get_moving_counter(); - if ((t % 4 == 0) && (_input.coord.x > -0.1)) { - _moving_counter--; - } + // left already). Speed divider is dependent on the number of coins you have, more coins + // means moves faster. + _skater.set_x_position_and_sprite(_input.coord.x, + _moving_counter, + _direction, + _input.coord.y); + _x = _skater.get_x_position(); + _moving_counter = _skater.get_moving_counter(); + _speed_divider = int(-0.05*_player_score + 4); + if ((game_counter % _speed_divider == 0) && (_input.coord.x > -0.1)) { + _moving_counter--; } } @@ -115,12 +116,12 @@ } } -void Engine::generate_level() { +void Engine::generate_level(int game_counter) { // Generate parameters for all objects in the level (except skater) generate_lower_lines(); generate_upper_lines(); _coin.generate_coin(); - generate_spikes(); + generate_spikes(game_counter); } void Engine::generate_lower_lines() { @@ -147,14 +148,15 @@ _upper_line_3 = _upper_map.get_line_3(); } -void Engine::generate_spikes() { +void Engine::generate_spikes(int game_counter) { _spikes.update_spikes(); + _spikes_y = int(float(-0.0089*game_counter*game_counter) + (0.8*game_counter) + 5); } void Engine::update_lcd(N5110 &lcd){ lcd.drawSprite(_x,_y,17,10,(int *)_skater.get_sprite(_sprite)); lcd.drawSprite(_coin.get_coin_x(),_coin.get_coin_y(),5,5,(int*)_coin.get_coin_sprite()); - lcd.drawSprite(_spikes.get_spikes_x(),23,5,8,(int*)_spikes.get_spikes_sprite()); + lcd.drawSprite(_spikes.get_spikes_x(),_spikes_y,5,8,(int*)_spikes.get_spikes_sprite()); lcd.drawLine(_lower_line_2.x_start,_lower_line_2.y,_lower_line_2.x_end,_lower_line_2.y,FILL_BLACK); lcd.drawLine(_lower_line_1.x_start,_lower_line_1.y,_lower_line_1.x_end,_lower_line_1.y,FILL_BLACK); lcd.drawLine(_lower_line_3.x_start,_lower_line_3.y,_lower_line_3.x_end,_lower_line_3.y,FILL_BLACK); @@ -184,9 +186,15 @@ void Engine::check_spikes_collision() { // If the skaters coord match the spikes and he is not ducking, the player has died - // and the game is reset. - if (_input.coord.y > -0.1 && _x == _spikes.get_spikes_x() && _y == 23) { + // and the game is reset. Game will also reset if skater goes off the screen. + if (_input.coord.y > -0.1 && _x == _spikes.get_spikes_x() && _spikes_y - 1 < _y < _spikes_y + 1) { + _skater.set_reset_flag(true); + } else if ( _x < -10 || _x > 84 ) { _skater.set_reset_flag(true); } } + +int Engine::get_player_score() { + return _player_score; +} \ No newline at end of file