利用easyx在c语言代码下实现和现在时间完全相同的钟表
easyx软件可以直接搜,在官网上进行下载。
只能在windows平台下实现
代码如下:
game.h:
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
#define HIGH 480
#define WIDTH 640
#define PI 3.1415926
test.cpp:
#include "game.h"
int main()
{
initgraph(WIDTH,HIGH);
int center_x = WIDTH / 2;
int center_y = HIGH / 2;
int secondLength = WIDTH / 5;
int secondEnd_x;
int secondEnd_y;
float secondAngle = 0;
int minuteLength = WIDTH / 6;
int minuteEnd_x;
int minuteEnd_y;
float minuteAngle = 0;
int hourLength = WIDTH / 10;
int hourEnd_x;
int hourEnd_y;
float hourAngle = 0;
SYSTEMTIME ti;
setcolor(WHITE);
circle(center_x, center_y, WIDTH / 4);
BeginBatchDraw();
while (1)
{
GetLocalTime(&ti);
secondAngle = ti.wSecond * 2 * PI / 60;
minuteAngle = ti.wMinute * 2 * PI / 60;
hourAngle = ti.wHour * 2 * PI / 60;
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);
setcolor(WHITE);
line(center_x, center_y, secondEnd_x, secondEnd_y);
setcolor(YELLOW);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
setcolor(RED);
line(center_x, center_y, hourEnd_x, hourEnd_y);
FlushBatchDraw();
Sleep(50);
setcolor(BLACK);
line(center_x, center_y, secondEnd_x, secondEnd_y);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
line(center_x, center_y, hourEnd_x, hourEnd_y);
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}