Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned int width = 500;
- unsigned int height = 500;
- unsigned int direction = 0;
- float gravity = .3;
- bool jumping = false;
- bool shooting = false;
- float projectile_accelerator = -.5;
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(width, height), "In progress");
- window.setFramerateLimit(60);
- sf::Vector2f velocity(0, 0);
- sf::RectangleShape player(sf::Vector2f(10, 10));
- player.setFillColor(sf::Color::Black);
- sf::RectangleShape object[15];
- for (int x = 0; x < 15; x++)
- {
- object[x].setFillColor(sf::Color::Red);
- object[x].setPosition(0, 0);
- object[x].setSize(sf::Vector2f(40, 40));
- }
- sf::RectangleShape goal(sf::Vector2f(30, 30));
- goal.setFillColor(sf::Color::Green);
- sf::RectangleShape projectile(sf::Vector2f(5, 5));
- projectile.setFillColor(sf::Color::Cyan);
- object[0].setPosition(0, 400);
- for (int x = 1; x < 15; x++)
- {
- object[x].setPosition(object[x - 1].getPosition().x + 30, 400);
- }
- goal.setPosition(450, 430);
- sf::Time time = sf::seconds(0);
- sf::Clock shooting_clock;
- sf::Vector2f projectile_velocity(0, 0);
- bool gameOver = true;
- sf::Event event;
- while (window.isOpen() && gameOver)
- {
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- {
- window.close();
- }
- else if (event.type == sf::Event::KeyPressed)
- {
- if (event.key.code == sf::Keyboard::Right)
- {
- velocity.x = 3;
- }
- else if (event.key.code == sf::Keyboard::Left)
- {
- velocity.x = -3;
- }
- if (event.key.code == sf::Keyboard::Space && jumping == false)
- {
- jumping = true;
- velocity.y = -10;
- }
- if (event.key.code == sf::Keyboard::Enter && shooting == false)
- {
- shooting = true;
- projectile.setPosition(player.getPosition().x + 5, player.getPosition().y - 11);
- shooting_clock.restart();
- }
- }
- }
- //Jumping and gravity
- if (player.getPosition().y > 450)
- {
- player.setPosition(player.getPosition().x, 450);
- velocity.y = 0;
- jumping = false;
- }
- else
- {
- velocity.y += gravity;
- }
- //Check collision
- sf::FloatRect player_boundingBox = player.getGlobalBounds();
- sf::FloatRect overlap;
- for (int x = 0; x < 15; x++)
- {
- if (player_boundingBox.intersects(object[x].getGlobalBounds(), overlap))
- {
- cout << overlap.height << endl;
- cout << overlap.width << endl;
- if (overlap.height >= overlap.width)
- {
- if (player.getPosition().x < object[x].getPosition().x)
- {
- cout << "The player just hit the object from the left side" << endl;
- player.setPosition(object[x].getPosition().x - 10, player.getPosition().y);
- //velocity.x = -0.5;
- velocity.x = 0;
- }
- else if (player.getPosition().x > object[x].getPosition().x)
- {
- cout << "The player just hit the object from the right side" << endl;
- player.setPosition((object[x].getPosition().x + 40), player.getPosition().y);
- //velocity.x = .5;
- velocity.x = 0;
- }
- }
- else if (overlap.height < overlap.width)
- {
- if (player.getPosition().y < object[x].getPosition().y)
- {
- cout << "The player just hit the object from the top side" << endl;
- player.setPosition(player.getPosition().x, object[x].getPosition().y - 10);
- velocity.y = 0;
- jumping = false;
- }
- else if (player.getPosition().y > object[x].getPosition().y)
- {
- cout << "The player just hit the object from the bottom side" << endl;
- player.setPosition(player.getPosition().x, object[x].getPosition().y + 40);
- velocity.y = 0;
- }
- }
- }
- }
- if (player_boundingBox.intersects(goal.getGlobalBounds()))
- gameOver = false;
- player.move(velocity);
- time = shooting_clock.getElapsedTime();
- if (shooting && time.asSeconds() < 1.5)
- {
- projectile.move(projectile_velocity);
- projectile_velocity.y += projectile_accelerator;
- window.draw(projectile);
- }
- else
- {
- projectile_velocity.y = 0;
- projectile.setPosition(-200, -200);
- shooting = false;
- }
- window.clear(sf::Color::White); //Clear
- window.draw(player);
- for (int x = 0; x < 15; x++)
- window.draw(object[x]);
- window.draw(projectile);
- window.draw(goal);
- window.display(); //Display
- }
- window.close();
- int pause; cin >> pause;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement