Advertisement
Domerk

Untitled

Dec 22nd, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //elements.cpp
  2.  
  3. #include "elements.h"
  4.  
  5. #define TL_RADIUS 14;
  6.  
  7.  
  8. Car::Car(int nx, int ny, int ndirection)
  9. {
  10.     x = nx;
  11.     y = ny;
  12.     direction = ndirection;
  13.     srand (time (NULL));
  14.     Turn = rand()%3;
  15. }
  16. //==================================================
  17. void Car::show(QPainter *painter)
  18. {
  19.     if (direction == 1)
  20.     {
  21.         QImage car1("://images/car1.jpg");
  22.         painter->drawImage(x, y, car1);
  23.     }
  24.  
  25.     if (direction == 2)
  26.     {
  27.     QImage car2("://images/car2.jpg");
  28.     painter->drawImage(x, y, car2);
  29.     }
  30.  
  31.     if (direction == 3)
  32.     {
  33.     QImage car3("://images/car3.jpg");
  34.     painter->drawImage(x, y, car3);
  35.     }
  36.  
  37.     if (direction == 4)
  38.     {
  39.     QImage car4("://images/car4.jpg");
  40.     painter->drawImage(x, y, car4);
  41.     }
  42.  
  43. }
  44. //==================================================
  45. void Car::moving()
  46. {
  47.  
  48.   // остановка на 320 или 10
  49.  
  50.     if (tc==1)
  51.     {
  52.         return;
  53.     }
  54.  
  55.     if (direction==1)
  56.         {
  57.             y++;
  58.         }
  59.         if (direction==2)
  60.         {
  61.             x--;
  62.         }
  63.  
  64.         if (direction==3)
  65.         {
  66.             y--;
  67.         }
  68.  
  69.  
  70.         if (direction==4)
  71.         {
  72.             x++;
  73.         }
  74.  
  75. }
  76.  
  77. void Car::slotTC(int tc_color)
  78. {
  79.  tc = tc_color;
  80. }
  81.  
  82. void Car::turn()
  83. {
  84.     if (Turn == 0)
  85.     {
  86.         return;
  87.     }
  88.  
  89.     if ((direction == 1 && Turn == 1) || (direction == 3 && Turn == 2))
  90.     {
  91.         direction = 4;
  92.     }
  93.  
  94.     if ((direction == 1 && Turn == 2) || (direction == 3 && Turn == 1))
  95.     {
  96.         direction = 2;
  97.     }
  98.  
  99.     if ((direction == 2 && Turn == 1) || (direction == 4 && Turn == 2))
  100.     {
  101.         direction = 1;
  102.     }
  103.  
  104.     if ((direction == 2 && Turn == 2) || (direction == 3 && Turn == 1))
  105.     {
  106.         direction = 3;
  107.     }
  108.  
  109.  
  110. }
  111.  
  112. //==================================================
  113. //==================================================
  114.  
  115. TrafficLight::TrafficLight (int npos, int ncolor)
  116. {
  117.     position = npos;
  118.     radius = TL_RADIUS;
  119.     Traffic_Color = ncolor;
  120.  
  121.     if (position == 1)
  122.     {
  123.         x1 = 65;
  124.         y1 = 42;
  125.         x2 = 356;
  126.         y2 = 335;
  127.         a = 0;
  128.     }
  129.  
  130.    if (position == 2)
  131.     {
  132.         x1 = 336;
  133.         y1 = 62;
  134.         x2 = 46;
  135.         y2 = 355;
  136.         a = 90;
  137.     }
  138.  
  139.   if (position == 3)
  140.     {
  141.         x1 = 356;
  142.         y1 = 376;
  143.         x2 = 65;
  144.         y2 = 83;
  145.         a = 180;
  146.     }
  147.    if (position == 4)
  148.     {
  149.         x1 = 87;
  150.         y1 = 355;
  151.         x2 = 376;
  152.         y2 = 62;
  153.         a = 270;
  154.     }
  155.  
  156. }
  157. //==================================================
  158.  
  159. void TrafficLight::light (int nt_red, int nt_yellow, int nt_green, int nc)
  160. {
  161.     Traffic_Color = nc;
  162.     SendTCS();
  163.  
  164. }
  165.  
  166. void TrafficLight::SendTCS()
  167. {
  168.    emit signalTC(Traffic_Color);
  169. }
  170.  
  171.  
  172.  
  173.  
  174. void TrafficLight::show(QPainter *painter)
  175. {
  176.  
  177.      //устанавливаем цвет и толщину
  178.  
  179.     //карандаша
  180.  
  181.     if (Traffic_Color == 1)
  182.     {
  183.     painter->setPen(QPen(Qt::red,1));
  184.     painter->setBrush(QBrush(Qt::red));
  185.     }
  186.  
  187.     if (Traffic_Color == 2)
  188.     {
  189.     painter->setPen(QPen(Qt::yellow,1));
  190.     painter->setBrush(QBrush(Qt::yellow));
  191.     }
  192.  
  193.     if (Traffic_Color == 3 )
  194.     {
  195.     painter->setPen(QPen(Qt::green,1));
  196.     painter->setBrush(QBrush(Qt::green));
  197.     }
  198.  
  199.     painter->drawChord(x1, y1, 2*radius, 2*radius, 16*a, 16*180);
  200.     painter->drawChord(x2, y2, 2*radius, 2*radius, 16*a, 16*180);
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement