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:
- 15:876c047a6ec9
- Parent:
- 14:9861fe85c803
- Child:
- 16:331be5c7ed80
--- a/Engine/Engine.cpp Thu Apr 04 18:19:26 2019 +0000 +++ b/Engine/Engine.cpp Sat Apr 06 17:13:37 2019 +0000 @@ -1,10 +1,24 @@ #include "Engine.h" +char buffer[14]; + // Constructor and destructor Engine::Engine() {} Engine::~Engine() {} +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(); + _coin_collision_flag = false; + _player_score = 0; + srand(time(NULL)); +} + void Engine::check_reset() { // 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. @@ -13,6 +27,8 @@ _lower_map.init(40); _upper_map.init(22); _skater.set_reset_flag(false); + _coin.init(); + _player_score = 0; wait(1); } } @@ -27,15 +43,6 @@ _y = 23; } -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); - srand(time(NULL)); -} - void Engine::read_input(Gamepad &gamepad) { _input.coord = gamepad.get_mapped_coord(); _input.A_flag = gamepad.check_event(Gamepad::A_PRESSED); @@ -109,6 +116,7 @@ void Engine::generate_map() { generate_lower_lines(); generate_upper_lines(); + _coin.generate_coin(); } void Engine::generate_lower_lines() { @@ -136,14 +144,18 @@ } void Engine::update_lcd(N5110 &lcd){ - _skate_sprite = _skater.get_sprite(_sprite); - lcd.drawSprite(_x,_y,17,10,(int *)_skate_sprite); + lcd.drawSprite(_x,_y,17,10,(int *)_skater.get_sprite(_sprite)); + // if (!_coin_collision_flag) { + lcd.drawSprite(_coin.get_coin_x(),_coin.get_coin_y(),5,5,(int*)_coin.get_coin_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); lcd.drawLine(_upper_line_2.x_start,_upper_line_2.y,_upper_line_2.x_end,_upper_line_2.y,FILL_BLACK); lcd.drawLine(_upper_line_1.x_start,_upper_line_1.y,_upper_line_1.x_end,_upper_line_1.y,FILL_BLACK); lcd.drawLine(_upper_line_3.x_start,_upper_line_3.y,_upper_line_3.x_end,_upper_line_3.y,FILL_BLACK); + sprintf(buffer,"%2d",_player_score); + lcd.printString(buffer,0,0); } bool Engine::get_start_platform() { @@ -151,4 +163,12 @@ // (so the starting text goes away after the first jump). if(_input.A_flag) _start_platform = false; return _start_platform; +} + +void Engine::check_coin_collision() { + if (_x == _coin.get_coin_x()) { + _coin_collision_flag = true; + _player_score++; + _coin.update_coin((rand() % 100),(abs(rand() % 100 - 20))); + } } \ No newline at end of file