Advertisement
kasper_k

qq1

Dec 28th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Audio.hpp>
  5.  
  6. using namespace std;
  7. using namespace sf;
  8.  
  9. class Object {
  10. public:
  11.     sf::Texture texture;
  12.     sf::Sprite image;
  13.     float x = 0;
  14.     float y = 0;
  15.     float width = 0;
  16.     float height = 0;
  17.     float mass = 0;
  18.     sf::Vector2f velocity;
  19.     bool moving = false;
  20.     bool can_collide = false;
  21.     bool gravitation = true;
  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. bool onground = false;
  58.  
  59. int flagclose = 0;
  60. int frd = 0;
  61. int last_frd = 0;
  62. int playerdirection = 0;
  63. vector <Object> objects;
  64.  
  65. Object sun("sun.png");
  66. Object player("banan.png");
  67. Object wall("wall.png");
  68. Object platform("platform2.png");
  69. Object background("start.png");
  70. Object buttonstart("mousebuttonstart.png");
  71. Object buttonexit("mousebuttonexit.png");
  72. Object platform2("block.png");
  73.  
  74.  
  75. RectangleShape healthbar(Vector2f(100, 20));
  76. RectangleShape whitebar(Vector2f(100, 20));
  77. RectangleShape healtpalyer(Vector2f(50, 10));
  78.  
  79. int healthpoint = 100;
  80.  
  81. void update(float time, int& frd, int& last_frd) {
  82.     int g = 10;
  83.     FloatRect playerbounds = objects[1].image.getGlobalBounds();//координаты персонажа
  84.     FloatRect rectanglebounds = objects[2].image.getGlobalBounds();//координаты стены в виде прямоугольника
  85.     FloatRect pol = objects[2].image.getGlobalBounds();
  86.     FloatRect platform = objects[3].image.getGlobalBounds();
  87.     if (objects[1].gravitation == true) {
  88.         if (objects[1].mass != 0) {
  89.             if (objects[1].y >= window_height - objects[3].height - 5)
  90.             {
  91.                 objects[1].y = window_height - objects[3].height - 5;
  92.                 objects[1].velocity.y = 0;
  93.                 onground = true;
  94.                 if (playerbounds.intersects(platform)) {
  95.                     objects[1].Move(objects[1].x, objects[1].y - 0.125);
  96.                 }
  97.             }
  98.             else if (objects[1].y >= window_height - objects[3].height - 42 + 5 && objects[1].x >= objects[7].x - 18) {
  99.                 objects[1].y = window_height - objects[3].height - 42 + 5;
  100.                 objects[1].velocity.y = 0;
  101.                 onground = true;
  102.             }
  103.             else {
  104.                 objects[1].velocity.y = objects[1].velocity.y + g * time;
  105.                 objects[1].y = objects[1].y + objects[1].velocity.y * time;
  106.                 objects[1].Move(objects[1].x, objects[1].y);
  107.                 healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  108.             }
  109.         }
  110.     }
  111.     if (objects[2].gravitation == true) {
  112.         if (objects[2].mass != 0) {
  113.             if (objects[2].y >= window_height - objects[3].height - objects[2].height / 2 + 10)
  114.             {
  115.                 objects[2].y = window_height - objects[3].height - objects[2].height / 2 + 10;
  116.                 objects[2].velocity.y = 0;
  117.                 if (rectanglebounds.intersects(platform)) {
  118.  
  119.                     objects[2].Move(objects[2].x, objects[2].y - 0.125);
  120.                 }
  121.  
  122.             }
  123.             else {
  124.                 objects[2].velocity.y = objects[2].velocity.y + g * time;
  125.                 objects[2].y = objects[2].y + objects[2].velocity.y * time;
  126.                 objects[2].Move(objects[2].x, objects[2].y);
  127.                 healthbar.setPosition(objects[2].x - 50, objects[2].y - 130);
  128.                 whitebar.setPosition(objects[2].x - 50, objects[2].y - 130);
  129.             }
  130.         }
  131.         else {
  132.             objects[2].y = objects[2].y;
  133.         }
  134.     }
  135.     if (frd == 1 && objects[1].x > objects[1].width / 2) {
  136.         player.setImage("banan.png");
  137.         objects[1].velocity.x = -1;
  138.         objects[1].x += (objects[1].velocity.x);
  139.         objects[1].Move(objects[1].x, objects[1].y);
  140.         healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  141.         objects[1].velocity.x = 0;
  142.         frd = 0;
  143.     }
  144.     if (frd == 2 && objects[1].x < window_width - (objects[1].width / 2)) {
  145.         player.setImage("bananreverse.png");
  146.         objects[1].velocity.x = 1;
  147.         objects[1].x += (objects[1].velocity.x);
  148.         objects[1].Move(objects[1].x, objects[1].y);
  149.         healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  150.         objects[1].velocity.x = 0;
  151.         frd = 0;
  152.     }
  153.     if (last_frd == 3) {
  154.         objects[1].velocity.y = 1;
  155.         objects[1].y -= 100;
  156.         healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  157.         onground = false;
  158.         last_frd = 0;
  159.     }
  160.     if (objects[1].x + 22 == objects[7].x && objects[1].y >= 740) {
  161.         objects[1].Move(objects[1].x - 1, objects[1].y);
  162.     }
  163.     //границы
  164.     if (objects[1].x < objects[1].width / 2) {
  165.         objects[1].Move(objects[1].x + 1, objects[1].y);
  166.     }
  167.     if (objects[1].x > window_width - (objects[1].width / 2)) {
  168.         objects[1].Move(objects[1].x - 1, objects[1].y);
  169.     }
  170.     if (objects[2].x < objects[2].width / 2) {
  171.         objects[2].Move(objects[2].x + 1, objects[2].y);
  172.     }
  173.     if (objects[1].y > window_height - objects[1].height / 2) {
  174.         objects[1].Move(objects[1].x, objects[1].y - 1);
  175.     }
  176.     if (objects[2].x > window_width - (objects[2].width / 2)) {
  177.         objects[2].Move(objects[2].x - 1, objects[2].y);
  178.     }
  179. }
  180.  
  181. int main()
  182. {
  183.     RenderWindow window(VideoMode(window_width, window_height), "banana adventure!");
  184.     Clock clock;
  185.     int max = 10;
  186.  
  187.     objects.push_back(sun);
  188.     objects.push_back(player);
  189.     objects.push_back(wall);//objects[2] это стена
  190.     objects.push_back(platform);//objects[3] это платформа
  191.     objects.push_back(background);//оbkects[4] это фон меню
  192.     objects.push_back(buttonstart);//objects[5] это кнопка старта в меню
  193.     objects.push_back(buttonexit);//objects[6] это кнопка выхода из игры
  194.     objects.push_back(platform2);//objects[7] платформа по которой можно двигаться
  195.  
  196.     objects[1].mass = 1;
  197.     objects[2].mass = 1;
  198.  
  199.     objects[1].Move(window_width / 2 + objects[1].height * 2, objects[1].height / 2);
  200.     objects[2].Move(objects[2].width / 2, window_height - objects[3].height * 3);
  201.     objects[3].Move(window_width / 2, window_height - objects[3].height / 4);
  202.     objects[0].Move(window_width / 2, sun.height / 2);
  203.     objects[4].Move(window_width / 2, window_height / 2);
  204.     objects[5].Move(window_width / 2 - 256, window_height / 2 + 40);
  205.     objects[6].Move(window_width / 2 + 256, window_height / 2);
  206.     objects[7].Move(window_width - 100, window_height - objects[3].height - 10);
  207.  
  208.     objects[7].y = window_height - objects[3].height - 42 + 14;
  209.     objects[7].x = window_height - objects[7].width / 2 + 300;
  210.  
  211.     objects[2].x = objects[2].width;
  212.     healthbar.setFillColor(Color::Red);
  213.     healthbar.setPosition(0, window_height - (objects[2].height + 25));
  214.     whitebar.setFillColor(Color::White);
  215.     whitebar.setPosition(0, window_height - (objects[2].height + 25));
  216.  
  217.     healtpalyer.setFillColor(Color::Red);
  218.     healtpalyer.setPosition(objects[1].x - 48, objects[1].y - 64);
  219.  
  220.  
  221.     SoundBuffer shootBuffer;
  222.     shootBuffer.loadFromFile("atack.ogg");
  223.     Sound shoot(shootBuffer);
  224.  
  225.     while (window.isOpen())
  226.     {
  227.         float time = clock.getElapsedTime().asMicroseconds();
  228.         clock.restart();
  229.         time = time / 80000;
  230.         Vector2i pos = Mouse::getPosition(window);//забираем коорд курсора
  231.         Event event;
  232.         if (flagclose == 1) {
  233.             while (window.pollEvent(event))
  234.             {
  235.                 if (Mouse::isButtonPressed(Mouse::Left))
  236.                 {
  237.                     if (objects[1].image.getGlobalBounds().contains(pos.x, pos.y) && objects[2].moving == false)
  238.                     {
  239.                         if (((pos.x > (objects[1].x - 25)) && (pos.x < (objects[1].x + 25))) && ((pos.y > (objects[1].y - 25)) && (pos.y < (objects[1].y + 25)))) {
  240.                             objects[1].moving = true;
  241.                         }
  242.                         objects[1].moving = true;
  243.                         objects[1].gravitation = false;
  244.                     }
  245.                     if (objects[2].image.getGlobalBounds().contains(pos.x, pos.y) && objects[1].moving == false)
  246.                     {
  247.                         objects[2].moving = true;
  248.                         objects[2].gravitation = false;
  249.                     }
  250.                 }
  251.                 if (Keyboard::isKeyPressed(Keyboard::Escape)) flagclose = 0;
  252.                 if (event.type == Event::MouseButtonReleased)
  253.                 {
  254.                     if (event.key.code == Mouse::Left && objects[1].moving)
  255.                     {
  256.                         objects[1].moving = false;
  257.                         objects[1].gravitation = true;
  258.                     }
  259.                     if (event.key.code == Mouse::Left && objects[2].moving)
  260.                     {
  261.                         objects[2].moving = false;
  262.                         objects[2].gravitation = true;
  263.                     }
  264.                 }
  265.                 if (event.type == sf::Event::KeyPressed)
  266.                 {
  267.                     if (event.key.code == sf::Keyboard::E) { // attack
  268.                         if (playerdirection == 1 && (0 < objects[1].x - objects[2].x) && (objects[1].x - objects[2].x <= 100))
  269.                         {
  270.                             cout << objects[1].x - objects[2].x << " ";
  271.                             if (healthpoint > 0) {
  272.                                 shoot.play();
  273.                                 healthbar.setSize(Vector2f(100 - max, 20));
  274.                                 healthpoint -= 10;
  275.                                 max += 10;
  276.                                 if (max == 100) {
  277.                                     healthbar.setSize(Vector2f(0, 0));
  278.                                     objects[2].setScale(0, 0);
  279.                                     whitebar.setSize(Vector2f(0, 0));
  280.                                 }
  281.                             }
  282.                         }
  283.                         else if (playerdirection == 2 && (0 < objects[2].x - objects[1].x) && (objects[2].x - objects[1].x <= 100))
  284.                         {
  285.                             cout << objects[2].x - objects[1].x << " ";
  286.                             if (healthpoint > 0) {
  287.                                 shoot.play();
  288.                                 healthbar.setSize(Vector2f(100 - max, 20));
  289.                                 healthpoint -= 10;
  290.                                 max += 10;
  291.                                 if (max == 100) {
  292.                                     healthbar.setSize(Vector2f(0, 0));
  293.                                     objects[2].setScale(0, 0);
  294.                                     whitebar.setSize(Vector2f(0, 0));
  295.                                 }
  296.                             }
  297.                         }
  298.  
  299.                     }
  300.                     if (event.key.code == sf::Keyboard::W && onground) { // jump
  301.                         last_frd = 3;
  302.  
  303.                     }
  304.                 }
  305.  
  306.             }
  307.             if (Keyboard::isKeyPressed(Keyboard::Left) || Keyboard::isKeyPressed(Keyboard::A)) { // move left
  308.                 frd = 1;
  309.                 playerdirection = 1;
  310.             }
  311.             if (Keyboard::isKeyPressed(Keyboard::Right) || Keyboard::isKeyPressed(Keyboard::D)) { // move right
  312.                 frd = 2;
  313.                 playerdirection = 2;
  314.             }
  315.             // взаимодействие стены с игроком
  316.             FloatRect playerbounds = objects[1].image.getGlobalBounds();//координаты персонажа
  317.             FloatRect rectanglebounds = objects[2].image.getGlobalBounds();//координаты стены в виде прямоугольника
  318.             if (playerbounds.intersects(rectanglebounds)) {
  319.                 if (playerdirection == 1) {
  320.                     objects[1].Move(objects[1].x + 1, objects[1].y);
  321.                     healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  322.                 }
  323.                 if (playerdirection == 2) {
  324.                     objects[1].Move(objects[1].x - 1, objects[1].y);
  325.                     healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  326.                 }
  327.             }
  328.             if (objects[1].moving) {
  329.                 onground = false;
  330.                 objects[1].x = pos.x;
  331.                 objects[1].y = pos.y;
  332.                 objects[1].Move(objects[1].x, objects[1].y);
  333.                 healtpalyer.setPosition(objects[1].x - 24, objects[1].y - 64);
  334.             }
  335.             if (objects[2].moving) {
  336.                 onground = false;
  337.                 objects[2].x = pos.x;
  338.                 objects[2].y = pos.y;
  339.                 objects[2].Move(objects[2].x, objects[2].y);
  340.                 healthbar.setPosition(objects[2].x - 50, objects[2].y - 130);
  341.                 whitebar.setPosition(objects[2].x - 50, objects[2].y - 130);
  342.             }
  343.  
  344.  
  345.             update(time, frd, last_frd);
  346.             window.clear();
  347.             for (int i = 0; i < int(objects.size()); i++)
  348.             {
  349.                 if (i != 4 && i != 5 && i != 6)
  350.                     window.draw(objects[i].image);
  351.  
  352.             }
  353.             window.draw(healtpalyer);
  354.             window.draw(whitebar);
  355.             window.draw(healthbar);
  356.             window.display();
  357.         }
  358.         else if (flagclose == 0) {
  359.             while (window.pollEvent(event)) {
  360.                 if (objects[5].image.getGlobalBounds().contains(pos.x, pos.y)) {
  361.                     if (Mouse::isButtonPressed(Mouse::Left))
  362.                         flagclose = 1;
  363.                 }if (objects[6].image.getGlobalBounds().contains(pos.x, pos.y) && Mouse::isButtonPressed(Mouse::Left)) {
  364.                     window.close();
  365.                     break;
  366.                 }
  367.                 if (event.type == sf::Event::KeyPressed)
  368.                 {
  369.                     if (event.key.code == sf::Keyboard::Escape) {
  370.                         window.close();
  371.                     }
  372.                 }
  373.             }
  374.             window.clear();
  375.             window.draw(objects[4].image);
  376.             window.draw(objects[5].image);
  377.             window.draw(objects[6].image);
  378.             window.display();
  379.         }
  380.     }
  381.     return 0;
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement