Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Window.hpp>
  3. #include <iostream>
  4. #include <string>
  5.  
  6.  
  7.  
  8.  
  9. using namespace std;
  10.  
  11. enum class Dir { UP, DOWN, LEFT, RIGHT};
  12.  
  13. bool setBoundary(sf::Sprite& sprite2, sf::FloatRect& backGround, Dir dir);
  14. int highScoreInt;
  15. std::string highScoreString = std::to_string(highScoreInt);
  16.  
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22.     sf::RenderWindow window(sf::VideoMode(800, 600), "Cross Road");
  23.     window.setFramerateLimit(60);
  24.    
  25.     sf::FloatRect bg;
  26.     bg.left = 0;
  27.     bg.top = 0;
  28.     bg.width = window.getSize().x;
  29.     bg.height = window.getSize().y;
  30.  
  31.  
  32.  
  33.     sf::Texture texture;
  34.     if (!texture.loadFromFile("assets/img/pavement.png"))
  35.         return EXIT_FAILURE;
  36.     sf::Sprite sprite(texture);
  37.  
  38.     sprite.setPosition(0, 520);
  39.  
  40.  
  41.  
  42.  
  43.     sf::Texture texture2;
  44.     if (!texture2.loadFromFile("assets/img/frogstatic.png"))
  45.         return EXIT_FAILURE;
  46.     sf::Sprite sprite2(texture2);
  47.     sf::Vector2f sprite2Size(32.f, 32.f);
  48.  
  49.     sprite2.setScale(sprite2Size.x / texture2.getSize().x, sprite2Size.y / texture2.getSize().y);
  50.     sprite2.setOrigin(sprite2Size.x / 2.f / sprite2.getScale().x, sprite2Size.y / 2.f / sprite2.getScale().y);
  51.     sprite2.setPosition(400, 548);
  52.  
  53.  
  54.  
  55.  
  56.     sf::Font timerFont;
  57.     if (!timerFont.loadFromFile("assets/fonts/arialbd.ttf"))
  58.         return EXIT_FAILURE;
  59.  
  60.     sf::Text timerText("Time: ", timerFont, 14);
  61.     timerText.setColor(sf::Color::Black);
  62.     timerText.setPosition(0, 568);
  63.  
  64.  
  65.  
  66.  
  67.     sf::Font highScoreFont;
  68.     if (!highScoreFont.loadFromFile("assets/fonts/arialbd.ttf"))
  69.         return EXIT_FAILURE;
  70.  
  71.     sf::Text highScoreText("Score: " + highScoreString, highScoreFont, 14);
  72.     highScoreText.setColor(sf::Color::Black);
  73.     highScoreText.setPosition(0, 584);
  74.  
  75.  
  76.  
  77.  
  78.     while (window.isOpen())
  79.     {
  80.         sf::Event event;
  81.         while (window.pollEvent(event))
  82.         {
  83.             if (event.type == sf::Event::Closed)
  84.                 window.close();
  85.             if (event.type == sf::Event::KeyPressed)
  86.             {
  87.  
  88.                 if (event.key.code == sf::Keyboard::Up) {
  89.                     sprite2.move(0.0f, -10.0f);
  90.                     sprite2.setRotation(0);
  91.                     highScoreInt += 2;
  92.                 }
  93.                 if (event.key.code == sf::Keyboard::Down && setBoundary(sprite2, bg, Dir::DOWN)) sprite2.move(0.0f, 10.0f), sprite2.setRotation(180), highScoreInt -= 2;
  94.                 if (event.key.code == sf::Keyboard::Left&& setBoundary(sprite2, bg, Dir::LEFT)) sprite2.move(-10.0f, 0.0f), sprite2.setRotation(270);
  95.                 if (event.key.code == sf::Keyboard::Right&& setBoundary(sprite2, bg, Dir::RIGHT)) sprite2.setRotation(90), sprite2.move(10.0f, 0.0f);
  96.  
  97.                 if (highScoreInt <= 0) {
  98.                     highScoreInt = 0;
  99.                 }
  100.             }
  101.         }
  102.  
  103.  
  104.  
  105.  
  106.         highScoreText.setString("Score: " + std::to_string(highScoreInt));
  107.         sf::Color backGround(255, 255, 255, 0);
  108.         window.setKeyRepeatEnabled(false);
  109.         window.clear(backGround);
  110.         window.draw(sprite);
  111.         window.draw(sprite2);
  112.         window.draw(timerText);
  113.         window.draw(highScoreText);
  114.         window.display();
  115.  
  116.  
  117.  
  118.     }
  119.     return EXIT_SUCCESS;
  120.  
  121.  
  122.  
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130. bool setBoundary(sf::Sprite& sprite2, sf::FloatRect& backGround, Dir dir)
  131. {
  132.     sf::FloatRect size = sprite2.getGlobalBounds();
  133.     sf::Vector2f pos = sprite2.getPosition();
  134.  
  135.     if ((dir==Dir::LEFT&&!(size.left<=0)) || (dir == Dir::RIGHT&&!(size.left+size.width >= backGround.width)) || (dir == Dir::DOWN&&!(size.top+size.height >= backGround.height)))
  136.     {
  137.         return true;
  138.     }
  139.     return false;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement