编译环境:Mictosoft Visual Studio 2022, EasyX_2021
作 者:代码骑士<1696297834@qq.com>
最后修改:2021-12-29
程序演示:
程序代码:
各位看官,喜欢本程序点个赞吧,创作不易,拒绝白嫖!
#include<graphics.h>
#include<conio.h>
#include<iostream>
#include<time.h>
using namespace std;
/// <summary>
/// 作品:红绿灯
/// 作者:代码骑士<1696297834@qq.com>
/// 编译环境:VS 2022,EasyX 2021
/// </summary>
/// <returns></returns>
int main()
{const int width = 640;const int height = 480;initgraph(width, height);//640是画布宽度,480是画布高度int x = 300, y = 100, r = 50, s = 20;setbkcolor(WHITE);cleardevice();setfillcolor(BLACK);fillrectangle(220, 30, 380, 420);fillrectangle(280, 420, 320, 490);clock_t start, now;while (1){start = clock();float maxTime = 10;float remainTime = maxTime;while (remainTime >= 0){now = clock();double duration = (double(now - start) / CLOCKS_PER_SEC);remainTime = maxTime - duration;if (remainTime <= 10 && remainTime >= 6){//灯1setfillcolor(RED);fillcircle(x, y, r);//灯2setfillcolor(GREEN);fillcircle(x, y + s + 2 * r, r);//灯3setfillcolor(YELLOW);fillcircle(x, y + 2 * s + 4 * r, r);}else if (remainTime < 6 && remainTime >= 3){setfillcolor(YELLOW);fillcircle(x, y, r);setfillcolor(RED);fillcircle(x, y + s + 2 * r, r);setfillcolor(GREEN);fillcircle(x, y + 2 * s + 4 * r, r);}else if (remainTime < 3 && remainTime >= 1){setfillcolor(GREEN);fillcircle(x, y, r);setfillcolor(YELLOW);fillcircle(x, y + s + 2 * r, r);setfillcolor(RED);fillcircle(x, y + 2 * s + 4 * r, r);}}}_getch();//暂停,等待键盘按键closegraph();//关闭当前画布return 0;
}