C语言实时钟表

代码如下
一、最终效果展示
二、绘制静态秒针
三、绘制动态秒针
四、根据实际时间转动秒针
五、添加时针分针
六、添加表盘 刻度

#include <graphics.h>
#include <conio.h>
#include <math.h>

#define high 480
#define width 640
#define PI 3.14159

int main()
{
	initgraph(width,high);
	int center_x,center_y;//中心坐标,也是秒针的起始坐标
	center_x=width/2;
	center_y=high/2;

	int secondEnd_x,secondEnd_y; //秒针的终点
	int minuteEnd_x,minuteEnd_y; // 分针的终点
	int hourEnd_x,hourEnd_y;     // 时针的终点

	float secondAngle;       // 秒钟对应的角度
	float minuteAngle;       // 分钟对应的角度
	float hourAngle;         // 时钟对应的角度

	
	int secondLength=width/5;             //秒针的长度
	int minuteLength = width/6;           // 分针的长度
	int hourLength = width/7;             // 时针的长度
	

	SYSTEMTIME ti;//定义变量存储系统时间

	BeginBatchDraw();
	
	while (1)
	{
		// 绘制一个简单的表盘
		setlinestyle(PS_SOLID, 1);
		setcolor(WHITE);
		circle(center_x, center_y, width/4);

		// 画刻度
		int x, y,i;
		for (i=0; i<60; i++)
		{
			x = center_x + int(width/4.3 * sin(PI * 2 * i / 60));
			y = center_y + int(width/4.3 * cos(PI * 2 * i / 60));
			
			if (i % 15 == 0)
				bar(x - 5, y - 5, x + 5, y + 5);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);
		}
		outtextxy(center_x - 25, center_y + width/6, "我的时钟");//打印汉字

		GetLocalTime(&ti);
		
		// 秒钟角度变化
		secondAngle = ti.wSecond * 2*PI/60;  // 一圈一共2*PI,一圈60秒,一秒钟秒钟走过的角度为2*PI/60
		// 分钟角度变化
		minuteAngle = ti.wMinute * 2*PI/60 + secondAngle/60;  // 一圈一共2*PI,一圈60分,一分钟分钟走过的角度为2*PI/60
		// 时钟角度变化
		hourAngle = ti.wHour * 2*PI/12 + minuteAngle/12;  // 一圈一共2*PI,一圈12小时,一小时时钟走过的角度为2*PI/12

		// 由角度决定的秒针端点坐标
		secondEnd_x = center_x + secondLength*sin(secondAngle);
		secondEnd_y = center_y - secondLength*cos(secondAngle);
		
		// 由角度决定的分针端点坐标
		minuteEnd_x = center_x + minuteLength*sin(minuteAngle);
		minuteEnd_y = center_y - minuteLength*cos(minuteAngle);
		
		// 由角度决定的时针端点坐标
		hourEnd_x = center_x + hourLength*sin(hourAngle);
		hourEnd_y = center_y - hourLength*cos(hourAngle);

		
		setlinestyle(PS_SOLID,2);//画秒针
		setcolor(RED);
		line(center_x,center_y,secondEnd_x,secondEnd_y);

		setlinestyle(PS_SOLID, 5);  
		setcolor(YELLOW);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y); // 画分针
		
		setlinestyle(PS_SOLID, 10);  
		setcolor(GREEN);
		line(center_x, center_y, hourEnd_x, hourEnd_y); // 画时针
	
		FlushBatchDraw();

		Sleep(50);
		setcolor(BLACK);
		line(center_x,center_y,secondEnd_x,secondEnd_y); // 隐藏前一帧的秒针
		
		setlinestyle(PS_SOLID, 5);  
		line(center_x, center_y, minuteEnd_x, minuteEnd_y); // 隐藏前一帧的分针
		setlinestyle(PS_SOLID, 10);  
		line(center_x, center_y, hourEnd_x, hourEnd_y); // 隐藏前一帧的时针
		
	}



	
	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}
  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值