Advertisement
Felanpro

my beautiful world

Apr 22nd, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4.  
  5. using namespace std;
  6.  
  7. int width = 500;
  8. int height = 500;
  9.  
  10. unsigned int direction = 0;
  11.  
  12. int main()
  13. {
  14.  
  15.     int map[10][10] =
  16.     {
  17.         {2, 2, 2, 3, 3, 3, 2, 2, 2, 2},
  18.         {2, 3, 2, 2, 2, 2, 2, 3, 2, 2},
  19.         {2, 2, 2, 3, 2, 3, 2, 2, 2, 2},
  20.         {2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
  21.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  22.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  23.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  24.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  25.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  26.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  27.     };
  28.  
  29.     sf::RenderWindow window(sf::VideoMode(width, height), "In Progress");
  30.     window.setVerticalSyncEnabled(true);
  31.     window.setFramerateLimit(120);
  32.  
  33.     sf::Texture sprite_sheet;
  34.     sprite_sheet.loadFromFile("mainguy.png");
  35.  
  36.     sf::Sprite sprite;
  37.     sprite.setTexture(sprite_sheet);
  38.     sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
  39.     sprite.setPosition(width / 2, height / 2); //Initial position of the sprite
  40.  
  41.     sf::RectangleShape sky(sf::Vector2f(50, 50));
  42.     sky.setFillColor(sf::Color::Cyan);
  43.  
  44.     sf::RectangleShape ground(sf::Vector2f(50, 50));
  45.     ground.setFillColor(sf::Color::Green);
  46.  
  47.     sf::RectangleShape cloud(sf::Vector2f(50, 50));
  48.     cloud.setFillColor(sf::Color::White);
  49.  
  50.     sf::Time time = sf::seconds(0);
  51.     sf::Clock clock;
  52.  
  53.     sf::Event event;
  54.     while (window.isOpen())
  55.     {
  56.         while (window.pollEvent(event))
  57.         {
  58.             if (event.type == sf::Event::Closed)
  59.             {
  60.                 window.close();
  61.             }
  62.         }
  63.  
  64.         //Handle other things
  65.  
  66.         //Gather input
  67.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  68.         {
  69.             if (direction != 1)
  70.             {
  71.                 clock.restart();
  72.             }
  73.             cout << "Walking right" << endl;
  74.             direction = 1;
  75.         }
  76.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  77.         {
  78.             if (direction != 2)
  79.             {
  80.                 clock.restart();
  81.             }
  82.             cout << "Walking left" << endl;
  83.             direction = 2;
  84.         }
  85.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  86.         {
  87.             if (direction != 3)
  88.             {
  89.                 clock.restart();
  90.             }
  91.             cout << "Walking up" << endl;
  92.             direction = 3;
  93.         }
  94.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  95.         {
  96.             if (direction != 4)
  97.             {
  98.                 clock.restart();
  99.             }
  100.             cout << "Walking down" << endl;
  101.             direction = 4;
  102.         }
  103.         else
  104.         {
  105.             cout << "not moving" << endl;
  106.             direction = 0;
  107.         }
  108.  
  109.         //Execute animations
  110.         if (direction == 1)
  111.         {
  112.             sprite.move(4, 0);
  113.             time = clock.getElapsedTime();
  114.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  115.             {
  116.                 sprite.setTextureRect(sf::IntRect(0, 36, 41, 36));
  117.             }
  118.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  119.             {
  120.                 sprite.setTextureRect(sf::IntRect(41, 36, 41, 36));
  121.             }
  122.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  123.             {
  124.                 sprite.setTextureRect(sf::IntRect(41 * 2, 36, 41, 36));
  125.             }
  126.             else if (time.asSeconds() > 1.5)
  127.             {
  128.                 clock.restart();
  129.             }
  130.         }
  131.         else if (direction == 2)
  132.         {
  133.             sprite.move(-4, 0);
  134.             time = clock.getElapsedTime();
  135.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  136.             {
  137.                 sprite.setTextureRect(sf::IntRect(0, 36 * 3, 41, 36));
  138.             }
  139.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  140.             {
  141.                 sprite.setTextureRect(sf::IntRect(41, 36 * 3, 41, 36));
  142.             }
  143.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  144.             {
  145.                 sprite.setTextureRect(sf::IntRect(41 * 2, 36 * 3, 41, 36));
  146.             }
  147.             else if (time.asSeconds() > 1.5)
  148.             {
  149.                 clock.restart();
  150.             }
  151.         }
  152.         else if (direction == 3)
  153.         {
  154.             sprite.move(0, -4);
  155.             time = clock.getElapsedTime();
  156.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  157.             {
  158.                 sprite.setTextureRect(sf::IntRect(41, 36 * 2, 41, 36));
  159.             }
  160.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  161.             {
  162.                 sprite.setTextureRect(sf::IntRect(0, 36 * 2, 41, 36));
  163.             }
  164.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  165.             {
  166.                 sprite.setTextureRect(sf::IntRect(41 * 2, 36 * 2, 41, 36));
  167.             }
  168.             else if (time.asSeconds() > 1.5)
  169.             {
  170.                 clock.restart();
  171.             }
  172.         }
  173.         else if (direction == 4)
  174.         {
  175.             sprite.move(0, 4);
  176.             time = clock.getElapsedTime();
  177.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  178.             {
  179.                 sprite.setTextureRect(sf::IntRect(0, 0, 41, 36));
  180.             }
  181.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  182.             {
  183.                 sprite.setTextureRect(sf::IntRect(41 * 2, 0, 41, 36));
  184.             }
  185.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  186.             {
  187.                 sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
  188.             }
  189.             else if (time.asSeconds() > 1.5)
  190.             {
  191.                 clock.restart();
  192.             }
  193.         }
  194.         else if (direction == 0)
  195.         {
  196.             sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
  197.         }
  198.  
  199.         //Check collision/Height limit
  200.         if (sprite.getPosition().y < 200)
  201.         {
  202.             sprite.setPosition(sprite.getPosition().x, 200);
  203.         }
  204.  
  205.         window.clear(sf::Color::White); //Clear
  206.  
  207.         //Here we draw everything
  208.         for (int mapY = 0; mapY < 10; mapY++)
  209.         {
  210.             for (int mapX = 0; mapX < 10; mapX++)
  211.             {
  212.                 if (map[mapY][mapX] == 1)
  213.                 {
  214.                     ground.setPosition(mapX * 50, mapY * 50);
  215.                     window.draw(ground);
  216.                 }
  217.                 else if (map[mapY][mapX] == 2)
  218.                 {
  219.                     sky.setPosition(mapX * 50, mapY * 50);
  220.                     window.draw(sky);
  221.                 }
  222.                 else if (map[mapY][mapX] == 3)
  223.                 {
  224.                     cloud.setPosition(mapX * 50, mapY * 50);
  225.                     window.draw(cloud);
  226.                 }
  227.             }
  228.         }
  229.         window.draw(sprite);
  230.  
  231.         window.display(); //Display
  232.     }
  233.  
  234.     int pause; cin >> pause;
  235.     return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement