Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
My_game_clases/Objects.cpp
- Committer:
- thestudent
- Date:
- 2020-04-29
- Revision:
- 14:739115711bf8
- Parent:
- 13:1dbef50789ed
- Child:
- 16:e2aaef863d7c
File content as of revision 14:739115711bf8:
//includes #include "Objects.h" #include "Gamepad.h" #include "N5110.h" #include "mbed.h" int _cannon[6][6] = { { 0,0,1,1,0,0 }, { 0,0,1,1,0,0 }, { 0,1,1,1,1,0 }, { 0,1,1,1,1,0 }, { 1,1,1,1,1,1 }, { 1,1,1,1,1,1 }, }; Objects::Objects() { _cannon_pos = 42; //sets the cannon postion to 0 _initial_shot_pos = 37;// sets the vertical position of the shot which is one above the cannon _shot_incrementer = 1; // default shot incrementer in y direction _shot_y_pos.push_back(_initial_shot_pos); // creates the first shot(was needed here for shot function) _shot_x_pos.push_back(_cannon_pos + 2); _radiuss = 2; // sets the initial ball radiuss }; void Objects::draw_base(N5110 &lcd) { lcd.drawRect(0,46,84,2,FILL_BLACK); }; void Objects::cannon_position(Gamepad &pad) { //reads the sensor float joy_mag = pad.get_mag(); float joy_angle = pad.get_angle(); // checks the joystics position and determines the speed of cannon movement if(joy_mag < 0.5f && _cannon_pos > 1 && joy_angle > 180.0f) { _cannon_pos += -1; } else if(joy_mag < 1.01f && _cannon_pos > 0 && joy_angle > 180.0f) { _cannon_pos += -2; } else if(joy_mag < 0.5f && _cannon_pos < 78 && joy_angle > 0.0f) { _cannon_pos += 1; } else if (joy_mag <= 1.01f && _cannon_pos < 77 && joy_angle > 0.0f) { _cannon_pos += 2; } else { _cannon_pos = _cannon_pos; } } void Objects::draw_cannon(N5110 &lcd) { lcd.drawSprite(_cannon_pos,40,6,6,(int *)_cannon); } void Objects::draw_shots(N5110 &lcd,Gamepad &pad) { for (int i = 0; i < _shot_y_pos.size(); i++) { lcd.drawRect(_shot_x_pos[i],_shot_y_pos[i],2,2,FILL_BLACK); // printf( " %d || %d \n",_shot_x_pos.size(),_shot_y_pos.size()); _shot_y_pos[i] -= _shot_incrementer; // moves the shots upwards } //adds another shot if the distance between //y pos of the initial pos and previous shot pos is more than 7 if(_shot_y_pos[_shot_y_pos.size() - 1 ] + 7 < _initial_shot_pos || _shot_y_pos.size() < 1) { if(pad.A_held()) { _shot_y_pos.push_back(_initial_shot_pos); _shot_x_pos.push_back(_cannon_pos + 2); } } //erasing a shot if the y position is below 0 if(_shot_y_pos[0] <= 0) { _shot_y_pos.erase(_shot_y_pos.begin()); _shot_x_pos.erase(_shot_x_pos.begin()); } } void Objects::draw_ball(N5110 &lcd,int ball_x, int ball_y, int delta_r) { lcd.drawCircle(ball_x,ball_y,_radiuss + delta_r,FILL_BLACK); //lcd.setPixel( ball_x, ball_y,0); } int Objects::get_size() { return _shot_y_pos.size(); }; int Objects::get_x_value(int i) { return _shot_x_pos[i]; } int Objects::get_y_value(int i) { return _shot_y_pos[i]; } void Objects::erase_shot(int i) { _shot_y_pos.erase(_shot_y_pos.begin() + i); _shot_x_pos.erase(_shot_x_pos.begin() + i); } int Objects::get_x_cannon() { return _cannon_pos; }