I want to check if two shapes are in the same position.
if(sFigures.getPosition() == Triangle.getPosition()){
break;
}
I want to check if two shapes are in the same position.
here is the main
int main(){
srand(time(NULL));
RenderWindow window(VideoMode(800, 600), "ROBOT !");
Texture figures;
float dx = 0.5, dy = 0.5;
if(!figures.loadFromFile("figures2.png")){
cout << "FAILED" << endl;
return -1;
}
CircleShape Triangle(32, 3);
Triangle.setFillColor(Color::Black);
int x1 = rand() % 768, y1 = rand() % 568;
Triangle.setPosition(x1, y1);
Sprite sFigures;
sFigures.setTexture(figures);
sFigures.setTextureRect(IntRect(0, 0, 32, 35));
float x = 400;
float y = 300;
sFigures.setPosition(x, y);
while(window.isOpen()){
Event e;
while(window.pollEvent(e)){
if(e.type == Event::Closed){
window.close();
}if(e.type == Event::LostFocus){
}
}
/////// MOVE CHARACTER ///////
if(Keyboard::isKeyPressed(Keyboard::Down) && sFigures.getPosition().y < 568){
sFigures.move(0,0.5);
sFigures.setTextureRect(IntRect(0, 0, 32, 35));
}else if(Keyboard::isKeyPressed(Keyboard::Up) && sFigures.getPosition().y > 0 ){
sFigures.move(0,-0.5);
sFigures.setTextureRect(IntRect(64, 71, 32, 35));
}else if(Keyboard::isKeyPressed(Keyboard::Left) && sFigures.getPosition().x > 0 ){
sFigures.move(-0.5,0);
sFigures.setTextureRect(IntRect(96, 106, 32, 35));
}else if(Keyboard::isKeyPressed(Keyboard::Right) && sFigures.getPosition().x < 768 ){
sFigures.move(0.5,0);
sFigures.setTextureRect(IntRect(32, 36, 32, 35));
}
///// ENEMY MOVE /////
if(Triangle.getPosition().x == 0){
dx = 0.5;
}else if(Triangle.getPosition().x == 768){
dx = -0.5;
}
if(Triangle.getPosition().y == 0){
dy = 0.5;
}else if (Triangle.getPosition().y == 568){
dy = -0.5;
}
Triangle.move(dx, dy);
///// CHECK //////
if(sFigures.getPosition() == Triangle.getPosition()){
break;
}
window.clear(Color(255, 0, 255));
window.draw(Triangle);
window.draw(sFigures);
window.display();
}
return 0;
}
Is there a better way to do this?
somethings like, checking with the player/enemy radius
sFigures = Player
Triangle = Enemy
ThankYou