给大家分享一个,利用c++做一个模拟时钟,比较有趣,适合像我这样的新手练习使用。
image.png
#include <iostream>
  • #include "graphics.h"
  • #include <math.h>
  • #include <time.h>
  • #include <windows.h>
  • #define x0 210.0
  • #define y0 210.0
  • #define r 200.0
  • POINT pt[60],pt_s[60],pt_m[60],pt_h[60],pt1[60];
  • using namespace std;
  • int main()
  • {
  •     initgraph(800,800);                                 //设置界面大小
  •     setfillcolor(0xffccff);
  •     setcolor(0xffccff);
  •     fillellipse(x0,y0,10,10);
  •     int s=45,m,h,n=0;
  •     while(n<60)
  •     {
  •         pt_s[s].x=x0+(int)((r-20)*cos((n-90)*3.1415926/30.0));
  •         pt_s[s].y=y0+(int)((r-20)*sin((n-90)*3.1415926/30.0));
  •          
  •         pt_m[s].x=x0+(int)((r-50)*cos((n-90)*3.1415926/30.0));
  •         pt_m[s].y=y0+(int)((r-50)*sin((n-90)*3.1415926/30.0));
  •          
  •         pt_h[s].x=x0+(int)((r-80)*cos((n-90)*3.1415926/30.0));
  •         pt_h[s].y=y0+(int)((r-80)*sin((n-90)*3.1415926/30.0));
  •          
  •         pt[s].x=x0+(int)(r*cos((n-90)*3.1415926/30.0));
  •         pt[s].y=y0+(int)(r*sin((n-90)*3.1415926/30.0));
  •         fillellipse(pt[s].x,pt[s].y,2,2);
  •         n++;
  •         s++;
  •         if(s>=60)
  •         {
  •             s=0;
  •         }
  •     }
  •     for(int i=0;i<12;i++)
  •     {
  •         fillellipse(pt[i*5].x,pt[i*5].y,5,5);
  •     }
  •     int xs,ys,xm=-1,ym=-1,xh=-1,yh=-1;
  •     while(1)
  •     {
  •         int n,m;
  •         SYSTEMTIME st={0};
  •         GetLocalTime(&st);
  •         setcolor(0xffccff);
  •         line(x0,y0,pt_s[st.wSecond].x,pt_s[st.wSecond].y);
  •         xs=pt_s[st.wSecond].x;
  •         ys=pt_s[st.wSecond].y;
  •         if(pt_m[st.wMinute].x!=xm && pt_m[st.wMinute].y!=ym)
  •         {
  •             setcolor(0);
  •             line(x0,y0,xm,ym);
  •             xm=pt_m[st.wMinute].x;
  •             ym=pt_m[st.wMinute].y;
  •             setcolor(0xffccff);
  •         }
  •         line(x0,y0,pt_m[st.wMinute].x,pt_m[st.wMinute].y);
  •         if(st.wHour>12)
  •         {
  •             n=st.wHour-12;
  •         }
  •         else
  •         {
  •             n=st.wHour;
  •         }
  •         m=(n*60+st.wMinute)/12;
  •         line(x0,y0,pt_h[m].x,pt_h[m].y);
  •         Sleep(1000);
  •         setcolor(0x0);
  •         line(x0,y0,xs,ys);
  •         fillellipse(x0,y0,10,10);
  •     }
  •     closegraph();
  •     return 0;
  • }
  • 复制代码