C語言實(shí)現(xiàn)實(shí)時(shí)鐘表
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)實(shí)時(shí)鐘表的具體代碼,供大家參考,具體內(nèi)容如下
一、最終效果展示
效果圖如下:
二、繪制靜態(tài)秒針
代碼如下:
#include<graphics.h> #include<conio.h> #include<math.h> #define High 480 //游戲畫面尺寸 #define Width 640 int main(void) { ?? ?initgraph(Width,High);//初始化繪圖窗口 ?? ?int center_x,center_y;//中心點(diǎn)的坐標(biāo),也是鐘表的坐標(biāo) ?? ?center_x=Width/2; ?? ?center_y=High/2; ?? ?int secondLength;//秒針的長度 ?? ?secondLength=Width/5; ?? ?int secondEnd_x,secondEnd_y;//秒針的終點(diǎn) ?? ?secondEnd_x=center_x+secondLength; ?? ?secondEnd_y=center_y; ?? ?//畫秒針 ?? ?setlinestyle(PS_SOLID,2);//畫實(shí)線,寬度為兩個(gè)像素 ?? ?setcolor(WHITE); ?? ?line(center_x,center_y,secondEnd_x,secondEnd_y); ?? ?getch(); ?? ?closegraph();//關(guān)閉繪圖窗口 ?? ?return 0; }
效果圖如下:
三、繪制動(dòng)態(tài)秒針
代碼如下:
#include<graphics.h> #include<conio.h> #include<math.h> #define High 480 //游戲畫面尺寸 #define Width 640 #define PI 3.14159 int main(void) { ?? ?initgraph(Width,High);//初始化繪圖窗口 ?? ?int center_x,center_y;//中心點(diǎn)的坐標(biāo),也是鐘表的坐標(biāo) ?? ?center_x=Width/2; ?? ?center_y=High/2; ?? ?int secondLength;//秒針的長度 ?? ?secondLength=Width/5; ?? ?int secondEnd_x,secondEnd_y;//秒針的終點(diǎn) ?? ?float secondAngle=0;//秒針對應(yīng)的角度 ?? ?while(1) ?? ?{ ?? ??? ?//由角度決定的秒針終點(diǎn)坐標(biāo) ?? ??? ?secondEnd_x=center_x+secondLength*sin(secondAngle); ?? ??? ?secondEnd_y=center_y-secondLength*cos(secondAngle); ?? ??? ?//畫秒針 ?? ??? ?setlinestyle(PS_SOLID,2);//畫實(shí)線,寬度為兩個(gè)像素 ?? ??? ?setcolor(WHITE); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y); ?? ??? ?Sleep(1000);//1秒 ?? ??? ?setcolor(BLACK); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y);//隱藏前一幀的秒針 ?? ??? ?//秒針角度的變化 ?? ??? ?secondAngle=secondAngle+2*PI/60; ?? ??? ?//一圈一共2*PI,一圈60秒,一秒鐘秒針走過的角度為2*PI/60 ?? ?} ?? ?getch(); ?? ?closegraph();//關(guān)閉繪圖窗口 ?? ?return 0; }
效果圖如下:
四、根據(jù)實(shí)際時(shí)間轉(zhuǎn)動(dòng)秒針
代碼如下:
#include<graphics.h> #include<conio.h> #include<math.h> #define High 480 //游戲畫面尺寸 #define Width 640 #define PI 3.14159 int main(void) { ?? ?initgraph(Width,High);//初始化繪圖窗口 ?? ?int center_x,center_y;//中心點(diǎn)的坐標(biāo),也是鐘表的坐標(biāo) ?? ?center_x=Width/2; ?? ?center_y=High/2; ?? ?int secondLength;//秒針的長度 ?? ?secondLength=Width/5; ?? ?int secondEnd_x,secondEnd_y;//秒針的終點(diǎn) ?? ?float secondAngle=0;//秒針對應(yīng)的角度 ?? ?SYSTEMTIME ti;//定義保存當(dāng)前時(shí)間的變量 ?? ?while(1) ?? ?{ ?? ??? ?GetLocalTime(&ti);//獲取當(dāng)前時(shí)間 ?? ??? ?//秒針角度的變化 ?? ??? ?secondAngle=ti.wSecond*2*PI/60; ?? ??? ?//一圈一共2*PI,一圈60秒,一秒鐘秒針走過的角度為2*PI/60 ?? ??? ?//由角度決定的秒針終點(diǎn)坐標(biāo) ?? ??? ?secondEnd_x=center_x+secondLength*sin(secondAngle); ?? ??? ?secondEnd_y=center_y-secondLength*cos(secondAngle); ?? ??? ?//畫秒針 ?? ??? ?setlinestyle(PS_SOLID,2);//畫實(shí)線,寬度為兩個(gè)像素 ?? ??? ?setcolor(WHITE); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y); ?? ??? ?Sleep(1000);//1秒 ?? ??? ?setcolor(BLACK); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y);//隱藏前一幀的秒針 ?? ?} ?? ?getch(); ?? ?closegraph();//關(guān)閉繪圖窗口 ?? ?return 0; }
五、添加時(shí)針分針
代碼如下:
#include<graphics.h> #include<conio.h> #include<math.h> #define High 480 //游戲畫面尺寸 #define Width 640 #define PI 3.14159 int main(void) { ?? ?initgraph(Width,High);//初始化繪圖窗口 ?? ?int center_x,center_y;//中心點(diǎn)的坐標(biāo),也是鐘表的坐標(biāo) ?? ?center_x=Width/2; ?? ?center_y=High/2; ?? ?int secondLength=Width/7;//秒針的長度 ?? ?int minuteLength=Width/6;//分針的長度 ?? ?int hourLength=Width/5;//時(shí)針的長度 ?? ?int secondEnd_x,secondEnd_y;//秒針的終點(diǎn) ?? ?int minuteEnd_x,minuteEnd_y;//分針的終點(diǎn) ?? ?int hourEnd_x,hourEnd_y;//時(shí)針的終點(diǎn) ?? ?float secondAngle=0;//秒針對應(yīng)的角度 ?? ?float minuteAngle=0;//分針對應(yīng)的角度 ?? ?float hourAngle=0;//時(shí)針對應(yīng)的角度 ?? ?SYSTEMTIME ti;//定義保存當(dāng)前時(shí)間的變量 ?? ?while(1) ?? ?{ ?? ??? ?GetLocalTime(&ti);//獲取當(dāng)前時(shí)間 ?? ??? ? ?? ??? ?//秒針角度的變化 ?? ??? ?secondAngle=ti.wSecond*2*PI/60; ?? ??? ?//一圈一共2*PI,一圈60秒,一秒鐘秒針走過的角度為2*PI/60 ?? ??? ?//由角度決定的秒針終點(diǎn)坐標(biāo) ?? ??? ?//分針角度的變化 ?? ??? ?minuteAngle=ti.wMinute*2*PI/60; ?? ??? ?//一圈一共2*PI,一圈60分,一分鐘秒針走過的角度為2*PI/60 ?? ??? ? ?? ??? ?//時(shí)針角度的變化 ?? ??? ?hourAngle=ti.wHour*2*PI/12; ?? ??? ?//一圈一共2*PI,一圈12時(shí),一小時(shí)時(shí)針走過的角度為2*PI/12 ?? ??? ?//由角度決定的秒針終點(diǎn)坐標(biāo) ?? ??? ?secondEnd_x=center_x+secondLength*sin(secondAngle); ?? ??? ?secondEnd_y=center_y-secondLength*cos(secondAngle); ?? ??? ?//由角度決定的分針終點(diǎn)坐標(biāo) ?? ??? ?minuteEnd_x=center_x+minuteLength*sin(minuteAngle); ?? ??? ?minuteEnd_y=center_y-minuteLength*cos(minuteAngle); ?? ??? ?//由角度決定的時(shí)針終點(diǎn)坐標(biāo) ?? ??? ?hourEnd_x=center_x+hourLength*sin(hourAngle); ?? ??? ?hourEnd_y=center_y-hourLength*cos(hourAngle); ?? ??? ?//畫秒針 ?? ??? ?setlinestyle(PS_SOLID,2);//畫實(shí)線,寬度為兩個(gè)像素 ?? ??? ?setcolor(WHITE); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y); ?? ??? ?//畫分針 ?? ??? ?setlinestyle(PS_SOLID,4);//畫實(shí)線,寬度為4個(gè)像素 ?? ??? ?setcolor(BLUE); ?? ??? ?line(center_x,center_y,minuteEnd_x,minuteEnd_y); ?? ??? ?//畫時(shí)針 ?? ??? ?setlinestyle(PS_SOLID,6);//畫實(shí)線,寬度為6個(gè)像素 ?? ??? ?setcolor(RED); ?? ??? ?line(center_x,center_y,hourEnd_x,hourEnd_y); ?? ??? ?Sleep(1000);//1秒 ?? ??? ?setcolor(BLACK); ?? ??? ?setlinestyle(PS_SOLID,2); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y);//隱藏前一幀的秒針 ?? ??? ?setlinestyle(PS_SOLID,4); ?? ??? ?line(center_x,center_y,minuteEnd_x,minuteEnd_y);//隱藏前一幀的分針 ?? ??? ?setlinestyle(PS_SOLID,6); ?? ??? ?line(center_x,center_y,hourEnd_x,hourEnd_y);//隱藏前一幀的時(shí)針 ?? ?} ?? ?getch(); ?? ?closegraph();//關(guān)閉繪圖窗口 ?? ?return 0; }
效果圖如下:
六、添加表盤 刻度
代碼如下:
#include<graphics.h> #include<conio.h> #include<math.h> #define High 480 //游戲畫面尺寸 #define Width 640 #define PI 3.14159 int main(void) { ?? ?initgraph(Width,High);//初始化繪圖窗口 ?? ?int center_x,center_y;//中心點(diǎn)的坐標(biāo),也是鐘表的坐標(biāo) ?? ?center_x=Width/2; ?? ?center_y=High/2; ?? ?int secondLength=Width/7;//秒針的長度 ?? ?int minuteLength=Width/6;//分針的長度 ?? ?int hourLength=Width/5;//時(shí)針的長度 ?? ?int secondEnd_x,secondEnd_y;//秒針的終點(diǎn) ?? ?int minuteEnd_x,minuteEnd_y;//分針的終點(diǎn) ?? ?int hourEnd_x,hourEnd_y;//時(shí)針的終點(diǎn) ?? ?float secondAngle=0;//秒針對應(yīng)的角度 ?? ?float minuteAngle=0;//分針對應(yīng)的角度 ?? ?float hourAngle=0;//時(shí)針對應(yīng)的角度 ?? ?SYSTEMTIME ti;//定義保存當(dāng)前時(shí)間的變量 ?? ?BeginBatchDraw(); ?? ?while(1) ?? ?{ ?? ??? ?//繪制一個(gè)簡單的表盤 ?? ??? ?setlinestyle(PS_SOLID,1);//設(shè)置表盤邊的粗細(xì) ?? ??? ?setcolor(WHITE);//設(shè)置表盤邊的顏色 ?? ??? ?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(i%15==0) ?? ??? ??? ??? ?bar(x-5,y-5,x+5,y+5); ?? ??? ??? ?else if(i%5==0) ?? ??? ??? ??? ?circle(x,y,3); ?? ??? ??? ?else ?? ??? ??? ??? ?putpixel(x,y,WHITE); ?? ??? ?} ?? ??? ?outtextxy(center_x-25,center_y+Width/6,"我的時(shí)鐘"); ?? ??? ?outtextxy(center_x-120,High-60,"[時(shí)針: 紅色] ?[分針: 藍(lán)色] ?[秒針: 白色]"); ?? ??? ?GetLocalTime(&ti);//獲取當(dāng)前時(shí)間 ?? ??? ? ?? ??? ?//秒針角度的變化 ?? ??? ?secondAngle=ti.wSecond*2*PI/60; ?? ??? ?//一圈一共2*PI,一圈60秒,一秒鐘秒針走過的角度為2*PI/60 ?? ??? ?//由角度決定的秒針終點(diǎn)坐標(biāo) ?? ??? ?//分針角度的變化 ?? ??? ?minuteAngle=ti.wMinute*2*PI/60; ?? ??? ?//一圈一共2*PI,一圈60分,一分鐘秒針走過的角度為2*PI/60 ?? ??? ? ?? ??? ?//時(shí)針角度的變化 ?? ??? ?hourAngle=ti.wHour*2*PI/12; ?? ??? ?//一圈一共2*PI,一圈12時(shí),一小時(shí)時(shí)針走過的角度為2*PI/12 ?? ??? ?//由角度決定的秒針終點(diǎn)坐標(biāo) ?? ??? ?secondEnd_x=center_x+secondLength*sin(secondAngle); ?? ??? ?secondEnd_y=center_y-secondLength*cos(secondAngle); ?? ??? ?//由角度決定的分針終點(diǎn)坐標(biāo) ?? ??? ?minuteEnd_x=center_x+minuteLength*sin(minuteAngle); ?? ??? ?minuteEnd_y=center_y-minuteLength*cos(minuteAngle); ?? ??? ?//由角度決定的時(shí)針終點(diǎn)坐標(biāo) ?? ??? ?hourEnd_x=center_x+hourLength*sin(hourAngle); ?? ??? ?hourEnd_y=center_y-hourLength*cos(hourAngle); ?? ??? ?//畫秒針 ?? ??? ?setlinestyle(PS_SOLID,2);//畫實(shí)線,寬度為兩個(gè)像素 ?? ??? ?setcolor(WHITE); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y); ?? ??? ?//畫分針 ?? ??? ?setlinestyle(PS_SOLID,4);//畫實(shí)線,寬度為4個(gè)像素 ?? ??? ?setcolor(BLUE); ?? ??? ?line(center_x,center_y,minuteEnd_x,minuteEnd_y); ?? ??? ?//畫時(shí)針 ?? ??? ?setlinestyle(PS_SOLID,6);//畫實(shí)線,寬度為6個(gè)像素 ?? ??? ?setcolor(RED); ?? ??? ?line(center_x,center_y,hourEnd_x,hourEnd_y); ?? ??? ?FlushBatchDraw(); ?? ??? ?Sleep(1000);//1秒 ?? ??? ?setcolor(BLACK); ?? ??? ?setlinestyle(PS_SOLID,2); ?? ??? ?line(center_x,center_y,secondEnd_x,secondEnd_y);//隱藏前一幀的秒針 ?? ??? ?setlinestyle(PS_SOLID,4); ?? ??? ?line(center_x,center_y,minuteEnd_x,minuteEnd_y);//隱藏前一幀的分針 ?? ??? ?setlinestyle(PS_SOLID,6); ?? ??? ?line(center_x,center_y,hourEnd_x,hourEnd_y);//隱藏前一幀的時(shí)針 ?? ?} ?? ?EndBatchDraw(); ?? ?getch(); ?? ?closegraph();//關(guān)閉繪圖窗口 ?? ?return 0; }
效果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)數(shù)字轉(zhuǎn)換為十六進(jìn)制字符串的方法
這篇文章主要介紹了C++實(shí)現(xiàn)數(shù)字轉(zhuǎn)換為十六進(jìn)制字符串的方法,涉及C++操作數(shù)字與字符串轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06c++調(diào)用python實(shí)現(xiàn)圖片ocr識(shí)別
所謂c++調(diào)用python,實(shí)際上就是在c++中把整個(gè)python當(dāng)作一個(gè)第三方庫引入,然后使用特定的接口來調(diào)用python的函數(shù)或者直接執(zhí)行python腳本,本文介紹的是調(diào)用python實(shí)現(xiàn)圖片ocr識(shí)別,感興趣的可以了解下2023-09-09C++實(shí)現(xiàn)string存取二進(jìn)制數(shù)據(jù)的方法
這篇文章主要介紹了C++實(shí)現(xiàn)string存取二進(jìn)制數(shù)據(jù)的方法,針對STL中string的用法進(jìn)行了較為詳細(xì)的分析,需要的朋友可以參考下2014-10-10C++實(shí)現(xiàn)LeetCode(189.旋轉(zhuǎn)數(shù)組)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(189.旋轉(zhuǎn)數(shù)組),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07