Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <random>
- #include <iostream>
- #include <math.h>
- #include <vector>
- #define PI acos(-1)
- std::random_device rng;
- std::mt19937 twister(rng());
- std::uniform_int_distribution<size_t> speed_gen(1, 20);
- std::uniform_int_distribution<size_t> angle_gen(0, 359);
- float degrees_to_radians(float degrees) {
- return (PI/180)*degrees;
- }
- sf::Vector2f ds_to_vec(float direction, float speed) {
- float radians = degrees_to_radians(direction);
- float x = speed*sin(radians);
- float angle_2 = degrees_to_radians(180-(direction+90));
- float y = speed*sin(angle_2);
- return sf::Vector2f(x, y);
- }
- class bullet {
- public:
- bullet(sf::Vector2f starting_position, float direction, float speed, sf::CircleShape circle) : circle(circle) {
- this->velocity = ds_to_vec(direction, speed);
- this->circle.setPosition(starting_position);
- }
- sf::CircleShape& get_circle() { return this->circle; }
- void update() {
- this->circle.move(velocity.x, velocity.y);
- }
- private:
- sf::CircleShape circle;
- sf::Vector2f velocity;
- };
- int main() {
- sf::RenderWindow window(sf::VideoMode(640, 480), "Shooting Test", sf::Style::Titlebar | sf::Style::Close);
- window.setFramerateLimit(60);
- std::vector<bullet> bullets;
- sf::Clock timer;
- sf::Event event;
- while(window.isOpen()) {
- while(window.pollEvent(event)) {
- if(event.type == sf::Event::Closed) {
- window.close(); // close the window when the user tries to close the window
- }
- }
- if(timer.getElapsedTime().asSeconds() >= 1) {
- bullet new_bullet = bullet(sf::Vector2f(290, 210), angle_gen(twister), speed_gen(twister), sf::CircleShape(30.F, 30U));
- new_bullet.get_circle().setFillColor(sf::Color(255, 255, 0, 255));
- bullets.push_back(new_bullet);
- timer.restart();
- }
- for(bullet b : bullets) {
- b.update();
- sf::Vector2f pos = b.get_circle().getPosition();
- std::cout << pos.x << " " << pos.y << std::endl;
- }
- window.clear();
- for(bullet b : bullets) { window.draw(b.get_circle()); }
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement