C语言基于EasyX库实现有图形界面钟表

本文实例为大家分享了C语言基于EasyX库实现有图形界面钟表的具体代码,供大家参考,具体内容如下

1.目标要求:

实现一个显示图像的时钟

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
/*
实现秒针的转动。定义secondAngle为秒针对应的角度,利用三角几何知识求出秒针的终点坐标:。

    secondEnd_ x= center_ x + secondLength * sin(secondAngle);
    secondEnd_ y= center_ y - secondLength * cos(secondAngle);

    让角度secondAngle循环变化,即实现了秒针转动的动画效果。
*/

void HideCursor(){    //隐藏光标位置 ,这个函数复制代码就行 
    CONSOLE_CURSOR_INFO cursor_info={1,0}; 
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

int IsEnd;//是否结束 
int center_x,center_y;//中点
int second_long;//长度
int second_x,second_y;//秒针位置
int minute_long;//长度
int minute_x,minute_y;//分针位置
int hour_long;//长度
int hour_x,hour_y;//时针位置
int second_num,minute_num,hour_num;//秒针分针时针计数用的

void startup(){    //【数据初始化】 
    
    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;
    
}
void show_begin(){//【初始页面展示】 
    BeginBatchDraw();//批量绘图开始
} 
void show(){    //【显示画面】 
    int i;
    
    setlinestyle(PS_SOLID,2);//设置线型为实线,线宽为2
    setcolor(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))
            );
        }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);//设置线条颜色黄色
    setlinestyle(PS_SOLID,3);//设置线条风格为实线,线宽为3
    line(center_x,center_y,second_x,second_y);//画秒针
    setlinecolor(BLUE);//设置颜色
    setlinestyle(PS_SOLID,4);//设置线条风格实线,线宽为4
    line(center_x,center_y,minute_x,minute_y);//画分针
    setlinecolor(RED);//设置颜色
    setlinestyle(PS_SOLID,6);//设置线条风格实线,线宽3
    line(center_x,center_y,hour_x,hour_y);//画时针

    FlushBatchDraw();//更新一次画面,解决画面闪的问题,需要配合BeginBatchDraw函数使用
    Sleep(1000);//延时
    cleardevice();//清除之前的画迹
    
    
}
void update_outinput(){    //【与输入无关的更新】 
    
    second_num = (second_num==59)? 0:second_num+1;//秒针为59下一次是0
    second_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下一次是0
        minute_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时,时针加1
        hour_num = (hour_num==59)? 0:hour_num+1;//时针为59下一次是0
        hour_x=center_x+hour_long*sin(((2*PI)/60)*hour_num);//计算时针端点位置
        hour_y=center_y-hour_long*cos(((2*PI)/60)*hour_num);
    }
    
}
void update_input(){//【与输入有关的更新】 
    char input;
    if(kbhit()){
        input = getch();
                
    }
}
void show_end(){//【显示失败界面】 
    EndBatchDraw();
}

int main(){
    startup();    //数据初始化
    show_begin();//初始页面 
    while(!IsEnd){    //游戏循环执行 
        show();    // 显示画面 
        update_outinput();    //与输入无关的更新 
        update_input();    //与输入有关的更新 
    }
    show_end(); //显示失败界面 
    return 0;
}

