以下是我用c语言写的扫雷游戏的完整代码,我将图片放在了程序的根目录,我自己电脑一切正常,将代码发给别人,也给他安了easyx并将图片放在了他创建程序的根目录,但是运行就不显示图片。(他们的环境也是vs2022)
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <graphics.h>
#define ROW 10
#define COL 10
#define MINE 10
#define SIZE 50
int count = 0; //点的格子数
int map[ROW+2][COL+2];
IMAGE img[12]; //img存放12张图片
//初始化棋盘
void initboard()
{
int i;
int j;
for (i = 0; i < ROW+2; i++)
{
for (j = 0; j < COL+2; j++)
{
map[i][j]=0;
}
}
srand((unsigned int)time(NULL));
int flag=0;
while (flag < MINE)
{
int r = rand() % ROW+1;
int c = rand() % COL+1;
//布雷
if (map[r][c] == 0)
{
map[r][c] = -1;
flag++;
}
}
//根据周边8格雷数填充数组
for (i = 1; i <= ROW; i++)
{
for (j = 1; j <= COL; j++)
{
if (map[i][j] != -1)
{
int m=0;
int n=0;
for (m=i-1;m<=i+1;m++)
{
for (n=j-1;n<=j+1;n++)
{
if (map[m][n] == -1)
{
map[i][j]++;
}
}
}
}
}
}
//数组值全+20变成空白图片
for (i = 1; i <= ROW; i++)
{
for (j = 1; j <= COL; j++)
{
map[i][j] += 20;
}
}
}
//打印棋盘
void printboard()
{
int i;
int j;
for (i = 1; i <= ROW; i++)
{
for (j = 1; j <= COL; j++)
{
printf("%3d", map[i][j]);
if (map[i][j] == -1) //雷图片
{
putimage((i-1)*SIZE,(j-1)*SIZE,&img[9]);
}
else if (map[i][j]>=0&&map[i][j]<=8) //数字图片
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[map[i][j]]);
}
else if (map[i][j] >= 19 && map[i][j] <= 28) //空白图片
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[10]);
}
else if (map[i][j] >30) //旗子图片
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);
}
}
printf("\n");
}
}
//加载图片
void loadimg()
{
loadimage(&img[0],L"0.jpg",SIZE,SIZE);
loadimage(&img[1], L"1.jpg", SIZE, SIZE);
loadimage(&img[2], L"2.jpg", SIZE, SIZE);
loadimage(&img[3], L"3.jpg", SIZE, SIZE);
loadimage(&img[4], L"4.jpg", SIZE, SIZE);
loadimage(&img[5], L"5.jpg", SIZE, SIZE);
loadimage(&img[6], L"6.jpg", SIZE, SIZE);
loadimage(&img[7], L"7.jpg", SIZE, SIZE);
loadimage(&img[8], L"8.jpg", SIZE, SIZE);
loadimage(&img[9], L"9.jpg", SIZE, SIZE); //雷
loadimage(&img[10], L"10.jpg", SIZE, SIZE); //空白
loadimage(&img[11], L"11.jpg", SIZE, SIZE); //旗子
}
//递归展开一片
void open(int r,int c)
{
map[r][c] -= 20;
count++;
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (m >= 1 && m <= ROW && n >= 1 && n <= COL)
{
if (map[m][n] >= 19 && map[m][n] <= 28)
{
if (map[m][n] != 20)
{
map[m][n] -= 20;
count++;
}
else
{
open(m, n);
}
}
}
}
}
}
//鼠标操作
int play()
{
MOUSEMSG msg = { 0 };
int r;
int c;
while (1)
{
msg = GetMouseMsg();
switch (msg.uMsg)
{
case WM_LBUTTONDOWN: //扫雷 19~28
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
if (map[r][c] == 20) //点的位置是0
{
open(r,c);
}
else
{
map[r][c] -= 20; //-1~8
count++;
}
}
return map[r][c];
break;
case WM_RBUTTONDOWN: //标雷或取消标记
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
map[r][c] += 50;
}
else if (map[r][c] > 30)
{
map[r][c] -= 50;
}
return map[r][c];
break;
}
}
}
int main()
{
//打开一个图形窗口
HWND hwnd = initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE);
initgraph(ROW * SIZE, COL * SIZE, SHOWCONSOLE);
loadimg();
initboard();
while (1)
{
printboard();
if (play() == -1)
{
printboard();
MessageBox(hwnd, L"你输了", L"", MB_OK);
break;
}
if (ROW * COL - MINE == count)
{
printboard();
MessageBox(hwnd, L"你赢了", L"", MB_OK);
break;
}
}
return 0;
}