C++小项目——实时钟表
0.前言
在很多游戏中,我们要使用时间,这个时间一个是系统当前的时间,一个是服务器给你的时间。这篇文章我们尝试做一个模拟时钟。
效果图如下
1.任务分解
1.首先我们尝试使用easyx来画一个。基本框架如下:
#include<graphics.h>
#include<conio.h>
#define High 480 //画布高
#define Width 640 //画布宽度
int main(){
initgraph(Width,High); //初始化
getch();
closegraph(); //结束
return 0;
}然后,我们先开始画时钟的线。
线是line。知道中心点和长度就可以了。
下面是画秒针的代码:
#include<graphics.h>
#include<conio.h>
#define High 480 //画布高
#define Width 640 //画布宽度
int main(){
initgraph(Width,High); //初始化
int center_x,center_y; //定义时钟的中心
center_x=Width/2;
center_y=High/2;
int secondLength; //定义秒针的长度
secondLength=Width/5;
int secondEnd_x,secondEnd_y; //定义秒针终点的坐标
secondEnd_x =center_x + secondLength; //终点坐标=中心点坐标+长度
secondEnd_y =center_y + secondLength;
//画秒针
setlinestyle(PS_SOLID,2);
setcolor(WHITE);
line(center_x,center_y,secondEnd_x,secondEnd_y);
getch();
closegraph(); //结束
return 0;
}效果图:
2.让秒针转动
定义secondAngle为秒针对应的角度,利用三角几何知识求出秒针的终点坐标。
secondEnd_x =center_x + secondLength*sin(secondAngle); secondEnd_y =center_y - secondLength*cos(secondAngle);
这样就可以实现转动动画效果
画角度秒针:
#include<graphics.h>
#include<conio.h>
#include<cmath>
#define High 480 //画布高
#define Width 640 //画布宽度
int main(){
initgraph(Width,High); //初始化
int center_x,center_y; //定义时钟的中心
center_x=Width/2;
center_y=High/2;
int secondLength; //定义秒针的长度
secondLength=Width/5;
int secondEnd_x,secondEnd_y; //定义秒针终点的坐标
secondEnd_x =center_x + secondLength; //终点坐标=中心点坐标+长度
secondEnd_y =center_y + secondLength;
float secodeAngle=0; //秒针对应转动角度
secondEnd_x =center_x + secondLength*sin(secodeAngle); //终点坐标=中心点坐标+长度
secondEnd_y =center_y - secondLength*cos(secodeAngle);
//画秒针
setlinestyle(PS_SOLID,2);
setcolor(WHITE);
line(center_x,center_y,secondEnd_x,secondEnd_y);
getch();
closegraph(); //结束
return 0;
}使用循环实现动的效果
#include<graphics.h>
#include<conio.h>
#include<cmath>
#define High 480 //画布高
#define Width 640 //画布宽度
#define PI 3.1415926
int main(){
initgraph(Width,High); //初始化
int center_x,center_y; //定义时钟的中心
center_x=Width/2;
center_y=High/2;
int secondLength; //定义秒针的长度
secondLength=Width/5;
int secondEnd_x,secondEnd_y; //定义秒针终点的坐标
secondEnd_x =center_x + secondLength; //终点坐标=中心点坐标+长度
secondEnd_y =center_y + secondLength;
float secodeAngle=0; //秒针对应转动角度
while(1){
secondEnd_x =center_x + secondLength*sin(secodeAngle);
secondEnd_y =center_y - secondLength*cos(secodeAngle);
//画秒针
setlinestyle(PS_SOLID,2);
setcolor(WHITE);
line(center_x,center_y,secondEnd_x,secondEnd_y);
Sleep(50); //休息几秒换颜色重新画
setcolor(BLACK);
line(center_x,center_y,secondEnd_x,secondEnd_y);
secodeAngle = secodeAngle + 2*PI/60; //求出一格的角度
}
getch();
closegraph(); //结束
return 0;
}效果图
我们可以用
SYSTEMTIME ti; //获取系统时间
来获取系统时间
代码:
#include<graphics.h>
#include<conio.h>
#include<cmath>
#define High 480 //画布高
#define Width 640 //画布宽度
#define PI 3.1415926
int main(){
initgraph(Width,High); //初始化
int center_x,center_y; //定义时钟的中心
center_x=Width/2;
center_y=High/2;
int secondLength; //定义秒针的长度
secondLength=Width/5;
int secondEnd_x,secondEnd_y; //定义秒针终点的坐标
secondEnd_x =center_x + secondLength; //终点坐标=中心点坐标+长度
secondEnd_y =center_y + secondLength;
float secodeAngle=0; //秒针对应转动角度
SYSTEMTIME ti; //获取系统时间
BeginBatchDraw();
while(1){
GetLocalTime(&ti); //获取当前时间
secodeAngle = ti.wSecond * 2*PI/60; //求出一格的角度
secondEnd_x =center_x + secondLength*sin(secodeAngle);
secondEnd_y =center_y - secondLength*cos(secodeAngle);
//画秒针
setlinestyle(PS_SOLID,2);
setcolor(WHITE);
line(center_x,center_y,secondEnd_x,secondEnd_y);
FlushBatchDraw();
Sleep(50); //休息几秒换颜色重新画
setcolor(BLACK);
line(center_x,center_y,secondEnd_x,secondEnd_y);
}
EndBatchDraw();
getch();
closegraph(); //结束
return 0;
}效果图:
最终代码,我们补全时针与分针。
#include <graphics.h>
#include <conio.h>
#include <math.h>
#define High 480 // 游戏画面尺寸
#define Width 640
#define PI 3.14159
int main()
{
initgraph(Width, High); // 初始化 640 x 480 的绘图窗口
int center_x,center_y; // 中心点的坐标,也是表的中心
center_x = Width/2;
center_y = High/2;
int secondLength = Width/5; // 秒针的长度
int minuteLength = Width/6; // 分针的长度
int hourLength = Width/7; // 时针的长度
int secondEnd_x,secondEnd_y; // 秒针的终点
int minuteEnd_x,minuteEnd_y; // 分针的终点
int hourEnd_x,hourEnd_y; // 时针的终点
float secondAngle; // 秒钟对应的角度
float minuteAngle; // 分钟对应的角度
float hourAngle; // 时钟对应的角度
SYSTEMTIME ti; // 定义变量保存当前时间
BeginBatchDraw();
while (1)
{
// 绘制一个简单的表盘
setlinestyle(PS_SOLID, 1);
setcolor(WHITE);
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, "我的时钟");
GetLocalTime(&ti); // 获取当前时间
// 秒钟角度变化
secondAngle = ti.wSecond * 2*PI/60; // 一圈一共2*PI,一圈60秒,一秒钟秒钟走过的角度为2*PI/60
// 分钟角度变化
minuteAngle = ti.wMinute * 2*PI/60 + secondAngle/60; // 一圈一共2*PI,一圈60分,一分钟分钟走过的角度为2*PI/60
// 时钟角度变化
hourAngle = ti.wHour * 2*PI/12 + minuteAngle/12; // 一圈一共2*PI,一圈12小时,一小时时钟走过的角度为2*PI/12
// 由角度决定的秒针端点坐标
secondEnd_x = center_x + secondLength*sin(secondAngle);
secondEnd_y = center_y - secondLength*cos(secondAngle);
// 由角度决定的分针端点坐标
minuteEnd_x = center_x + minuteLength*sin(minuteAngle);
minuteEnd_y = center_y - minuteLength*cos(minuteAngle);
// 由角度决定的时针端点坐标
hourEnd_x = center_x + hourLength*sin(hourAngle);
hourEnd_y = center_y - hourLength*cos(hourAngle);
setlinestyle(PS_SOLID, 2);
setcolor(YELLOW);
line(center_x, center_y, secondEnd_x, secondEnd_y); // 画秒针
setlinestyle(PS_SOLID, 5);
setcolor(BLUE);
line(center_x, center_y, minuteEnd_x, minuteEnd_y); // 画分针
setlinestyle(PS_SOLID, 10);
setcolor(RED);
line(center_x, center_y, hourEnd_x, hourEnd_y); // 画时针
FlushBatchDraw();
Sleep(10);
setcolor(BLACK);
setlinestyle(PS_SOLID, 2);
line(center_x, center_y, secondEnd_x, secondEnd_y); // 隐藏前一帧的秒针
setlinestyle(PS_SOLID, 5);
line(center_x, center_y, minuteEnd_x, minuteEnd_y); // 隐藏前一帧的分针
setlinestyle(PS_SOLID, 10);
line(center_x, center_y, hourEnd_x, hourEnd_y); // 隐藏前一帧的时针
}
EndBatchDraw();
getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
return 0;
}扫描二维码推送至手机访问。
版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。


