Reputation: 1
I am trying to make collisions for my platformer game in SFML, I saw somewhere that you can use sprite.getBounds().intersects()
for collision, but I'm not sure what exactly to do afterwards. I didn't try to do anything yet.
Here's my code:
int main()
{
// Variables
RenderWindow window(VideoMode(900, 900), "Daisy the Frog", Style::Close | Style::Titlebar);
float dt;
Clock clock;
// Texture Variables
Texture frogFront;
Texture bg;
Texture platform;
Texture frogLeft;
Texture frogLeftAnim;
Sprite frogL;
Sprite frogLA;
Sprite frogF;
Sprite bgs;
Sprite platforms;
// Code for the txts
frogF.setPosition(Vector2f(200.f, 500.f));
platforms.setScale(Vector2f(6.f, 4.5f));
platforms.setPosition(Vector2f(0.f, 330.f));
View view(FloatRect(200.f, 200.f, 300.f, 200.f));
// Player config
const float movementSpeed = 250.f;
Vector2f velocity;
if (!frogFront.loadFromFile("frog_front.png")) {
cout << "Couldn't load texture" << endl;
}
if (!bg.loadFromFile("bg.png")) {
cout << "Couldn't load texture" << endl;
}
if (!platform.loadFromFile("platform.png")) {
cout << "Couldn't load texture" << endl;
}
if (!frogLeft.loadFromFile("frog_left.png")) {
cout << "Couldn't load texture" << endl;
}
if (!frogLeftAnim.loadFromFile("frog_left_walkanim.png")) {
cout << "Couldn't load texture" << endl;
}
bgs.setTexture(bg);
frogF.setTexture(frogFront);
platforms.setTexture(platform);
frogL.setTexture(frogLeft);
frogLA.setTexture(frogLeftAnim);
while (window.isOpen()) {
Event event;
dt = clock.restart().asSeconds();
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
frogF.setScale(Vector2f(5.f, 5.f));
bgs.setScale(Vector2f(7.24f, 7.24f));
// Player config 2
velocity.x = 0.f;
velocity.y = 0.f;
if (Keyboard::isKeyPressed(sf::Keyboard::A)) {
velocity.x += -movementSpeed * dt;
} else if (Keyboard::isKeyPressed(sf::Keyboard::D)) {
velocity.x += movementSpeed * dt;
} else if (Keyboard::isKeyPressed(Keyboard::Space)) {
velocity.y += -movementSpeed * dt;
}
frogF.move(velocity);
window.draw(bgs);
window.draw(platforms);
window.draw(frogF);
window.display();
}
return 0;
}
I need collisions with the player and the platform
Upvotes: 0
Views: 225