Advertisement
PieInlaw

Events

Apr 16th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.84 KB | None | 0 0
  1.         // Ивенты
  2.        
  3.         Event event;
  4.         while (window.pollEvent(event))
  5.         {
  6.            
  7.            
  8.             // Close window: exit
  9.             if (event.type == sf::Event::Closed) {
  10.                 window.close();
  11.             }
  12.             ///// Кнопка перезагрузки /////
  13.             if (event.type == Event::MouseButtonReleased)//если нажата клавиша мыши
  14.             {
  15.                 if (restartsprite.getGlobalBounds().contains(pos.x, pos.y))//и при этом координата курсора попадает в спрайт
  16.                 {return true;}
  17.             }
  18.            
  19.             ///// Кнопка esc /////
  20.             if (event.type == Event::MouseButtonReleased)//если нажата клавиша мыши
  21.             {
  22.                 if (Escsprite.getGlobalBounds().contains(pos.x, pos.y))//и при этом координата курсора попадает в спрайт
  23.                 {return false;}
  24.             }
  25.            
  26.             ///// Движение по главной диагонале( 1 и 9 ) /////
  27.             a=50;
  28.            
  29.             if (pos.y>=170 && pos.y<=570 && pos.x>=90 && pos.x<=490)
  30.                 if (event.type == Event::MouseButtonPressed)//если нажата клавиша мыши
  31.                    
  32.                     if (iconsprite.getGlobalBounds().contains(pos.x - a, pos.y - a ))//и при этом координата курсора попадает в спрайт
  33.                     {
  34.                        
  35.                         dX =pos.x - iconsprite.getPosition().x;//делаем разность между позицией курсора и спрайта.для корректировки нажатия
  36.                         dY =pos.y - iconsprite.getPosition().y;//тоже самое по игрику
  37.                         isMove = true;//можем двигать спрайт
  38.                        
  39.                     }
  40.            
  41.             if (event.type == Event::MouseButtonReleased)//если отпустили клавишу
  42.                 isMove = false; //то не можем двигать спрайт
  43.            
  44.            
  45.             if (isMove) {//если можем двигать
  46.                 iconsprite.setPosition((pos.x - dX + a), (pos.y - dY + a ));//можно и так написать,если у вас нету х и у в классе игрока
  47.                 isMove =false;
  48.                 paint++;
  49.                 if (paint & 1) iconsprite.setColor(Color::White);
  50.                 else   iconsprite.setColor(Color::Magenta);
  51.                
  52.                
  53.             }
  54.            
  55.             a=-50;
  56.             if (pos.y>=170 && pos.y<=570 && pos.x>=90 && pos.x<=490)
  57.                 if (event.type == Event::MouseButtonPressed)//если нажата клавиша мыши
  58.                    
  59.                     if (iconsprite.getGlobalBounds().contains(pos.x - a, pos.y - a ))//и при этом координата курсора попадает в спрайт
  60.                     {
  61.                        
  62.                         dX =pos.x - iconsprite.getPosition().x;//делаем разность между позицией курсора и спрайта.для корректировки нажатия
  63.                         dY =pos.y - iconsprite.getPosition().y;//тоже самое по игрику
  64.                         isMove = true;//можем двигать спрайт
  65.                        
  66.                     }
  67.            
  68.             if (event.type == Event::MouseButtonReleased)//если отпустили клавишу
  69.                 isMove = false; //то не можем двигать спрайт
  70.            
  71.            
  72.             if (isMove) {//если можем двигать
  73.                 iconsprite.setPosition((pos.x - dX + a), (pos.y - dY + a ));//можно и так написать,если у вас нету х и у в классе игрока
  74.                 isMove =false;
  75.                 paint++;
  76.                 if (paint & 1) iconsprite.setColor(Color::White);
  77.                 else   iconsprite.setColor(Color::Magenta);
  78.                
  79.             }
  80.            
  81.            
  82.            
  83.            
  84.             ///// Движение вправо вверх /////
  85.             a=-50;
  86.            
  87.             if (event.type == Event::MouseButtonPressed)//если нажата клавиша мыши
  88.                 if (pos.x<=490 && pos.y>=170)
  89.                     if (iconsprite.getGlobalBounds().contains(pos.x + a, pos.y - a ))//и при этом координата курсора попадает в спрайт
  90.                     {
  91.                        
  92.                         dX =pos.x - iconsprite.getPosition().x;//делаем разность между позицией курсора и спрайта.для корректировки нажатия
  93.                         dY =pos.y - iconsprite.getPosition().y;//тоже самое по игрику
  94.                         isMove = true;//можем двигать спрайт
  95.                        
  96.                     }
  97.            
  98.             if (event.type == Event::MouseButtonReleased)//если отпустили клавишу
  99.                 isMove = false; //то не можем двигать спрайт
  100.            
  101.             if (isMove) {//если можем двигать
  102.                 iconsprite.setPosition((pos.x - dX - a), (pos.y - dY + a ));//можно и так написать,если у вас нету х и у в классе игрока
  103.                 isMove =false;
  104.                 paint++;
  105.                 if (paint & 1) iconsprite.setColor(Color::White);
  106.                 else   iconsprite.setColor(Color::Magenta);
  107.             }
  108.            
  109.            
  110.             ///// Движение вправо /////
  111.             if (event.type == Event::MouseButtonPressed)//если нажата клавиша мыши
  112.                 if (pos.x<=490)
  113.                    
  114.                     if (iconsprite.getGlobalBounds().contains(pos.x + a, pos.y ))//и при этом координата курсора попадает в спрайт
  115.                     {
  116.                        
  117.                         dX =pos.x - iconsprite.getPosition().x;//делаем разность между позицией курсора и спрайта.для корректировки нажатия
  118.                         dY =pos.y - iconsprite.getPosition().y;//тоже самое по игрику
  119.                         isMove = true;//можем двигать спрайт
  120.                        
  121.                     }
  122.            
  123.             if (event.type == Event::MouseButtonReleased)//если отпустили клавишу
  124.                 isMove = false; //то не можем двигать спрайт
  125.            
  126.             if (isMove) {//если можем двигать
  127.                 iconsprite.setPosition((pos.x - dX - a), (pos.y - dY));//можно и так написать,если у вас нету х и у в классе игрока
  128.                 isMove =false;
  129.                 paint++;
  130.                 if (paint & 1) iconsprite.setColor(Color::White);
  131.                 else   iconsprite.setColor(Color::Magenta);
  132.             }
  133.            
  134.            
  135.             ///// Движение вверх /////
  136.             if (event.type == Event::MouseButtonPressed)//если нажата клавиша мыши
  137.                 if (pos.y>=170)
  138.                    
  139.                     if (iconsprite.getGlobalBounds().contains(pos.x, pos.y - a ))//и при этом координата курсора попадает в спрайт
  140.                     {
  141.                        
  142.                         dX =pos.x - iconsprite.getPosition().x;//делаем разность между позицией курсора и спрайта.для корректировки нажатия
  143.                         dY =pos.y - iconsprite.getPosition().y;//тоже самое по игрику
  144.                        
  145.                         isMove = true;//можем двигать спрайт
  146.                        
  147.                     }
  148.            
  149.             if (event.type == Event::MouseButtonReleased)//если отпустили клавишу
  150.                 isMove = false; //то не можем двигать спрайт
  151.            
  152.             if (isMove) {//если можем двигать
  153.                 iconsprite.setPosition((pos.x - dX), (pos.y - dY + a ));//можно и так написать,если у вас нету х и у в классе игрока
  154.                 isMove =false;
  155.                 paint++;
  156.                 if (paint & 1) iconsprite.setColor(Color::White);
  157.                 else   iconsprite.setColor(Color::Magenta);
  158.             }
  159.            
  160.            
  161.            
  162.         }
  163.        
  164.         //// Выыод победителя ////
  165.         if (iconsprite.getPosition().x == 440 && iconsprite.getPosition().y == 170)
  166.             if (paint & 1) window.close();
  167.        
  168.        
  169.        
  170.         // Escape pressed: exit
  171.         if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
  172.             window.close();
  173.         }
  174.        
  175.        
  176.         // Clear screen
  177.         window.clear();
  178.        
  179.         // Draw the sprite
  180.         window.draw(bgsprite);
  181.         window.draw(desk);
  182.        
  183.         window.draw(restartsprite);
  184.         window.draw(Escsprite);
  185.        
  186.        
  187.         // Draw the string
  188.         window.draw(text);
  189.         // window.draw(specification);
  190.         window.draw(iconsprite);
  191.        
  192.        
  193.        
  194.         // Update the window
  195.         window.display();
  196.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement