Заблокирован
Регистрация: 06.02.2011
Сообщений: 1,999
|
Имитатор движения поезда
Здрасти !
Нужно было создать такую анимацию.
Кто предложит другой вариант этой задачи буду очень признателен.
Задачка не простая - так как требует знания работы АБ
Код:
struct Cell
{
RECT rect;
bool color;
};
Cell cell[20];
HBRUSH hbrush[4];
struct Semaphore
{
RECT rect[3];
COORD line[4];
bool Yellow,Green,Red;
Cell *First,*Second;
int first,second;
void Inspection()
{
int i;
for (i=0;i<first;i++)
{
if (!First[i].color) break;
}
if (i<first)
{
Yellow = false;
Green = false;
Red = true;
return;
}
for (i=0;i<second;i++)
{
if (!Second[i].color) break;
}
if (i<second)
{
Yellow = true;
Green = false;
Red = false;
return;
}
Yellow = false;
Green = true;
Red = false;
}
void Show_Semaphore(HDC hdc)
{
Inspection();
if (Yellow) SelectObject(hdc, hbrush[2]);
else SelectObject(hdc, hbrush[3]);
Ellipse(hdc,rect[2].left,rect[2].top,rect[2].right,rect[2].bottom);
if (Green) SelectObject(hdc, hbrush[1]);
else SelectObject(hdc, hbrush[3]);
Ellipse(hdc,rect[1].left,rect[1].top,rect[1].right,rect[1].bottom);
if (Red) SelectObject(hdc, hbrush[0]);
else SelectObject(hdc, hbrush[3]);
Ellipse(hdc,rect[0].left,rect[0].top,rect[0].right,rect[0].bottom);
for (int i=0;i<4;i++)
{
MoveToEx(hdc, line[i].X, line[i].Y, NULL);
i++;
LineTo(hdc, line[i].X, line[i].Y);
}
}
};
Semaphore semaphore[3];
// дескрипторы кистей
bool launch = false;
bool condition[20];
bool logic = false;
int number;
void Imitation()
{
if (!logic)
{
logic = true;
number = 0;
}
if (number-2 >= 0)
{
if (!cell[number-2].color)
{
cell[number-2].color = true;
if (number >= 21) logic = false;
}
else
{
if (number < 20) cell[number].color = false;
number++;
}
}
else
{
if (number == 0)
{
if (cell[number].color)
{
cell[number].color = false;
}
else number++;
}
else
{
cell[number].color = false;
number++;
}
}
}
void Show_Rect(HDC hdc)
{
for (int i=0;i<20;i++)
{
if (cell[i].color)
FillRect(hdc,&cell[i].rect,hbrush[1]);
else
FillRect(hdc,&cell[i].rect,hbrush[0]);
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_TIMER:
if (launch)
{
Imitation();
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_CREATE:
hbrush[0] = CreateSolidBrush(RGB(255, 0, 0)); //красная кисть
hbrush[1] = CreateSolidBrush(RGB(0, 255, 0)); //зеленая кисть
hbrush[2] = CreateSolidBrush(RGB(255, 255, 0)); //желтая кисть
hbrush[3] = CreateSolidBrush(RGB(128, 128, 128)); //серая кисть
SetTimer(hWnd, 1, 1000, NULL); //создать таймер
for (int i=0,left=50,top=100;i<20;i++,left+=40)
{
cell[i].rect.left = left;
cell[i].rect.top = top;
cell[i].rect.right = left+30;
cell[i].rect.bottom = top+30;
cell[i].color = true;
condition[i] = false;
}
for (int i=0,x0=245,y0=150,n=5;i<3;i++,x0+=200,n+=5)
{
semaphore[i].First = cell + n;
if (n + 5 < 20) semaphore[i].Second = cell + n + 5;
else semaphore[i].Second = cell;
semaphore[i].first = semaphore[i].second = 5;
semaphore[i].line[0].X = x0;
semaphore[i].line[0].Y = y0;
semaphore[i].line[1].X = x0;
semaphore[i].line[1].Y = y0+30;
semaphore[i].line[2].X = x0;
semaphore[i].line[2].Y = y0+15;
semaphore[i].line[3].X = x0+20;
semaphore[i].line[3].Y = y0+15;
semaphore[i].Yellow = false;
semaphore[i].Green = false;
semaphore[i].Red = true;
for (int j=0,left=x0+20,top=y0;j<3;j++,left+=30)
{
semaphore[i].rect[j].left = left;
semaphore[i].rect[j].top = top;
semaphore[i].rect[j].right = left+30;
semaphore[i].rect[j].bottom = top+30;
}
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Разобрать выбор в меню:
switch (wmId)
{
case ID_32771:
launch = true;
break;
case ID_32772:
launch = false;
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Show_Rect(hdc);
for (int i=0;i<3;i++) semaphore[i].Show_Semaphore(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
for (int i=0;i<4;i++) DeleteObject(hbrush[i]);
KillTimer(hWnd, 1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Последний раз редактировалось RAFA91; 21.06.2016 в 12:20.
|