标题
怎么编写一个“消除棋子”的游戏?
#include <graphics.h> // 引用图形库头文件
#include <conio.h>
#include <time.h>
int main1()
{
initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素
MOUSEMSG m;
int r;
while (1)
{
m = GetMouseMsg();
switch (m.uMsg)
{
//case WM_LBUTTONDOWN:
// setcolor(RGB(rand()%256, rand() % 256, rand() % 256));
// circle(m.x, m.y, rand()%91+10);
// break;
//case WM_RBUTTONDOWN:
// setcolor(RGB(rand()%256, rand() % 256, rand() % 256));
// r = rand() % 91 + 10;
// rectangle(m.x - r, m.y-r, m.x+r, m.y+r);
// break;
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 = 5;
const int BoardH = 5;
const int GridSz = 90;
const int Edge = 30;
void drawGrid()
{
setbkcolor(RGB(214,167,140));
cleardevice();
setcolor(RGB(0, 0, 0));
for (int i = 0; i <= BoardH; ++i)
{
line(Edge, Edge + i * GridSz,
Edge + BoardW * GridSz, Edge + i * GridSz);
}
for (int i = 0; i <= BoardH; ++i)
{
line(Edge + i * GridSz, Edge,
Edge + i * GridSz, Edge + BoardH * GridSz);
}
}
void drawLayout(int layout[BoardH][BoardW])
{
setcolor(RGB(32,27,27));
setfillcolor(RGB(32, 27,27));
int r = GridSz * 4 / 10;
int x, y;
for (int i = 0; i < BoardH; ++i)
{
for (int j = 0; j < BoardW; ++j)
{
if (1 == layout[i][j])
{
x = Edge + j*GridSz + GridSz / 2;
y = Edge + i*GridSz + GridSz / 2;
fillcircle(x, y, r);
}
}
}
}
int pos2Index(int x, int y, int* pi, int* pj)
{
if (x <= Edge || x >= Edge + BoardW*GridSz ||
y <= Edge || y >= Edge + BoardH*GridSz)
return 0;
*pi = (y - Edge) / GridSz;
*pj = (x - Edge) / GridSz;
return 1;
}
void trunLayout(int layout[BoardH][BoardW], int i, int j)
{
layout[i][j] = 1 - layout[i][j];
if (i - 1 >= 0)
{
layout[i - 1][j] = 1 - layout[i - 1][j];
}
if (i + 1 < BoardH)
{
layout[i + 1][j] = 1 - layout[i + 1][j];
}
if (j - 1 >= 0)
{
layout[i][j - 1] = 1 - layout[i][j - 1];
}
if (j + 1 < BoardW)
{
layout[i][j + 1] = 1 - layout[i][j + 1];
}
}
void updateLayout(int layout[BoardH][BoardW], int x, int y)
{
int i, j;
if (pos2Index(x, y, &i, &j))
{
trunLayout(layout, i, j);
}
}
int getPiecesCount(int layout[BoardH][BoardW])
{
int sum = 0;
for (int i = 0; i < BoardH; ++i)
{
for (int j = 0; j < BoardW; ++j)
{
sum += layout[i][j];
}
}
return sum;
}
void initLayout(int layout[BoardH][BoardW])
{
for (int i = 0; i < 10; ++i)
{
trunLayout(layout, rand() % BoardH, rand() % BoardW);
}
//while (1)
//{
// trunLayout(layout, rand() % BoardH, rand() % BoardW);
// if (getPiecesCount(layout) < 7 && getPiecesCount(layout) > 0)
// break;
//}
}
int main()
{
//int layout[3][BoardH][BoardW] =
//{
// {
// 1,1,0,1,1,
// 1,0,0,0,1,
// 0,0,0,0,0,
// 1,0,0,0,1,
// 1,1,0,1,1},
// {
// 0,0,0,0,0,
// 0,0,1,0,0,
// 0,1,1,1,0,
// 0,0,1,0,0,
// 0,0,0,0,0
// },
// {
// 1,0,0,0,1,
// 0,0,0,0,0,
// 0,0,0,0,0,
// 0,0,0,0,0,
// 1,0,0,0,1
// }
//};
srand(time(NULL));
int layout[BoardH][BoardW] = { 0 };
HWND hwnd;
hwnd = initgraph(BoardW * GridSz + 2 * Edge, BoardH * GridSz + 2 * Edge);
//for(int i=0; i<3; ++i)
while (1)
{
initLayout(layout);
BeginBatchDraw();
drawGrid();
drawLayout(layout);
EndBatchDraw();
MOUSEMSG m;
while (getPiecesCount(layout))
{
m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
updateLayout(layout, m.x, m.y);
break;
}
BeginBatchDraw();
drawGrid();
drawLayout(layout);
EndBatchDraw();
}
MessageBox(hwnd, _T("恭喜过关!"), _T("提示"), MB_OK);
}
_getch(); // 按任意键继续
closegraph(); // 关闭绘图窗口
}
#if 0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));//1970.1.1 00:00:00
for (int i = 0; i < 10; ++i)
{
printf("%d,", rand());
}
return 0;
}
/*
伪随机
*/
#endif