Arturs Kozlovskis / Mbed 2 deprecated ELEC2645_Project_el18ak

Dependencies:   mbed

Committer:
thestudent
Date:
Thu Apr 02 11:51:44 2020 +0000
Revision:
7:82079de8bcd6
Parent:
6:33bdb54c2c88
Child:
8:09eb8fe2bb20
Added a new function to the balls. They can now go in linear or parabolic way. Optimized screen speed. Next is movement randomizer function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thestudent 6:33bdb54c2c88 1 //includes
thestudent 6:33bdb54c2c88 2 #include "Objects.h"
thestudent 6:33bdb54c2c88 3 #include "Gamepad.h"
thestudent 6:33bdb54c2c88 4 #include "N5110.h"
thestudent 6:33bdb54c2c88 5 #include "mbed.h"
thestudent 6:33bdb54c2c88 6
thestudent 6:33bdb54c2c88 7 int _cannon[6][6] = {
thestudent 6:33bdb54c2c88 8 { 0,0,1,1,0,0 },
thestudent 6:33bdb54c2c88 9 { 0,0,1,1,0,0 },
thestudent 6:33bdb54c2c88 10 { 0,1,1,1,1,0 },
thestudent 6:33bdb54c2c88 11 { 0,1,1,1,1,0 },
thestudent 6:33bdb54c2c88 12 { 1,1,1,1,1,1 },
thestudent 6:33bdb54c2c88 13 { 1,1,1,1,1,1 },
thestudent 6:33bdb54c2c88 14 };
thestudent 6:33bdb54c2c88 15
thestudent 6:33bdb54c2c88 16 Objects::Objects()
thestudent 6:33bdb54c2c88 17 {
thestudent 6:33bdb54c2c88 18 _cannon_pos = 0; //sets the speed counter to 0
thestudent 6:33bdb54c2c88 19 _initial_shot_pos = 37;// sets the vertical position of the shot which is one above the cannon
thestudent 6:33bdb54c2c88 20 _shot_incrementer = 2;
thestudent 6:33bdb54c2c88 21 _shot_y_pos.push_back(_initial_shot_pos);
thestudent 6:33bdb54c2c88 22 _shot_x_pos.push_back(_cannon_pos + 2);
thestudent 6:33bdb54c2c88 23 _radiuss = 2;
thestudent 6:33bdb54c2c88 24 };
thestudent 6:33bdb54c2c88 25
thestudent 6:33bdb54c2c88 26 void Objects::draw_base(N5110 &lcd)
thestudent 6:33bdb54c2c88 27 {
thestudent 6:33bdb54c2c88 28 lcd.drawRect(0,46,84,2,FILL_BLACK);
thestudent 6:33bdb54c2c88 29 };
thestudent 6:33bdb54c2c88 30 void Objects::cannon_position(Gamepad &pad)
thestudent 6:33bdb54c2c88 31 {
thestudent 6:33bdb54c2c88 32 float joy_mag = pad.get_mag();
thestudent 6:33bdb54c2c88 33 float joy_angle = pad.get_angle();
thestudent 6:33bdb54c2c88 34
thestudent 6:33bdb54c2c88 35 if(joy_mag < 0.5 && _cannon_pos > 1 && joy_angle > 180.0) {
thestudent 6:33bdb54c2c88 36 _cannon_pos += -1;
thestudent 6:33bdb54c2c88 37 } else if(joy_mag < 1.01 && _cannon_pos > 0 && joy_angle > 180.0) {
thestudent 6:33bdb54c2c88 38 _cannon_pos += -2;
thestudent 6:33bdb54c2c88 39 } else if(joy_mag < 0.5 && _cannon_pos < 78 && joy_angle > 0.0) {
thestudent 6:33bdb54c2c88 40 _cannon_pos += 1;
thestudent 6:33bdb54c2c88 41 } else if (joy_mag <= 1.01 && _cannon_pos < 77 && joy_angle > 0.0) {
thestudent 6:33bdb54c2c88 42 _cannon_pos += 2;
thestudent 6:33bdb54c2c88 43 } else {
thestudent 6:33bdb54c2c88 44 _cannon_pos = _cannon_pos;
thestudent 6:33bdb54c2c88 45 }
thestudent 6:33bdb54c2c88 46 }
thestudent 6:33bdb54c2c88 47 void Objects::draw_cannon(N5110 &lcd)
thestudent 6:33bdb54c2c88 48 {
thestudent 6:33bdb54c2c88 49
thestudent 6:33bdb54c2c88 50 lcd.drawSprite(_cannon_pos,40,6,6,(int *)_cannon);
thestudent 6:33bdb54c2c88 51
thestudent 6:33bdb54c2c88 52 }
thestudent 6:33bdb54c2c88 53 void Objects::draw_shots(N5110 &lcd)
thestudent 6:33bdb54c2c88 54 {
thestudent 6:33bdb54c2c88 55
thestudent 6:33bdb54c2c88 56 for (int i = 0; i < _shot_y_pos.size(); i++) {
thestudent 6:33bdb54c2c88 57
thestudent 6:33bdb54c2c88 58 lcd.drawRect(_shot_x_pos[i],_shot_y_pos[i],2,2,FILL_BLACK);
thestudent 7:82079de8bcd6 59 printf( " %d || %d \n",_shot_x_pos.size(),_shot_y_pos.size());
thestudent 6:33bdb54c2c88 60 _shot_y_pos[i] -= _shot_incrementer; // moves the shots upwards
thestudent 6:33bdb54c2c88 61
thestudent 6:33bdb54c2c88 62 }
thestudent 6:33bdb54c2c88 63 //adds another shot if the distance between
thestudent 7:82079de8bcd6 64 //y pos of the initial pos and previous shot pos is more than 7
thestudent 7:82079de8bcd6 65 if(_shot_y_pos[_shot_y_pos.size() - 1 ] + 7 < _initial_shot_pos && _shot_y_pos.size() < 10) {
thestudent 6:33bdb54c2c88 66 _shot_y_pos.push_back(_initial_shot_pos);
thestudent 6:33bdb54c2c88 67 _shot_x_pos.push_back(_cannon_pos + 2);
thestudent 6:33bdb54c2c88 68 }
thestudent 6:33bdb54c2c88 69 //erasing a shot if the y position is below 0
thestudent 6:33bdb54c2c88 70 if(_shot_y_pos[0] <= 0) {
thestudent 6:33bdb54c2c88 71 _shot_y_pos.erase(_shot_y_pos.begin());
thestudent 6:33bdb54c2c88 72 _shot_x_pos.erase(_shot_x_pos.begin());
thestudent 6:33bdb54c2c88 73 }
thestudent 6:33bdb54c2c88 74 }
thestudent 6:33bdb54c2c88 75 void Objects::draw_ball(N5110 &lcd,int ball_x, int ball_y, int delta_r)
thestudent 6:33bdb54c2c88 76 {
thestudent 6:33bdb54c2c88 77 lcd.drawCircle(ball_x,ball_y,_radiuss + delta_r,FILL_BLACK);
thestudent 6:33bdb54c2c88 78 }