擁有一個屬於自己的鐘表DIY MY OWN CLOCK!(C語言和EasyX實現)

2020-08-10 10:07:39

首先繪製一個靜態秒針

#include<graphics.h>
#include<conio.h>
#include<math.h>

#define high 480
#define width 640

int main()
{
	initgraph(width, high);		//註釋亂碼,所以有些地方用英文代替,還望見諒
	int x, y;					//clock的center
	x = width / 2;
	y = high / 2;
	int secLength;				//second hand's長度
	secLength = width / 5;

	int secEnd_x, secEnd_y;		//second hand's末端situation

	secEnd_x = x + secLength;
	secEnd_y = y;

	setlinestyle(PS_SOLID, 2);
	setcolor(WHITE);
	line(x, y, secEnd_x, secEnd_y);
	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);		//註釋亂碼,所以有些地方用英文代替,還望見諒
	int x, y;					//clock的center
	x = width / 2;
	y = high / 2;
	int secLength;				//second hand's長度
	secLength = width / 5;

	int secEnd_x, secEnd_y;		//second hand's末端situation
	float secAngle = 0;			//second hand's角度

	while(1)
	{
		secEnd_x = x + secLength * sin(secAngle);
		secEnd_y = y - secLength * cos(secAngle);

		setlinestyle(PS_SOLID, 2);
		setcolor(WHITE);
		line(x, y, secEnd_x, secEnd_y);

		Sleep(1000);
		setcolor(BLACK);
		line(x, y, secEnd_x, secEnd_y);

		secAngle += (2 * PI) / 60;		//在C/C++中需要轉爲弧度才能 纔能正常
	}
	getch();
	closegraph();
	return 0;
}

實時轉動的秒針

GetLocalTime(&ti)可以獲取當前時間
在这里插入图片描述

#include<graphics.h>
#include<conio.h>
#include<math.h>

#define high 480
#define width 640
#define PI 3.14159

