Advertisement
Domerk

Untitled

Dec 22nd, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // elements.h
  2.  
  3. #ifndef ELEMENTS_H
  4. #define ELEMENTS_H
  5.  
  6. #include <QtGui>
  7. #include <time.h>
  8.  
  9. class Car : public QObject // машинка
  10. {
  11. private:
  12.     int x; // координаты
  13.     int y;
  14.     short int direction; // направление
  15.     short int Turn; // значение поворота, которое должна сделать машинка
  16.     int tc;
  17. public:
  18.     Car(int nx, int ny, int ndirection); // конструктор
  19.     void show(QPainter *painter); // рисовалка
  20.     void moving(); //движение
  21.     void turn();
  22. public slots:
  23.     void slotTC(int tc_color); // слот, принимающий значение сигнала светофора
  24. };
  25.  
  26. class TrafficLight : public QObject // светофор
  27. {
  28. protected:
  29.     int x1, y1, x2, y2, a; // координаты, угол наклона полукруга
  30.     int radius; // радиус полукруга
  31.     int position; // метоположение
  32.     int Traffic_Color; // цвет
  33. public:
  34.     TrafficLight(int npos, int ncolor);
  35.     void light (int nt_red, int nt_yellow, int nt_green, int nc); // светить, принимает время, через которое должен сменяться сигнал
  36.     void show(QPainter *painter); // рисовалка
  37.     void SendTCS();
  38. signals:
  39.     void signalTC(int tc_color);
  40. };
  41.  
  42.  
  43. #endif // ELEMENTS_H
  44.  
  45.  
  46. // из elements.cpp
  47.  
  48. void Car::slotTC(int tc_color)
  49. {
  50.  tc = tc_color;
  51. }
  52.  
  53. void TrafficLight::light (int nt_red, int nt_yellow, int nt_green, int nc)
  54. {
  55.     Traffic_Color = nc;
  56.     SendTCS();
  57.  
  58. }
  59.  
  60. void TrafficLight::SendTCS()
  61. {
  62.    emit signalTC(Traffic_Color);
  63. }
  64.  
  65. // соединение
  66.  
  67. Road::Road(QWidget *parent)
  68.     : QWidget(parent)
  69. {
  70.     painter = new QPainter(); //художник
  71.     setFixedSize(450,450);
  72.  
  73.     //============ вспомогательные переменные ===============
  74.  
  75.     time = 1;
  76.  
  77.     Car1 = new Car(150,-128,1);
  78.     Car2 = new Car(450,150,2);
  79.     Car3 = new Car(250,450,3);
  80.     Car4 = new Car(-128, 250, 4);
  81.  
  82.     TL1 = new TrafficLight(1,1);
  83.     TL2 = new TrafficLight(2,2);
  84.     TL3 = new TrafficLight(3,3);
  85.     TL4 = new TrafficLight(4,1);
  86.  
  87.     startTimer(15);
  88. }
  89.  
  90.  
  91. //==================================================
  92. void Road::paintEvent(QPaintEvent* event) // event служит для определения координат и размеров
  93.                                             //конкретной области виджета, которую нужно перерисовать
  94.  
  95. {
  96.  
  97.     painter->begin(this); //начинаем рисовать в окне
  98.     painter->drawImage(0,0,QImage("://images/crossroad.jpg"));
  99.  
  100.     TL1->show(painter);
  101.     TL2->show(painter);
  102.     TL3->show(painter);
  103.     TL4->show(painter);
  104.  
  105.     Car1->moving();
  106.     Car1->show(painter);
  107.  
  108.     Car2->moving();
  109.     Car2->show(painter);
  110.  
  111.     Car3->moving();
  112.     Car3->show(painter);
  113.  
  114.     Car4->moving();
  115.     Car4->show(painter);
  116.  
  117.  
  118.     connect(TL1, SIGNAL(signalTC(int)), Car1, SLOT(slotTC(int)));
  119.     connect(TL2, SIGNAL(signalTC(int)), Car2, SLOT(slotTC(int)));
  120.     connect(TL3, SIGNAL(signalTC(int)), Car3, SLOT(slotTC(int)));
  121.     connect(TL4, SIGNAL(signalTC(int)), Car4, SLOT(slotTC(int)));
  122.  
  123.     painter->end();//освобождаем окно
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement