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: Doodler/Doodler.cpp
- Revision:
- 9:5e53bca2a4c2
- Parent:
- 8:90e789413e0b
- Child:
- 10:e1d2289705ef
--- a/Doodler/Doodler.cpp Tue Apr 16 16:16:08 2019 +0000 +++ b/Doodler/Doodler.cpp Tue Apr 16 19:51:12 2019 +0000 @@ -1,5 +1,4 @@ #include "Doodler.h" - Doodler::Doodler(){ } Doodler::~Doodler(){ @@ -7,52 +6,52 @@ void Doodler::init(int radius){ // initial position of doodler at centre - _x = (70/2)+6; - _y = 24; _radius = radius; + _pos.x = (70/2)+6; + _pos.y = 24; _velocity.y = 1.5; // dropped down _gravity = 1.5; // moves down - _up = 0.9; + _up = -0.2; } void Doodler::draw(N5110 &lcd){ - lcd.drawCircle(_x, _y, _radius, FILL_BLACK); + lcd.drawCircle(_pos.x, _pos.y, _radius, FILL_BLACK); } -void Doodler::update(Direction d, float mag, Vector2D current_pos, Vector2D current_vel){ -////////////// y - direction of doodler //////////////////////// - _current_pos = current_pos; /// tienes que ponerlo para la direccion de x tambien y borrar lo del engine +void Doodler::update(Direction d, float mag, Vector2D current_vel){ +//////////////////////////// y - direction of doodler ///////////////////////////////////// _current_vel = current_vel; - if ((_current_vel.y > 0) || (_current_vel.y == 0 )){ // no jump or jump finishes - _new_vel.y *= _gravity; // falls accelerating + if (_current_vel.y > 0 ){ // no jump + _velocity.y *= _gravity; // falls accelerating } else if(_current_vel.y < 0){ // jump has started and continues - _new_vel.y *= (-_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0) - } - _current_pos.y += _new_vel.y; + _velocity.y = -(_velocity.y*_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0) + } else if (_current_vel.y == 0) { // jumping has finished + _velocity.y = _gravity; + } + _pos.y += _velocity.y; -////////////// x - direction of doodler //////////////////////// - _new_vel.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value) +/////////////////////////// x - direction of doodler ///////////////////////////////////////// + _velocity.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value) if (d == W){ // if direction is left - _current_pos.x-= _new_vel.x; + _pos.x -= _velocity.x; } else if (d == E){ - _current_pos.x+= _new_vel.x; - } - // checking doodler does not leave screen: - if (_current_pos.x > WIDTH-4){ // right side - _current_pos.x = WIDTH-4; + _pos.x += _velocity.x; } - if (_current_pos.x < _radius){ // left side - _current_pos.x = _radius; - } - set_position(_current_pos); - set_velocity(_new_vel); + // checking doodler does not leave screen: + if (_pos.x > WIDTH-4){ // right side + _pos.x = WIDTH-4; + } + if (_pos.x < _radius){ // left side + _pos.x = _radius; + } + set_position(_pos); + set_velocity(_velocity); } - Vector2D Doodler::get_position(){ - Vector2D p = {_x,_y}; + Vector2D p = {_pos.x, _pos.y}; return p; } @@ -67,6 +66,6 @@ } void Doodler::set_position(Vector2D p){ - _x = (int)p.x; //integer position values - _y = (int)p.y; + _pos.x = p.x; + _pos.y = p.y; } \ No newline at end of file