Advertisement
Guest User

Untitled

a guest
May 30th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. //include main
  2. #include <iostream>
  3. #include <stdio.h>
  4. //include header
  5. #include "Animation.h"
  6. //include SFML
  7. #include <SFML/Window.hpp>
  8. #include <SFML/Graphics.hpp>
  9.  
  10. //namespace
  11. using namespace std;
  12. using namespace sf;
  13.  
  14.  
  15. //main
  16. int main()
  17. {
  18. sf::RenderWindow window(sf::VideoMode(1000, 500), "Home", sf::Style::Titlebar | sf::Style::Close);
  19. sf::RectangleShape player(sf::Vector2f(100.0f, 150.0f));
  20. sf::RectangleShape player2(Vector2f(50.0f, 50.0f));
  21. player.setPosition(206.0f, 206.0f);
  22.  
  23. player2.setFillColor(Color::Blue);
  24. player2.setOrigin(25.0f, 25.0f);
  25.  
  26. //starting texture
  27. Texture plytex;
  28. plytex.loadFromFile("tux_from_linux.png");
  29. player.setTexture(&plytex);
  30.  
  31. Animation animation(&plytex, Vector2u(3, 9), 0.3f);
  32. float deltaTime = 0.0f;
  33. Clock clock;
  34.  
  35. while (window.isOpen())
  36. {
  37. deltaTime = clock.restart().asSeconds();
  38. sf::Event event;
  39. while (window.pollEvent(event))
  40. {
  41. switch (event.type)
  42. {
  43. case sf::Event::Closed:
  44. window.close();
  45. break;
  46. case sf::Event::TextEntered:
  47. if (event.text.unicode < 128)
  48. {
  49. printf("%c", event.text.unicode);
  50. }
  51. break;
  52. }
  53. }
  54. //update
  55. animation.Update(0, deltaTime);
  56. player.setTextureRect(animation.uvRect);
  57. //KEYBOARD
  58. if (Keyboard::isKeyPressed(Keyboard::Key::W))
  59. {
  60. player.move(0.f, -0.1f);
  61. }
  62. if (Keyboard::isKeyPressed(Keyboard::Key::A))
  63. {
  64. player.move(-0.1f, 0.0f);
  65. }
  66. if (Keyboard::isKeyPressed(Keyboard::Key::S))
  67. {
  68. player.move(0.0f, 0.1f);
  69. }
  70. if (Keyboard::isKeyPressed(Keyboard::Key::D))
  71. {
  72. player.move(0.1f, 0.0f);
  73. }
  74. if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::W))
  75. {
  76. player.move(0.0f, -0.5f);
  77. }
  78. if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::A))
  79. {
  80. player.move(-0.5f, 0.0f);
  81. }
  82. if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::S))
  83. {
  84. player.move(0.0f, 0.5f);
  85. }
  86. if (Keyboard::isKeyPressed(Keyboard::Key::LShift) && Keyboard::isKeyPressed(Keyboard::Key::D))
  87. {
  88. player.move(0.5f, 0.0f);
  89. }
  90.  
  91.  
  92. //mouse
  93. if (Mouse::isButtonPressed(Mouse::Button::Right))
  94. {
  95. Vector2i mousePos = Mouse::getPosition(window);
  96. player2.setPosition((float)mousePos.x, (float)mousePos.y);
  97. }
  98. //render
  99. window.clear(sf::Color(69, 69, 69));
  100.  
  101. //draw game
  102. window.draw(player);
  103. window.draw(player2);
  104. window.display();
  105. }
  106. //end
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement