Advertisement
Felanpro

shooting

Apr 28th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. unsigned int width = 500;
  2. unsigned int height = 500;
  3. unsigned int direction = 0;
  4. float gravity = .3;
  5. bool jumping = false;
  6. bool shooting = false;
  7. float projectile_accelerator = -.5;
  8.  
  9. int main()
  10. {
  11.     sf::RenderWindow window(sf::VideoMode(width, height), "In progress");
  12.     window.setFramerateLimit(60);
  13.  
  14.     sf::Vector2f velocity(0, 0);
  15.  
  16.     sf::RectangleShape player(sf::Vector2f(10, 10));
  17.     player.setFillColor(sf::Color::Black);
  18.  
  19.     sf::RectangleShape object[15];
  20.  
  21.     for (int x = 0; x < 15; x++)
  22.     {
  23.         object[x].setFillColor(sf::Color::Red);
  24.         object[x].setPosition(0, 0);
  25.         object[x].setSize(sf::Vector2f(40, 40));
  26.     }
  27.  
  28.     sf::RectangleShape goal(sf::Vector2f(30, 30));
  29.     goal.setFillColor(sf::Color::Green);
  30.  
  31.     sf::RectangleShape projectile(sf::Vector2f(5, 5));
  32.     projectile.setFillColor(sf::Color::Cyan);
  33.  
  34.     object[0].setPosition(0, 400);
  35.     for (int x = 1; x < 15; x++)
  36.     {
  37.         object[x].setPosition(object[x - 1].getPosition().x + 30, 400);
  38.     }
  39.  
  40.     goal.setPosition(450, 430);
  41.  
  42.     sf::Time time = sf::seconds(0);
  43.     sf::Clock shooting_clock;
  44.  
  45.     sf::Vector2f projectile_velocity(0, 0);
  46.  
  47.     bool gameOver = true;
  48.     sf::Event event;
  49.     while (window.isOpen() && gameOver)
  50.     {
  51.  
  52.         while (window.pollEvent(event))
  53.         {
  54.             if (event.type == sf::Event::Closed)
  55.             {
  56.                 window.close();
  57.             }
  58.             else if (event.type == sf::Event::KeyPressed)
  59.             {
  60.                 if (event.key.code == sf::Keyboard::Right)
  61.                 {
  62.                     velocity.x = 3;
  63.                 }
  64.                 else if (event.key.code == sf::Keyboard::Left)
  65.                 {
  66.                     velocity.x = -3;
  67.                 }
  68.  
  69.                 if (event.key.code == sf::Keyboard::Space && jumping == false)
  70.                 {
  71.                     jumping = true;
  72.                     velocity.y = -10;
  73.                 }
  74.  
  75.                 if (event.key.code == sf::Keyboard::Enter && shooting == false)
  76.                 {
  77.                     shooting = true;
  78.                     projectile.setPosition(player.getPosition().x + 5, player.getPosition().y - 11);
  79.                     shooting_clock.restart();
  80.                 }
  81.             }
  82.         }
  83.  
  84.         //Jumping and gravity
  85.         if (player.getPosition().y > 450)
  86.         {
  87.             player.setPosition(player.getPosition().x, 450);
  88.             velocity.y = 0;
  89.             jumping = false;
  90.         }
  91.         else
  92.         {
  93.             velocity.y += gravity;
  94.         }
  95.  
  96.  
  97.         //Check collision
  98.         sf::FloatRect player_boundingBox = player.getGlobalBounds();
  99.         sf::FloatRect overlap;
  100.         for (int x = 0; x < 15; x++)
  101.         {
  102.             if (player_boundingBox.intersects(object[x].getGlobalBounds(), overlap))
  103.             {
  104.                 cout << overlap.height << endl;
  105.                 cout << overlap.width << endl;
  106.  
  107.                 if (overlap.height >= overlap.width)
  108.                 {
  109.                     if (player.getPosition().x < object[x].getPosition().x)
  110.                     {
  111.                         cout << "The player just hit the object from the left side" << endl;
  112.                         player.setPosition(object[x].getPosition().x - 10, player.getPosition().y);
  113.                         //velocity.x = -0.5;
  114.                         velocity.x = 0;
  115.                     }
  116.                     else if (player.getPosition().x > object[x].getPosition().x)
  117.                     {
  118.                         cout << "The player just hit the object from the right side" << endl;
  119.                         player.setPosition((object[x].getPosition().x + 40), player.getPosition().y);
  120.                         //velocity.x = .5;
  121.                         velocity.x = 0;
  122.                     }
  123.                 }
  124.                 else if (overlap.height < overlap.width)
  125.                 {
  126.                     if (player.getPosition().y < object[x].getPosition().y)
  127.                     {
  128.                         cout << "The player just hit the object from the top side" << endl;
  129.                         player.setPosition(player.getPosition().x, object[x].getPosition().y - 10);
  130.                         velocity.y = 0;
  131.                         jumping = false;
  132.                     }
  133.                     else if (player.getPosition().y > object[x].getPosition().y)
  134.                     {
  135.                         cout << "The player just hit the object from the bottom side" << endl;
  136.                         player.setPosition(player.getPosition().x, object[x].getPosition().y + 40);
  137.                         velocity.y = 0;
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143.         if (player_boundingBox.intersects(goal.getGlobalBounds()))
  144.             gameOver = false;
  145.  
  146.         player.move(velocity);
  147.  
  148.         time = shooting_clock.getElapsedTime();
  149.         if (shooting && time.asSeconds() < 1.5)
  150.         {
  151.             projectile.move(projectile_velocity);
  152.             projectile_velocity.y += projectile_accelerator;
  153.             window.draw(projectile);
  154.         }
  155.         else
  156.         {
  157.             projectile_velocity.y = 0;
  158.             projectile.setPosition(-200, -200);
  159.             shooting = false;
  160.         }
  161.            
  162.  
  163.         window.clear(sf::Color::White); //Clear
  164.  
  165.         window.draw(player);
  166.  
  167.         for (int x = 0; x < 15; x++)
  168.             window.draw(object[x]);
  169.  
  170.         window.draw(projectile);
  171.         window.draw(goal);
  172.  
  173.         window.display(); //Display
  174.     }
  175.  
  176.     window.close();
  177.  
  178.     int pause; cin >> pause;
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement