赞
踩
行吧,开始了
- #include<stdio.h>
- #include<graphics.h>//包含图形库头文件
- #define ROW 9 //行
- #define COL 9 //列 共81个格子
- #define MINE_NUM 18//雷的数量
- #define IMG_SIZE 40
- void GameInit()
- {
- //1,创建窗口 一行有九张图片每张图片40px,
- initgraph(IMG_SIZE*ROW, IMG_SIZE*COL,SHOWCONSOLE);
- //加载图片(赋值) load加载 第一个参数,存储图片的变量 错误:字符集问题
- for (int i = 0; i < 12; i++)
- {
- char file[20] = "";
- sprintf(file, "./image/%d.jpg", i);
- loadimage(&img[i], file, IMG_SIZE, IMG_SIZE);
- }
- //布雷
- for (int i = 0; i < MINE_NUM; )
- {
- //排除辅助区
- int row = rand() % ROW+1;//0-8 1-9
- int col = rand() % COL+1;
- if (mine[row][col] == 0)
- {
- mine[row][col] = 9;
- i++;
- }
- }
- //雷所在的九宫格,要加1(雷除外)
- for (int i = 1; i < ROW+1; i++)
- {
- for (int k = 1; k < COL+1; k++)
- {
- if (mine[i][k] == 9)
- {
- //遍历雷的九宫格 细心
- for (int a = i - 1; a <= i + 1; a++)
- {
- for (int b = k - 1; b <= k + 1; b++)
- {
- //非雷的格子加1
- if (mine[a][b] != 9)
- {
- mine[a][b]++;
- }
- }
- }
- }
- }
- }
- //加密 开始时全部为掩码图
- for (int i = 1; i < ROW + 1; i++)
- {
- for (int k = 1; k < COL + 1; k++)
- {
- mine[i][k] += 20;
- }
- }
-
- }
- void MouseEvent()
- {
- //检测是否有鼠标消息
- if (MouseHit())
- {
- //获取鼠标消息,坐标,左键还是右键
- MOUSEMSG msg = GetMouseMsg();
- //把坐标转化成数组下标
- openr = msg.y / IMG_SIZE+1;
- openc = msg.x / IMG_SIZE+1;
- //判断是左键还是右键
- switch (msg.uMsg)
- {
- case WM_LBUTTONDOWN:
- //如果格子没有打开,就打开格子,否则不做处理
- if (mine[openr][openc] > 9)
- {
- mine[openr][openc] -= 20;
- OpenNull(openr,openc);
- num++;
- }
- break;
- case WM_RBUTTONDOWN:
- //标记
- if (mine[openr][openc] > 9 && mine[openr][openc] <= 29)
- {
- mine[openr][openc] += 20;
- }
- else
- {
- mine[openr][openc] -= 20;
- }
- break;
- }
- }
- }
- //递归打开所有空白,以及空白周围的数字
- void OpenNull(int row,int col)
- {
- //点击的是空白才能打开
- if (mine[row][col] == 0)
- {
- for (int i = row - 1; i <= row + 1; i++)
- {
- for (size_t k = col-1; k <= col+1; k++)
- {
- //如果为空或者不是雷就打开
- if ((mine[i][k] == 20 || mine[i][k] != 29)&& mine[i][k]>9)
- {
- mine[i][k] -= 20;
- num++;
- OpenNull(i, k);
- }
- }
- }
- }
- }
- void Jude()
- {
- //判断输
- if (mine[openr][openc] == 9)
- {
- int isok = MessageBox(GetHWnd(), "是否继续!", "点到雷了", MB_OKCANCEL);
- if (isok == IDOK)
- {
- mine[openr][openc] += 20;
- }
- else
- {
- exit(666);
- }
- }
- //判断赢
- if (num == ROW*COL - MINE_NUM)
- {
- int isok = MessageBox(GetHWnd(), "是否继续!", "点到雷了", MB_OKCANCEL);
- if (isok == IDOK)
- {
- num = 0;
- GameInit();
- }
- else
- {
- exit(666);
- }
- }
- }
- int main()
- {
- GameInit();
- show();
- //循环处理游戏逻辑
- while (1)
- {
- MouseEvent();
- GameDraw();
- Jude();
- }
-
- getchar();
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。