Arturs Kozlovskis / Mbed 2 deprecated ELEC2645_Project_el18ak

Dependencies:   mbed

Committer:
thestudent
Date:
Wed Apr 29 13:51:14 2020 +0000
Revision:
14:739115711bf8
Parent:
13:1dbef50789ed
Child:
16:e2aaef863d7c
Before changing parabolic ball 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 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 14:739115711bf8 54 void Objects::draw_shots(N5110 &lcd,Gamepad &pad)
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 14:739115711bf8 66 if(pad.A_held()) {
thestudent 14:739115711bf8 67 _shot_y_pos.push_back(_initial_shot_pos);
thestudent 14:739115711bf8 68 _shot_x_pos.push_back(_cannon_pos + 2);
thestudent 14:739115711bf8 69 }
thestudent 6:33bdb54c2c88 70 }
thestudent 6:33bdb54c2c88 71 //erasing a shot if the y position is below 0
thestudent 6:33bdb54c2c88 72 if(_shot_y_pos[0] <= 0) {
thestudent 6:33bdb54c2c88 73 _shot_y_pos.erase(_shot_y_pos.begin());
thestudent 6:33bdb54c2c88 74 _shot_x_pos.erase(_shot_x_pos.begin());
thestudent 6:33bdb54c2c88 75 }
thestudent 11:4722bf70b2be 76
thestudent 6:33bdb54c2c88 77 }
thestudent 6:33bdb54c2c88 78 void Objects::draw_ball(N5110 &lcd,int ball_x, int ball_y, int delta_r)
thestudent 11:4722bf70b2be 79 {
thestudent 6:33bdb54c2c88 80 lcd.drawCircle(ball_x,ball_y,_radiuss + delta_r,FILL_BLACK);
thestudent 10:f5b920a6a71a 81 //lcd.setPixel( ball_x, ball_y,0);
thestudent 9:4b11ee1155ad 82 }
thestudent 9:4b11ee1155ad 83
thestudent 11:4722bf70b2be 84 int Objects::get_size()
thestudent 11:4722bf70b2be 85 {
thestudent 11:4722bf70b2be 86 return _shot_y_pos.size();
thestudent 9:4b11ee1155ad 87 };
thestudent 9:4b11ee1155ad 88
thestudent 11:4722bf70b2be 89 int Objects::get_x_value(int i)
thestudent 11:4722bf70b2be 90 {
thestudent 9:4b11ee1155ad 91 return _shot_x_pos[i];
thestudent 9:4b11ee1155ad 92 }
thestudent 9:4b11ee1155ad 93
thestudent 11:4722bf70b2be 94 int Objects::get_y_value(int i)
thestudent 11:4722bf70b2be 95 {
thestudent 9:4b11ee1155ad 96 return _shot_y_pos[i];
thestudent 9:4b11ee1155ad 97 }
thestudent 11:4722bf70b2be 98 void Objects::erase_shot(int i)
thestudent 11:4722bf70b2be 99 {
thestudent 11:4722bf70b2be 100 _shot_y_pos.erase(_shot_y_pos.begin() + i);
thestudent 11:4722bf70b2be 101 _shot_x_pos.erase(_shot_x_pos.begin() + i);
thestudent 10:f5b920a6a71a 102 }
thestudent 11:4722bf70b2be 103 int Objects::get_x_cannon()
thestudent 11:4722bf70b2be 104 {
thestudent 10:f5b920a6a71a 105 return _cannon_pos;
thestudent 6:33bdb54c2c88 106 }