Arturs Kozlovskis / Mbed 2 deprecated ELEC2645_Project_el18ak

Dependencies:   mbed

Committer:
thestudent
Date:
Fri Apr 10 08:24:48 2020 +0000
Revision:
9:4b11ee1155ad
Parent:
8:09eb8fe2bb20
Child:
10:f5b920a6a71a
Got the shots to interact with the balls

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 9:4b11ee1155ad 20 _shot_incrementer = 1;
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 lcd.drawRect(_shot_x_pos[i],_shot_y_pos[i],2,2,FILL_BLACK);
thestudent 9:4b11ee1155ad 58 // printf( " %d || %d \n",_shot_x_pos.size(),_shot_y_pos.size());
thestudent 6:33bdb54c2c88 59 _shot_y_pos[i] -= _shot_incrementer; // moves the shots upwards
thestudent 6:33bdb54c2c88 60 }
thestudent 9:4b11ee1155ad 61
thestudent 6:33bdb54c2c88 62 //adds another shot if the distance between
thestudent 7:82079de8bcd6 63 //y pos of the initial pos and previous shot pos is more than 7
thestudent 9:4b11ee1155ad 64 if(_shot_y_pos[_shot_y_pos.size() - 1 ] + 7 < _initial_shot_pos || _shot_y_pos.size() < 1) {
thestudent 6:33bdb54c2c88 65 _shot_y_pos.push_back(_initial_shot_pos);
thestudent 6:33bdb54c2c88 66 _shot_x_pos.push_back(_cannon_pos + 2);
thestudent 6:33bdb54c2c88 67 }
thestudent 6:33bdb54c2c88 68 //erasing a shot if the y position is below 0
thestudent 6:33bdb54c2c88 69 if(_shot_y_pos[0] <= 0) {
thestudent 6:33bdb54c2c88 70 _shot_y_pos.erase(_shot_y_pos.begin());
thestudent 6:33bdb54c2c88 71 _shot_x_pos.erase(_shot_x_pos.begin());
thestudent 6:33bdb54c2c88 72 }
thestudent 9:4b11ee1155ad 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 9:4b11ee1155ad 78 lcd.setPixel( ball_x, ball_y,0);
thestudent 9:4b11ee1155ad 79 }
thestudent 9:4b11ee1155ad 80
thestudent 9:4b11ee1155ad 81 int Objects::get_size(){
thestudent 9:4b11ee1155ad 82 return _shot_y_pos.size();
thestudent 9:4b11ee1155ad 83 };
thestudent 9:4b11ee1155ad 84
thestudent 9:4b11ee1155ad 85 int Objects::get_x_value(int i){
thestudent 9:4b11ee1155ad 86 return _shot_x_pos[i];
thestudent 9:4b11ee1155ad 87 }
thestudent 9:4b11ee1155ad 88
thestudent 9:4b11ee1155ad 89 int Objects::get_y_value(int i){
thestudent 9:4b11ee1155ad 90 return _shot_y_pos[i];
thestudent 9:4b11ee1155ad 91 }
thestudent 9:4b11ee1155ad 92 void Objects::erase_shot(int i){
thestudent 9:4b11ee1155ad 93 _shot_y_pos.erase(_shot_y_pos.begin() + i);
thestudent 9:4b11ee1155ad 94 _shot_x_pos.erase(_shot_x_pos.begin() + i);
thestudent 6:33bdb54c2c88 95 }