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
Doodler/Doodler.cpp@7:0d9cee90ab0d, 2019-04-14 (annotated)
- Committer:
- el17m2h
- Date:
- Sun Apr 14 17:07:02 2019 +0000
- Revision:
- 7:0d9cee90ab0d
- Parent:
- 6:848d1e4c1a31
- Child:
- 8:90e789413e0b
Removed the jump function and instead created a gravity value for the doodler to accelerate downwards at all times.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17m2h | 4:8ec314f806ae | 1 | #include "Doodler.h" |
el17m2h | 4:8ec314f806ae | 2 | |
el17m2h | 4:8ec314f806ae | 3 | Doodler::Doodler(){ |
el17m2h | 4:8ec314f806ae | 4 | } |
el17m2h | 4:8ec314f806ae | 5 | Doodler::~Doodler(){ |
el17m2h | 4:8ec314f806ae | 6 | } |
el17m2h | 4:8ec314f806ae | 7 | |
el17m2h | 5:8814d6de77d0 | 8 | void Doodler::init(int radius){ |
el17m2h | 7:0d9cee90ab0d | 9 | // initial position of doodler at centre |
el17m2h | 7:0d9cee90ab0d | 10 | _x = (70/2)+6; |
el17m2h | 7:0d9cee90ab0d | 11 | _y = 24; |
el17m2h | 5:8814d6de77d0 | 12 | _radius = radius; |
el17m2h | 7:0d9cee90ab0d | 13 | _velocity.y = 1; // dropped down |
el17m2h | 7:0d9cee90ab0d | 14 | _gravity = 1; // moves down |
el17m2h | 7:0d9cee90ab0d | 15 | _up = -0.5; |
el17m2h | 4:8ec314f806ae | 16 | } |
el17m2h | 4:8ec314f806ae | 17 | |
el17m2h | 4:8ec314f806ae | 18 | void Doodler::draw(N5110 &lcd){ |
el17m2h | 4:8ec314f806ae | 19 | lcd.drawCircle(_x, _y, _radius, FILL_BLACK); |
el17m2h | 4:8ec314f806ae | 20 | } |
el17m2h | 4:8ec314f806ae | 21 | |
el17m2h | 5:8814d6de77d0 | 22 | void Doodler::update(Direction d, float mag){ |
el17m2h | 7:0d9cee90ab0d | 23 | if (_y < 43 ){ |
el17m2h | 7:0d9cee90ab0d | 24 | _y += _velocity.y; |
el17m2h | 7:0d9cee90ab0d | 25 | _velocity.y = _gravity; // gravity pulls it down |
el17m2h | 7:0d9cee90ab0d | 26 | } else { |
el17m2h | 7:0d9cee90ab0d | 27 | _velocity.y = _up; //jumps |
el17m2h | 7:0d9cee90ab0d | 28 | _y += _velocity.y*20; |
el17m2h | 7:0d9cee90ab0d | 29 | } |
el17m2h | 5:8814d6de77d0 | 30 | |
el17m2h | 7:0d9cee90ab0d | 31 | _velocity.x = int(mag*5.0f); // 5 frames times the magnitude of the joystick (int holds that value) |
el17m2h | 5:8814d6de77d0 | 32 | if (d == W){ // if direction is left |
el17m2h | 7:0d9cee90ab0d | 33 | _x-= _velocity.x; |
el17m2h | 5:8814d6de77d0 | 34 | } else if (d == E){ |
el17m2h | 7:0d9cee90ab0d | 35 | _x+= _velocity.x; |
el17m2h | 5:8814d6de77d0 | 36 | } |
el17m2h | 7:0d9cee90ab0d | 37 | |
el17m2h | 5:8814d6de77d0 | 38 | // checking doodler does not leave screen: |
el17m2h | 6:848d1e4c1a31 | 39 | if (_x > WIDTH-4){ // right side |
el17m2h | 5:8814d6de77d0 | 40 | _x = WIDTH-4; |
el17m2h | 5:8814d6de77d0 | 41 | } |
el17m2h | 6:848d1e4c1a31 | 42 | if (_x < _radius){ // left side |
el17m2h | 5:8814d6de77d0 | 43 | _x = _radius; |
el17m2h | 6:848d1e4c1a31 | 44 | } |
el17m2h | 4:8ec314f806ae | 45 | } |
el17m2h | 4:8ec314f806ae | 46 | |
el17m2h | 4:8ec314f806ae | 47 | |
el17m2h | 7:0d9cee90ab0d | 48 | Vector2D Doodler::get_position(){ |
el17m2h | 4:8ec314f806ae | 49 | Vector2D p = {_x,_y}; |
el17m2h | 4:8ec314f806ae | 50 | return p; |
el17m2h | 4:8ec314f806ae | 51 | } |
el17m2h | 7:0d9cee90ab0d | 52 | |
el17m2h | 7:0d9cee90ab0d | 53 | Vector2D Doodler::get_velocity(){ |
el17m2h | 7:0d9cee90ab0d | 54 | Vector2D v = {_velocity.x, _velocity.y}; |
el17m2h | 7:0d9cee90ab0d | 55 | return v; |
el17m2h | 7:0d9cee90ab0d | 56 | } |
el17m2h | 7:0d9cee90ab0d | 57 | |
el17m2h | 7:0d9cee90ab0d | 58 | void Doodler::set_velocity(Vector2D v){ |
el17m2h | 7:0d9cee90ab0d | 59 | _velocity.x = v.x; |
el17m2h | 7:0d9cee90ab0d | 60 | _velocity.y = v.y; |
el17m2h | 7:0d9cee90ab0d | 61 | } |
el17m2h | 7:0d9cee90ab0d | 62 | |
el17m2h | 7:0d9cee90ab0d | 63 | void Doodler::set_position(Vector2D p){ |
el17m2h | 7:0d9cee90ab0d | 64 | _x = p.x; |
el17m2h | 7:0d9cee90ab0d | 65 | _y = p.y; |
el17m2h | 7:0d9cee90ab0d | 66 | } |