概述
用EasyX制作的一个简易钟表,需设置字符集属性为多字节字符集。效果如下所示:
GIF图会有些闪动,在实际中这种闪动几乎不可见。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#define width 900 //屏幕的宽
#define high 600 //屏幕的高
#define P 3.1415926 //圆周率
typedef struct CLOCKDATA
{
int center_x , center_y ; //表盘中心,即针的其起点
int secondLong ; //秒针的长度
int secondEnd_x,secondEnd_y; //秒针终点的位置
float secondAngle; //秒针单位转动角度
int minuteLong; //分针的长度
int minuteEnd_x, minuteEnd_y; //分针终点的位置
float minuteAngle; //分针单位转动角度
int hourLong; //分针的长度
int hourEnd_x, hourEnd_y; //时针终点的位置
float hourAngle; //时针单位转动角度
int R_dial; //表盘半径
int scale; //刻度到表盘中心的距离
SYSTEMTIME time; //系统时间
}Clock;
void showDial(Clock *clock); //打印表盘
void Init(Clock *clock); //初始化
void UpdatePtrPos(Clock *clock); //更新指针的位置
void showHour(Clock *clock); //显示时针
void showMinute(Clock *clock); //显示分针
void showSecond(Clock *clock); //显示秒针
void showTime(Clock *clock); //显示时间
void showDial(Clock *clock)
{
setlinecolor(WHITE); //设置划线颜色
circle(clock->center_x, clock->center_y, clock->R_dial);//画表盘的圆
//显示0、3、6、9刻度,用黄边红圆表示
setfillcolor(RED);
setcolor(YELLOW);
int cir1_x = clock->center_x;
int cir1_y = clock->center_y - clock->secondLong - 25;
fillcircle(cir1_x, cir1_y, 5); //画填充圆
setfillcolor(RED);
setcolor(YELLOW);
int cir2_x = clock->center_x + clock->secondLong + 25;
int cir2_y = clock->center_y;
fillcircle(cir2_x, cir2_y, 5);
setfillcolor(RED);
setcolor(YELLOW);
int cir3_x = clock->center_x;
int cir3_y = clock->center_y + clock->secondLong + 25;
fillcircle(cir3_x, cir3_y, 5);
setfillcolor(RED);
setcolor(YELLOW);
int cir4_x = clock->center_x- clock->secondLong - 25;
int cir4_y = clock->center_y ;
fillcircle(cir4_x, cir4_y, 5);
}
void Init(Clock *clock)
{
initgraph(width, high); //生成显示钟表的窗口
clock->center_x = width / 2; //表盘中心坐标
clock->center_y = high / 2;
clock->secondLong = width / 5; //秒针长度
clock->minuteLong = width / 6; //分针长度
clock->hourLong = width / 7; //时针长度
clock->R_dial = width / 4; //表盘半径
clock->scale = width / 4 + 30;
showDial(clock);
}
void UpdatePtrPos(Clock *clock)
{
GetLocalTime(&(clock->time)); //获取时间
//秒针转动
clock->secondAngle = (float)(clock->time.wSecond * 2 * P / 60); //根据时间秒数计算出秒针角度
clock->secondEnd_x = (int)(clock->center_x + clock->secondLong*sin(clock->secondAngle));//计算秒针终点坐标
clock->secondEnd_y = (int)(clock->center_y - clock->secondLong*cos(clock->secondAngle));
//分针转动
clock->minuteAngle = (float)(clock->time.wMinute * 2 * P / 60);//根据时间分钟数计算出秒针角度
clock->minuteEnd_x = (int)(clock->center_x + clock->minuteLong*sin(clock->minuteAngle));
clock->minuteEnd_y = (int)(clock->center_y - clock->minuteLong*cos(clock->minuteAngle));
//时针转动
clock->hourAngle = (float)(clock->time.wHour * 2 * P / 12);//根据时间小时数计算出秒针角度
clock->hourEnd_x = (int)(clock->center_x + clock->hourLong*sin(clock->hourAngle));
clock->hourEnd_y = (int)(clock->center_y - clock->hourLong*cos(clock->hourAngle));
}
void showHour(Clock *clock) //用红色显示时针
{
setlinestyle(PS_SOLID,2);
setcolor(RED);
line(clock->center_x, clock->center_y, clock->hourEnd_x, clock->hourEnd_y);
}
void showMinute(Clock *clock)//用蓝色显示时针
{
setlinestyle(PS_SOLID, 2);
setcolor(BLUE);
line(clock->center_x, clock->center_y, clock->minuteEnd_x, clock->minuteEnd_y);
}
void showSecond(Clock *clock)//用白色显示时针
{
setlinestyle(PS_SOLID, 2);
setcolor(WHITE);
line(clock->center_x, clock->center_y, clock->secondEnd_x, clock->secondEnd_y);
FlushBatchDraw();
Sleep(10);
setcolor(BLACK);
line(clock->center_x, clock->center_y, clock->secondEnd_x, clock->secondEnd_y);
if (clock->time.wSecond == 0)//每到一分钟就清空一次表盘
{
clearcircle(clock->center_x, clock->center_y, clock->secondLong);
}
}
void showTime(Clock *clock)
{
char year[10] = " ", month[10] = " ",day[10] = " ", week[10] = " ";
char hour[10] = " ", minute[10] = " ", second[10] = " ";
sprintf(year, "%d /", clock->time.wYear);
sprintf(month, "%d /", clock->time.wMonth);
sprintf(day, "%d ", clock->time.wDay);
sprintf(week, "星期 %d", clock->time.wDayOfWeek);
sprintf(hour, "%d 时", clock->time.wHour);
if (clock->time.wMinute<10)
sprintf(minute, "0%d 分", clock->time.wMinute);
else
sprintf(minute, "%d 分", clock->time.wMinute);
if (clock->time.wSecond<10)
sprintf(second, "0%d 秒", clock->time.wSecond);
else
sprintf(second, "%d 秒", clock->time.wSecond);
settextstyle(15, 15, "楷体");
setcolor(WHITE);
outtextxy(clock->center_x - 125, clock->center_y + clock->R_dial + 15, hour);
outtextxy(clock->center_x -25, clock->center_y + clock->R_dial + 15, minute);
outtextxy(clock->center_x +75, clock->center_y + clock->R_dial + 15, second);
outtextxy(clock->center_x - 150, clock->center_y + clock->R_dial + 50,year);
outtextxy(clock->center_x - 50, clock->center_y + clock->R_dial + 50,month);
outtextxy(clock->center_x +50, clock->center_y + clock->R_dial + 50,day);
outtextxy(clock->center_x + 150, clock->center_y + clock->R_dial + 50, week);
if (clock->time.wMilliseconds == 0)//每到一秒就清空一次显示时间的区域
{
clearrectangle(clock->center_x - 160, clock->center_y + clock->R_dial + 5, clock->center_x + 250, clock->center_y + clock->R_dial + 70);
}
}
int main()
{
Clock clock;
Init(&clock);
while (1)
{
UpdatePtrPos(&clock); //更新钟表时分秒针位置
showSecond(&clock);
showMinute(&clock);
showHour(&clock);
showTime(&clock);
}
EndBatchDraw();
system("pause");
return 0;
}
最后
以上就是清脆乐曲为你收集整理的制作一个钟表的全部内容,希望文章能够帮你解决制作一个钟表所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复