时钟

本文介绍了一个使用 C 语言实现的动态时钟程序。该程序利用了 graphics.h 和其他标准库来绘制一个不断更新显示当前时间的时钟。时钟包括秒针、分针和时针,并且每秒刷新一次来准确地反映当前时间。
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>

#define WIDTH 480
#define HIGH 640
#define PI 3.1415926

int main()
{
    initgraph(WIDTH, HIGH);
    int center_x = WIDTH / 2;
    int center_y = HIGH / 2;
    int second_length = WIDTH / 5;
    int minute_length = WIDTH / 7;
    int hour_length = WIDTH / 9;

    int second_end_x, second_end_y;
    int minute_end_x, minute_end_y;
    int hour_end_x, hour_end_y;
    double second_angle, minute_angle, hour_angle;

    SYSTEMTIME time;
    setbkcolor(WHITE); //设定背景颜色
    cleardevice();
    BeginBatchDraw();//一次性输出
    while (1)
    {
        setlinestyle(PS_SOLID, 1);
        setcolor(BLACK);
        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 (x % 15 == 0)
                bar(x - 5, y - 5, x + 5, y + 5);
             if (i % 5 == 0)
                circle(x, y, 3);
            else putpixel(x, y, BLUE);
        }

        GetLocalTime(&time);
        second_angle = time.wSecond * 2 * PI / 60;
        minute_angle = time.wMinute * 2 * PI / 60 + second_angle/60;
        hour_angle = time.wHour * 2 * PI / 12 + minute_angle/12;

        minute_end_x = center_x + minute_length*sin(minute_angle);
        minute_end_y = center_y - minute_length*cos(minute_angle);

        second_end_x = center_x + second_length*sin(second_angle);
        second_end_y = center_y - second_length*cos(second_angle);

        hour_end_x = center_x + hour_length*sin(hour_angle);
        hour_end_y = center_y - hour_length*cos(hour_angle);

        setlinestyle(PS_SOLID, 2);
        setcolor(YELLOW);
        line(center_x, center_y, second_end_x, second_end_y);

        setlinestyle(PS_SOLID, 4);
        setcolor(BLUE);
        line(center_x, center_y, minute_end_x, minute_end_y);

        setlinestyle(PS_SOLID, 6);
        setcolor(RED);
        line(center_x, center_y, hour_end_x, hour_end_y);

        FlushBatchDraw();
        Sleep(10); //sleep函数不控制转动速度,其受到SYSTEMTIME控制,sleep只是对指针出现的速度有微调
        //隐藏上一秒的指针
        setcolor(WHITE);
        setlinestyle(PS_SOLID, 2);
        line(center_x, center_y, second_end_x, second_end_y);

        setlinestyle(PS_SOLID, 5);
        line(center_x, center_y, minute_end_x, minute_end_y);

        setlinestyle(PS_SOLID, 10);
        line(center_x, center_y, hour_end_x, hour_end_y);
    }

    EndBatchDraw();
    _getch();
    closegraph();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值