C语言基于EasyX库怎么实现有图形界面钟表
导读:本文共2720.5字符,通常情况下阅读需要9分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 1.目标要求:实现一个显示图像的时钟2.C语言代码:#include<graphics.h>//需要提前下载EasyX库哦#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<conio.h>#include<mat... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。1.目标要求:
实现一个显示图像的时钟
2.C语言代码:
#include<graphics.h>//需要提前下载EasyX库哦#include<stdio.h>#include<stdlib.h>#include<windows.h>#include<conio.h>#include<math.h>#defineHigh480#defineWidth640//画布尺寸#definePI3.1415/*实现秒针的转动。定义secondAngle为秒针对应的角度,利用三角几何知识求出秒针的终点坐标:。secondEnd_x=center_x+secondLength*sin(secondAngle);secondEnd_y=center_y-secondLength*cos(secondAngle);让角度secondAngle循环变化,即实现了秒针转动的动画效果。*/voidHideCursor(){//隐藏光标位置,这个函数就行CONSOLE_CURSOR_INFOcursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}intIsEnd;//是否结束intcenter_x,center_y;//中点intsecond_long;//长度intsecond_x,second_y;//秒针位置intminute_long;//长度intminute_x,minute_y;//分针位置inthour_long;//长度inthour_x,hour_y;//时针位置intsecond_num,minute_num,hour_num;//秒针分针时针计数用的voidstartup(){//【数据初始化】HideCursor();//不显示光标IsEnd=0;initgraph(Width,High);//展示画布center_x=Width/2;center_y=High/2;//中心点信息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;//时针信息second_num=0;minute_num=0;hour_num=0;}voidshow_begin(){//【初始页面展示】BeginBatchDraw();//批量绘图开始}voidshow(){//【显示画面】inti;setlinestyle(PS_SOLID,2);//设置线型为实线,线宽为2setcolor(DARKGRAY);//设置颜色为暗灰色setfillcolor(DARKGRAY);//设置填充颜色也为暗灰色fillcircle(center_x,center_y,second_long*5/4);//画表盘外圆setcolor(LIGHTGRAY);//设置颜色亮灰色setfillcolor(LIGHTGRAY);//设置填充颜色为亮灰色fillcircle(center_x,center_y,second_long*6/5);//填充表盘背景for(i=0;i<=59;i++){//画表盘上的刻度setcolor(BLACK);if(i%5==0){//每个小时点线长更长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)));}elseline((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);//设置线条颜色黄色setlinestyle(PS_SOLID,3);//设置线条风格为实线,线宽为3line(center_x,center_y,second_x,second_y);//画秒针setlinecolor(BLUE);//设置颜色setlinestyle(PS_SOLID,4);//设置线条风格实线,线宽为4line(center_x,center_y,minute_x,minute_y);//画分针setlinecolor(RED);//设置颜色setlinestyle(PS_SOLID,6);//设置线条风格实线,线宽3line(center_x,center_y,hour_x,hour_y);//画时针FlushBatchDraw();//更新一次画面,解决画面闪的问题,需要配合BeginBatchDraw函数使用Sleep(1000);//延时cleardevice();//清除之前的画迹}voidupdate_outinput(){//【与输入无关的更新】second_num=(second_num==59)?0:second_num+1;//秒针为59下一次是0second_x=center_x+second_long*sin(((2*PI)/60)*second_num);//计算秒针端点位置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下一次是0minute_x=center_x+minute_long*sin(((2*PI)/60)*minute_num);//计算分针端点位置minute_y=center_y-minute_long*cos(((2*PI)/60)*minute_num);}if(minute_num%12==0&&second_num==0){//如果分针到0了且秒针也为0时,时针加1hour_num=(hour_num==59)?0:hour_num+1;//时针为59下一次是0hour_x=center_x+hour_long*sin(((2*PI)/60)*hour_num);//计算时针端点位置hour_y=center_y-hour_long*cos(((2*PI)/60)*hour_num);}}voidupdate_input(){//【与输入有关的更新】charinput;if(kbhit()){input=getch();}}voidshow_end(){//【显示失败界面】EndBatchDraw();}intmain(){startup();//数据初始化show_begin();//初始页面while(!IsEnd){//游戏循环执行show();//显示画面update_outinput();//与输入无关的更新update_input();//与输入有关的更新}show_end();//显示失败界面return0;}
3.运行结果:
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
C语言基于EasyX库怎么实现有图形界面钟表的详细内容,希望对您有所帮助,信息来源于网络。