Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Graphics.hpp>
- using namespace std;
- unsigned int width = 500;
- unsigned int height = 500;
- unsigned int direction = 0;
- float gravity = .3;
- bool jumping = false;
- 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);
- srand(time(NULL)); //Generate random seed
- int randomX;
- int randomY;
- for (int x = 0; x < 15; x++)
- {
- randomX = rand() % 500;
- randomY = rand() % 500;
- object[x].setPosition(randomX, randomY);
- }
- randomX = rand() % 500;
- randomY = rand() % 500;
- goal.setPosition(randomX, randomY);
- 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;
- }
- }
- }
- //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);
- window.clear(sf::Color::White); //Clear
- window.draw(player);
- for (int x = 0; x < 15; x++)
- window.draw(object[x]);
- window.draw(goal);
- window.display(); //Display
- }
- window.close();
- int pause; cin >> pause;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement