Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <io.h>
- #include <vector>
- #include <SFML/Graphics.hpp>
- #include <SFML/Audio.hpp>
- using namespace std;
- using namespace sf;
- class Object {
- public:
- sf::Texture texture;
- sf::Sprite image;
- float x = 0;
- float y = 0;
- float width = 0;
- float height = 0;
- float mass = 0;
- sf::Vector2f velocity;
- bool moving = false;
- bool can_collide = false;
- Object(string filename) {
- texture.loadFromFile(filename);
- image.setTexture(texture);
- width = texture.getSize().x;
- height = texture.getSize().y;
- image.setOrigin(width / 2, height / 2);
- x = image.getPosition().x;
- y = image.getPosition().y;
- velocity = sf::Vector2f();
- velocity.x = 0.;
- velocity.y = 0.;
- };
- ~Object(){};
- void Move(float new_x, float new_y) {
- image.setPosition(new_x, new_y);
- x = new_x;
- y = new_y;
- };
- void setImage(string filename) {
- texture.loadFromFile(filename);
- image.setTexture(texture);
- };
- void setScale(float x_scale, float y_scale) {
- image.setScale(x_scale, y_scale);
- width *= x_scale;
- height *= y_scale;
- };
- };
- int window_width = 1200;
- int window_height = 800;
- vector <Object> objects;
- Object sun("sun.png");
- Object player("player.png");
- void update() {
- if (objects[1].mass != 0)
- {
- if (objects[1].y < (window_height - objects[1].height / 2)) {
- int g = 10;
- float t = 0.005;
- objects[1].velocity.y = objects[1].velocity.y + g * t;
- objects[1].y = objects[1].y + objects[1].velocity.y * t;
- objects[1].Move(objects[1].x, objects[1].y);
- }
- }
- else objects[1].y = objects[1].y;
- }
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(window_width, window_height), "Window title");
- // add sun to the vector of objects
- objects.push_back(sun);
- objects.push_back(player);
- objects[1].mass = 6;
- int player_weidth = 200;
- int player_height = 200;
- // move sun to the center of the screen
- objects[0].Move(window_width / 2, window_height / 2);
- objects[1].Move(player_weidth, player_height);
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event)) {
- // here you need to catch events
- }
- // call update every tick
- update();
- window.clear();
- // draw all objects in vector
- for (int i = 0; i < int(objects.size()); i++) {
- window.draw(objects[i].image);
- }
- // display everything
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement