Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //include main
- #include <iostream>
- #include <stdio.h>
- //include header
- #include "Animation.h"
- //include SFML
- #include <SFML/Window.hpp>
- #include <SFML/Graphics.hpp>
- //namespace
- using namespace std;
- using namespace sf;
- //main
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(1000, 500), "Home", sf::Style::Titlebar | sf::Style::Close);
- sf::RectangleShape player(sf::Vector2f(100.0f, 150.0f));
- sf::RectangleShape player2(Vector2f(50.0f, 50.0f));
- player.setPosition(206.0f, 206.0f);
- player2.setFillColor(Color::Blue);
- player2.setOrigin(25.0f, 25.0f);
- //starting texture
- Texture plytex;
- plytex.loadFromFile("tux_from_linux.png");
- player.setTexture(&plytex);
- Animation animation(&plytex, Vector2u(3, 9), 0.3f);
- float deltaTime = 0.0f;
- Clock clock;
- while (window.isOpen())
- {
- deltaTime = clock.restart().asSeconds();
- sf::Event event;
- while (window.pollEvent(event))
- {
- switch (event.type)
- {
- case sf::Event::Closed:
- window.close();
- break;
- case sf::Event::TextEntered:
- if (event.text.unicode < 128)
- {
- printf("%c", event.text.unicode);
- }
- break;
- }
- }
- //update
- animation.Update(0, deltaTime);
- player.setTextureRect(animation.uvRect);
- //KEYBOARD
- if (Keyboard::isKeyPressed(Keyboard::Key::W))
- {
- player.move(0.f, -0.1f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::A))
- {
- player.move(-0.1f, 0.0f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::S))
- {
- player.move(0.0f, 0.1f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::D))
- {
- player.move(0.1f, 0.0f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::W))
- {
- player.move(0.0f, -0.5f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::A))
- {
- player.move(-0.5f, 0.0f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::S))
- {
- player.move(0.0f, 0.5f);
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::D))
- {
- player.move(0.5f, 0.0f);
- }
- //mouse
- if (Mouse::isButtonPressed(Mouse::Button::Right))
- {
- Vector2i mousePos = Mouse::getPosition(window);
- player2.setPosition((float)mousePos.x, (float)mousePos.y);
- }
- //render
- window.clear(sf::Color(69, 69, 69));
- //draw game
- window.draw(player);
- window.draw(player2);
- window.display();
- }
- //end
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement