Advertisement
Guest User

Untitled

a guest
May 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <SFML/Audio.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/System.hpp>
  4. #include <SFML/Window.hpp>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace sf;
  9. using namespace std;
  10.  
  11. int longueur = 1280;
  12. int hauteur = 720;
  13. int e = 1601;
  14.  
  15. RenderWindow window(VideoMode(longueur, hauteur, 32), "chiass");
  16.  
  17. class Particule {
  18. public:
  19. Particule(int x, int y, int charge = 0, int mass = 10000, int radius = 30, int vx = 0, int vy = 0, int ax = 0, int ay = 0) :
  20. q(charge), m(mass), vx(vx), vy(vy), x(x), y(y), ax(ax), ay(ay), radius(radius)
  21. {
  22. circle.setRadius(radius);
  23. circle.setPointCount(500);
  24. circle.setFillColor(Color(255, 99, 71));
  25. circle.setOrigin(radius, radius);
  26. circle.setPosition(x, y);
  27. /*const char* name;
  28. name = __func__;
  29. cout << name << endl;*/
  30. }
  31.  
  32. void PrintInfo() {
  33. cout << "Mass = " << (*this).m << endl;
  34. cout << "Charge = " << (*this).q << endl;
  35. cout << "X position = " << (*this).x << endl;
  36. cout << "Y position = " << (*this).y << endl;
  37. cout << "Vx = " << (*this).vx << endl;
  38. cout << "Vy = " << (*this).vy << endl;
  39. cout << "Ax = " << (*this).ax << endl;
  40. cout << "Ay = " << (*this).ay << endl;
  41. }
  42.  
  43. vector<int> infos() {
  44. vector <int> infos = {x, y, q, m, radius, vx, vy, ax, ay};
  45. return infos;
  46. };
  47.  
  48. template <class T>
  49. void SetAcceleration(T ax, T ay) {
  50. (*this).ax = ax;
  51. (*this).ay = ay;
  52. }
  53.  
  54. template <class T>
  55. void SetVitesse(T vx, T vy) {
  56. (*this).vx = vx;
  57. (*this).vy = vy;
  58. }
  59.  
  60. void changeV() {
  61. vx += ax;
  62. vy += ay;
  63. }
  64.  
  65. void changeXY() {
  66. x += vx;
  67. y += vy;
  68.  
  69. }
  70.  
  71. void update() {
  72. changeV();
  73.  
  74. if (vx > 1) {
  75. vx = 1;
  76. }
  77. if (vy > 1) {
  78. vy = 1;
  79. }
  80.  
  81. changeXY();
  82.  
  83. if(x - radius<0) {
  84. x = radius;
  85. ax *= -1;
  86. vx *= -1;
  87. }
  88. if (x +radius> longueur) {
  89. x = longueur- radius;
  90. ax *= -1;
  91. vx *= -1;
  92. }
  93. if (y - radius< 0) {
  94. y = radius;
  95. ay *= -1;
  96. vy *= -1;
  97. }
  98. if (y + radius > hauteur) {
  99. y = hauteur - radius;
  100. ay *= -1;
  101. vy *= -1;
  102. }
  103.  
  104. circle.setPosition(Vector2f(x, y));
  105. window.draw(circle);
  106. }
  107.  
  108. void text() {
  109. Font font;
  110. if (!font.loadFromFile("arial.ttf"))
  111. cout << "Merde";
  112. vector<int> faichier = (*this).infos();
  113.  
  114. int x = faichier[0];
  115. char xs[12];
  116. sprintf_s(xs, "%d", x);
  117. Text X(xs, font, 20);
  118.  
  119. int y = faichier[1];
  120. char ys[12];
  121. sprintf_s(ys, "%d", y);
  122. Text Y(ys, font, 20);
  123. Y.setPosition(0, 20);
  124.  
  125. window.draw(X);
  126. window.draw(Y);
  127.  
  128.  
  129.  
  130. }
  131.  
  132. private:
  133. int q;
  134. int m;
  135. int vx;
  136. int vy;
  137. int x;
  138. int y;
  139. int ax;
  140. int ay;
  141. CircleShape circle;
  142. int radius;
  143. };
  144.  
  145.  
  146. int main()
  147. {
  148. Particule electron(0, 0, -e, 9109);
  149. sf::ContextSettings settings;
  150. settings.antialiasingLevel = 32;
  151.  
  152. electron.SetVitesse(1.5, 1.5);
  153.  
  154. Font font;
  155. if (!font.loadFromFile("arial.ttf"))
  156. return EXIT_FAILURE;
  157. CircleShape circle;
  158. circle.setRadius(1);
  159. circle.setFillColor(Color(255, 255, 255));
  160. circle.setPointCount(1000);
  161.  
  162. while (window.isOpen())
  163. {
  164. vector<int> faichier = electron.infos();
  165.  
  166. Event event;
  167. Clock clock;
  168. Text infos;
  169. while (window.pollEvent(event))
  170. {
  171. if (event.type == Event::Closed)
  172. window.close();
  173. }
  174. window.clear(Color(40, 40, 40));
  175. circle.setPosition(faichier[0], faichier[1]);
  176. electron.update();
  177. electron.text();
  178. window.draw(circle);
  179. window.display();
  180. }
  181. return EXIT_SUCCESS;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement