设计程序,画一个中国象棋棋盘,配上你喜欢的底色,中间写上“楚河汉界”四字。
选做:找一个象棋残局画出来
先附上效果图:
运行环境:vc 6.0,含EasyX库
源代码如下:
#include <graphics.h>
#include <conio.h>
#define ge 50 //1格50*50
void main() {
initgraph(480,640);
setbkcolor(RGB(156,125,18)); //设置背景色
cleardevice(); //再用背景色清空屏幕
setcolor(BLACK);
int X1 = (480 - 8 * ge) / 2,Y1 = (640 - 9 * ge) / 2;
int X2 = 480 - (480 - 8 * ge) / 2,Y2 = 640 - (640 - 9 * ge) / 2;
rectangle(X1,Y1,X2,Y2);
rectangle(X1 - 20,Y1- 20,X2 + 20,Y2 + 20); //画外框
//画横纵线
for (int i = 1; i < 9; i++) {
line(X1,Y1 + ge * i,X2,Y1 + ge * i);
}
for(int j = 1; j < 8; j++) {
line(X1 + ge * j,Y1,X1 + ge * j,Y1 + 4 * ge);
line(X1 + ge * j,Y1 + ge * 5,X1 + ge * j,Y2);
}
//画斜线
line(X1 + 3 * ge,Y1,X1 + 5 * ge,Y1 + 2 * ge);
line(X1 + 3 * ge,Y1 + 2 * ge,X1 + 5 * ge,Y1);
line(X1 + 3 * ge,Y2 - 2 * ge,X1 + 5 * ge,Y2);
line(X1 + 5 * ge,Y2 - 2 * ge,X1 + 3 * ge,Y2);
//画4个小折线
for (int a = 0; a < 2;a++){
line(X1 + ge - 15,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + ge - 5,Y1 + 2 * ge - 5 + 5 * ge * a);
line(X1 + ge - 15 + 6 * ge,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + ge - 5 + 6 * ge,Y1 + 2 * ge - 5 + 5 * ge * a);
line(X1 + ge - 5,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + ge - 5,Y1 + 2 * ge - 15 + 5 * ge * a);
line(X1 + 7 * ge - 5,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + 7 * ge - 5,Y1 + 2 * ge - 15 + 5 * ge * a);
line(X1 + ge + 5,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + ge + 15,Y1 + 2 * ge - 5 + 5 * ge * a);
line(X1 + 7 * ge + 5,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + 7 * ge + 15,Y1 + 2 * ge - 5 + 5 * ge * a);
line(X1 + ge + 5,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + ge + 5,Y1 + 2 * ge - 15 + 5 * ge * a);
line(X1 + 7 * ge + 5,Y1 + 2 * ge - 5 + 5 * ge * a,X1 + 7 * ge + 5,Y1 + 2 * ge - 15 + 5 * ge * a);
line(X1 + ge - 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + ge - 15,Y1 + 2 * ge + 5 + 5 * ge * a);
line(X1 + 7 * ge - 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + 7 * ge - 15,Y1 + 2 * ge + 5 + 5 * ge * a);
line(X1 + ge - 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + ge - 5,Y1 + 2 * ge + 15 + 5 * ge * a);
line(X1 + 7 * ge - 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + 7 * ge - 5,Y1 + 2 * ge + 15 + 5 * ge * a);
line(X1 + ge + 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + ge + 15,Y1 + 2 * ge + 5 + 5 * ge * a);
line(X1 + 7 * ge + 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + 7 * ge + 15,Y1 + 2 * ge + 5 + 5 * ge * a);
line(X1 + ge + 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + ge + 5,Y1 + 2 * ge + 15 + 5 * ge * a);
line(X1 + 7 * ge + 5,Y1 + 2 * ge + 5 + 5 * ge * a,X1 + 7 * ge + 5,Y1 + 2 * ge + 15 + 5 * ge * a);
}
settextstyle(40, 0, _T("楷体"),0,1800,0,false,false,false);
outtextxy(X1 + 5 * ge, Y1 + 4 * ge + 5 + 40, _T("界 汉"));
settextstyle(40, 0, _T("楷体"),0,0,0,false,false,false);
outtextxy(X1 + ge, Y1 + 4 * ge + 5, _T("楚 河"));
//画棋子
setfillcolor(RGB(207,176,118)); //设置填充色
fillcircle(X1,Y1 + 3 * ge,15);
circle(X1,Y1 + 3 * ge,13);
fillcircle(X1 + 4 * ge,Y1,15);
circle(X1 + 4 * ge,Y1,13);
fillcircle(X1 + 4 * ge,Y1 + 9 * ge,15);
circle(X1 + 4 * ge,Y1 + 9 * ge,13);
fillcircle(X1 + 2 * ge,Y1 + 2 * ge,15);
circle(X1 + 2 * ge,Y1 + 2 * ge,13);
fillcircle(X1 + 3 * ge,Y1 + 3 * ge,15);
circle(X1 + 3 * ge,Y1 + 3 * ge,13);
fillcircle(X1 + 4 * ge,Y2 - 2 * ge,15);
circle(X1 + 4 * ge,Y2 - 2 * ge,13);
fillcircle(X1 + 3 * ge,Y2,15);
circle(X1 + 3 * ge,Y2,13);
fillcircle(X2 - ge,Y1 + 3 * ge,15);
circle(X2 - ge,Y1 + 3 * ge,13);
fillcircle(X1 + 2 * ge,Y2 - 2 * ge,15);
circle(X1 + 2 * ge,Y2 - 2 * ge,13);
setcolor(RED);
setbkcolor(RGB(207,176,118));
settextstyle(20, 0, _T("楷体"),0,1800,0,false,false,false);
outtextxy(X1 - 10,Y1 + 3 * ge - 10 + 20,_T("卒"));
outtextxy(X1 + 4 * ge - 10,Y1 - 10 + 20,_T("帥"));
outtextxy(X1 + 2 * ge - 10,Y1 + 2 * ge + 10,("車"));
outtextxy(X1 + 3 * ge - 10,Y1 + 3 * ge + 10,("炮"));
settextstyle(20, 0, _T("楷体"),0,0,0,false,false,false);
setcolor(BLACK);
outtextxy(X1 + 4 * ge - 10,Y1 + 9 * ge - 10,_T("将"));
outtextxy(X1 + 4 * ge - 10,Y2 - 2 * ge - 10,("象"));
outtextxy(X1 + 3 * ge - 10,Y2 - 10,("仕"));
outtextxy(X2 - ge - 10,Y1 + 3 * ge - 10,("車"));
outtextxy(X1 + 2 * ge - 10,Y2 - 2 * ge - 10,("馬"));
getch();
closegraph();
}