int main()
{
	initgraph(width, high);		//註釋亂碼,所以有些地方用英文代替,還望見諒
	int x, y;					//clock的center
	x = width / 2;
	y = high / 2;
	int secLength;				//second hand's長度
	secLength = width / 5;

	int secEnd_x, secEnd_y;		//second hand's末端situation
	float secAngle = 0;			//second hand's角度

	SYSTEMTIME ti;				//define變數儲存當前時間

	while(1)
	{
		GetLocalTime(&ti);
		secAngle = ti.wSecond * 2 * PI / 60;
		secEnd_x = x + secLength * sin(secAngle);
		secEnd_y = y - secLength * cos(secAngle);

		setlinestyle(PS_SOLID, 2);
		setcolor(WHITE);
		line(x, y, secEnd_x, secEnd_y);

		Sleep(1000);
		setcolor(BLACK);
		line(x, y, secEnd_x, secEnd_y);

	}
	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);
	int x, y;
	x = width / 2;
	y = high / 2;
	int secLength = width / 4;
	int minLength = width / 5;
	int hourLength = width / 6;

	int secEnd_x, secEnd_y;
	int minEnd_x, minEnd_y;
	int hourEnd_x, hourEnd_y;
	float secAngle, minAngle, hourAngle;

	SYSTEMTIME ti;			//儲存當前時間

	while(1)
	{
		GetLocalTime(&ti);
		secAngle = ti.wSecond * 2 * PI / 60;
		minAngle = ti.wMinute * 2 * PI / 60;
		hourAngle = ti.wHour * 2 * PI / 12;			//1圈12小時
		
		secEnd_x = x + secLength * sin(secAngle);
		secEnd_y = y - secLength * cos(secAngle);

		minEnd_x = x + minLength * sin(minAngle);
		minEnd_y = y - minLength * cos(minAngle);

		hourEnd_x = x + hourLength * sin(hourAngle);
		hourEnd_y = y - hourLength * cos(hourAngle);

		setlinestyle(PS_SOLID, 1);
		setcolor(WHITE);
		line(x, y, secEnd_x, secEnd_y);

		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(x, y, minEnd_x, minEnd_y);

		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(x, y, hourEnd_x, hourEnd_y);

		Sleep(1000);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(x, y, secEnd_x, secEnd_y);
		setlinestyle(PS_SOLID, 4);
		line(x, y, minEnd_x, minEnd_y);
		setlinestyle(PS_SOLID, 6);
		line(x, y, hourEnd_x, hourEnd_y);
	}
	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);
	int x, y;
	x = width / 2;
	y = high / 2;
	int secLength = width / 3.8;
	int minLength = width / 4;
	int hourLength = width / 5.5;

	int secEnd_x, secEnd_y;
	int minEnd_x, minEnd_y;
	int hourEnd_x, hourEnd_y;
	float secAngle, minAngle, hourAngle;

	SYSTEMTIME ti;			//儲存當前時間
	BeginBatchDraw();

	while(1)
	{
		setbkcolor(DARKGRAY);
		cleardevice();
		setlinestyle(PS_SOLID, 3);
		setcolor(LIGHTGREEN);
		circle(x, y, width / 3);

		int x2, y2, i;
		for(i = 0; i < 60; i++)
		{
			x2 = x + int(width / 3.3 * sin(PI * 2 * i / 60));
			y2 = y - int(width / 3.3 * cos(PI * 2 * i / 60));
			
			if(i % 15 == 0)
				bar(x2 - 5, y2 - 5, x2 + 5, y2 + 5);
			else if(i % 5 == 0)
				circle(x2, y2, 3);
			else
				putpixel(x2, y2, CYAN);
		}
		
		settextstyle(32, 0, _T("Consolas"));
		outtextxy(x - 100, y + width / 7, "Richard's Clock");
		GetLocalTime(&ti);
		secAngle = ti.wSecond * 2 * PI / 60;
		minAngle = ti.wMinute * 2 * PI / 60;
		hourAngle = ti.wHour * 2 * PI / 12;			//1圈12小時
		
		secEnd_x = x + secLength * sin(secAngle);
		secEnd_y = y - secLength * cos(secAngle);

		minEnd_x = x + minLength * sin(minAngle);
		minEnd_y = y - minLength * cos(minAngle);

		hourEnd_x = x + hourLength * sin(hourAngle);
		hourEnd_y = y - hourLength * cos(hourAngle);

		setlinestyle(PS_SOLID, 2);
		setcolor(YELLOW);
		line(x, y, secEnd_x, secEnd_y);

		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(x, y, minEnd_x, minEnd_y);

		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(x, y, hourEnd_x, hourEnd_y);

		FlushBatchDraw();
		Sleep(1000);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(x, y, secEnd_x, secEnd_y);
		setlinestyle(PS_SOLID, 4);
		line(x, y, minEnd_x, minEnd_y);
		setlinestyle(PS_SOLID, 6);
		line(x, y, hourEnd_x, hourEnd_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);
	int x, y;
	x = width / 2;
	y = high / 2;
	int secLength = width / 3.8;
	int minLength = width / 4;
	int hourLength = width / 5.5;

	int secEnd_x, secEnd_y;
	int minEnd_x, minEnd_y;
	int hourEnd_x, hourEnd_y;
	float secAngle, minAngle, hourAngle;

	SYSTEMTIME ti;			//儲存當前時間
	BeginBatchDraw();

	while(1)
	{
		setbkcolor(DARKGRAY);
		cleardevice();
		setlinestyle(PS_SOLID, 3);
		setcolor(LIGHTGREEN);
		circle(x, y, width / 3);

		int x2, y2, i;
		for(i = 0; i < 60; i++)
		{
			x2 = x + int(width / 3.3 * sin(PI * 2 * i / 60));
			y2 = y - int(width / 3.3 * cos(PI * 2 * i / 60));
			
			if(i % 15 == 0)
				bar(x2 - 5, y2 - 5, x2 + 5, y2 + 5);
			else if(i % 5 == 0)
				circle(x2, y2, 3);
			else
				putpixel(x2, y2, CYAN);
		}
		
		settextstyle(32, 0, _T("Consolas"));
		outtextxy(x - 110, y - width / 6, "Made in ChangSha");
		outtextxy(x - 110, y + width / 9, "Richard's Clock");
		GetLocalTime(&ti);
		secAngle = ti.wSecond * 2 * PI / 60;
		minAngle = ti.wMinute * 2 * PI / 60;
		hourAngle = ti.wHour * 2 * PI / 12;			//1圈12小時
		
		secEnd_x = x + secLength * sin(secAngle);
		secEnd_y = y - secLength * cos(secAngle);

		minEnd_x = x + minLength * sin(minAngle);
		minEnd_y = y - minLength * cos(minAngle);

		hourEnd_x = x + hourLength * sin(hourAngle);
		hourEnd_y = y - hourLength * cos(hourAngle);

		setlinestyle(PS_SOLID, 2);
		setcolor(YELLOW);
		line(x, y, secEnd_x, secEnd_y);

		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(x, y, minEnd_x, minEnd_y);

		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(x, y, hourEnd_x, hourEnd_y);

		FlushBatchDraw();
		Sleep(1000);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(x, y, secEnd_x, secEnd_y);
		setlinestyle(PS_SOLID, 4);
		line(x, y, minEnd_x, minEnd_y);
		setlinestyle(PS_SOLID, 6);
		line(x, y, hourEnd_x, hourEnd_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);
	int x, y;
	x = width / 2;
	y = high / 2;
	int secLength = width / 3.8;
	int minLength = width / 4;
	int hourLength = width / 5.5;

	int secEnd_x, secEnd_y;
	int minEnd_x, minEnd_y;
	int hourEnd_x, hourEnd_y;
	float secAngle, minAngle, hourAngle;

	SYSTEMTIME ti;			//儲存當前時間
	BeginBatchDraw();

	while(1)
	{
		setbkcolor(DARKGRAY);
		cleardevice();
		setlinestyle(PS_SOLID, 3);
		setcolor(LIGHTGREEN);
		circle(x, y, width / 3);

		int x2, y2, i;
		for(i = 0; i < 60; i++)
		{
			x2 = x + int(width / 3.3 * sin(PI * 2 * i / 60));
			y2 = y - int(width / 3.3 * cos(PI * 2 * i / 60));
			
			if(i % 15 == 0)
				bar(x2 - 5, y2 - 5, x2 + 5, y2 + 5);
			else if(i % 5 == 0)
				circle(x2, y2, 3);
			else
				putpixel(x2, y2, CYAN);
		}
		
		settextstyle(32, 0, _T("Consolas"));
		outtextxy(x - 110, y - width / 6, "Made in ChangSha");
		outtextxy(x - 110, y + width / 9, "Richard's Clock");
		GetLocalTime(&ti);
		secAngle = ti.wSecond * 2 * PI / 60;
		minAngle = ti.wMinute * 2 * PI / 60;
		hourAngle = ti.wHour * 2 * PI / 12;			//1圈12小時
		
		secEnd_x = x - secLength * sin(secAngle);
		secEnd_y = y - secLength * cos(secAngle);

		minEnd_x = x - minLength * sin(minAngle);
		minEnd_y = y - minLength * cos(minAngle);

		hourEnd_x = x - hourLength * sin(hourAngle);
		hourEnd_y = y - hourLength * cos(hourAngle);

		setlinestyle(PS_SOLID, 2);
		setcolor(YELLOW);
		line(x, y, secEnd_x, secEnd_y);

		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(x, y, minEnd_x, minEnd_y);

		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(x, y, hourEnd_x, hourEnd_y);

		FlushBatchDraw();
		Sleep(1000);

		setcolor(BLACK);
		setlinestyle(PS_SOLID, 2);
		line(x, y, secEnd_x, secEnd_y);
		setlinestyle(PS_SOLID, 4);
		line(x, y, minEnd_x, minEnd_y);
		setlinestyle(PS_SOLID, 6);
		line(x, y, hourEnd_x, hourEnd_y);
	}
	EndBatchDraw();
	getch();
	closegraph();
	return 0;
}
		


如果喜歡我的文章,請記得一鍵三連哦,點贊關注收藏,你的每一個贊每一份關注每一次收藏都將是我前進路上的無限動力 !!!↖(▔▽▔)↗感謝支援!