C語言基于EasyX庫實(shí)現(xiàn)有圖形界面鐘表
本文實(shí)例為大家分享了C語言基于EasyX庫實(shí)現(xiàn)有圖形界面鐘表的具體代碼,供大家參考,具體內(nèi)容如下
1.目標(biāo)要求:
實(shí)現(xiàn)一個(gè)顯示圖像的時(shí)鐘
2.C語言代碼:
#include<graphics.h> //需要提前下載EasyX庫哦 #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #include<math.h> #define High 480 #define Width 640//畫布尺寸 #define PI 3.1415 /* 實(shí)現(xiàn)秒針的轉(zhuǎn)動(dòng)。定義secondAngle為秒針對應(yīng)的角度,利用三角幾何知識求出秒針的終點(diǎn)坐標(biāo):。 ?? ?secondEnd_ x= center_ x + secondLength * sin(secondAngle); ?? ?secondEnd_ y= center_ y - secondLength * cos(secondAngle); ?? ?讓角度secondAngle循環(huán)變化,即實(shí)現(xiàn)了秒針轉(zhuǎn)動(dòng)的動(dòng)畫效果。 */ void HideCursor(){?? ?//隱藏光標(biāo)位置 ,這個(gè)函數(shù)復(fù)制代碼就行? ?? ?CONSOLE_CURSOR_INFO cursor_info={1,0};? ?? ?SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } int IsEnd;//是否結(jié)束? int center_x,center_y;//中點(diǎn) int second_long;//長度 int second_x,second_y;//秒針位置 int minute_long;//長度 int minute_x,minute_y;//分針位置 int hour_long;//長度 int hour_x,hour_y;//時(shí)針位置 int second_num,minute_num,hour_num;//秒針分針時(shí)針計(jì)數(shù)用的 void startup(){?? ?//【數(shù)據(jù)初始化】? ?? ? ?? ?HideCursor();//不顯示光標(biāo)? ?? ?IsEnd = 0;?? ? ?? ?initgraph(Width,High);//展示畫布 ?? ?center_x=Width/2; ?? ?center_y=High/2;//中心點(diǎn)信息 ?? ?second_long= (Width>High)? High/4:Width/4; ?? ?second_x=center_x; ?? ?second_y = center_y-second_long;//秒針信息 ?? ?minute_long=second_long*4/5; ?? ?minute_x=center_x; ?? ?minute_y = center_y-minute_long;//分針信息 ?? ?hour_long=second_long*2/3; ?? ?hour_x=center_x; ?? ?hour_y = center_y-hour_long;//時(shí)針信息 ?? ?second_num=0; ?? ?minute_num=0; ?? ?hour_num=0; ?? ? } void show_begin(){//【初始頁面展示】? ?? ?BeginBatchDraw();//批量繪圖開始 }? void show(){?? ?//【顯示畫面】? ?? ?int i; ?? ? ?? ?setlinestyle(PS_SOLID,2);//設(shè)置線型為實(shí)線,線寬為2 ?? ?setcolor(DARKGRAY);//設(shè)置顏色為暗灰色 ?? ?setfillcolor(DARKGRAY);//設(shè)置填充顏色也為暗灰色 ?? ?fillcircle(center_x,center_y,second_long*5/4);//畫表盤外圓 ?? ?setcolor(LIGHTGRAY);//設(shè)置顏色亮灰色 ?? ?setfillcolor(LIGHTGRAY);//設(shè)置填充顏色為亮灰色 ?? ?fillcircle(center_x,center_y,second_long*6/5);//填充表盤背景 ?? ?for(i=0;i<=59;i++){//畫表盤上的刻度 ?? ??? ?setcolor(BLACK); ?? ??? ?if(i%5==0){//每個(gè)小時(shí)點(diǎn)線長更長 ?? ??? ??? ?line( ?? ??? ??? ?(center_x+second_long*15/14*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*15/14*cos(((2*PI)/60)*i)), ?? ??? ??? ?(center_x+second_long*6/5*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*6/5*cos(((2*PI)/60)*i)) ?? ??? ??? ?); ?? ??? ?}else line( ?? ??? ??? ?(center_x+second_long*8/7*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*8/7*cos(((2*PI)/60)*i)), ?? ??? ??? ?(center_x+second_long*6/5*sin(((2*PI)/60)*i)), ?? ??? ??? ?(center_y-second_long*6/5*cos(((2*PI)/60)*i)) ?? ??? ??? ?); ?? ?} ?? ?setlinecolor(YELLOW);//設(shè)置線條顏色黃色 ?? ?setlinestyle(PS_SOLID,3);//設(shè)置線條風(fēng)格為實(shí)線,線寬為3 ?? ?line(center_x,center_y,second_x,second_y);//畫秒針 ?? ?setlinecolor(BLUE);//設(shè)置顏色 ?? ?setlinestyle(PS_SOLID,4);//設(shè)置線條風(fēng)格實(shí)線,線寬為4 ?? ?line(center_x,center_y,minute_x,minute_y);//畫分針 ?? ?setlinecolor(RED);//設(shè)置顏色 ?? ?setlinestyle(PS_SOLID,6);//設(shè)置線條風(fēng)格實(shí)線,線寬3 ?? ?line(center_x,center_y,hour_x,hour_y);//畫時(shí)針 ?? ?FlushBatchDraw();//更新一次畫面,解決畫面閃的問題,需要配合BeginBatchDraw函數(shù)使用 ?? ?Sleep(1000);//延時(shí) ?? ?cleardevice();//清除之前的畫跡 ?? ? ?? ? } void update_outinput(){?? ?//【與輸入無關(guān)的更新】? ?? ? ?? ?second_num = (second_num==59)? 0:second_num+1;//秒針為59下一次是0 ?? ?second_x=center_x+second_long*sin(((2*PI)/60)*second_num);//計(jì)算秒針端點(diǎn)位置 ?? ?second_y=center_y-second_long*cos(((2*PI)/60)*second_num); ?? ?if(second_num==0){//如果秒針到0了分針加一 ?? ??? ?minute_num = (minute_num==59)? 0:minute_num+1;//分針為59下一次是0 ?? ??? ?minute_x=center_x+minute_long*sin(((2*PI)/60)*minute_num);//計(jì)算分針端點(diǎn)位置 ?? ??? ?minute_y=center_y-minute_long*cos(((2*PI)/60)*minute_num); ?? ?} ?? ?if(minute_num%12==0&&second_num==0){//如果分針到0了且秒針也為0時(shí),時(shí)針加1 ?? ??? ?hour_num = (hour_num==59)? 0:hour_num+1;//時(shí)針為59下一次是0 ?? ??? ?hour_x=center_x+hour_long*sin(((2*PI)/60)*hour_num);//計(jì)算時(shí)針端點(diǎn)位置 ?? ??? ?hour_y=center_y-hour_long*cos(((2*PI)/60)*hour_num); ?? ?} ?? ? } void update_input(){//【與輸入有關(guān)的更新】? ?? ?char input; ?? ?if(kbhit()){ ?? ??? ?input = getch(); ?? ??? ??? ??? ? ?? ?} } void show_end(){//【顯示失敗界面】? ?? ?EndBatchDraw(); } int main(){ ?? ?startup();?? ?//數(shù)據(jù)初始化 ?? ?show_begin();//初始頁面? ?? ?while(!IsEnd){?? ?//游戲循環(huán)執(zhí)行? ?? ??? ?show();?? ?// 顯示畫面? ?? ??? ?update_outinput();?? ?//與輸入無關(guān)的更新? ?? ??? ?update_input();?? ?//與輸入有關(guān)的更新? ?? ?} ?? ?show_end(); //顯示失敗界面? ?? ?return 0; }
3.運(yùn)行結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c++中臨時(shí)變量不能作為非const的引用參數(shù)的方法
下面小編就為大家?guī)硪黄猚++中臨時(shí)變量不能作為非const的引用參數(shù)的方法。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01C++實(shí)現(xiàn)LeetCode(312.打氣球游戲)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(312.打氣球游戲),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C語言實(shí)現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式
這篇文章主要介紹了C語言實(shí)現(xiàn)輸入ascii碼,輸出對應(yīng)的字符方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01C++報(bào)錯(cuò) XX does not name a type;
這篇文章主要給大家介紹了C++報(bào)錯(cuò) XX does not name a type;field `XX’ has incomplete type解決方案,文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2023-08-08C++ 實(shí)現(xiàn)即時(shí)通信的示例代碼(直接運(yùn)行)
本文主要介紹了C++ 實(shí)現(xiàn)即時(shí)通信的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05