实时时钟

此作业的要求:https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11577
一、功能和截图
考虑到之后的游戏制作对于时间方面可能会有要求,为了掌握时刻方面的知识,决定去做实时时钟游戏。
GUI版本:

二、代码

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

#pragma comment(lib,"Winmm.lib")
#define  High 505
#define  Width 649
#define PI 3.14159265358979323846
IMAGE clock_im;
SYSTEMTIME time;    //定义存储系统的时间的变量

int main() {
    
    int center_x, center_y; //指针的中心坐标
    center_x = Width / 2;
    center_y = High / 2;
    double second_x, second_y;  //秒针的结束坐标
    double second_len = Width -480;  //秒针的长度
    double min_x, min_y;
    double min_len = Width - 520;
    double hour_x, hour_y;
    int hour_len = Width - 550;
    double secondAngler = 0;
    double mindAngler = 0;
    double hourAngler = 0;
    initgraph(Width,High);   //画布
    loadimage(&clock_im, L"D:\c\time3.png");
    //BeginBatchDraw();
    while (1) {
        putimage(0, 0, &clock_im);
        GetLocalTime(&time); //得到当前系统的时间
        secondAngler = time.wSecond * 2 * PI / 60;
        mindAngler = time.wMinute * 2 * PI / 60;
        hourAngler = time.wHour * 2 * PI / 12;

        second_x = center_x +second_len * sin(secondAngler);
        second_y = center_y - second_len * cos(secondAngler);

        min_x = center_x + min_len * sin(mindAngler);
        min_y= center_y - min_len * cos(mindAngler);
        double tmp = cos(hourAngler);
        hour_x = center_x + hour_len * sin(hourAngler);
        hour_y = center_y - hour_len * cos(hourAngler);
        //对指针进行绘制
        setlinestyle(PS_SOLID, 2); //设置当前画线宽度和类型。
        setcolor(RED);            //设置指针的颜色    
        line(center_x, center_y, second_x, second_y);

        setlinestyle(PS_SOLID, 4); 
        setcolor(GREEN);            
        line(center_x, center_y, min_x, min_y);

        setlinestyle(PS_SOLID, 6); 
        setcolor(YELLOW);            
        line(center_x, center_y, hour_x, hour_y);

        FlushBatchDraw();
        setcolor(WHITE);            //设置指针的颜色    
        line(center_x, center_y, second_x, second_y);

        setcolor(WHITE);                
        line(center_x, center_y, min_x, min_y);

        setcolor(WHITE);                
        line(center_x, center_y, hour_x, hour_y);
        
        
    }
    EndBatchDraw();
    _getch();
    closegraph();//关闭画布
    return 0;
}

 三、实现

1.对实时时钟的实现可以先去考虑秒针转动,中心是固定不变的,随着时间的变化来改变秒针的结束位置。

先定义用来存储时间的变量。

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

需要使用头文件:

#include <windows.h>

定义:常用的数据 年、月、日、时、分、秒、星期,这些值可以直接存储在 int中。

typedef struct _SYSTEMTIME {
    WORD wYear;//
    WORD wMonth;//
    WORD wDayOfWeek;//星期
    WORD wDay;//
    WORD wHour;//
    WORD wMinute;//
    WORD wSecond;//
    WORD wMilliseconds;//毫秒
  } SYSTEMTIME;

秒针的转动是围绕圆心,每秒钟去转动一个角度。一分钟是60秒秒针旋转一周。所以我们可以得到秒针的旋转角度。

GetLocalTime(&time); //得到当前系统的时间
secondAngler = time.wSecond * 2 * PI / 60;

秒针的结束位置可以根据角度来得到。

second_x = center_x +second_len * sin(secondAngler);
second_y = center_y - second_len * cos(secondAngler);

分针与时针与秒针实现方法类似,在这里就不在叙述。

四、PSP

任务开始时间结束时间中断时间(分钟)delta时间(分钟)
任务逻辑整理 12.8 12:07 12.8 12:14 0 7
秒针实现 12.8 12:14 12.8 12:52 0 37
寻找合适的空白钟表 12.8 13:43 12.8 14:02 0 19
实时时钟实现 12.8 14:02 12.8 14:57 0 55
调试 12.8 20:42 12.8 21:03 0 21
原文地址:https://www.cnblogs.com/shizhiteng/p/14105848.html