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:
- 18:304700b5d8f8
- Parent:
- 17:f377df4ea7b1
- Child:
- 19:f35887b14c23
--- a/Engine/Engine.cpp Thu Apr 11 11:37:49 2019 +0000 +++ b/Engine/Engine.cpp Fri Apr 12 11:30:25 2019 +0000 @@ -30,7 +30,7 @@ void Engine::reset_skater() { _moving_counter = 0; _jump_counter = 0; - _direction = Left; + _skater_direction = Left; _level_condition = 0; _start_platform = true; _skater.set_reset_flag(false); @@ -40,10 +40,10 @@ _input.coord.x = 0; _input.coord.y = 0; _input.A_flag = false; - _lower_map.init(40); - _upper_map.init(22); + _lower_platforms.init(40); + _upper_platforms.init(22); _coin.init(); - _spikes.init(); + _fire.init(); _coin_collision_flag = false; _player_score = 0; } @@ -63,17 +63,17 @@ _skater.set_y_position( _input.A_flag, _jump_counter, _level_condition ); } _fall_flag = _skater.get_fall_flag(); - _y = _skater.get_y_position(); + _skater_y = _skater.get_y_position(); _jump_counter = _skater.get_jump_counter(); } void Engine::set_fall_flag() { // Set the fall flag to true if the skater is not on one of the platforms. - if (((_lower_line_1.x_end < _x) && (_x < (_lower_line_2.x_start - 6))) && _y == 23) { + if (((_lower_line_1.x_end < _skater_x) && (_skater_x < (_lower_line_2.x_start - 6))) && _skater_y == 23) { _fall_flag = true; - } else if (((_lower_line_2.x_end < _x) && (_x < (_lower_line_3.x_start - 6))) && _y == 23) { + } else if (((_lower_line_2.x_end < _skater_x) && (_skater_x < (_lower_line_3.x_start - 6))) && _skater_y == 23) { _fall_flag = true; - } else if (((_lower_line_3.x_end < _x) && (_x < (_lower_line_1.x_start - 6))) && _y == 23) { + } else if (((_lower_line_3.x_end < _skater_x) && (_skater_x < (_lower_line_1.x_start - 6))) && _skater_y == 23) { _fall_flag = true; } } @@ -85,9 +85,9 @@ // means moves faster. _skater.set_x_position_and_sprite(_input.coord.x, _moving_counter, - _direction, + _skater_direction, _input.coord.y); - _x = _skater.get_x_position(); + _skater_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)) { @@ -97,19 +97,19 @@ void Engine::process_sprite() { // Update the sprite and direction. - _sprite = _skater.get_sprite_value(); - _direction = _skater.get_direction(); + _skater_sprite = _skater.get_sprite_value(); + _skater_direction = _skater.get_direction(); } void Engine::set_level_condition() { // If the skater is under or on top of any of the upper platforms, set // level condition to 1. - if (((_upper_line_1.x_start - 6) <= _x) && (_x <= _upper_line_1.x_end)) { + if (((_upper_line_1.x_start - 6) <= _skater_x) && (_skater_x <= _upper_line_1.x_end)) { _level_condition = 1; - } else if (((_upper_line_2.x_start - 6) <= _x) && (_x <= _upper_line_2.x_end)) { + } else if (((_upper_line_2.x_start - 6) <= _skater_x) && (_skater_x <= _upper_line_2.x_end)) { _level_condition = 1; - } else if (((_upper_line_3.x_start - 6) <= _x) && (_x <= _upper_line_3.x_end)) { + } else if (((_upper_line_3.x_start - 6) <= _skater_x) && (_skater_x <= _upper_line_3.x_end)) { _level_condition = 1; } else { _level_condition = 0; @@ -121,42 +121,45 @@ generate_lower_lines(); generate_upper_lines(); _coin.generate_coin(); - generate_spikes(game_counter); + generate_fire(game_counter); } void Engine::generate_lower_lines() { // Use a scaled random number to generate the length of the lower lines. _length_1 = (rand() %20) + 10; - _lower_map.generate_line_1(_length_1); - _lower_line_1 = _lower_map.get_line_1(); + _lower_platforms.generate_line_1(_length_1); + _lower_line_1 = _lower_platforms.get_line_1(); _length_2 = (rand() %20) + 10; - _lower_map.generate_line_2(_length_2); - _lower_line_2 = _lower_map.get_line_2(); + _lower_platforms.generate_line_2(_length_2); + _lower_line_2 = _lower_platforms.get_line_2(); _length_3 = (rand() %20) + 10; - _lower_map.generate_line_3(_length_3); - _lower_line_3 = _lower_map.get_line_3(); + _lower_platforms.generate_line_3(_length_3); + _lower_line_3 = _lower_platforms.get_line_3(); } void Engine::generate_upper_lines() { // Set the length of the lower lines to be proportionally smaller to // the length of the upper lines. - _upper_map.generate_line_1(_length_1 / 2); - _upper_line_1 = _upper_map.get_line_1(); - _upper_map.generate_line_2(_length_2 / 2); - _upper_line_2 = _upper_map.get_line_2(); - _upper_map.generate_line_3(_length_3 / 2); - _upper_line_3 = _upper_map.get_line_3(); + _upper_platforms.generate_line_1(_length_1 / 2); + _upper_line_1 = _upper_platforms.get_line_1(); + _upper_platforms.generate_line_2(_length_2 / 2); + _upper_line_2 = _upper_platforms.get_line_2(); + _upper_platforms.generate_line_3(_length_3 / 2); + _upper_line_3 = _upper_platforms.get_line_3(); } -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::generate_fire(int game_counter) { + // Generate the x and y coordinate of the fire. X is from the fire class method and + // y is claculated from a quadratic expression that oscilates from 5 to 23 with the + // game counter going from 0 to 90 periodicly as its input. + _fire.update_fire(); + _fire_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(_skater_x,_skater_y,17,10,(int *)_skater.get_sprite(_skater_sprite)); lcd.drawSprite(_coin.get_coin_x(),_coin.get_coin_y(),5,5,(int*)_coin.get_coin_sprite()); - lcd.drawSprite(_spikes.get_spikes_x(),_spikes_y,5,8,(int*)_spikes.get_spikes_sprite()); + lcd.drawSprite(_fire.get_fire_x(),_fire_y,5,8,(int*)_fire.get_fire_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); @@ -177,19 +180,19 @@ void Engine::check_coin_collision() { // If the skater coords match the coins, add 1 to the players score and print // a new coin in a random position - if (_x == _coin.get_coin_x() && (_y == _coin.get_coin_y() - 10)) { + if (_skater_x == _coin.get_coin_x() && (_skater_y == _coin.get_coin_y() - 10)) { _coin_collision_flag = true; _player_score++; _coin.update_coin((rand() % 100),(abs(rand() % 100 - 20))); } } -void Engine::check_spikes_collision() { - // If the skaters coord match the spikes and he is not ducking, the player has died +void Engine::check_fire_collision() { + // If the skaters coord match the fire and he is not ducking, the player has died // 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) { + if (_input.coord.y > -0.1 && _skater_x == _fire.get_fire_x() && _fire_y - 1 < _skater_y < _fire_y + 1) { _skater.set_reset_flag(true); - } else if ( _x < -10 || _x > 84 ) { + } else if ( _skater_x < -10 || _skater_x > 84 ) { _skater.set_reset_flag(true); } }