EasyX太空人表盘
- 既然买不起,就自己动手画一个太空人表盘
先上图
1.绘制圆盘
2.绘制文字
3.绘制动画
4.绘制动态时间
5.音乐播放
6.窗口去标题栏以及边框
- 初始化一个窗口
initgraph(WIDTH, HEIGHT); //窗口宽高为500
setbkcolor(RGB(223, 230, 240)); //背景色设置
cleardevice();
- 绘制圆盘
//绘制表盘
void drawCircle()
{
setlinestyle(PS_SOLID, 20); //实线,线宽20
setfillcolor(RGB(223, 230, 240));
fillcircle(WIDTH/2, HEIGHT/2, RADIUS); //填充圆
setlinecolor(RGB(49, 49, 49));
}
//绘制表盘内部线条
void drawLine()
{
setlinestyle(PS_SOLID, 4);
line(WIDTH / 2 - 40, 50, WIDTH / 2 - 40, 150);
line(WIDTH / 2 - RADIUS+30, HEIGHT / 2 - 100, WIDTH / 2 + RADIUS-30, HEIGHT / 2 - 100);
line(WIDTH / 2 - RADIUS + 30, HEIGHT / 2 + 100, WIDTH / 2 + RADIUS - 30, HEIGHT / 2 + 100);
line(WIDTH / 2 + 50, HEIGHT / 2 + 100, WIDTH / 2 + 50, HEIGHT / 2 + 140);
line(WIDTH / 2 + 50, HEIGHT / 2 + 140, WIDTH / 2 - 50, HEIGHT / 2 + 140);
line(WIDTH / 2 - 50, HEIGHT / 2 + 140, WIDTH / 2 - 50, HEIGHT / 2 + 190);
}
- 绘制基本文字及图片
//绘制左上角火箭以及文字
void drawLeftTop()
{
char buff[6] = { 0 };
setTextStyle(30, 20, "Arial"); //自定义函数
sprintf(buff, "%d%%", 90);
outtextxy(WIDTH / 2 - 130, HEIGHT / 2 - 130, buff);
putimage(WIDTH / 2 - 83, HEIGHT / 2 - 170, img + 5);
}
//绘制右上角文字以及图片
void drawRightTop()
{
char buff1[20] = { 0 };
char buff2[20] = { 0 };
sprintf(buff1, "晴天 %d°", 26);
sprintf(buff2, "%d°C %d°", 25,17);
setTextStyle(20, 12, "Arial");
outtextxy(WIDTH / 2 - 30, HEIGHT / 2 - 170, "空气良好");
setTextStyle(18, 10, "Arial");
outtextxy(WIDTH / 2 - 30, HEIGHT / 2 - 150, buff1);
outtextxy(WIDTH / 2 - 30, HEIGHT / 2 - 130, buff2);
putimage(WIDTH / 2 + 60, HEIGHT / 2 - 150, &img[4]);
putimage(WIDTH / 2 + 60, HEIGHT / 2 - 125, &img[3]);
putimage(WIDTH / 2 + 85, HEIGHT / 2 - 142, &img[1]);
}
//绘制左下角文字以及图片
void drawLeftDown()
{
char buff[10] = { 0 };
setTextStyle(25, 15, "Arial");
outtextxy(WIDTH / 2 - 130, HEIGHT / 2 + 110, "睡眠");
setTextStyle(23, 15, "Arial");
sprintf(buff, "%dh%dm", 7, 30);
outtextxy(WIDTH / 2 - 60, HEIGHT / 2 + 110, buff);
}
//绘制右下角文字以及图片
void drawRightDown()
{
char buff[10] = { 0 };
setTextStyle(25, 15, "Arial");
outtextxy(WIDTH / 2 + 60, HEIGHT / 2 + 110, "距离");
setTextStyle(23, 15, "Arial");
sprintf(buff, "%.2fkm", 9.88);
outtextxy(WIDTH / 2 - 40, HEIGHT / 2 + 150, buff);
}
//绘制心图片以及文字
void drawHeart()
{
char buff[10] = { 0 };
setTextStyle(20, 10, "Arial");
sprintf(buff, "%d ~ %d", 80, 128);
outtextxy(WIDTH / 2 - 170, HEIGHT / 2 + 20, buff);
putimage(WIDTH / 2 - 160, HEIGHT / 2 + 45, &img[0]);
setTextStyle(35, 20, "Arial");
sprintf(buff, "%d", 92);
outtextxy(WIDTH / 2 - 105, HEIGHT / 2 + 60, buff);
}
//绘制运动图片以及文字
void drawSportSteps()
{
putimage(WIDTH / 2 + 70, HEIGHT / 2 + 60, &img[2]);
char buff[10] = { 0 };
setTextStyle(25, 10, "Arial");
sprintf(buff, "%d", 9527);
outtextxy(WIDTH / 2 + 110, HEIGHT / 2 + 70, buff);
}
- 绘制太空人动画
IMAGE spaceMan[59]; //太空人图片
IMAGE img[6]; //其它图片
//加载图片
void loadImg()
{
char fileName[30] = "\0";
//加载图片
for (int i = 0; i < 59; i++)
{
sprintf(fileName, "./images/human (%d).jpg", i + 2);
loadimage(&spaceMan[i], fileName, 120, 120);
}
//加载心
loadimage(&img[0], "./images/other1.jpg", 50, 50);
//加载右上角图片
loadimage(&img[1], "./images/other2.jpg", 40, 40);
loadimage(&img[2], "./images/other3.jpg", 35, 35);
loadimage(&img[3], "./images/other4.jpg", 20, 20);
//加载鞋子
loadimage(&img[4], "./images/other5.jpg", 20, 20);
//加载火箭
loadimage(&img[5], "./images/other6.jpg", 40, 40);
}
//动画输出
void putImg()
{
//时间差解决绘制快慢问题(控制图片的切换速度)
static DWORD t1 = clock();
DWORD t2 = clock();
static int i = 0;
if (t2 - t1 > 20)
{
i = (i + 1) % 59;
t1 = t2;
}
putimage(WIDTH / 2 - 60, HEIGHT / 2 - 40, spaceMan + i);
}
- 绘制动态的时间
//将数字日期转换为汉字日期
char* changeDate(int mon, int day)
{
static char buff[20] = { 0 };
switch (mon + 1)
{
case 1:
switch (day)
{
case 1:
strcpy(buff, "一月一日");
break;
case 2:
strcpy(buff, "一月二日");
break;
case 3:
strcpy(buff, "一月三日");
break;
case 4:
strcpy(buff, "一月四日");
break;
case 5:
strcpy(buff, "一月五日");
break;
case 6:
strcpy(buff, "一月六日");
break;
case 7:
strcpy(buff, "一月七日");
break;
case 8:
strcpy(buff, "一月八日");
break;
case 9:
strcpy(buff, "一月九日");
break;
case 10:
strcpy(buff, "一月十日");
break;
case 11:
strcpy(buff, "一月十一日");
break;
case 12:
strcpy(buff, "一月十二日");
break;
case 13:
strcpy(buff, "一月十三日");
break;
case 14:
strcpy(buff, "一月十四日");
break;
case 15:
strcpy(buff, "一月十五日");
break;
case 16:
strcpy(buff, "一月十六日");
break;
case 17:
strcpy(buff, "一月十七日");
break;
case 18:
strcpy(buff, "一月十八日");
break;
case 19:
strcpy(buff, "一月十九日");
break;
case 20:
strcpy(buff, "一月二十日");
break;
case 21:
strcpy(buff, "一月二十一日");
break;
case 22:
strcpy(buff, "一月二十二日");
break;
case 23:
strcpy(buff, "一月二十三日");
break;
case 24:
strcpy(buff, "一月二十四日");
break;
case 25:
strcpy(buff, "一月二十五日");
break;
case 26:
strcpy(buff, "一月二十六日");
break;
case 27:
strcpy(buff, "一月二十七日");
break;
case 28:
strcpy(buff, "一月二十八日");
break;
case 29:
strcpy(buff, "一月二十九日");
break;
case 30:
strcpy(buff, "一月三十日");
break;
case 31:
strcpy(buff, "一月三十一日");
break;
default:
break;
}
break;
//此处省略......
}
return buff;
}
//将数字星期转换为汉字星期
char* changeWinday(int wday)
{
char cWday[10] = { 0 };
switch (wday)
{
case 0:
strcpy(cWday,"星期日");
break;
case 1:
strcpy(cWday, "星期一");
break;
case 2:
strcpy(cWday, "星期二");
break;
case 3:
strcpy(cWday, "星期三");
break;
case 4:
strcpy(cWday, "星期四");
break;
case 5:
strcpy(cWday, "星期五");
break;
case 6:
strcpy(cWday, "星期六");
break;
default:
break;
}
return cWday;
}
void drawTime()
{
//获取当前时间
time_t curTime = time(NULL);
//将时间戳转换为本地时间
tm* humanTime = localtime(&curTime);
char buff[20] = { 0 };
if (humanTime->tm_min < 10)
{
sprintf(buff, "%d:0%d", humanTime->tm_hour, humanTime->tm_min);
}
else
{
sprintf(buff, "%d:%d", humanTime->tm_hour, humanTime->tm_min);
}
setTextStyle(65, 40, "Arial");
outtextxy(100,152,buff);
if (humanTime->tm_sec < 10)
{
sprintf(buff, "0%d", humanTime->tm_sec);
}
else
{
sprintf(buff, "%d", humanTime->tm_sec);
}
setTextStyle(50, 20, "Arial");
outtextxy(WIDTH/2 + 80, HEIGHT/2 - 70, buff);
//绘制几月几日
sprintf(buff, "%s", changeDate(humanTime->tm_mon, humanTime->tm_mday));
setTextStyle(18, 6, "Arial");
outtextxy(WIDTH / 2 + 70, HEIGHT / 2 - 10, buff);
//绘制星期几
sprintf(buff, "%s", changeWinday(humanTime->tm_wday));
setTextStyle(20, 10, "Arial");
outtextxy(WIDTH / 2 + 70, HEIGHT / 2 + 20, buff);
sprintf(buff, "%d-%d", humanTime->tm_mon + 1,humanTime->tm_mday);
setTextStyle(20, 10, "Arial");
outtextxy(WIDTH / 2 + 140, HEIGHT / 2 + 20, buff);
}
- 音乐播放
#include<mmsystem.h> //添加音视频播放头文件
#pragma comment(lib,"winmm.lib") //连接音频播放库
//打开音乐
mciSendString("open ./images/风儿吹.mp3 alias BGM", NULL, NULL, NULL);
//播放音乐
mciSendString("play BGM", NULL, NULL, NULL);
- 去除标题栏以及边框
// 去掉标题
SetWindowLong(GetHWnd(), GWL_STYLE, GetWindowLong(GetHWnd(), GWL_STYLE) & ~WS_CAPTION);
// 初始化界面为圆形
SetWindowRgn(GetHWnd(), CreateEllipticRgn(40, 40, WIDTH - 40, HEIGHT - 40), true);
整体代码
#define _CRT_SECURE_NO_WARNINGS
#include<graphics.h>
#include<cstdio>
#include<cstring>
#include<conio.h>
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define WIDTH 500
#define HEIGHT 500
#define RADIUS 200
IMAGE spaceMan[59]; //太空人图片
IMAGE img[6]; //其它图片
//加载图片
void loadImg()
{
char fileName[30] = "\0";
//加载图片
for (int i = 0; i < 59; i++)
{
sprintf(fileName, "./images/human (%d).jpg", i + 2);
loadimage(&spaceMan[i], fileName, 120, 120);
}
//加载心
loadimage(&img[0], "./images/other1.jpg", 50, 50);
//加载右上角图片
loadimage(&img[1], "./images/other2.jpg", 40, 40);
loadimage(&img[2], "./images/other3.jpg", 35, 35);
loadimage(&img[3], "./images/other4.jpg", 20, 20);
//加载鞋子
loadimage(&img[4], "./images/other5.jpg", 20, 20);
//加载火箭
loadimage(&img[5], "./images/other6.jpg", 40, 40);
}
//设置文字样式
void setTextStyle(int h,int w,const char* faceName)
{
LOGFONT font = { 0 };
font.lfHeight = h;
font.lfWidth = w;
strcpy(font.lfFaceName, faceName);
settextstyle(&font);
settextcolor(RGB(0, 2, 4));
}
//绘制表盘
void drawCircle()
{
setlinestyle(PS_SOLID, 20);
setfillcolor(RGB(223, 230, 240));
fillcircle(WIDTH/2, HEIGHT/2, RADIUS);
setlinecolor(RGB(49, 49, 49));
}
//绘制内部线条
void drawLine()
{
setlinestyle(PS_SOLID, 4);
line(WIDTH / 2 - 40, 50, WIDTH / 2 - 40, 150);
line(WIDTH / 2 - RADIUS+30, HEIGHT / 2 - 100, WIDTH / 2 + RADIUS-30, HEIGHT / 2 - 100);
line(WIDTH / 2 - RADIUS + 30, HEIGHT / 2 + 100, WIDTH / 2 + RADIUS - 30, HEIGHT / 2 + 100);
line(WIDTH / 2 + 50, HEIGHT / 2 + 100, WIDTH / 2 + 50, HEIGHT / 2 + 140);
line(WIDTH / 2 + 50, HEIGHT / 2 + 140, WIDTH / 2 - 50, HEIGHT / 2 + 140);
line(WIDTH / 2 - 50, HEIGHT / 2 + 140, WIDTH / 2 - 50, HEIGHT / 2 + 190);
}
//绘制左上角火箭
void drawLeftTop()
{
char buff[6] = { 0 };
setTextStyle(30, 20, "Arial");
sprintf(buff, "%d%%", 90);
outtextxy(WIDTH / 2 - 130, HEIGHT / 2 - 130, buff);
putimage(WIDTH / 2 - 83, HEIGHT / 2 - 170, img + 5);
}
//绘制右上角文字
void drawRightTop()
{
char buff1[20] = { 0 };
char buff2[20] = { 0 };
sprintf(buff1, "晴天 %d°", 26);
sprintf(buff2, "%d°C %d°", 25,17);
setTextStyle(20, 12, "Arial");
outtextxy(WIDTH / 2 - 30, HEIGHT / 2 - 170, "空气良好");
setTextStyle(18, 10, "Arial");
outtextxy(WIDTH / 2 - 30, HEIGHT / 2 - 150, buff1);
outtextxy(WIDTH / 2 - 30, HEIGHT / 2 - 130, buff2);
putimage(WIDTH / 2 + 60, HEIGHT / 2 - 150, &img[4]);
putimage(WIDTH / 2 + 60, HEIGHT / 2 - 125, &img[3]);
putimage(WIDTH / 2 + 85, HEIGHT / 2 - 142, &img[1]);
}
//绘制左下角
void drawLeftDown()
{
char buff[10] = { 0 };
setTextStyle(25, 15, "Arial");
outtextxy(WIDTH / 2 - 130, HEIGHT / 2 + 110, "睡眠");
setTextStyle(23, 15, "Arial");
sprintf(buff, "%dh%dm", 7, 30);
outtextxy(WIDTH / 2 - 60, HEIGHT / 2 + 110, buff);
}
//绘制右下角
void drawRightDown()
{
char buff[10] = { 0 };
setTextStyle(25, 15, "Arial");
outtextxy(WIDTH / 2 + 60, HEIGHT / 2 + 110, "距离");
setTextStyle(23, 15, "Arial");
sprintf(buff, "%.2fkm", 9.88);
outtextxy(WIDTH / 2 - 40, HEIGHT / 2 + 150, buff);
}
//绘制心跳
void drawHeart()
{
char buff[10] = { 0 };
setTextStyle(20, 10, "Arial");
sprintf(buff, "%d ~ %d", 80, 128);
outtextxy(WIDTH / 2 - 170, HEIGHT / 2 + 20, buff);
putimage(WIDTH / 2 - 160, HEIGHT / 2 + 45, &img[0]);
setTextStyle(35, 20, "Arial");
sprintf(buff, "%d", 92);
outtextxy(WIDTH / 2 - 105, HEIGHT / 2 + 60, buff);
}
//绘制运动步数
void drawSportSteps()
{
putimage(WIDTH / 2 + 70, HEIGHT / 2 + 60, &img[2]);
char buff[10] = { 0 };
setTextStyle(25, 10, "Arial");
sprintf(buff, "%d", 9527);
outtextxy(WIDTH / 2 + 110, HEIGHT / 2 + 70, buff);
}
//动画输出
void putImg()
{
static DWORD t1 = clock();
DWORD t2 = clock();
static int i = 0;
if (t2 - t1 > 20)
{
i = (i + 1) % 59;
t1 = t2;
}
putimage(WIDTH / 2 - 60, HEIGHT / 2 - 40, spaceMan + i);
}
//将数字日期转换为汉字日期
char* changeDate(int mon, int day)
{
static char buff[20] = { 0 };
switch (mon + 1)
{
case 1:
switch (day)
{
case 1:
strcpy(buff, "一月一日");
break;
case 2:
strcpy(buff, "一月二日");
break;
case 3:
strcpy(buff, "一月三日");
break;
case 4:
strcpy(buff, "一月四日");
break;
case 5:
strcpy(buff, "一月五日");
break;
case 6:
strcpy(buff, "一月六日");
break;
case 7:
strcpy(buff, "一月七日");
break;
case 8:
strcpy(buff, "一月八日");
break;
case 9:
strcpy(buff, "一月九日");
break;
case 10:
strcpy(buff, "一月十日");
break;
case 11:
strcpy(buff, "一月十一日");
break;
case 12:
strcpy(buff, "一月十二日");
break;
case 13:
strcpy(buff, "一月十三日");
break;
case 14:
strcpy(buff, "一月十四日");
break;
case 15:
strcpy(buff, "一月十五日");
break;
case 16:
strcpy(buff, "一月十六日");
break;
case 17:
strcpy(buff, "一月十七日");
break;
case 18:
strcpy(buff, "一月十八日");
break;
case 19:
strcpy(buff, "一月十九日");
break;
case 20:
strcpy(buff, "一月二十日");
break;
case 21:
strcpy(buff, "一月二十一日");
break;
case 22:
strcpy(buff, "一月二十二日");
break;
case 23:
strcpy(buff, "一月二十三日");
break;
case 24:
strcpy(buff, "一月二十四日");
break;
case 25:
strcpy(buff, "一月二十五日");
break;
case 26:
strcpy(buff, "一月二十六日");
break;
case 27:
strcpy(buff, "一月二十七日");
break;
case 28:
strcpy(buff, "一月二十八日");
break;
case 29:
strcpy(buff, "一月二十九日");
break;
case 30:
strcpy(buff, "一月三十日");
break;
case 31:
strcpy(buff, "一月三十一日");
break;
default:
break;
}
break;
//此处省略很多字......
case 12:
switch (day)
{
case 1:
strcpy(buff, "十二月一日");
break;
case 2:
strcpy(buff, "十二月二日");
break;
case 3:
strcpy(buff, "十二月三日");
break;
case 4:
strcpy(buff, "十二月四日");
break;
case 5:
strcpy(buff, "十二月五日");
break;
case 6:
strcpy(buff, "十二月六日");
break;
case 7:
strcpy(buff, "十二月七日");
break;
case 8:
strcpy(buff, "十二月八日");
break;
case 9:
strcpy(buff, "十二月九日");
break;
case 10:
strcpy(buff, "十二月十日");
break;
case 11:
strcpy(buff, "十二月十一日");
break;
case 12:
strcpy(buff, "十二月十二日");
break;
case 13:
strcpy(buff, "十二月十三日");
break;
case 14:
strcpy(buff, "十二月十四日");
break;
case 15:
strcpy(buff, "十二月十五日");
break;
case 16:
strcpy(buff, "十二月十六日");
break;
case 17:
strcpy(buff, "十二月十七日");
break;
case 18:
strcpy(buff, "十二月十八日");
break;
case 19:
strcpy(buff, "十二月十九日");
break;
case 20:
strcpy(buff, "十二月二十日");
break;
case 21:
strcpy(buff, "十二月二十一日");
break;
case 22:
strcpy(buff, "十二月二十二日");
break;
case 23:
strcpy(buff, "十二月二十三日");
break;
case 24:
strcpy(buff, "十二月二十四日");
break;
case 25:
strcpy(buff, "十二月二十五日");
break;
case 26:
strcpy(buff, "十二月二十六日");
break;
case 27:
strcpy(buff, "十二月二十七日");
break;
case 28:
strcpy(buff, "十二月二十八日");
break;
case 29:
strcpy(buff, "十二月二十九日");
break;
case 30:
strcpy(buff, "十二月三十日");
break;
case 31:
strcpy(buff, "十二月三十一日");
break;
default:
break;
}
break;
default:
break;
}
return buff;
}
//将数字星期转换为汉字星期
char* changeWinday(int wday)
{
char cWday[10] = { 0 };
switch (wday)
{
case 0:
strcpy(cWday,"星期日");
break;
case 1:
strcpy(cWday, "星期一");
break;
case 2:
strcpy(cWday, "星期二");
break;
case 3:
strcpy(cWday, "星期三");
break;
case 4:
strcpy(cWday, "星期四");
break;
case 5:
strcpy(cWday, "星期五");
break;
case 6:
strcpy(cWday, "星期六");
break;
default:
break;
}
return cWday;
}
void drawTime()
{
//获取当前时间
time_t curTime = time(NULL);
tm* humanTime = localtime(&curTime);
char buff[20] = { 0 };
if (humanTime->tm_min < 10)
{
sprintf(buff, "%d:0%d", humanTime->tm_hour, humanTime->tm_min);
}
else
{
sprintf(buff, "%d:%d", humanTime->tm_hour, humanTime->tm_min);
}
setTextStyle(65, 40, "Arial");
outtextxy(100,152,buff);
if (humanTime->tm_sec < 10)
{
sprintf(buff, "0%d", humanTime->tm_sec);
}
else
{
sprintf(buff, "%d", humanTime->tm_sec);
}
setTextStyle(50, 20, "Arial");
outtextxy(WIDTH/2 + 80, HEIGHT/2 - 70, buff);
//绘制几月几日
sprintf(buff, "%s", changeDate(humanTime->tm_mon, humanTime->tm_mday));
setTextStyle(18, 6, "Arial");
outtextxy(WIDTH / 2 + 70, HEIGHT / 2 - 10, buff);
//绘制星期几
sprintf(buff, "%s", changeWinday(humanTime->tm_wday));
setTextStyle(20, 10, "Arial");
outtextxy(WIDTH / 2 + 70, HEIGHT / 2 + 20, buff);
sprintf(buff, "%d-%d", humanTime->tm_mon + 1,humanTime->tm_mday);
setTextStyle(20, 10, "Arial");
outtextxy(WIDTH / 2 + 140, HEIGHT / 2 + 20, buff);
}
int main()
{
initgraph(WIDTH, HEIGHT);
// 去掉标题
SetWindowLong(GetHWnd(), GWL_STYLE, GetWindowLong(GetHWnd(), GWL_STYLE) & ~WS_CAPTION);
// 初始化界面为圆形
SetWindowRgn(GetHWnd(), CreateEllipticRgn(40, 40, WIDTH - 40, HEIGHT - 40), true);
setbkcolor(RGB(223, 230, 240));
cleardevice();
loadImg(); //加载图片
BeginBatchDraw(); //双缓冲绘图
//播放音乐
mciSendString("open ./images/风儿吹.mp3 alias BGM", NULL, NULL, NULL);
mciSendString("play BGM", NULL, NULL, NULL);
while (1)
{
drawCircle(); //绘制圆盘
drawLine(); //绘制线条
drawLeftTop(); //绘制左上角
drawRightTop(); //绘制右上角
drawLeftDown(); //绘制左下角
drawRightDown(); //绘制右下角
drawHeart(); //绘制心跳
drawSportSteps(); //绘制运动步数
putImg(); //绘制太空人动画
drawTime(); //绘制时间
FlushBatchDraw();
}
//关闭音乐
mciSendString("close BGM", NULL, NULL, NULL);
closegraph();
return 0;
}