Advertisement
kasper_k

aasq

Dec 12th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <io.h>
  3. #include <vector>
  4. #include <SFML/Graphics.hpp>
  5. #include <SFML/Audio.hpp>
  6.  
  7. using namespace std;
  8. using namespace sf;
  9.  
  10. class Object {
  11.     public:
  12.         sf::Texture texture;
  13.         sf::Sprite image;
  14.         float x = 0;
  15.         float y = 0;
  16.         float width = 0;
  17.         float height = 0;
  18.         float mass = 0;
  19.         sf::Vector2f velocity;
  20.         bool moving = false;
  21.         bool can_collide = false;
  22.  
  23.         Object(string filename) {
  24.             texture.loadFromFile(filename);
  25.             image.setTexture(texture);
  26.             width = texture.getSize().x;
  27.             height = texture.getSize().y;
  28.             image.setOrigin(width / 2, height / 2);
  29.  
  30.             x = image.getPosition().x;
  31.             y = image.getPosition().y;
  32.  
  33.             velocity = sf::Vector2f();
  34.             velocity.x = 0.;
  35.             velocity.y = 0.;
  36.         };
  37.         ~Object(){};
  38.         void Move(float new_x, float new_y) {
  39.             image.setPosition(new_x, new_y);
  40.             x = new_x;
  41.             y = new_y;
  42.         };
  43.         void setImage(string filename) {
  44.             texture.loadFromFile(filename);
  45.             image.setTexture(texture);
  46.         };
  47.         void setScale(float x_scale, float y_scale) {
  48.             image.setScale(x_scale, y_scale);
  49.             width *= x_scale;
  50.             height *= y_scale;
  51.         };
  52.  
  53. };
  54.  
  55. int window_width = 1200;
  56. int window_height = 800;
  57. vector <Object> objects;
  58.  
  59. Object sun("sun.png");
  60. Object player("player.png");
  61.  
  62.  
  63. void update() {
  64.     if (objects[1].mass != 0)
  65.     {
  66.         if (objects[1].y < (window_height - objects[1].height / 2)) {
  67.             int g = 10;
  68.             float t = 0.005;
  69.             objects[1].velocity.y = objects[1].velocity.y + g * t;
  70.             objects[1].y = objects[1].y + objects[1].velocity.y * t;
  71.             objects[1].Move(objects[1].x, objects[1].y);
  72.         }
  73.        
  74.        
  75.     }
  76.     else objects[1].y = objects[1].y;
  77.    
  78. }
  79.  
  80. int main()
  81. {
  82.     sf::RenderWindow window(sf::VideoMode(window_width, window_height), "Window title");
  83.     // add sun to the vector of objects
  84.     objects.push_back(sun);
  85.     objects.push_back(player);
  86.     objects[1].mass = 6;
  87.     int player_weidth = 200;
  88.     int player_height = 200;
  89.     // move sun to the center of the screen
  90.     objects[0].Move(window_width / 2, window_height / 2);
  91.     objects[1].Move(player_weidth, player_height);
  92.  
  93.     while (window.isOpen())
  94.     {
  95.         sf::Event event;
  96.         while (window.pollEvent(event)) {
  97.             // here you need to catch events
  98.         }
  99.         // call update every tick
  100.         update();
  101.         window.clear();
  102.         // draw all objects in vector
  103.         for (int i = 0; i < int(objects.size()); i++) {
  104.             window.draw(objects[i].image);
  105.         }
  106.         // display everything
  107.         window.display();
  108.     }
  109.     return 0;
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement