作者:优美rosner_704 | 来源:互联网 | 2023-10-10 23:00
消除棋子小游戏
每次点击鼠标即可增加或消除以点击目标为中心的四周的棋子
#include // 引用图形库头文件
#include
#include
int main1()
{initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素MOUSEMSG m;int r;while (1){m = GetMouseMsg();switch (m.uMsg){case WM_MOUSEMOVE:setcolor(RGB(rand() % 256, rand() % 256, rand() % 256));if (m.mkLButton){circle(m.x, m.y, rand() % 91 + 10);}else if (m.mkRButton){r = rand() % 91 + 10;rectangle(m.x - r, m.y - r, m.x + r, m.y + r);}break;}}_getch(); // 按任意键继续closegraph(); // 关闭绘图窗口
}const int BoardW = 15;
const int BoardH = 15;
const int GridSz = 30;
const int Edge = 30;void drawGrid()
{setbkcolor(RGB(156, 162, 232));cleardevice();setcolor(RGB(0, 0, 0));for (int i &#61; 0; i <&#61; BoardH; &#43;&#43;i){line(Edge, Edge &#43; i * GridSz,Edge &#43; BoardW * GridSz, Edge &#43; i * GridSz);}for (int i &#61; 0; i <&#61; BoardH; &#43;&#43;i){line(Edge &#43; i * GridSz, Edge,Edge &#43; i * GridSz, Edge &#43; BoardH * GridSz);}
}void drawLayout(int layout[BoardH][BoardW])
{setcolor(RGB(0, 201, 14));setfillcolor(RGB(0, 201, 14));int r &#61; GridSz * 4 / 10;int x, y;for (int i &#61; 0; i }void pos2Index(int x, int y, int* pi, int* pj)
{if (x <&#61; Edge || x >&#61; Edge &#43; BoardW*GridSz ||y <&#61; Edge || y >&#61; Edge &#43; BoardH*GridSz)return;*pi &#61; (y - Edge) / GridSz;*pj &#61; (x - Edge) / GridSz;
}void trunLayout(int layout[BoardH][BoardW], int i, int j)
{layout[i][j] &#61; 1 - layout[i][j];if (i - 1 >&#61; 0){layout[i - 1][j] &#61; 1 - layout[i - 1][j];}if (i &#43; 1 &#61; 0){layout[i][j - 1] &#61; 1 - layout[i][j - 1];}if (j &#43; 1 }void updateLayout(int layout[BoardH][BoardW], int x, int y)
{int i, j;pos2Index(x, y, &i, &j);trunLayout(layout, i, j);
}int getPiecesCount(int layout[BoardH][BoardW])
{int sum &#61; 0;for (int i &#61; 0; i }void initLayout(int layout[BoardH][BoardW])
{for (int i &#61; 0; i <10; &#43;&#43;i){trunLayout(layout, rand() % BoardH, rand() % BoardW);}
}int main()
{srand(time(NULL));int layout[BoardH][BoardW] &#61; { 0 };HWND hwnd;hwnd &#61; initgraph(BoardW * GridSz &#43; 2 * Edge, BoardH * GridSz &#43; 2 * Edge);//for(int i&#61;0; i<3; &#43;&#43;i)while (1){initLayout(layout);drawGrid();drawLayout(layout);MOUSEMSG m;while (getPiecesCount(layout)){m &#61; GetMouseMsg();switch (m.uMsg){case WM_LBUTTONDOWN:updateLayout(layout, m.x, m.y);break;}drawGrid();drawLayout(layout);}MessageBox(hwnd, _T("恭喜过关&#xff01;"), _T("提示"), MB_OK);}_getch(); // 按任意键继续closegraph(); // 关闭绘图窗口
}#if 0
#include
#include
#include
int main()
{srand(time(NULL));//1970.1.1 00:00:00for (int i &#61; 0; i <10; &#43;&#43;i){printf("%d,", rand());}return 0;
} //伪随机
#endif