ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Tue Apr 23 23:14:06 2019 +0000
Revision:
15:4efa04a6a376
Parent:
14:529f798adae4
Child:
16:e0542761fc8c
Changed the main function so that the back button takes the user back to the welcome menu.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17m2h 4:8ec314f806ae 1 #include "Doodler.h"
el17m2h 4:8ec314f806ae 2 Doodler::Doodler(){
el17m2h 4:8ec314f806ae 3 }
el17m2h 4:8ec314f806ae 4 Doodler::~Doodler(){
el17m2h 4:8ec314f806ae 5 }
el17m2h 4:8ec314f806ae 6
el17m2h 14:529f798adae4 7 void Doodler::init(int radius, float position_x, float position_y, double velocity_y){
el17m2h 7:0d9cee90ab0d 8 // initial position of doodler at centre
el17m2h 5:8814d6de77d0 9 _radius = radius;
el17m2h 15:4efa04a6a376 10 _gravity = 1.1; // moves down
el17m2h 15:4efa04a6a376 11 _up = -0.5; // elasticity
el17m2h 14:529f798adae4 12 _position_x = position_x;
el17m2h 14:529f798adae4 13 _position_y = position_y;
el17m2h 14:529f798adae4 14
el17m2h 15:4efa04a6a376 15 _velocity_y = (double)velocity_y;
el17m2h 14:529f798adae4 16 if ((double)_velocity_y > 0.00){ // no jump
el17m2h 14:529f798adae4 17 _velocity_y = (double)_velocity_y*(double)_gravity; // falls accelerating
el17m2h 14:529f798adae4 18 } else if ( (double)_velocity_y == 0.00){
el17m2h 14:529f798adae4 19 _velocity_y = (double)_gravity;
el17m2h 14:529f798adae4 20 } else {
el17m2h 14:529f798adae4 21 _velocity_y = -((double)_velocity_y * (double)_up); // jumps decelerating upwards (0<_up<1 so _velocity.y reaches 0)
el17m2h 14:529f798adae4 22 if (fabs((double)_velocity_y) < 0.008){ // decelerated completely
el17m2h 14:529f798adae4 23 _velocity_y = 0.00;
el17m2h 14:529f798adae4 24 }
el17m2h 14:529f798adae4 25 }
el17m2h 4:8ec314f806ae 26 }
el17m2h 4:8ec314f806ae 27
el17m2h 4:8ec314f806ae 28 void Doodler::draw(N5110 &lcd){
el17m2h 14:529f798adae4 29 lcd.drawCircle(_position_x, _position_y, _radius, FILL_BLACK);
el17m2h 4:8ec314f806ae 30 }
el17m2h 4:8ec314f806ae 31
el17m2h 15:4efa04a6a376 32 void Doodler::update(Direction d, float mag){
el17m2h 15:4efa04a6a376 33 // 5 frames times the magnitude of the joystick (int holds that value)
el17m2h 15:4efa04a6a376 34 _velocity_x = int(mag*5.0f);
el17m2h 5:8814d6de77d0 35 if (d == W){ // if direction is left
el17m2h 14:529f798adae4 36 _position_x -= _velocity_x;
el17m2h 5:8814d6de77d0 37 } else if (d == E){
el17m2h 14:529f798adae4 38 _position_x += _velocity_x;
el17m2h 8:90e789413e0b 39 }
el17m2h 9:5e53bca2a4c2 40 // checking doodler does not leave screen:
el17m2h 14:529f798adae4 41 if (_position_x > WIDTH-4.00){ // right side
el17m2h 14:529f798adae4 42 _position_x = WIDTH-4.00;
el17m2h 9:5e53bca2a4c2 43 }
el17m2h 14:529f798adae4 44 if (_position_x < _radius){ // left side
el17m2h 14:529f798adae4 45 _position_x = _radius;
el17m2h 9:5e53bca2a4c2 46 }
el17m2h 15:4efa04a6a376 47 _velocity_y = (double)get_velocity_y();
el17m2h 15:4efa04a6a376 48 if ((double)_velocity_y >= 0.00){ // no jump
el17m2h 15:4efa04a6a376 49 _position_y += _velocity_y;
el17m2h 15:4efa04a6a376 50 } else { // jump has started until vel = 0
el17m2h 15:4efa04a6a376 51 _position_y += _velocity_y*5;
el17m2h 15:4efa04a6a376 52 }
el17m2h 4:8ec314f806ae 53 }
el17m2h 4:8ec314f806ae 54
el17m2h 10:e1d2289705ef 55 float Doodler::get_position_x(){
el17m2h 14:529f798adae4 56 float p_x = _position_x;
el17m2h 10:e1d2289705ef 57 return p_x;
el17m2h 10:e1d2289705ef 58 }
el17m2h 10:e1d2289705ef 59
el17m2h 10:e1d2289705ef 60 float Doodler::get_position_y(){
el17m2h 14:529f798adae4 61 float p_y = _position_y;
el17m2h 10:e1d2289705ef 62 return p_y;
el17m2h 4:8ec314f806ae 63 }
el17m2h 7:0d9cee90ab0d 64
el17m2h 10:e1d2289705ef 65 float Doodler::get_velocity_x(){
el17m2h 10:e1d2289705ef 66 float v_x = _velocity_x;
el17m2h 10:e1d2289705ef 67 return v_x;
el17m2h 10:e1d2289705ef 68 }
el17m2h 10:e1d2289705ef 69
el17m2h 10:e1d2289705ef 70 double Doodler::get_velocity_y(){
el17m2h 10:e1d2289705ef 71 double v_y = (double)_velocity_y;
el17m2h 10:e1d2289705ef 72 return (double)v_y;
el17m2h 7:0d9cee90ab0d 73 }
el17m2h 7:0d9cee90ab0d 74
el17m2h 14:529f798adae4 75 void Doodler::set_velocity(float vel_x, double vel_y){
el17m2h 14:529f798adae4 76 _velocity_x = vel_x;
el17m2h 14:529f798adae4 77 _velocity_y = (double)vel_y;
el17m2h 7:0d9cee90ab0d 78 }
el17m2h 7:0d9cee90ab0d 79
el17m2h 14:529f798adae4 80 void Doodler::set_position(float pos_x, float pos_y){
el17m2h 14:529f798adae4 81 _position_x = pos_x;
el17m2h 14:529f798adae4 82 _position_y = pos_y;
el17m2h 7:0d9cee90ab0d 83 }