Skip to content
Snippets Groups Projects
Commit aa1969af authored by alaskalinuxuser's avatar alaskalinuxuser
Browse files

Section 3 completed, added timebar and text.

parent b29e55e3
No related branches found
No related tags found
No related merge requests found
#include <SFML/Graphics.hpp>
#include <sstream>
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 1024), "Timber!");
int sWidth = 1024;
int sHeight = 786;
sf::RenderWindow window(sf::VideoMode(sWidth, sHeight), "Timber!");
// Create a texture for our background and load it.
sf::Texture textureBackground;
......@@ -20,7 +23,7 @@ int main()
textureTree.loadFromFile("graphics/tree.png");
sf::Sprite spriteTree;
spriteTree.setTexture(textureTree);
spriteTree.setPosition(640,0);
spriteTree.setPosition(sWidth / 2.0f,0);
// Add some clouds
sf::Texture textureCloud;
......@@ -31,9 +34,9 @@ int main()
spriteCloudOne.setTexture(textureCloud);
spriteCloudTwo.setTexture(textureCloud);
spriteCloudThree.setTexture(textureCloud);
spriteCloudOne.setPosition(0,250);
spriteCloudTwo.setPosition(80,125);
spriteCloudThree.setPosition(10,0);
spriteCloudOne.setPosition(sWidth + 100,250);
spriteCloudTwo.setPosition(sWidth + 100,125);
spriteCloudThree.setPosition(sWidth + 100,0);
bool cloudOneActive = false;
bool cloudTwoActive = false;
bool cloudThreeActive = false;
......@@ -46,13 +49,52 @@ int main()
textureBee.loadFromFile("graphics/insect.png");
sf::Sprite spriteBee;
spriteBee.setTexture(textureBee);
spriteBee.setPosition(50,600);
spriteBee.setPosition(sWidth - 100,600);
bool beeActive = false;
float beeSpeed = 0.0f;
// Add a font.
sf::Font font;
font.loadFromFile("fonts/KOMIKAP_.ttf");
// Add startText
sf::Text startText;
startText.setString("Press the Enter key to start!");
startText.setCharacterSize(40);
startText.setColor(sf::Color::White);
startText.setFont(font);
// Set position based on center
sf::FloatRect textRect = startText.getLocalBounds();
startText.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
startText.setPosition(sWidth / 2.0f,sHeight / 2.0f);
// Add scoreText
int scoreInt = 0;
sf::Text scoreText;
scoreText.setString("Score = 0");
scoreText.setCharacterSize(40);
scoreText.setColor(sf::Color::White);
scoreText.setFont(font);
scoreText.setPosition(10,10);
// Add a time bar
sf::RectangleShape timeBar;
float timeBarStartWidth = 400;
float timeBarHeight = 40;
timeBar.setSize(sf::Vector2f(timeBarStartWidth,timeBarHeight));
timeBar.setFillColor(sf::Color::Blue);
timeBar.setPosition(sWidth / 2.0f - (timeBarStartWidth / 2.0f),sHeight - 45);
sf::Time gameTime;
float timeRemains = 6.0f;
float widthPerSecond = timeBarStartWidth / timeRemains;
// Time control
sf::Clock clock;
// Pause the game
bool gamePaused = true;
while (window.isOpen())
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
......@@ -60,6 +102,15 @@ int main()
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {
// Return key is pressed: pause/unpause.
scoreInt = 0;
timeRemains = 6.0f;
gamePaused=false;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
// right mouse button is pressed: exit
window.close();
......@@ -71,8 +122,23 @@ int main()
window.close();
}
if (!gamePaused) {
sf::Time dt = clock.restart();
// Update the time bar.
timeRemains -= dt.asSeconds();
timeBar.setSize(sf::Vector2f(widthPerSecond * timeRemains, timeBarHeight));
if (timeRemains <= 0.0f) {
gamePaused=true;
startText.setString("Time Over! Press Enter to play again!");
sf::FloatRect textRect = startText.getLocalBounds();
startText.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
startText.setPosition(sWidth / 2.0f,sHeight / 2.0f);
}
if (!beeActive) {
// Since the bee is not active.
// Seed our random number with time from system.
......@@ -91,7 +157,7 @@ int main()
spriteBee.getPosition().y);
}
if (spriteBee.getPosition().x > 1300) {
if (spriteBee.getPosition().x > sWidth + 100) {
// The bee is now off screen. Let's move it back.
beeActive = false;
// This will then call the setup for the bee again.
......@@ -106,7 +172,7 @@ int main()
// Now set the Clouds's starting position.
srand((int)time(0) * 10);
float height = ((rand() % 300) + 10);
spriteCloudOne.setPosition(1300,height); //off screen on left.
spriteCloudOne.setPosition(sWidth + 100,height); //off screen on right.
cloudOneActive = true; // Make the cloud active, so only runs once.
} else {
// Since the Cloud is active, let's move it.
......@@ -130,7 +196,7 @@ int main()
// Now set the Clouds's starting position.
srand((int)time(0) * 20);
float height = ((rand() % 200) + 10);
spriteCloudTwo.setPosition(1300,height); //off screen on left.
spriteCloudTwo.setPosition(sWidth + 100,height); //off screen on right.
cloudTwoActive = true; // Make the cloud active, so only runs once.
} else {
// Since the Cloud is active, let's move it.
......@@ -154,7 +220,7 @@ int main()
// Now set the Clouds's starting position.
srand((int)time(0) * 10);
float height = ((rand() % 250) + 10);
spriteCloudThree.setPosition(1300,height); //off screen on left.
spriteCloudThree.setPosition(sWidth + 100,height); //off screen on right.
cloudThreeActive = true; // Make the cloud active, so only runs once.
} else {
// Since the Cloud is active, let's move it.
......@@ -168,6 +234,11 @@ int main()
cloudThreeActive = false;
// This will then call the setup for the cloud again.
}
}
std::stringstream ss;
ss << "Score = " << scoreInt;
scoreText.setString(ss.str());
window.clear();
window.draw(spriteBackground);
......@@ -176,7 +247,15 @@ int main()
window.draw(spriteCloudThree);
window.draw(spriteTree);
window.draw(spriteBee);
window.draw(scoreText);
window.draw(timeBar);
if (gamePaused) {
window.draw(startText);
}
window.display();
}
return 0;
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment