Animating background on SFML C++
Hello,
I am very knew to programming and have started my first project with C++ and SFML. I am learning quickly, but there is a problem I have ran into that I can not seem to solve myself. I have written the if and else statements to get my bee and clouds to activated and float across the screen right to left. However, when I run the program the bee and the clouds do not float across the x axis. I have noticed that the y position does randomize and place the clouds in a different starting spot each time, but once again the x axis animation is not working.
can anybody see a fault in my code or help a new programmer out?
Thank you,
//This telling the computer which header to find
include <SFML/Graphics.hpp>
using namespace sf;
//This is where the game starts from
int main() {
VideoMode vm(1920, 1080);
RenderWindow window(vm, "Timber!!!!", Style::Fullscreen);
//NOTE THAT WE WRITE BACKGROUND FIRST, COMP WILL LAYER GRAPHICS BY ORDER
Texture textureBackground;
textureBackground.loadFromFile("graphics/background.png");
//Background Block
Sprite spriteBackground;
spriteBackground.setTexture(textureBackground);
spriteBackground.setPosition(0, 0);
//Tree Block
Texture textureTree;
textureTree.loadFromFile("graphics/tree.png");
Sprite spriteTree;
spriteTree.setTexture(textureTree);
spriteTree.setPosition(810, 0);
//Branch Block
Texture textureBranch;
textureBranch.loadFromFile("graphics/branch.png");
Sprite spriteBranch;
spriteBranch.setTexture(textureBranch);
spriteBranch.setPosition(1100, 400);
//Player Block
Texture texturePlayer;
texturePlayer.loadFromFile("graphics/player.png");
Sprite spritePlayer;
spritePlayer.setTexture(texturePlayer);
spritePlayer.setPosition(1200, 800);
//Bee Block
Texture textureBee;
textureBee.loadFromFile("graphics/bee.png");
Sprite spriteBee;
spriteBee.setTexture(textureBee);
spriteBee.setPosition(0, 800);
//Bee activity set to false
bool beeActive = false;
//Bee Speed
float beeSpeed = 0.0f;
//Building Clouds 1 2 3
Texture textureCloud;
textureCloud.loadFromFile("graphics/cloud.png");
Sprite spriteCloud1;
Sprite spriteCloud2;
Sprite spriteCloud3;
spriteCloud1.setTexture(textureCloud);
spriteCloud2.setTexture(textureCloud);
spriteCloud3.setTexture(textureCloud);
spriteCloud1.setPosition(0, 0);
spriteCloud2.setPosition(0, 250);
spriteCloud3.setPosition(0, 500);
//Setting Cloud activity to false
bool cloud1Active = false;
bool cloud2Active = false;
bool cloud3Active = false;
//Cloud speed
float cloud1Speed = 0.0f;
float cloud2Speed = 0.0f;
float cloud3Speed = 0.0f;
Clock clock;
Time dt = clock.restart();
if (!beeActive)
{
srand((int)time(0) * 10);
beeSpeed = (rand() % 200) + 200;
srand((int)time(0) * 10);
float height = (rand() % 500) + 500;
spriteBee.setPosition(2000, height);
beeActive = true;
}
else
{
spriteBee.setPosition(
spriteBee.getPosition().x -
(beeSpeed * dt.asSeconds()),
spriteBee.getPosition().y);
if (spriteBee.getPosition().x < -100)
{
beeActive = false;
}
}
if (!cloud1Active)
{
srand((int)time(0) * 10);
cloud1Speed = (rand() % 200);
srand((int)time(0) * 10);
float height = (rand() % 150);
spriteCloud1.setPosition(-250, height);
cloud1Active = true;
}
else
{
spriteCloud1.setPosition(
spriteCloud1.getPosition().x +
(cloud1Speed * dt.asSeconds()),
spriteCloud1.getPosition().y);
if (spriteCloud1.getPosition().x > 1920)
{
cloud1Active = false;
}
}
if (!cloud2Active)
{
srand((int)time(0) * 20);
cloud2Speed = (rand() % 200);
srand((int)time(0) * 10);
float height = (rand() % 300) - 150;
spriteCloud2.setPosition(-250, height);
cloud2Active = true;
}
else
{
spriteCloud2.setPosition(
spriteCloud2.getPosition().x +
(cloud2Speed * dt.asSeconds()),
spriteCloud2.getPosition().y);
if (spriteCloud2.getPosition().x > 1920)
{
cloud2Active = false;
}
}
if (!cloud3Active)
{
srand((int)time(0) * 30);
cloud3Speed = (rand() % 200);
srand((int)time(0) * 30);
float height = (rand() % 450) - 150;
spriteCloud3.setPosition(-250, height);
cloud3Active = true;
}
else
{
spriteCloud3.setPosition(
spriteCloud3.getPosition().x +
(cloud3Speed * dt.asSeconds()),
spriteCloud3.getPosition().y);
if (spriteCloud3.getPosition().x > 1920)
{
cloud3Active = false;
}
}
while (window.isOpen()) {
if (Keyboard::isKeyPressed(Keyboard::Escape)) {
window.close();
}
window.clear();
window.draw(spriteBackground);
window.draw(spriteCloud1);
window.draw(spriteCloud2);
window.draw(spriteCloud3);
window.draw(spriteTree);
window.draw(spriteBranch);
window.draw(spritePlayer);
window.draw(spriteBee);
window.display();
}
return 0;
}