2019年春第三次课程设计实验报告

2019年春第三次课程设计实验报告
实验项目名称:
实时钟表
功能描述:
利用EasyX实现一个实时钟表的小程序,同时学习时间函数的使用。
项目结构模块介绍:
1.全局变量
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();

2.绘制简单表盘
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);
}
3.根据角度设置表盘
outtextxy(center_x - 25, center_y + Width/6, "我的时钟");
GetLocalTime(&ti); // 获取当前时间
// 秒钟角度变化
secondAngle = ti.wSecond * 2PI/60; // 一圈一共2PI,一圈60秒,一秒钟秒钟走过的角度为2PI/60
// 分钟角度变化
minuteAngle = ti.wMinute * 2
PI/60 + secondAngle/60; // 一圈一共2PI,一圈60分,一分钟分钟走过的角度为2PI/60
// 时钟角度变化
hourAngle = ti.wHour * 2PI/12 + minuteAngle/12; // 一圈一共2PI,一圈12小时,一小时时钟走过的角度为2PI/12
// 由角度决定的秒针端点坐标
secondEnd_x = center_x + secondLength
sin(secondAngle);
secondEnd_y = center_y - secondLengthcos(secondAngle);
4. 由角度决定的分针端点坐标
minuteEnd_x = center_x + minuteLength
sin(minuteAngle);
minuteEnd_y = center_y - minuteLength*cos(minuteAngle);

5.由角度决定的时针端点坐标
hourEnd_x = center_x + hourLengthsin(hourAngle);
hourEnd_y = center_y - hourLength
cos(hourAngle);
6.画表盘上的指针
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);
7.隐藏指针
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); // 隐藏前一帧的时针

代码托管连接
https://gitee.com/wjx0229/difficult_team/blob/master/实际时钟.cpp
实验总结:
本章加入了以前没有接触过的新程序EasyX,学习起来有点难度。

posted @ 2019-06-20 13:02  wjx0229  阅读(132)  评论(0编辑  收藏  举报