代码语言:javascript复制
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define M 10 //10*10
#define N 10//雷的个数
void InitMap(int(*map)[M]);//随机埋雷
void Init(IMAGE *img);//初始化
void DrawMap(int(*map)[M], IMAGE *img); //贴图
void PlayGame(int(*map)[M]);//游戏开始,鼠标点击
int TongJi(int(*map)[M]);//判断游戏是否结束
int main()
{
int map[M][M] = { 0 };
srand((unsigned)time(NULL));//设置随机种子
InitMap(map);
IMAGE img[12];
Init(img);
while (TongJi(map)==1)
{
DrawMap( map, img);
PlayGame(map);
}
//输出赢
outtextxy(0, 0, "WIN");
getchar();
closegraph();
return 0;
}
void InitMap(int(*map)[M])
{
mciSendString("open res小幸运.mp3 alias MESSI",NULL,NULL,NULL);
mciSendString("play MESSI repeat", NULL, NULL, NULL);
int x, y;
for (int i = 0; i <N;)//在地图上随机埋雷
{
x = rand() % M;
y = rand() % M;
if (map[x][y] != -1)//位置不是雷
{
map[x][y] = -1;//赋值雷,-1
i ;
}
}
for (int i = 0; i < M; i )//测试是否赋值雷
{
for (int j = 0; j < M; j )
{
if (map[i][j] == -1)//找到雷
{
for (int x = i - 1; x <= i 1; x )//周围元素
{
for (int y = j - 1; y <= j 1; y )
{
if (0 <= x&&x <= M && 0 <= y&&y <= M&&map[x][y] != -1)//注意是否越界,而且如果是雷不加1
{
map[x][y] ;//给周围元素加1
}
}
}
}
}
}
}
void Init(IMAGE *img)
{
initgraph(64 * M, 64 * M);//图片64*64
loadimage(&img[0], "res/0.jpg", 64, 64);
loadimage(&img[1], "res/1.jpg", 64, 64);
loadimage(&img[2], "res/2.jpg", 64, 64);
loadimage(&img[3], "res/3.jpg", 64, 64);
loadimage(&img[4], "res/4.jpg", 64, 64);
loadimage(&img[5], "res/5.jpg", 64, 64);
loadimage(&img[6], "res/6.jpg", 64, 64);
loadimage(&img[7], "res/7.jpg", 64, 64);
loadimage(&img[8], "res/8.jpg", 64, 64);
loadimage(&img[9], "res/标记.jpg", 64, 64);
loadimage(&img[10], "res/空.jpg", 64, 64);
loadimage(&img[11], "res/雷.jpg", 64, 64);
}
//void DrawMap(map[][M],IMAGE,img[]) 用数组名或者指针
void DrawMap(int(*map)[M], IMAGE *img) //贴图
{
for (int i = 0; i < M; i )
{
for (int j = 0; j < M; j )
{
switch (map[i][j])
{
case 9:putimage(j * 64, i * 64, &img[11]);
break;
case 10:putimage(j * 64, i * 64, &img[0]);
break;
case 11:putimage(j * 64, i * 64, &img[1]);
break;
case 12:putimage(j * 64, i * 64, &img[2]);
break;
case 13:putimage(j * 64, i * 64, &img[3]);
break;
case 14:putimage(j * 64, i * 64, &img[4]);
break;
case 15:putimage(j * 64, i * 64, &img[5]);
break;
case 16:putimage(j * 64, i * 64, &img[6]);
break;
case 17:putimage(j * 64, i * 64, &img[7]);
break;
case 18:putimage(j * 64, i * 64, &img[8]);
break;
default:putimage(j * 64, i * 64, &img[10]);
break;
}
}
}
}
void PlayGame(int (*map)[M])
{
MOUSEMSG msg;
while (1)
{
msg = GetMouseMsg();//获取鼠标信息
switch (msg.uMsg)
{
case WM_LBUTTONDBLCLK://左键按下,翻开,首先判断是不是雷
if (map[msg.y / 64][ msg.x / 64] == -1)//表示点到雷
{
exit(0);//直接结束
}
else if (map[msg.y / 64][ msg.x / 64] <= 8 && map[msg.y / 64][ msg.x / 64] >= 0)
{
map[msg.y / 64][msg.x / 64] = 10;//表示点到其他,翻开
}
else if (map[msg.y / 64][ msg.x / 64] == 0)//0时翻开一圈
{
for (int x = msg.y / 64 - 1; x <= msg.y / 64 1; x )//周围元素
{
for (int y = msg.x / 64 - 1; y <= msg.x / 64 1; y )
{
if (0 <= x&&x <= M && 0 <= y&&y <= M&&map[x][y]<9)//注意是否越界,翻开一圈
{
map[x][y] =10;
}
}
}
}
return;
break;
default:
break;
}
}
}
int TongJi(int (*map)[M])//判断游戏是否结束
{
int x=0;//统计翻开元素个数
for (int i = 0; i < M; i )
{
for (int j = 0; j < M; j )
{
if (map[i][j] >= 9)//翻开过
{
x ;
}
}
}
if (x == M*M - N)
{
return 0;//游戏结束
}
else return 1;//游戏没结束
}
声明:本文为原创,作者为 对弈,转载时请保留本声明及附带文章链接:http://www.duiyi.xyz/c实现雷霆战机-50/