ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Sat Apr 13 19:50:00 2019 +0000
Revision:
6:848d1e4c1a31
Parent:
5:8814d6de77d0
Child:
7:0d9cee90ab0d
I created a jump function in the doodler file in order to make the doodler constantly jump vertically if it collides with any of the floors.

Who changed what in which revision?

UserRevisionLine numberNew 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 4:8ec314f806ae 9
el17m2h 5:8814d6de77d0 10 // initial position of doodler at middle
el17m2h 5:8814d6de77d0 11 _x = (70/2)+6; // middle
el17m2h 5:8814d6de77d0 12 _y = 43; // if doodler is above the middle, the screen shifts up (floors shift down)
el17m2h 5:8814d6de77d0 13 _radius = radius;
el17m2h 5:8814d6de77d0 14 _speed =0.5; // default speed
el17m2h 4:8ec314f806ae 15 }
el17m2h 4:8ec314f806ae 16
el17m2h 4:8ec314f806ae 17 void Doodler::draw(N5110 &lcd){
el17m2h 4:8ec314f806ae 18 lcd.drawCircle(_x, _y, _radius, FILL_BLACK);
el17m2h 4:8ec314f806ae 19 }
el17m2h 4:8ec314f806ae 20
el17m2h 5:8814d6de77d0 21 void Doodler::update(Direction d, float mag){
el17m2h 5:8814d6de77d0 22 _speed = int(mag*10.0f);
el17m2h 5:8814d6de77d0 23
el17m2h 5:8814d6de77d0 24 if (d == W){ // if direction is left
el17m2h 5:8814d6de77d0 25 _x-= _speed;
el17m2h 5:8814d6de77d0 26 } else if (d == E){
el17m2h 5:8814d6de77d0 27 _x+= _speed;
el17m2h 5:8814d6de77d0 28 }
el17m2h 5:8814d6de77d0 29 // checking doodler does not leave screen:
el17m2h 6:848d1e4c1a31 30 if (_x > WIDTH-4){ // right side
el17m2h 5:8814d6de77d0 31 _x = WIDTH-4;
el17m2h 5:8814d6de77d0 32 }
el17m2h 6:848d1e4c1a31 33 if (_x < _radius){ // left side
el17m2h 5:8814d6de77d0 34 _x = _radius;
el17m2h 6:848d1e4c1a31 35 }
el17m2h 4:8ec314f806ae 36 }
el17m2h 4:8ec314f806ae 37
el17m2h 6:848d1e4c1a31 38 void Doodler::jump(){
el17m2h 6:848d1e4c1a31 39 _y-= _speed; // (y max = 43, y min = 28)
el17m2h 6:848d1e4c1a31 40
el17m2h 6:848d1e4c1a31 41 if ( _y < 28){
el17m2h 6:848d1e4c1a31 42 _y+= _speed;
el17m2h 6:848d1e4c1a31 43 }
el17m2h 6:848d1e4c1a31 44 }
el17m2h 4:8ec314f806ae 45
el17m2h 4:8ec314f806ae 46 Vector2D Doodler::get_pos(){
el17m2h 4:8ec314f806ae 47 Vector2D p = {_x,_y};
el17m2h 4:8ec314f806ae 48 return p;
el17m2h 4:8ec314f806ae 49 }