先上效果:
这是参照:
数字1
到9
是按照黑方视角,字母A
到J
是自己编号的。
chess.h
#pragma once
#include <easyx.h>
#include <conio.h>
#define WIDTH 650
#define HEIGHT 774
// A到J
#define CHESS_A 71
#define CHESS_B 141
#define CHESS_C 212
#define CHESS_D 283
#define CHESS_E 356
#define CHESS_F 426
#define CHESS_G 498
#define CHESS_H 569
#define CHESS_I 642
#define CHESS_J 713
// 1到9
#define CHESS_ONE 38
#define CHESS_TWO 110
#define CHESS_THREE 181
#define CHESS_FOUR 253
#define CHESS_FIVE 324
#define CHESS_SIX 395
#define CHESS_SEVEN 466
#define CHESS_EIGHT 537
#define CHESS_NINE 610
void draw_chessboard();// 画棋盘
void draw_line(int x, int y);// 画完整的兵、炮旁边的线,即画出四个方向的线
void draw_left_up_line(int x, int y);
void draw_right_up_line(int x, int y);
void draw_left_down_line(int x, int y);
void draw_right_down_line(int x, int y);
void centerText(int rx, int ry, int rw, int rh, const char* str);//画楚河汉界
draw_chessboard.cpp
#include "chess.h"
void draw_chessboard()
{
initgraph(WIDTH, HEIGHT, EX_SHOWCONSOLE);
setbkcolor(RGB(250, 225, 100));
cleardevice();
setlinecolor(BLACK);
// 最外边一层
setlinestyle(PS_SOLID, 6);
line(31, 62, 617, 62);
line(31, 62, 31, 720);
line(31, 720, 617, 720);
line(617, 62, 617, 720);
// 中间一层,横着的
setlinestyle(PS_SOLID, 3);
line(CHESS_ONE, CHESS_A, CHESS_NINE, CHESS_A);// A
line(CHESS_ONE, CHESS_J, CHESS_NINE, CHESS_J);// J
// 中间一层,竖着的
setlinestyle(PS_SOLID, 3);
line(CHESS_ONE, CHESS_A, CHESS_ONE, CHESS_J);// 1
line(CHESS_NINE, CHESS_A, CHESS_NINE, CHESS_J);// 9
// 内层横着的
setlinestyle(PS_SOLID, 2);
line(CHESS_ONE, CHESS_B, CHESS_NINE, CHESS_B);// B
line(CHESS_ONE, CHESS_C, CHESS_NINE, CHESS_C);// C
line(CHESS_ONE, CHESS_D, CHESS_NINE, CHESS_D);// D
line(CHESS_ONE, CHESS_E, CHESS_NINE, CHESS_E);// E
line(CHESS_ONE, CHESS_F, CHESS_NINE, CHESS_F);// F
line(CHESS_ONE, CHESS_G, CHESS_NINE, CHESS_G);// G
line(CHESS_ONE, CHESS_H, CHESS_NINE, CHESS_H);// H
line(CHESS_ONE, CHESS_I, CHESS_NINE, CHESS_I);// I
// 内层竖着的
setlinestyle(PS_SOLID, 2);
line(CHESS_TWO, CHESS_A, CHESS_TWO, CHESS_E);// 2
line(CHESS_THREE, CHESS_A, CHESS_THREE, CHESS_E);// 3
line(CHESS_FOUR, CHESS_A, CHESS_FOUR, CHESS_E);// 4
line(CHESS_FIVE, CHESS_A, CHESS_FIVE, CHESS_E);// 5
line(CHESS_SIX, CHESS_A, CHESS_SIX, CHESS_E);// 6
line(CHESS_SEVEN, CHESS_A, CHESS_SEVEN, CHESS_E);// 7
line(CHESS_EIGHT, CHESS_A, CHESS_EIGHT, CHESS_E);// 8
line(CHESS_TWO, CHESS_F, CHESS_TWO, CHESS_J);
line(CHESS_THREE, CHESS_F, CHESS_THREE, CHESS_J);
line(CHESS_FOUR, CHESS_F, CHESS_FOUR, CHESS_J);
line(CHESS_FIVE, CHESS_F, CHESS_FIVE, CHESS_J);
line(CHESS_SIX, CHESS_F, CHESS_SIX, CHESS_J);
line(CHESS_SEVEN, CHESS_F, CHESS_SEVEN, CHESS_J);
line(CHESS_EIGHT, CHESS_F, CHESS_EIGHT, CHESS_J);
//九宫
setlinestyle(PS_SOLID, 2);
line(CHESS_FOUR, CHESS_A, CHESS_SIX, CHESS_C);
line(CHESS_FOUR, CHESS_C, CHESS_SIX, CHESS_A);
line(CHESS_FOUR, CHESS_H, CHESS_SIX, CHESS_J);
line(CHESS_FOUR, CHESS_J, CHESS_SIX, CHESS_H);
// 炮和兵旁边的线
setlinestyle(PS_SOLID, 3);
draw_line(CHESS_TWO, CHESS_C);
draw_line(CHESS_EIGHT, CHESS_C);
draw_line(CHESS_TWO, CHESS_H);
draw_line(CHESS_EIGHT, CHESS_H);
draw_right_up_line(CHESS_ONE, CHESS_D);
draw_right_down_line(CHESS_ONE, CHESS_D);
draw_line(CHESS_THREE, CHESS_D);
draw_line(CHESS_FIVE, CHESS_D);
draw_line(CHESS_SEVEN, CHESS_D);
draw_left_up_line(CHESS_NINE, CHESS_D);
draw_left_down_line(CHESS_NINE, CHESS_D);
draw_right_up_line(CHESS_ONE, CHESS_G);
draw_right_down_line(CHESS_ONE, CHESS_G);
draw_line(CHESS_THREE, CHESS_G);
draw_line(CHESS_FIVE, CHESS_G);
draw_line(CHESS_SEVEN, CHESS_G);
draw_left_up_line(CHESS_NINE, CHESS_G);
draw_left_down_line(CHESS_NINE, CHESS_G);
// 楚河汉界
settextstyle((CHESS_F - CHESS_E) * 0.618, 0, TEXT("楷体"));
settextcolor(BLACK);
centerText(CHESS_ONE, CHESS_E, CHESS_NINE - CHESS_ONE, CHESS_F - CHESS_E , TEXT("楚河 汉界"));
}
void draw_line(int x, int y)
{
draw_left_up_line(x, y);
draw_right_up_line(x, y);
draw_left_down_line(x, y);
draw_right_down_line(x, y);
}
void draw_left_up_line(int x, int y)
{
line(x - 4, y - 4, x - 18, y - 4);
line(x - 4, y - 4, x - 4, y - 18);
}
void draw_right_up_line(int x, int y)
{
line(x + 4, y - 4, x + 18, y - 4);
line(x + 4, y - 4, x + 4, y - 18);
}
void draw_left_down_line(int x, int y)
{
line(x - 4, y + 4, x - 18, y + 4);
line(x - 4, y + 4, x - 4, y + 18);
}
void draw_right_down_line(int x, int y)
{
line(x + 4, y + 4, x + 18, y + 4);
line(x + 4, y + 4, x + 4, y + 18);
}
void centerText(int rx, int ry, int rw, int rh, const char* str)
{
int hSpace = (rw - textwidth(str)) / 2;
int vSpace = (rh - textheight(str)) / 2;
outtextxy(rx + hSpace, ry + vSpace, str);
}