#include <graphics.h> //调用 EasyX 图形库
#include <conio.h>
#include <math.h>
#define width 480 //定义画布大小
#define high 640
#define pi 3.1415926
int main()
{
initgraph(width, high); //绘制画布
setbkcolor(WHITE); //定义背景色为白色
cleardevice(); //清空设备背景色
int center_x, center_y; //定义中心点坐标
center_x = width / 2;
center_y = high / 2;
//时钟各针长度设置
int secondlength; //定义秒针长度
secondlength = width / 4;
int minutelength; //定义分针长度
minutelength = width / 4 - 20;
int hourlength; //定义时针长度
hourlength = width / 6;
float secondangle = 0; //秒针转动角度
float minuteangle = 0; //分针转动角度
float hourangle = 0; //时针转动角度
SYSTEMTIME ti; //定义变量存储系统时间
int secondend_x, secondend_y; //定义秒针尾部坐标
int minute_x, minute_y; //定义分针尾部坐标
int hour_x, hour_y; //定义时针尾巴坐标
float secondcacle_x, secondcacle_y; //定义秒刻度坐标
float number_x, number_y; //定义数字刻度坐标
BeginBatchDraw();
while (1)
{
GetLocalTime(&ti);
//时钟转动
secondangle = ti.wSecond * 2 * pi / 60; //获取系统时间秒数 *2pi/60 求出转动角度
minuteangle = ti.wMinute * 2 * pi / 60; //获取分钟数 求转动角度
hourangle = ti.wHour * 2 * pi / 12; //获取小时数,求转动角度
//秒针转动后的秒针坐标
secondend_x = center_x + secondlength * sin(secondangle);
secondend_y = center_y - secondlength * cos(secondangle);
//分针转动后的分针坐标
minute_x = center_x + minutelength * sin(minuteangle);
minute_y = center_y - minutelength * cos(minuteangle);
//时针转动后坐标
hour_x = center_x + hourlength * sin(hourangle);
hour_y = center_y - hourlength * cos(hourangle);
//画表盘
//外表盘
setlinestyle(PS_SOLID, 4);
setcolor(RGB(147, 112, 219));
circle(center_x, center_y, 150);
//画秒刻度点
int a = -1;
for (float i = 0; i < 2*pi; i += 2*pi/60)
{
//秒刻度位置及画点
secondcacle_x = center_x + 140 * sin(i);
secondcacle_y = center_y - 140 * cos(i);
putpixel(secondcacle_x, secondcacle_y, BLACK);
//刻度标点
a++;
if (a % 5 == 0 )
{
//标点
setcolor(BLACK);
fillcircle(secondcacle_x, secondcacle_y, 2);
}
}
//画数字刻度
setcolor(RGB(255, 99, 71));
outtextxy(center_x - 8,center_y - 135,"12");
outtextxy(center_x - 4, center_y + 120, "6");
outtextxy(center_x + 125, center_y - 8, "3");
outtextxy(center_x - 130, center_y - 8, "9");
//画秒针
setlinestyle(PS_SOLID, 2); //画实线 像素为1
setcolor(BLUE); //设置颜色蓝色
line(center_x, center_y, secondend_x, secondend_y);
//画分针
setlinestyle(PS_SOLID, 3); //画实线 像素为3
setcolor(RED); //颜色设置为红色
line(center_x, center_y, minute_x, minute_y);
//画时针
setlinestyle(PS_SOLID, 5); //画实线 像素为5
setcolor(BLACK); //颜色设置为黑色
line(center_x, center_y, hour_x, hour_y);
FlushBatchDraw();
//隐藏上一帧画面
setcolor(WHITE);
line(center_x, center_y, secondend_x, secondend_y);
line(center_x, center_y, minute_x, minute_y);
line(center_x, center_y, hour_x, hour_y);
setcolor(BLACK);
outtextxy(center_x - 20 , center_y + 40, "Time"); //输出字符串
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}
基于C语言和EasyX的时钟编程
于 2021-06-23 18:34:35 首次发布