C/C++項目源碼——掃雷游戲
這是我自己寫的一個掃雷的小游戲
分享給大家
希能和大家一起進步
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<graphics.h>
/*
課程內(nèi)容:掃雷游戲
開發(fā)環(huán)境:vs+easyx? C語言
二維數(shù)組
*/
#define ROW 10 //定義數(shù)組行列
#define COL 10
#define MINE 10 //雷的數(shù)量
#define SIZE 50
//全局變量自動初始化為0
int map[ROW][COL];
//保存圖片
IMAGE img[12];
int row;
int col;
//初始化數(shù)據(jù)
void GameInit()
{
//創(chuàng)建窗口
initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE);
//設(shè)置隨機數(shù)種子
srand((unsigned)time(NULL));
//數(shù)組初始化為0
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
map[i][j] = 0;
}
}
//埋雷,用-1表示雷安券,要10個雷
for (int i = 0; i < MINE;)
{
int r = rand()%ROW;//0-9
int c = rand()%COL;
if (map[r][c] == 0)//不等于雷的時候蜕依,就設(shè)置為雷 -1
{
map[r][c] = -1;
i++; //每埋雷成功一次,加一次
}
}
//讓雷周圍的九宮格都加1(雷除外)
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j] == -1)//如果是雷
{
for (int m = i - 1; m <= i + 1; m++)
{
for (int n = j - 1; n <= j + 1; n++)
{
//排除雷
if (map[m][n] != -1 && (m >= 0 && m < ROW && n >= 0 && n < COL))
{
map[m][n] ++;
}
}
}
}
}
}
//加載圖片
for (int i = 0; i < 12; i++)
{
char file[20] = "";
sprintf(file, "./image/%d.jpg", i);
loadimage(&img[i], file,SIZE,SIZE);
}
//加密格子
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
map[i][j] += 20;
}
}
}
void GameDraw()
{
//繪圖 0 1 2 3 4 5 6 7 8 9 10 11 -1
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (map[i][j] == -1)
{
putimage(j * SIZE, i * SIZE, &img[9]);
}
else if (map[i][j] >= 0 && map[i][j] <= 8)
{
putimage(j * SIZE, i * SIZE, &img[map[i][j]]);
}
else if (map[i][j] >= 19 && map[i][j] <= 28)
{
putimage(j * SIZE, i * SIZE, &img[10]);
}
else if(map[i][j]>30)
{
putimage(j * SIZE, i * SIZE, &img[11]);
}
}
}
}
void openNull(int row, int col);
//鼠標(biāo)操作
int Mouse()
{
if (MouseHit())
{
MOUSEMSG msg = GetMouseMsg();
row = msg.y / SIZE;//通過鼠標(biāo)點擊的坐標(biāo)管跺,求出數(shù)組下標(biāo)
col = msg.x / SIZE;
switch (msg.uMsg)
{
case WM_LBUTTONDOWN://鼠標(biāo)左鍵點擊
if (map[row][col] > 8)
{
map[row][col] -= 20;
openNull(row, col);
}
break;
case WM_RBUTTONDOWN://鼠標(biāo)右鍵點擊
if (map[row][col] <= 28 && map[row][col] >= 8)
{
map[row][col] += 20;
}
else
{
map[row][col] -= 20;
}
break;
}
return map[row][col];
}
}
//點擊空白格子,打開一篇
void openNull(int row,int col)
{
if (map[row][col] == 0)
{
for (int i = row - 1; i <= row + 1; i++)
{
for (int j = col - 1; j <= col + 1; j++)
{
if ((map[i][j] == 20 || map[i][j] != 19) && map[i][j]>8
&& (i >= 0 && i < ROW && j >= 0 && j < COL))
{
map[i][j] -= 20;
openNull(i, j);
}
}
}
}
}
void show(int arr[][COL])
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
printf("%2d? ",arr[i][j]);
}
putchar('\n');
}
system("cls");
}
int main()
{
GameInit();
while (1)
{
//show(map);
int isok = Mouse();
GameDraw();
if (isok==-1)//點到雷了
{
int flag=MessageBox(GetHWnd(), "歡迎關(guān)注微信公眾號:C語言愛好者", "提示", MB_OKCANCEL);
if (flag == IDOK)
{
map[row][col] += 40;//關(guān)上雷,并標(biāo)記
}
else
{
//結(jié)束游戲
exit(666);
}
}
}
getchar();
return 0;
}