-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroad.cpp
More file actions
319 lines (253 loc) · 9.56 KB
/
road.cpp
File metadata and controls
319 lines (253 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//road.cpp
#include "road.h"
//==================================================
//============= конструктор дороги =================
//==================================================
Road::Road(QWidget *parent)
: QWidget(parent)
{
painter = new QPainter(); //художник
setFixedSize(450,450);
//============ вспомогательные переменные ===============
level = 1; // уровень - по умолчанию первый
nc1 = false; // флаги смены цвета
nc2 = false;
nc3 = false;
nc4 = false;
int i; // дополнительная переменная
srand (time(NULL)); // псевдослучайное число
i = rand()%2;
if (i == 1) // если единичка
{
tc1 = 1; // то цвета чередуются вот так
tc2 = 3;
tc3 = 1;
tc4 = 3;
}
else // если нет
{
tc1 = 3; // то вот так
tc2 = 1;
tc3 = 3;
tc4 = 1;
}
t1=1; // таймеры
t2=1;
t3=1;
t4=1;
for (int i = 0; i<4; i++) // массив таймеров
{
times+=1000; // заполняем его значениями по умолчанию
times+=500; // учитывая то, что шаг таймера 10 мс
times+=1000;
}
Car1 = new Car(150,-128,1,1); // создам машинки
Car2 = new Car(450,150,2,2);
Car3 = new Car(250,450,3,3);
Car4 = new Car(-128,250,4,4);
TL1 = new TrafficLight(1,tc1); // создаём светофоры
TL2 = new TrafficLight(2,tc2);
TL3 = new TrafficLight(3,tc3);
TL4 = new TrafficLight(4,tc4);
//============ соединяем сигналы и слоты ================
connect(this, SIGNAL(signalTT(QVector<int>)), TL1, SLOT(slotTT(QVector<int>)));
connect(this, SIGNAL(signalTT(QVector<int>)), TL2, SLOT(slotTT(QVector<int>)));
connect(this, SIGNAL(signalTT(QVector<int>)), TL3, SLOT(slotTT(QVector<int>)));
connect(this, SIGNAL(signalTT(QVector<int>)), TL4, SLOT(slotTT(QVector<int>)));
connect(TL1, SIGNAL(signalR(int)), this, SLOT(slotTC(int)));
connect(TL2, SIGNAL(signalR(int)), this, SLOT(slotTC(int)));
connect(TL3, SIGNAL(signalR(int)), this, SLOT(slotTC(int)));
connect(TL4, SIGNAL(signalR(int)), this, SLOT(slotTC(int)));
connect(TL1, SIGNAL(signalTC(int, int)), Car1, SLOT(slotTC(int,int)));
connect(TL1, SIGNAL(signalTC(int, int)), Car2, SLOT(slotTC(int,int)));
connect(TL1, SIGNAL(signalTC(int, int)), Car3, SLOT(slotTC(int,int)));
connect(TL1, SIGNAL(signalTC(int, int)), Car4, SLOT(slotTC(int,int)));
connect(TL2, SIGNAL(signalTC(int,int)), Car1, SLOT(slotTC(int,int)));
connect(TL2, SIGNAL(signalTC(int,int)), Car2, SLOT(slotTC(int,int)));
connect(TL2, SIGNAL(signalTC(int,int)), Car3, SLOT(slotTC(int,int)));
connect(TL2, SIGNAL(signalTC(int,int)), Car4, SLOT(slotTC(int,int)));
connect(TL3, SIGNAL(signalTC(int,int)), Car1, SLOT(slotTC(int,int)));
connect(TL3, SIGNAL(signalTC(int,int)), Car2, SLOT(slotTC(int,int)));
connect(TL3, SIGNAL(signalTC(int,int)), Car3, SLOT(slotTC(int,int)));
connect(TL3, SIGNAL(signalTC(int,int)), Car4, SLOT(slotTC(int,int)));
connect(TL4, SIGNAL(signalTC(int,int)), Car1, SLOT(slotTC(int,int)));
connect(TL4, SIGNAL(signalTC(int,int)), Car2, SLOT(slotTC(int,int)));
connect(TL4, SIGNAL(signalTC(int,int)), Car3, SLOT(slotTC(int,int)));
connect(TL4, SIGNAL(signalTC(int,int)), Car4, SLOT(slotTC(int,int)));
connect (Car1, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car2, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car1, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car3, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car1, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car4, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car2, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car1, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car2, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car3, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car2, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car4, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car3, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car1, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car3, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car2, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car3, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car4, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car4, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car1, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car4, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car2, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (Car4, SIGNAL(siganlXY(int,int,int,int,bool,bool)), Car3, SLOT(slotXY(int,int,int,int,bool,bool)));
connect (this, SIGNAL(signalSTYLE(int,int)), Car1, SLOT(slotSTYLE(int,int)));
connect (this, SIGNAL(signalSTYLE(int,int)), Car2, SLOT(slotSTYLE(int,int)));
connect (this, SIGNAL(signalSTYLE(int,int)), Car3, SLOT(slotSTYLE(int,int)));
connect (this, SIGNAL(signalSTYLE(int,int)), Car4, SLOT(slotSTYLE(int,int)));
//================= запускаем таймер =====================
startTimer(10);
}
//==================================================
//================ рисование дороги ================
//==================================================
void Road::paintEvent(QPaintEvent* event)
{
painter->begin(this); //начинаем рисовать в окне
painter->drawImage(0,0,QImage("://images/crossroad.jpg"));
// светофоры
TL1->light(t1); // светить - передаём светофору значение таймера
TL1->show(painter); // показать
TL2->light(t2);
TL2->show(painter);
TL3->light(t3);
TL3->show(painter);
TL4->light(t4);
TL4->show(painter);
// машинки
if (level == 1) // если 1-й уровень
{
Car1->moving(); // то 1 машинка
Car1->show(painter);
}
if (level == 2) // если 2-й
{
Car1->moving(); // то 2 машинки
Car1->show(painter);
Car2->moving();
Car2->show(painter);
}
if (level == 3) // ну и так далее до 4-х
{
Car1->moving();
Car1->show(painter);
Car2->moving();
Car2->show(painter);
Car3->moving();
Car3->show(painter);
}
if (level == 4)
{
Car1->moving();
Car1->show(painter);
Car2->moving();
Car2->show(painter);
Car3->moving();
Car3->show(painter);
Car4->moving();
Car4->show(painter);
}
painter->end();//освобождаем окно
}
//==================================================
//================ работа с таймером ===============
//==================================================
void Road::timerEvent(QTimerEvent *event)
{
// для 1-го светофора
if (!nc1) // если цвет не изменился
{
t1++; // увеличиваем таймер
}
else // иначе
{
t1=1; // сбрасываем таймер
nc1=false; // опускаем флаг
}
// для 2-го светофора
if (!nc2)
{
t2++;
}
else
{
t2=1;
nc2=false;
}
// для 3-го светофора
if (!nc3)
{
t3++;
}
else
{
t3=1;
nc3=false;
}
// для 4-го светофора
if (!nc4)
{
t4++;
}
else
{
t4=1;
nc4=false;
}
repaint();
}
//==================================================
//================ слот для времени ================
//==================================================
void Road::slotDO (QVector <int> vct)
{
times = vct; // принимаем вектор времени
emit signalTT(times); // передаём его светофорам
}
//==================================================
//=============== слот для светофоров ==============
//==================================================
void Road::slotTC (int no)
{
if (no == 1) // если сигнал пришёл от 1-го светофора
{
nc1=true; // поднимаем первый флаг
}
if (no == 2) // если от 2-го
{
nc2=true; // то второй
}
if (no == 3) // и т.д.
{
nc3=true;
}
if (no == 4)
{
nc4=true;
}
}
//==================================================
//================= слот для уровня ================
//==================================================
void Road::slotLEVEL(int nlevel)
{
level = nlevel; // сохраняем значение уровня
}
//==================================================
//================ слот для дизайна ================
//==================================================
void Road::slotSTYLE(int n, int s)
{
emit signalSTYLE(n, s); // высылаем то, что получили
}
//==================================================
//================= деструктор =====================
//==================================================
Road::~Road()
{
delete painter;
delete Car1;
delete Car2;
delete Car3;
delete Car4;
delete TL1;
delete TL2;
delete TL3;
delete TL4;
}
//==================================================