Arturs Kozlovskis / Mbed 2 deprecated ELEC2645_Project_el18ak

Dependencies:   mbed

Committer:
thestudent
Date:
Wed Apr 29 12:48:40 2020 +0000
Revision:
13:1dbef50789ed
Parent:
11:4722bf70b2be
Child:
14:739115711bf8
Finished with linear ball design and movement

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 13:1dbef50789ed 18 _cannon_pos = 42; //sets the cannon postion to 0
thestudent 6:33bdb54c2c88 19 _initial_shot_pos = 37;// sets the vertical position of the shot which is one above the cannon
thestudent 10:f5b920a6a71a 20 _shot_incrementer = 1; // default shot incrementer in y direction
thestudent 10:f5b920a6a71a 21 _shot_y_pos.push_back(_initial_shot_pos); // creates the first shot(was needed here for shot function)
thestudent 6:33bdb54c2c88 22 _shot_x_pos.push_back(_cannon_pos + 2);
thestudent 10:f5b920a6a71a 23 _radiuss = 2; // sets the initial ball radiuss
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 10:f5b920a6a71a 32 //reads the sensor
thestudent 6:33bdb54c2c88 33 float joy_mag = pad.get_mag();
thestudent 6:33bdb54c2c88 34 float joy_angle = pad.get_angle();
thestudent 10:f5b920a6a71a 35 // checks the joystics position and determines the speed of cannon movement
thestudent 11:4722bf70b2be 36 if(joy_mag < 0.5f && _cannon_pos > 1 && joy_angle > 180.0f) {
thestudent 6:33bdb54c2c88 37 _cannon_pos += -1;
thestudent 11:4722bf70b2be 38 } else if(joy_mag < 1.01f && _cannon_pos > 0 && joy_angle > 180.0f) {
thestudent 6:33bdb54c2c88 39 _cannon_pos += -2;
thestudent 11:4722bf70b2be 40 } else if(joy_mag < 0.5f && _cannon_pos < 78 && joy_angle > 0.0f) {
thestudent 6:33bdb54c2c88 41 _cannon_pos += 1;
thestudent 11:4722bf70b2be 42 } else if (joy_mag <= 1.01f && _cannon_pos < 77 && joy_angle > 0.0f) {
thestudent 6:33bdb54c2c88 43 _cannon_pos += 2;
thestudent 6:33bdb54c2c88 44 } else {
thestudent 6:33bdb54c2c88 45 _cannon_pos = _cannon_pos;
thestudent 6:33bdb54c2c88 46 }
thestudent 6:33bdb54c2c88 47 }
thestudent 6:33bdb54c2c88 48 void Objects::draw_cannon(N5110 &lcd)
thestudent 6:33bdb54c2c88 49 {
thestudent 6:33bdb54c2c88 50
thestudent 6:33bdb54c2c88 51 lcd.drawSprite(_cannon_pos,40,6,6,(int *)_cannon);
thestudent 6:33bdb54c2c88 52
thestudent 6:33bdb54c2c88 53 }
thestudent 6:33bdb54c2c88 54 void Objects::draw_shots(N5110 &lcd)
thestudent 6:33bdb54c2c88 55 {
thestudent 6:33bdb54c2c88 56
thestudent 6:33bdb54c2c88 57 for (int i = 0; i < _shot_y_pos.size(); i++) {
thestudent 6:33bdb54c2c88 58 lcd.drawRect(_shot_x_pos[i],_shot_y_pos[i],2,2,FILL_BLACK);
thestudent 11:4722bf70b2be 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 11:4722bf70b2be 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 9:4b11ee1155ad 65 if(_shot_y_pos[_shot_y_pos.size() - 1 ] + 7 < _initial_shot_pos || _shot_y_pos.size() < 1) {
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 11:4722bf70b2be 74
thestudent 6:33bdb54c2c88 75 }
thestudent 6:33bdb54c2c88 76 void Objects::draw_ball(N5110 &lcd,int ball_x, int ball_y, int delta_r)
thestudent 11:4722bf70b2be 77 {
thestudent 6:33bdb54c2c88 78 lcd.drawCircle(ball_x,ball_y,_radiuss + delta_r,FILL_BLACK);
thestudent 10:f5b920a6a71a 79 //lcd.setPixel( ball_x, ball_y,0);
thestudent 9:4b11ee1155ad 80 }
thestudent 9:4b11ee1155ad 81
thestudent 11:4722bf70b2be 82 int Objects::get_size()
thestudent 11:4722bf70b2be 83 {
thestudent 11:4722bf70b2be 84 return _shot_y_pos.size();
thestudent 9:4b11ee1155ad 85 };
thestudent 9:4b11ee1155ad 86
thestudent 11:4722bf70b2be 87 int Objects::get_x_value(int i)
thestudent 11:4722bf70b2be 88 {
thestudent 9:4b11ee1155ad 89 return _shot_x_pos[i];
thestudent 9:4b11ee1155ad 90 }
thestudent 9:4b11ee1155ad 91
thestudent 11:4722bf70b2be 92 int Objects::get_y_value(int i)
thestudent 11:4722bf70b2be 93 {
thestudent 9:4b11ee1155ad 94 return _shot_y_pos[i];
thestudent 9:4b11ee1155ad 95 }
thestudent 11:4722bf70b2be 96 void Objects::erase_shot(int i)
thestudent 11:4722bf70b2be 97 {
thestudent 11:4722bf70b2be 98 _shot_y_pos.erase(_shot_y_pos.begin() + i);
thestudent 11:4722bf70b2be 99 _shot_x_pos.erase(_shot_x_pos.begin() + i);
thestudent 10:f5b920a6a71a 100 }
thestudent 11:4722bf70b2be 101 int Objects::get_x_cannon()
thestudent 11:4722bf70b2be 102 {
thestudent 10:f5b920a6a71a 103 return _cannon_pos;
thestudent 6:33bdb54c2c88 104 }