图形化用到了EasyX , 声音用到了beep函数
// 航海钟.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#include <graphics.h>
#include<conio.h>
#include<easyx.h>
#include<cmath>
#include<string>
#define Width 640
#define High 480
#define PI 3.14159
using namespace std;
int main()
{
initgraph(Width, High);
int centX = Width / 2;
int centY = High /2;
int radus = High / 3 ;
setcolor(WHITE);
circle(centX,centY, radus);
char group[] = "轮换开始";
float secondLength = (0.8)*radus;
float secondAngle = 0;
float secondEndX = 0;
float secondEndY = 0;
float minuteLength = (0.5) * radus;
float minuteAngle = 0;
float minuteEndX = 0;
float minuteEndY = 0;
float hourLength = (0.3) * radus;
float hourAngle = 0;
float hourEndX = 0;
float hourEndY = 0;
SYSTEMTIME TI;
BeginBatchDraw();
for (int i = 1; i <= 12; i++)
{
if (i == 3)
{
setcolor(GREEN);
solidcircle(centX + radus, centY, 2);
}
if (i == 6)
{
setcolor(GREEN);
solidcircle(centX, centY + radus, 2);
}
if (i == 9)
{
setcolor(GREEN);
solidcircle(centX - radus, centY, 2);
}
if (i == 12)
{
setcolor(GREEN);
solidcircle(centX, centY-radus, 2);
}
}
GetLocalTime(&TI);
while (1)
{
GetLocalTime(&TI);
secondAngle = TI.wSecond * 2 * PI / 60;
secondEndX =centX+ sin(secondAngle) * secondLength;
secondEndY = centY-cos(secondAngle) * secondLength;
setcolor(WHITE);
setlinestyle(PS_SOLID, 2);
line(centX, centY, secondEndX, secondEndY);
minuteAngle = TI.wMinute * 2 * PI / 60;
minuteEndX = centX + sin(minuteAngle) * minuteLength;
minuteEndY = centY - cos(minuteAngle) * minuteLength;
setcolor(WHITE);
setlinestyle(PS_SOLID, 4);
line(centX, centY, minuteEndX, minuteEndY);
hourAngle = TI.wHour * 2 * PI / 12;
hourEndX = centX + sin(hourAngle) * hourLength;
hourEndY = centY - cos(hourAngle) * hourLength;
setcolor(WHITE);
setlinestyle(PS_SOLID, 8);
line(centX, centY, hourEndX, hourEndY);
FlushBatchDraw();
setcolor(BLACK);
line(centX, centY, secondEndX, secondEndY);
line(centX, centY, minuteEndX, minuteEndY);
line(centX, centY, hourEndX, hourEndY);
if (TI.wSecond == 10 || TI.wSecond == 50)//时间设置
{
Beep(300, 3000);
Sleep(30);
}
}
EndBatchDraw();
_getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
return 0;
}
转载自原文链接, 如需删除请联系管理员。
原文链接:C++ 航海钟(到点自动敲钟),转载请注明来源!