Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- using namespace std;
- bool looking_left = false;
- bool looking_right = true;
- bool walking_right = false;
- bool walking_left = false;
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(800, 600), "Game In Development");
- sf::Texture character_sheet;
- character_sheet.loadFromFile("hero_spritesheet.png");
- sf::Sprite character;
- character.setTexture(character_sheet);
- sf::Time time = sf::seconds(0);
- sf::Time time2 = sf::seconds(0);
- sf::Clock clock2; //Start Second clock
- sf::Clock clock; //Start clock
- sf::Event event;
- while (window.isOpen())
- {
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- walking_right = true;
- looking_right = false;
- looking_left = false;
- walking_left = false;
- }
- else
- {
- if (walking_right == true)
- {
- walking_right = false;
- looking_right = true;
- }
- else
- walking_right = false;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- walking_left = true;
- looking_right = false;
- }
- }
- //----------------------------PREPERATIONS----------------------------
- time = clock.getElapsedTime();
- time2 = clock2.getElapsedTime();
- if (!walking_right && !walking_left)
- clock2.restart();
- //--------------------------PREPERATIONS END--------------------------
- //Check character state and execute animations
- if (looking_right)
- {
- if (time.asSeconds() > .5)
- {
- character.setTextureRect(sf::IntRect(0, 0, 80, 94));
- if (time.asSeconds() > 1)
- clock.restart();
- }
- else
- {
- character.setTextureRect(sf::IntRect(80, 0, 80, 94));
- }
- }
- else if (walking_right)
- {
- if (time2.asSeconds() > 0 && time2.asSeconds() < .5)
- character.setTextureRect(sf::IntRect(0, 94, 80, 94));
- else if (time2.asSeconds() > .5 && time2.asSeconds() < 1)
- character.setTextureRect(sf::IntRect(80, 94, 80, 94));
- else if (time2.asSeconds() > 1 && time2.asSeconds() < 1.5)
- character.setTextureRect(sf::IntRect(80 * 2, 94, 80, 94));
- else if (time2.asSeconds() > 1.5 && time2.asSeconds() < 2)
- character.setTextureRect(sf::IntRect(80 * 3, 94, 80, 94));
- else if (time2.asSeconds() > 2 && time2.asSeconds() < 2.5)
- character.setTextureRect(sf::IntRect(80 * 4, 94, 80, 94));
- else if (time2.asSeconds() > 2.5 && time2.asSeconds() < 3)
- character.setTextureRect(sf::IntRect(80 * 5, 94, 80, 94));
- else if (time2.asSeconds() > 3)
- clock2.restart();
- character.setPosition(character.getPosition().x + 0.05, character.getPosition().y);
- }
- else if (walking_left)
- {
- if (time2.asSeconds() > 0 && time2.asSeconds() < .5)
- {
- character.setTextureRect(sf::IntRect(0, 94, 80 * -1, 94));
- }
- else if (time2.asSeconds() > .5 && time2.asSeconds() < 1)
- {
- character.setTextureRect(sf::IntRect());
- }
- else if (time2.asSeconds() > 1 && time2.asSeconds() < 1.5)
- {
- character.setTextureRect(sf::IntRect());
- }
- else if (time2.asSeconds() > 1.5 && time2.asSeconds() < 2)
- {
- character.setTextureRect(sf::IntRect());
- }
- else if (time2.asSeconds() > 2 && time2.asSeconds() < 2.5)
- {
- character.setTextureRect(sf::IntRect());
- }
- else if (time2.asSeconds() > 2.5 && time2.asSeconds() < 3)
- {
- character.setTextureRect(sf::IntRect());
- }
- else if (time2.asSeconds() > 3)
- clock2.restart();
- character.setPosition(character.getPosition().x - 0.05, character.getPosition().y);
- }
- window.clear(sf::Color::White); //Clear
- window.draw(character);
- window.display(); //Display
- }
- int pause; cin >> pause; //Pause the program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement