Advertisement
Domerk

Движение машинки

Dec 22nd, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Car::moving()
  2. {
  3.    
  4.     // движение при подъезде к перекрёстку, если светофор не зелёный, то стоп
  5.    
  6.     if (tc !=3 && f==false)
  7.     {
  8.        
  9.         if (direction==1 && y>=10)
  10.         {
  11.             emit siganlXY(direction, x, y);
  12.             return;
  13.         }
  14.         if (direction==2 && x<=320)
  15.         {
  16.             int nx = x + CLENGTH;
  17.             emit siganlXY(direction, nx, y);
  18.             return;
  19.         }
  20.        
  21.         if (direction==3 && y<=320)
  22.         {
  23.             int ny = y + CLENGTH;
  24.             emit siganlXY(direction, x, ny);
  25.             return;
  26.         }
  27.        
  28.        
  29.         if (direction==4 && x>=10)
  30.         {
  31.             emit siganlXY(direction, x, y);
  32.             return;
  33.         }
  34.        
  35.        
  36.     }
  37.    
  38.     if (tc == 3 && f==false)
  39.     {
  40.        
  41.         if (direction==1 && y>=20)
  42.         {
  43.             turn();
  44.             return;
  45.         }
  46.         if (direction==2 && x<=310)
  47.         {
  48.             turn();
  49.             return;
  50.         }
  51.        
  52.         if (direction==3 && y<=310)
  53.         {
  54.             turn();
  55.             return;
  56.         }
  57.        
  58.         if (direction==4 && x>=20)
  59.         {
  60.             turn();
  61.             return;
  62.         }
  63.        
  64.        
  65.     }
  66.    
  67.     // если машинка после повторота исчезла за краем экрана, то
  68.     if (f==true)
  69.     {
  70.        
  71.         if (direction==1 && y>=450)
  72.         {
  73.             x = 150;
  74.             y = -138;
  75.             srand (time(NULL));
  76.             Turn = rand()%3;
  77.             f = false;
  78.             return;
  79.         }
  80.         if (direction==2 && x<=0)
  81.         {
  82.             x = 460;
  83.             y = 150;
  84.             srand (time(NULL));
  85.             Turn = rand()%3;
  86.             f = false;
  87.             return;
  88.         }
  89.        
  90.         if (direction==3 && y<=0)
  91.         {
  92.             x = 250;
  93.             y = 460;
  94.             srand (time(NULL));
  95.             Turn = rand()%3;
  96.             f = false;
  97.             return;
  98.         }
  99.        
  100.        
  101.         if (direction==4 && x>=450)
  102.         {
  103.             x = -138;
  104.             y = 250;
  105.             srand (time(NULL));
  106.             Turn = rand()%3;
  107.             f = false;
  108.             return;
  109.         }
  110.        
  111.     }
  112.    
  113.     // движение в зависимости от направления
  114.    
  115.     if (direction==1)
  116.     {
  117.         y++;
  118.         emit siganlXY(direction, x, y);
  119.         return;
  120.     }
  121.     if (direction==2)
  122.     {
  123.         x--;
  124.         int nx = x + CLENGTH;
  125.         emit siganlXY(direction, nx, y);
  126.         return;
  127.     }
  128.    
  129.     if (direction==3)
  130.     {
  131.         y--;
  132.         int ny = y + CLENGTH;
  133.         emit siganlXY(direction, x, ny);
  134.         return;
  135.     }
  136.    
  137.    
  138.     if (direction==4)
  139.     {
  140.         x++;
  141.         emit siganlXY(direction, x, y);
  142.         return;
  143.     }
  144.    
  145.    
  146.    
  147. }
  148.  
  149.  
  150. //==================================================
  151. void Car::turn()
  152. {
  153.     if (Turn == 0)
  154.     {
  155.         f = true;
  156.         return;
  157.     }
  158.  
  159.     if ((direction == 1 && Turn == 1) || (direction == 3 && Turn == 2))
  160.     {
  161.         direction = 4;
  162.         x++;
  163.         y = 250;
  164.     }
  165.  
  166.     if ((direction == 1 && Turn == 2) || (direction == 3 && Turn == 1))
  167.     {
  168.         direction = 2;
  169.         x--;
  170.         y = 150;
  171.     }
  172.  
  173.     if ((direction == 2 && Turn == 1) || (direction == 4 && Turn == 2))
  174.     {
  175.         direction = 1;
  176.         y++;
  177.         x = 150;
  178.     }
  179.  
  180.     if ((direction == 2 && Turn == 2) || (direction == 4 && Turn == 1))
  181.     {
  182.         direction = 3;
  183.         y--;
  184.         x = 250;
  185.     }
  186.  
  187.     f = true;
  188.  
  189. }
  190.  
  191. //==================================================
  192. //==================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement