利用c语言编写,在vs2017上编译运行
废话不多说直接上完整代码
#include
#include "graphics.h" //图形库头文件
//五子棋
int main()
{
initgraph(600, 600); //打开图形库窗口
fillrectangle(20, 20, 580, 580); //画填充矩形
setlinestyle(0, 2); //设置画线颜色为黑色
setlinecolor(BLACK); //设置画线样式为实线,画线宽度为2像素
for (int i = 1; i <= 11; i++) //循环:用于做重复的事情
{
line(50, 50 * i, 550, 50 * i);
line(50 * i, 50, 50 * i, 550);
}
MOUSEMSG msg; //定义鼠标消息变量
int color = BLACK; //color代表当前棋子颜色(默认为黑色)
setfillcolor(BLACK);
while (1) //使用while循环重复获取鼠标消息
{
msg = GetMouseMsg(); //获取鼠标消息
if (msg.uMsg == WM_LBUTTONUP) //如果鼠标左键弹起则画圆
{
//鼠标点击位置以50为单位取整
int x = (msg.x + 25) / 50 * 50;
int y = (msg.y + 25) / 50 * 50;
if (x >= 50 && x <= 550 && y >= 50 && y <= 550)
{ //只能在50到550之间下棋
fillcircle(x, y, 23); //画填充圆
}
if (BLACK == color)
{ //如果棋子颜色为黑色,下次下白棋
setfillcolor(WHITE);
color = WHITE;
}
else if (WHITE == color)
{ //如果棋子颜色为白色,下次下黑棋
setfillcolor(BLACK);
color = BLACK;
}
}
}
getchar();
closegraph(); //关闭图形库窗口
return 0;
}
-------------------------------------------------------------------------------------------
运行效果图:
未来几天,我会深刻探究一下所用到的图形库的内容,期待更新。


