Advertisement
Guest User

how to shoot multiple times

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <SFML\Main.hpp>
  2. #include <SFML\Window.hpp>
  3. #include <SFML\System.hpp>
  4. #include <SFML\Graphics.hpp>
  5.  
  6.  
  7. #if !defined(NDEBUG)
  8. #pragma comment(lib, "sfml-main-d.lib")
  9. #pragma comment(lib, "sfml-window-d.lib")
  10. #pragma comment(lib, "sfml-system-d.lib")
  11. #pragma comment(lib, "sfml-graphics-d.lib")
  12. #else
  13. #pragma comment(lib, "sfml-main.lib")
  14. #pragma comment(lib, "sfml-window.lib")
  15. #pragma comment(lib, "sfml-system.lib")
  16. #pragma comment(lib, "sfml-graphics.lib")
  17. #endif
  18.  
  19. int main(int argc, char** argv)
  20. {
  21.     sf::RenderWindow window(sf::VideoMode(1000, 1000), "sfml works!", sf::Style::Titlebar | sf::Style::Close);
  22.     sf::RectangleShape crosshair(sf::Vector2f(10, 10));
  23.    
  24.     sf::RectangleShape bullet[x](sf::Vector2f(20, 5));
  25.     sf::CircleShape player(10.f);
  26.     sf::Vector2f playerPos(490, 490);
  27.     sf::Vector2f shotPos(500,500);
  28.     float bulletSpeed = 3.f;
  29.     sf::Vector2f v(0, 0);
  30.     player.setPosition(playerPos);
  31.    
  32.  
  33.     player.setFillColor(sf::Color::Yellow);
  34.     bool shot = false;
  35.    
  36.     crosshair.setFillColor(sf::Color::Cyan);
  37.     if (!window.isOpen())return-1;
  38.     const float targetTime = 1.0f / 60.f;
  39.     float accumalotor = 0.0f;
  40.     float frameTime = 0.0f;
  41.    
  42.     sf::Clock clock;
  43.  
  44.     while (window.isOpen())
  45.     {
  46.         sf::Time deltaTime = clock.restart();
  47.         frameTime = std::min(deltaTime.asSeconds(), 0.1f);
  48.         accumalotor += frameTime;
  49.         while (accumalotor > targetTime)
  50.         {
  51.             accumalotor -= targetTime;
  52.             sf::Event event;
  53.             while (window.pollEvent(event))
  54.             {
  55.                 if (event.type == sf::Event::Closed)
  56.                 {
  57.                     window.close();
  58.                 }
  59.             }
  60.         }
  61.        
  62.         sf::Vector2f mouse(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
  63.         sf::Vector2f aimPos(mouse.x - playerPos.x, mouse.y - playerPos.y);
  64.         crosshair.setPosition(mouse.x, mouse.y);
  65.  
  66.         window.clear(sf::Color(0x11,0x12,0x33,0xff));
  67.         if (sf::Mouse::isButtonPressed(sf::Mouse::Left)&&shot==false)
  68.         {
  69.             shot = true;
  70.         }
  71.         if (shot == true)
  72.         {
  73.            
  74.             bullet[x].setPosition(shotPos.x, shotPos.y);
  75.             sf::Vector2f v;
  76.             float distance = sqrt((aimPos.x - playerPos.x)*(aimPos.x - playerPos.x) + (aimPos.y - playerPos.y)*(aimPos.y - playerPos.y));
  77.             shotPos.x += aimPos.x/distance;
  78.             shotPos.y += aimPos.y/distance;
  79.             window.draw(bullet[x]);
  80.             //shot = false;
  81.            
  82.         }
  83.        
  84.         window.draw(crosshair);
  85.         window.draw(player);
  86.         window.display();
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement