Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <time.h>
  4. #include <math.h>
  5. using namespace std;
  6. using namespace sf;
  7.  
  8. sf::RenderWindow window(sf::VideoMode(300, 800), "Space Invaders");
  9. float pi = 3.14159;
  10.  
  11. class spelare
  12. {
  13. Vector2f pos, dim;
  14. RectangleShape skepp, kanon;
  15. clock_t t0;
  16. float v, vdir;
  17. public:
  18. spelare(float lengd, float bredd, float xpos, float ypos)
  19. {
  20. dim.x = lengd; dim.y = bredd;
  21. pos.x = xpos; pos.y = ypos;
  22. v = 0; vdir = 0;
  23. skepp.setSize(dim);
  24. skepp.setOrigin(dim.x * 2 / 5, dim.y / 2);
  25. kanon.setOrigin(dim.x * 2 / 5, dim.y / 2);
  26. kanon.setSize(Vector2f(dim.x / 5, dim.y));
  27. kanon.setFillColor(Color::Red);
  28. }
  29. void uppdatera()
  30. {
  31. clock_t dt = clock() - t0;
  32. Vector2f vrp = pos;
  33. vrp.x += 0.7*dim.x*cos(vdir*pi / 180);
  34. vrp.y += 0.7*dim.x*sin(vdir*pi / 180);
  35. t0 += dt;
  36. pos.x += v * cos(vdir*pi / 180)*dt;
  37. pos.y += v * sin(vdir*pi / 180)*dt;
  38. skepp.setPosition(pos);
  39. skepp.setRotation(vdir);
  40. kanon.setPosition(vrp);
  41. kanon.setRotation(vdir);
  42. }
  43. void rita()
  44. {
  45. window.draw(skepp);
  46. window.draw(kanon);
  47. }
  48. void acc(float a)
  49. {
  50. v += a;
  51. }
  52. };
  53.  
  54. int main()
  55. {
  56. clock_t t0, t1;
  57. spelare skepp(85, 35, 200, 200);
  58. while (window.isOpen())
  59. {
  60. sf::Event event;
  61. while (window.pollEvent(event))
  62. {
  63. if (event.type == sf::Event::Closed)
  64. window.close();
  65. else if (event.type == sf::Event::KeyPressed)
  66. if (event.key.code == sf::Keyboard::Right)
  67. {
  68. skepp.acc(0.001);
  69. }
  70. else if (event.key.code == sf::Keyboard::Left)
  71. {
  72. skepp.acc(-0.001);
  73. }
  74. else
  75. {
  76. skepp.acc(0);
  77. }
  78. }
  79. window.clear();
  80. skepp.uppdatera();
  81. skepp.rita();
  82. window.display();
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement