Advertisement
Guest User

Untitled

a guest
May 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <random>
  3. #include <iostream>
  4. #include <math.h>
  5. #include <vector>
  6. #define PI acos(-1)
  7.  
  8. std::random_device rng;
  9. std::mt19937 twister(rng());
  10.  
  11. std::uniform_int_distribution<size_t> speed_gen(1, 20);
  12. std::uniform_int_distribution<size_t> angle_gen(0, 359);
  13.  
  14. float degrees_to_radians(float degrees) {
  15. return (PI/180)*degrees;
  16. }
  17.  
  18. sf::Vector2f ds_to_vec(float direction, float speed) {
  19. float radians = degrees_to_radians(direction);
  20. float x = speed*sin(radians);
  21. float angle_2 = degrees_to_radians(180-(direction+90));
  22. float y = speed*sin(angle_2);
  23.  
  24. return sf::Vector2f(x, y);
  25. }
  26.  
  27. class bullet {
  28. public:
  29. bullet(sf::Vector2f starting_position, float direction, float speed, sf::CircleShape circle) : circle(circle) {
  30. this->velocity = ds_to_vec(direction, speed);
  31. this->circle.setPosition(starting_position);
  32. }
  33. sf::CircleShape& get_circle() { return this->circle; }
  34.  
  35. void update() {
  36. this->circle.move(velocity.x, velocity.y);
  37. }
  38. private:
  39. sf::CircleShape circle;
  40. sf::Vector2f velocity;
  41. };
  42.  
  43. int main() {
  44. sf::RenderWindow window(sf::VideoMode(640, 480), "Shooting Test", sf::Style::Titlebar | sf::Style::Close);
  45. window.setFramerateLimit(60);
  46.  
  47. std::vector<bullet> bullets;
  48.  
  49. sf::Clock timer;
  50. sf::Event event;
  51. while(window.isOpen()) {
  52. while(window.pollEvent(event)) {
  53. if(event.type == sf::Event::Closed) {
  54. window.close(); // close the window when the user tries to close the window
  55. }
  56. }
  57.  
  58. if(timer.getElapsedTime().asSeconds() >= 1) {
  59. bullet new_bullet = bullet(sf::Vector2f(290, 210), angle_gen(twister), speed_gen(twister), sf::CircleShape(30.F, 30U));
  60. new_bullet.get_circle().setFillColor(sf::Color(255, 255, 0, 255));
  61. bullets.push_back(new_bullet);
  62. timer.restart();
  63. }
  64.  
  65. for(bullet b : bullets) {
  66. b.update();
  67. sf::Vector2f pos = b.get_circle().getPosition();
  68. std::cout << pos.x << " " << pos.y << std::endl;
  69. }
  70. window.clear();
  71. for(bullet b : bullets) { window.draw(b.get_circle()); }
  72. window.display();
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement