Advertisement
nguyen2812

Va_cham

Aug 21st, 2020
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.81 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include<vector>
  4.  
  5. using namespace sf;
  6.  
  7. int main()
  8. {
  9.     // Create the main window
  10.     RenderWindow window(VideoMode(800,600) , "Wall");
  11.     window.setFramerateLimit(60);
  12.     //Grid
  13.     float girdSize = 50.f;
  14.     //Creat player
  15.     RectangleShape player(Vector2f(50 , 50));
  16.     player.setFillColor(Color::Green);
  17.  
  18.     //Creat wall
  19.     RectangleShape wall(Vector2f(girdSize , girdSize));
  20.     wall.setFillColor(Color::Red);
  21.     int red = 1;
  22.  
  23.     std::vector<RectangleShape> walls;
  24.     //Creat acceleration, drag, maxVelocity, ..
  25.     float acceleration = 2.5f;
  26.     float drag = 0.5f;
  27.     float maxVelocity = 6.f;
  28.     Vector2f currentVelocity( 0 , 0);
  29.     float deltaTime = 0;
  30.     float multi = 15;
  31.  
  32.     Clock clock;
  33.     //Main loop
  34.     while(window.isOpen()){
  35.         Event event;
  36.         while(window.pollEvent(event)){
  37.             if(event.type == Event::Closed){
  38.                 window.close();
  39.             }
  40.         }
  41.  
  42.         //Update
  43.  
  44.         //Change color
  45.         if(Keyboard::isKeyPressed(Keyboard::R) && red == 1){
  46.             wall.setFillColor(Color::Cyan);
  47.             red = 0;
  48.         }
  49.         else if(Keyboard::isKeyPressed(Keyboard::R) && red == 0){
  50.             wall.setFillColor(Color::Red);
  51.             red = 1;
  52.         }
  53.         //Restart deltaTime
  54.         deltaTime = clock.restart().asSeconds();
  55.  
  56.         //Acceleration
  57.         if(Keyboard::isKeyPressed(Keyboard::S)){
  58.             if(currentVelocity.y < maxVelocity){
  59.                 currentVelocity.y += acceleration * deltaTime * multi;
  60.             }
  61.         }
  62.         if(Keyboard::isKeyPressed(Keyboard::D)){
  63.             if(currentVelocity.x < maxVelocity){
  64.                 currentVelocity.x += acceleration  * deltaTime * multi;
  65.             }
  66.         }
  67.         if(Keyboard::isKeyPressed(Keyboard::A)){
  68.             if(currentVelocity.x > -maxVelocity){
  69.                 currentVelocity.x -= acceleration * deltaTime * multi;
  70.             }
  71.         }
  72.         if(Keyboard::isKeyPressed(Keyboard::W)){
  73.             if(currentVelocity.y > -maxVelocity){
  74.                 currentVelocity.y -= acceleration  * deltaTime * multi;
  75.             }
  76.         }
  77.  
  78.         //Drag
  79.         if(currentVelocity.x > 0){
  80.             currentVelocity.x -= drag  * deltaTime * multi;
  81.             if(currentVelocity.x < 0){
  82.                 currentVelocity.x = 0 ;
  83.             }
  84.         }
  85.         if(currentVelocity.x < 0){
  86.             currentVelocity.x += drag  * deltaTime * multi;
  87.             if(currentVelocity.x > 0){
  88.                 currentVelocity.x = 0 ;
  89.             }
  90.         }
  91.         if(currentVelocity.y > 0){
  92.             currentVelocity.y -= drag  * deltaTime * multi;
  93.             if(currentVelocity.y < 0){
  94.                 currentVelocity.y = 0 ;
  95.             }
  96.         }
  97.         if(currentVelocity.y < 0){
  98.             currentVelocity.y += drag  * deltaTime * multi;
  99.             if(currentVelocity.y > 0){
  100.                 currentVelocity.y = 0 ;
  101.             }
  102.         }
  103.         //Colision with window
  104.         if(player.getPosition().x < 0 ){
  105.             currentVelocity.x = 0;
  106.             player.setPosition(0 , player.getPosition().y);
  107.         }
  108.         if(player.getPosition().y < 0 ){
  109.             currentVelocity.y = 0;
  110.             player.setPosition(player.getPosition().x , 0);
  111.         }
  112.         if(player.getPosition().x > window.getSize().x - player.getSize().x ){
  113.             currentVelocity.x = 0;
  114.             player.setPosition(window.getSize().x - player.getSize().x, player.getPosition().y  );
  115.         }
  116.         if(player.getPosition().y > window.getSize().y - player.getSize().y ){
  117.             currentVelocity.y = 0;
  118.             player.setPosition(player.getPosition().x, window.getSize().y - player.getSize().y  );
  119.         }
  120.         //Set wall
  121.         Vector2i mousePos = Mouse::getPosition(window);
  122.  
  123.         if(Mouse::isButtonPressed(Mouse::Right)){
  124.             int check = 1;
  125.             wall.setPosition((int) (mousePos.x / girdSize) * girdSize , (int) (mousePos.y / girdSize) * girdSize);
  126.             //Duyet tu dau den cuoi walls
  127.             for(size_t i = 0 ; i < walls.size() ; i++){
  128.                 if((int) (mousePos.x / girdSize) * girdSize == walls[i].getPosition().x
  129.                     && (int) (mousePos.y / girdSize) * girdSize == walls[i].getPosition().y){
  130.                     /*================*/
  131.                     check = 0;
  132.                     break;
  133.                 }
  134.             }
  135.             //Push back
  136.             if(check == 1){
  137.                 walls.push_back(wall);
  138.             }
  139.  
  140.         }
  141.         //Erase wall
  142.         if(Mouse::isButtonPressed(Mouse::Left)){
  143.             for(size_t i = 0 ; i < walls.size() ; i++){
  144.                 if( (int) (mousePos.x / girdSize) * girdSize == walls[i].getPosition().x
  145.                     && (int) (mousePos.y / girdSize) * girdSize == walls[i].getPosition().y ){
  146.                     /*=================*/
  147.                     walls.erase(walls.begin() + i);
  148.                     break;
  149.                 }
  150.             }
  151.         }
  152.         if(Mouse::isButtonPressed(Mouse::Middle)){
  153.             walls.erase(walls.begin() , walls.begin() + walls.size() );
  154.         }
  155.         //Colisions
  156.         for(size_t i = 0 ; i < walls.size() ;i++){
  157.             if(walls[i].getGlobalBounds().intersects(player.getGlobalBounds())){
  158.  
  159.                 //Left colission
  160.                 if(player.getGlobalBounds().left < walls[i].getGlobalBounds().left
  161.                    && player.getGlobalBounds().left + player.getGlobalBounds().width < walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
  162.                    && player.getGlobalBounds().top < walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
  163.                    && player.getGlobalBounds().top + player.getGlobalBounds().height > walls[i].getGlobalBounds().top
  164.                    ){
  165.                     currentVelocity.x *= -0.0 ;
  166.                     player.setPosition(walls[i].getGlobalBounds().left - player.getGlobalBounds().width , player.getGlobalBounds().top);
  167.                     std::cout << "left!" << i << std::endl;
  168.                 }
  169.                 //Right colission
  170.                 else if(player.getGlobalBounds().left > walls[i].getGlobalBounds().left
  171.                    && player.getGlobalBounds().left + player.getGlobalBounds().width > walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
  172.                    && player.getGlobalBounds().top < walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
  173.                    && player.getGlobalBounds().top + player.getGlobalBounds().height > walls[i].getGlobalBounds().top
  174.                    ){
  175.                     currentVelocity.x *= -0.0;
  176.                     player.setPosition(walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width , player.getGlobalBounds().top);
  177.                     std::cout << "right!" << i << std::endl;
  178.                 }
  179.                 //Bottom colission
  180.                 else if(player.getGlobalBounds().top > walls[i].getGlobalBounds().top
  181.                    && player.getGlobalBounds().top + player.getGlobalBounds().height > walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
  182.                    && player.getGlobalBounds().left < walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
  183.                    && player.getGlobalBounds().left + player.getGlobalBounds().width > walls[i].getGlobalBounds().left
  184.                    ){
  185.                     currentVelocity.y *= -0.0;
  186.                     player.setPosition(player.getGlobalBounds().left , walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height);
  187.                     std::cout << "bottom!" << i<< std::endl;
  188.                 }
  189.                 //Top colission
  190.                 else if(player.getGlobalBounds().top < walls[i].getGlobalBounds().top
  191.                    && player.getGlobalBounds().top + player.getGlobalBounds().height < walls[i].getGlobalBounds().top + walls[i].getGlobalBounds().height
  192.                    && player.getGlobalBounds().left < walls[i].getGlobalBounds().left + walls[i].getGlobalBounds().width
  193.                    && player.getGlobalBounds().left + player.getGlobalBounds().width > walls[i].getGlobalBounds().left
  194.                    ){
  195.                     currentVelocity.y *= 0.0;
  196.                     player.setPosition(player.getGlobalBounds().left , walls[i].getGlobalBounds().top - player.getGlobalBounds().height);
  197.                     std::cout << "top!" << i << std::endl;
  198.                    }
  199.             }
  200.         }
  201.         //Player move
  202.         player.move(currentVelocity);
  203.         //Render
  204.         window.clear();
  205.  
  206.  
  207.         for(size_t i = 0 ; i < walls.size() ; i++){
  208.             window.draw(walls[i]);
  209.         }
  210.         window.draw(player);
  211.         window.display();
  212.     }
  213.  
  214.  
  215.  
  216.     return EXIT_SUCCESS;
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement