Advertisement
Felanpro

randomly generated game

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