[C] c语言绘制简单的时钟 →→→→→进入此内容的聊天室

来自 Unique Bushbaby, 2024-03-07, 写在 C, 查看 12 次.
URL http://www.code666.cn/view/3ea83b2f
  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define high 480
  7. #define width 640
  8. #define pi 3.141592
  9.  
  10. int main()
  11. {
  12.  
  13.     initgraph(high, width);
  14.  
  15.     int center_x, center_y;
  16.     center_y = width / 2;  // 320
  17.     center_x = high / 2; // 240
  18.      
  19.     SYSTEMTIME ti; //获取系统的时间
  20.  
  21.     // 秒针属性
  22.     int secondEnd_x, secondEnd_y;
  23.     int secondLenth = 120;
  24.     secondEnd_x = center_x + secondLenth;
  25.     secondEnd_y = center_y;
  26.     float secondAngle = 0;
  27.      
  28.     // 分钟属性
  29.     int minuteEnd_x, minuteEnd_y;
  30.     float minuteAngle = 0;
  31.     int minuteLenth = 90;
  32.     minuteEnd_x = center_x + minuteLenth;
  33.     minuteEnd_y = center_y + minuteLenth;
  34.  
  35.     //  时钟属性
  36.     int hoursEnd_x, hoursEnd_y;
  37.     float hoursAngle = 0;
  38.     int hoursLenth = 60;
  39.     hoursEnd_x = center_x + hoursLenth;
  40.     hoursEnd_y = center_y + hoursLenth;
  41.  
  42.     BeginBatchDraw();
  43.  
  44.     while (1)
  45.     {
  46.         // 获取时间
  47.         GetLocalTime(&ti);
  48.  
  49.         //绘制中心坐标
  50.         //setlinecolor(WHITE);
  51.         //fillcircle(center_x, center_y, 2);
  52.  
  53.         // 绘制一个表盘
  54.         setbkcolor(BLACK);
  55.         setlinestyle(PS_SOLID, 1);
  56.         setlinecolor(WHITE);
  57.         circle(center_x, center_y, 130);
  58.         //outtextxy(center_x - 25, center_y + width / 6, "我的时钟");
  59.         // 输出字符串 (VC6)
  60.         TCHAR s[] = _T("我的时钟");
  61.         outtextxy(210, 400, s);
  62.  
  63.         // 绘制表盘刻度
  64.         int  x, y, i;
  65.         for (i = 0; i < 60; i++)
  66.         {
  67.             x = center_x + 125 * sin(i * 2 * pi / 60);
  68.             y = center_y - 125 * cos(i * 2 * pi / 60);
  69.  
  70.             // 一刻钟
  71.             if (i % 15 == 0)
  72.             {
  73.                 bar(x - 5, y - 5, x + 5, y + 5);
  74.             }
  75.             else if ( i % 5 == 0) //5分钟
  76.             {
  77.                 circle(x, y, 3);
  78.             }
  79.             else
  80.             {
  81.                 putpixel(x, y, WHITE); // 小白点
  82.             }
  83.         }
  84.  
  85.          
  86.         //转动秒针
  87.         secondAngle = ti.wSecond * 2 * pi / 60;    //    2 * pi / 60 =一秒钟走的角度   ti.wSecond  =系统当前秒
  88.         secondEnd_x = center_x + secondLenth * sin(secondAngle);
  89.         secondEnd_y = center_y - secondLenth * cos(secondAngle);
  90.  
  91.         //转动分钟
  92.         minuteAngle = ti.wMinute * 2 * pi / 60  + secondAngle / 60;
  93.         minuteEnd_x = center_x + minuteLenth * sin(minuteAngle);
  94.         minuteEnd_y = center_y - minuteLenth * cos(minuteAngle);
  95.  
  96.         //转动时钟
  97.         hoursAngle = ti.wHour * 2 * pi / 12 + minuteAngle / 60;
  98.         hoursEnd_x = center_x + hoursLenth * sin(hoursAngle);
  99.         hoursEnd_y = center_y + hoursLenth * cos(hoursAngle);
  100.  
  101.         // 绘制秒针
  102.         setlinecolor(YELLOW);
  103.         setlinestyle(PS_SOLID, 1);
  104.         line(center_x, center_y, secondEnd_x, secondEnd_y);
  105.  
  106.         // 绘制分钟
  107.         setlinecolor(BLUE);
  108.         setlinestyle(PS_SOLID, 3);
  109.         line(center_x, center_y, minuteEnd_x, minuteEnd_y);
  110.  
  111.         // 绘制时钟
  112.         setlinecolor(RED);
  113.         setlinestyle(PS_SOLID, 5);
  114.         line(center_x, center_y, hoursEnd_x, hoursEnd_y);
  115.  
  116.         FlushBatchDraw();
  117.         Sleep(50);
  118.  
  119.         //隐藏 秒针
  120.         setlinecolor(BLACK);
  121.         line(center_x, center_y, secondEnd_x, secondEnd_y);
  122.  
  123.         //隐藏 分针
  124.         setlinecolor(BLACK);
  125.         line(center_x, center_y, minuteEnd_x, minuteEnd_y);
  126.  
  127.         //隐藏 时针
  128.         setlinecolor(BLACK);
  129.         line(center_x, center_y, hoursEnd_x, hoursEnd_y);
  130.     }
  131.  
  132.     EndBatchDraw();
  133.     _getch();
  134.     closegraph();
  135.  
  136.     return 0;
  137. }

回复 "c语言绘制简单的时钟"

这儿你可以回复上面这条便签

captcha