3.运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C语言基于EasyX库实现有图形界面时钟

    本文实例为大家分享了C语言基于EasyX库实现有图形界面时钟的具体代码,供大家参考,具体内容如下 1.目标要求: 1.实现一个显示图像的时钟2.时间与本地时间一致 2.C语言代码: #include<graphics.h> //需要提前安装库函数EasyX,网上官网下载 #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #include<ma

  • C语言实现电子时钟程序

    本文实例为大家分享了C语言实现电子时钟程序的具体代码,供大家参考,具体内容如下 Qt 里面运行 #include<windows.h> #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> typedef struct { int x; int y; }Point; time_t now; struct tm *pt, t1, t2; int pri

  • C语言基于EasyX库实现有图形界面钟表

    本文实例为大家分享了C语言基于EasyX库实现有图形界面钟表的具体代码,供大家参考,具体内容如下 1.目标要求: 实现一个显示图像的时钟 2.C语言代码: #include<graphics.h> //需要提前下载EasyX库哦 #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #include<math.h> #define High

  • C语言基于EasyX库实现有颜色弹跳小球

    本文实例为大家分享了基于EasyX库实现有颜色弹跳小球的具体代码,供大家参考,具体内容如下 1.目标要求 1.实现一个有颜色小球在窗口中弹跳2.遇到边界弹跳 2.C语言代码 #include<graphics.h>  #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #define High 480 #define Width 640//画布尺寸

  • C++实现基于EASYX库扫描线算法

    本文实例为大家分享了C++实现基于EASYX库扫描线算法的具体代码,供大家参考,具体内容如下 扫描线算法的基本原理 * 作者在扫描线算法的基础上自己设计的更易于理解的地物填充绘制算法 流程图 代码 #include<graphics.h> //#include<conio.h> #include<iostream> using namespace std; //-----------------------------草图形-----------------------

  • C语言基于EasyX实现贪吃蛇

    本文实例为大家分享了C语言基于EasyX实现贪吃蛇的具体代码,供大家参考,具体内容如下 成品展示: 实现思路: 贪吃蛇的实现思路并不复杂,由于我们需要将数据展示在图形窗口上,因此就不需要之前那种用数组表示整个游戏地图的方法. 贪吃蛇的蛇有X坐标和Y坐标,而且不止一节,因此需要一个坐标结构体数组来保存,蛇的移动思路是除了第一节以外,后面每一节都是前面一节的坐标,然后通过键盘的输入输出使蛇的X或Y坐标加或者减来起到上下左右移动的作用.通过判断蛇与食物的坐标是否重合绝对是否吃到食物,吃到食物以后,蛇的

  • C语言基于EasyX绘制时钟

    本文实例为大家分享了C语言基于EasyX绘制时钟的具体代码,供大家参考,具体内容如下 函数说明: void line(     int x1,     int y1,     int x2,     int y2 ); 参数 x1直线的起始点的 x 坐标. y1直线的起始点的 y 坐标. x2直线的终止点的 x 坐标. y2直线的终止点的 y 坐标. 文件素材 源代码 #include <graphics.h> #include <conio.h> #include <mat

  • C++基于EasyX库实现拼图小游戏

    用C++的EasyX库做的拼图小游戏,供大家参考,具体内容如下   记录一下自己做的第一个项目,还有一些改进空间QWQ,可以支持难度升级,但是通关判断似乎有点小问题肯定不是我菜通不了关 . #pragma once #include <iostream> #include <graphics.h> #include <Windows.h> #include <algorithm> #include <easyx.h> #include <c

  • C++使用easyX库实现三星环绕效果流程详解

    目录 1,项目描述 2,解决思路 3,关键代码 4,项目运行截图 5,具体代码实现 1,项目描述 功能1:使用图形化的方式描述地球围绕着太阳转动,月球围绕着地球转动 功能2:在转动的过程中当用户按下1,2,3,4,5,6,7时它可以变换出7种不同的颜色,当用户按下8时它可以变换从1-7的颜色依次变换当用户再次按下8键时停止变换颜色 功能3:当用户按下上键时,地球会围绕太阳反转,当再次按下上键时地球会恢复到正转 功能4:当用户按下空格键的时候,所有动画暂停,当再次按下空格键的时候所有动画继续进行.

  • Python四款GUI图形界面库介绍

    目录 一.Python官方标准库:Tkinter (必须了解) 用法: 二.三方库:PyQt5(推荐,但是还是累) 1.安装: 2.QtDesigner 3.配置PyCharm 4.使用PyQt 三.三方库:wxPython 四.三方库:PyGTK 一.Python官方标准库:Tkinter (必须了解) Python内置图形界面库——Tkinter. Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数的 Unix 平

  • PHP基于GD库实现的生成图片缩略图函数示例

    本文实例讲述了PHP基于GD库实现的生成图片缩略图函数.分享给大家供大家参考,具体如下: <?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @param int $width 缩略图宽度(只指定高度时进行等比缩放) * @param int $width 缩略图高度(只指定宽度时进行等比缩放) * @param string $filename 保存路径(不指

随机推荐