如果在C语言编译器里面没有安装第三方图像库, 是无法编译通过的, 如此请看我第一篇文章<基于C&C++推荐图形库easyX>, 介绍了安装配置方法
废话不多说, 直接上代码:
#include <graphics.h>
#include <conio.h>
#include <iostream>
using namespace std;
const int WIDTH = 640;
const int HEIGHT = 480;
void DrawLine(int a, int b, int c, int d); //实现画一条线
typedef void(*DLine)(int, int, int, int); //自定义函数指针
int length = 100; //定义画线长度
struct LinePosi //定义六个点
{
int point1_x, point1_y;
int point2_x, point2_y;
int point3_x, point3_y;
int point4_x, point4_y;
int point5_x, point5_y;
int point6_x, point6_y;
}LinePosi;
struct Line //定义函数指针
{
DLine line1, line2, line3, line4, line5, line6, line7;
}Line;
void Init() //初始化点的位置
{
LinePosi.point1_x = 210;
LinePosi.point1_y = 100;
LinePosi.point2_x = LinePosi.point1_x + length;
LinePosi.point2_y = LinePosi.point1_y;
LinePosi.point3_x = LinePosi.point1_x;
LinePosi.point3_y = LinePosi.point1_y + length;
LinePosi.point4_x = LinePosi.point2_x;
LinePosi.point4_y = LinePosi.point2_y + length;
LinePosi.point5_x = LinePosi.point3_x;
LinePosi.point5_y = LinePosi.point3_y + length;
LinePosi.point6_x = LinePosi.point4_x;
LinePosi.point6_y = LinePosi.point4_y + length;
Line.line1 = DrawLine; //7个函数指针指向画线函数
Line.line2 = DrawLine;
Line.line3 = DrawLine;
Line.line4 = DrawLine;
Line.line5 = DrawLine;
Line.line6 = DrawLine;
Line.line7 = DrawLine;
initgraph(WIDTH, HEIGHT);
}
void Show()
{
int choose = 1;
while(choose != -1)
{
cin >> choose; //接收一个数字,按enter后画出这个数字
switch(choose)
{
case 0: //画出0
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 1: //画1
cleardevice();
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
break;
case 2:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 3:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 4:
cleardevice();
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 5:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 6:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 7:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 8:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case 9:
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
break;
case -1: //按-1退出
choose = -1;
break;
}
}
}
void DrawLine(int a, int b, int c, int d) //画线
{
setlinestyle(PS_SOLID, 10);
setlinecolor(RED);
line(a, b, c, d);
}
void CountDown() //倒计时
{
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000); //延迟1000毫秒, 也就是1秒
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line4(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
Sleep(1000);
cleardevice();
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Sleep(1000);
cleardevice();
Line.line1(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point2_x, LinePosi.point2_y);
Line.line2(LinePosi.point1_x, LinePosi.point1_y, LinePosi.point3_x, LinePosi.point3_y);
Line.line3(LinePosi.point2_x, LinePosi.point2_y, LinePosi.point4_x, LinePosi.point4_y);
Line.line5(LinePosi.point3_x, LinePosi.point3_y, LinePosi.point5_x, LinePosi.point5_y);
Line.line6(LinePosi.point4_x, LinePosi.point4_y, LinePosi.point6_x, LinePosi.point6_y);
Line.line7(LinePosi.point5_x, LinePosi.point5_y, LinePosi.point6_x, LinePosi.point6_y);
// getch();
}
int main()
{
Init();
CountDown();
Show();
return 0;
}
/*
此函数运行之后从9开始一直倒数到0, 之后你按个1, 再按enter,会在屏幕画出1, 其他数字都是如此, 不过仅限于0-9
*/
运行结果如图,这里是截取了一张,:
代码原创, 切勿进行任何商业用途, 只限于学习交流 !
网友评论