Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to shoot on direction of ship asteroids sfml #40

Open
mahmoud31415 opened this issue Sep 7, 2020 · 0 comments
Open

how to shoot on direction of ship asteroids sfml #40

mahmoud31415 opened this issue Sep 7, 2020 · 0 comments

Comments

@mahmoud31415
Copy link

#include <SFML/Graphics.hpp>
#include
#include
#include
#include <SFML/Audio.hpp>
#include
#include

using namespace std;

float d = 0.017453f;

float dd = 0.017453f;
int main()
{
sf::Font font;
if (!font.loadFromFile("/system/fonts/Roboto-Regular.ttf"))
return EXIT_FAILURE;

int wx=1080,wy=2340;
sf::RenderWindow app(sf::VideoMode(wx,wy),"my app");

float x=500,y=300;
float xx=xx,yy=y;
float dy=0,dx=0,angle=0;
float dyy=0,dxx=0,anglee=0;
bool go=false;
int l=0;
//////// the Ship
sf::CircleShape ship(50,3);
ship.setOutlineThickness(5);
ship.setFillColor(sf::Color(0,0,0));
ship.setScale(1,1.4);
ship.setOrigin(25,25);
//////The buttons
sf::CircleShape but(50,3);
but.setScale(1,1.4);
but.setFillColor(sf::Color(200,200,0));
vector<sf::CircleShape >butt(5);
for(int i=0;i<5;i++)
{
butt[i]=but;
butt[0].setPosition(200,100);
butt[1].setPosition(150,350);
butt[1].rotate(45);
butt[2].setPosition(200,2000);
butt[2].rotate(-90);
}
/////

//////////the fire

sf::CircleShape f(10);

vector<sf::CircleShape >fire;
fire.push_back(sf::CircleShape (f));

while(app.isOpen())
{
sf::Event event;
while(app.pollEvent(event))
{
if(event.type == sf::Event::Closed)
app.close();
}
butt[3].setPosition(1000,500);
butt[4].setPosition(1000,700);

for(int i=0;i<2;i++)
if (event.type == sf::Event::TouchMoved)
{

sf::Vector2i touchpos = sf::Touch::getPosition(0);
fire.push_back(sf::CircleShape (f));

f.setPosition(ship.getPosition().x,ship.getPosition().y);
l+=1;
}

xx+=sin(angle)*20;
yy+=cos(angle)*20;
for(int j=0;j<fire.size();j+=1)
{

// fire[j].setPosition(xx,yy);
anglee=sin(angle);
fire[j].move(anglee*20,cos(angle)*20);
//fire[j].setPosition();

}

for(int i=0;i<=1;i++)
if(sf::Touch::isDown(i))
{
sf::Vector2i touchpo = sf::Touch::getPosition(1);
butt[4].setPosition(touchpo.x,touchpo.y);
sf::Vector2i touchpos = sf::Touch::getPosition(0);
go=false;
butt[3].setPosition(touchpos.x,touchpos.y);

}

if(butt[3].getGlobalBounds().intersects(butt[0].getGlobalBounds()))angle-=3;
if(butt[3].getGlobalBounds().intersects(butt[1].getGlobalBounds()))angle+=3;
if(butt[3].getGlobalBounds().intersects(butt[2].getGlobalBounds())||butt[4].getGlobalBounds().intersects(butt[2].getGlobalBounds()))go=true;

else go=false;
if(butt[4].getGlobalBounds().intersects(butt[0].getGlobalBounds()))angle-=3;
if(butt[4].getGlobalBounds().intersects(butt[1].getGlobalBounds()))angle+=3;

if(go)
{
dxx+=cos(anglee*dd)10;
dyy+=sin(anglee
dd)*10;

dx+=cos(angle*d)0.2;
dy+=sin(angle
d)*0.2;

}
else
{
////سرعة التوقف
dx*=0.99;
dy*=0.99;

}

int max=15;
float sp=sqrt(dxdx+dydy);
if(sp>max)
{
dx*=max/sp;
dy*=max/sp;
}

x+=dx;
y+=dy;

if(x>1100)x=-100;
if(y>2400)y=-100;
if(x<-100)x=1100;
if(y<-100)y=2400;

ship.setPosition(x,y);
ship.setRotation(angle+90);

/////////////
stringstream ss;
ss<< ship.getRotation();
string s;
ss>>s;
sf::Text text(s, font, 50);
text.setPosition(100,5);

////shoing iny thing here draw

for(int i=0;i<fire.size();i++)
{
app.draw(fire[i]);

}
for(int i=0;i<3;i++)
app.draw(butt[i]);
app.draw(text);
app.draw(ship);
app.display();
}
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant