new一个FINDBOOM

This commit is contained in:
wlx
2025-07-17 20:46:02 +08:00
parent 68eebdd09f
commit 457bf79d39
15 changed files with 314 additions and 321 deletions

View File

@ -13,12 +13,17 @@ find_package(SDL2_image REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_ttf REQUIRED)
file(GLOB res res/**)
file(COPY ${res} DESTINATION ${CMAKE_BINARY_DIR}/res)
add_executable(saolei
src/main.c
src/context.c
src/window.c
src/Global.c
include/Global.h
src/game.c
include/game.h
src/data.c
include/data.h
src/img.c
include/img.h
)
target_link_libraries(saolei

View File

@ -1,14 +0,0 @@
#ifndef GLOBAL_H
#define GLOBAL_H
#include "def.h"
#include "context.h"
#include "window.h"
typedef struct Global {
Context context;
Window window;
} Global;
extern Global global;
#endif //GLOBAL_H

View File

@ -1,17 +0,0 @@
#ifndef INIT_H
#define INIT_H
#include "def.h"
#include <SDL.h>
typedef struct Context {
int map[ROW][COL];
int mapcopy[ROW][COL];
SDL_Texture * imgs[10];
} Context;
void contextInit();
void imgInit();
#endif //INIT_H

15
include/data.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef DATA_H
#define DATA_H
typedef struct Data {
int x,y,boom;
int **arr;
int **arrcopy;
} Data;
void initData(Data *data, int x, int y ,int boom);
void getData(Data *data, int x, int y);
void showData(Data *data);
#endif //DATA_H

View File

@ -1,24 +0,0 @@
#ifndef DEF_H
#define DEF_Hs
// 行列数
#define ROW 10
#define COL 10
// 地雷个数
#define BOOM 15
// 窗口大小
#define WinWidth 400
#define WinHeight 400
// 矩形大小
#define rectWidth 40
#define rectHeight 40
// 颜色
#define OVER (Color){0X4E,0X4E,0X4E,255}
#define BACKGROUND (Color){0XE1,0XE2,0XE3,0XFF}
#define FLOW (Color){175,175,175,0}
#endif //DEF_H

29
include/game.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef RUNNING_H
#define RUNNING_H
#include "SDL_render.h"
#include "SDL_video.h"
typedef struct Window {
int width, height;
SDL_Window *window;
SDL_Renderer *renderer;
} Window;
typedef struct Game {
const char* title;
Window window;
} Game;
typedef struct Color {
int r,g,b,a;
} Color;
#define RED (Color){150,40,40,255}
#define GREEN (Color){40,150,40,255}
#define BLUE (Color){40,40,150,255}
void initGame(const char* name, int width, int height);
void gameLoop();
void windowClose();
#endif //RUNNING_H

11
include/img.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef IMG_H
#define IMG_H
#include "SDL_render.h"
typedef struct Img {
SDL_Texture *texture[10];
} Img;
void initImg(Img * img, const char * arr[],int len, SDL_Renderer * renderer);
#endif //IMG_H

View File

@ -1,25 +0,0 @@
#ifndef WINDOW_H
#define WINDOW_H
#include <SDL_events.h>
#include <SDL_render.h>
// 颜色
typedef struct Color {
int r;
int g;
int b;
int a;
} Color;
// 窗口
typedef struct Window{
SDL_Window *Window;
SDL_Renderer *Renderer;
SDL_Event Event;
} Window;
void windowInit();
void windowLoop();
void windowShutdown();
#endif //WINDOW_H

View File

@ -1,3 +0,0 @@
#include "../include/Global.h"
Global global = { 0 } ;

View File

@ -1,57 +0,0 @@
#include <stdio.h>
#include <rend.h>
#include <time.h>
#include <SDL_image.h>
#include "../include/Global.h"
char * img_paths[10] = {
"./res/1.png",
"./res/2.png",
"./res/3.png",
"./res/4.png",
"./res/5.png",
"./res/6.png",
"./res/7.png",
"./res/8.png",
"./res/boom.png",
"./res/flag.png"
};
// 初始化数据
void contextInit() {
// 设置随机位置为-1
srand((unsigned)time(NULL));
for (int i = 0; i < BOOM;) {
char k, l;
k = rand() % ROW;
l = rand() % COL;
if (!global.context.map[k][l]) {
global.context.map[k][l] = -1;
i++;
}
}
// 把以 -1 为中心的九宫格数据都+1-1除外
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (global.context.map[i][j] == -1) {
for (int k = i-1; k <= i+1; k++) {
for (int l = j-1; l <= j+1; l++) {
if ((k >= 0 && k < ROW && l >= 0 && l < COL ) && global.context.map[k][l] != -1) {
global.context.map[k][l]++;
}
}
}
}
}
}
}
void imgInit () {
for (int i = 0; i < sizeof(global.context.imgs)/sizeof(global.context.imgs[0]); i++) {
SDL_Surface * surface = IMG_Load(img_paths[i]);
global.context.imgs[i] = SDL_CreateTextureFromSurface(global.window.Renderer,surface);
SDL_FreeSurface(surface);
}
}

59
src/data.c Normal file
View File

@ -0,0 +1,59 @@
#include "../include/data.h"
#include <stdlib.h>
#include <string.h>
#include <rend.h>
#include <stdio.h>
#include <time.h>
void initData(Data *data ,int x, int y, int boom) {
data->x = x;
data->y = y;
data->arr = (int **)malloc( x * sizeof(int*));
for (int i = 0; i < x; i++) {
data->arr[i] = (int *)malloc( y * sizeof(int));
memset(data->arr[i], 0, y * sizeof(int));
}
data->arrcopy = (int **)malloc( x * sizeof(int*));
for (int i = 0; i < x; i++) {
data->arrcopy[i] = (int *)malloc( y * sizeof(int));
memset(data->arrcopy[i], 0, y * sizeof(int));
}
data->boom = boom;
}
void getData(Data *data, int x, int y) {
srand((unsigned)time(NULL));
for (int n = 0; n < data->boom;) {
char i,j;
i = rand() % data->x;
j = rand() % data->y;
if (i != x && j != y && data->arr[i][j] != -1) {
data->arr[i][j] = -1;
n++;
}
}
for (int i = 0; i < data->x; i++) {
for (int j = 0; j < data->y; j++) {
if (data->arr[i][j] == -1) {
for (int n = i - 1; n <= i + 1; n++) {
for (int m = j - 1; m <= j + 1; m++) {
if (n >= 0 && m >= 0 && n < data->x && m < data->y && data->arr[n][m] != -1) {
data->arr[n][m]++;
}
}
}
}
}
}
}
void showData(Data *data){
for (int i = 0; i < data->x; i++) {
for (int j = 0; j < data->y; j++ ) {
printf("%3d", data->arrcopy[i][j]);
}
printf("\n");
}
}

171
src/game.c Normal file
View File

@ -0,0 +1,171 @@
#include "../include/game.h"
#include "../include/data.h"
#include "../include/img.h"
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
Game game;
Data data;
Img img;
static int running = 1;
static int isfirst = 1;
const char * imgArr[] = {
"./res/boom.png",
"./res/1.png",
"./res/2.png",
"./res/3.png",
"./res/4.png",
"./res/5.png",
"./res/6.png",
"./res/7.png",
"./res/8.png",
"./res/flag.png"
};
void initGame(const char* name,int width,int height) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
game.title = name;
game.window.width = width;
game.window.height = height;
game.window.window = SDL_CreateWindow(name,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN);
game.window.renderer = SDL_CreateRenderer(game.window.window,-1,SDL_RENDERER_ACCELERATED);
initImg(&img,imgArr,10,game.window.renderer);
}
void windowClose() {
SDL_DestroyRenderer(game.window.renderer);
SDL_DestroyWindow(game.window.window);
SDL_Quit();
}
void normalization (int* x, int* y) {
*x = *x / (game.window.width / data.x);
*y = *y / (game.window.height / data.y);
*x = *x ^ *y;
*y = *y ^ *x;
*x = *x ^ *y;
}
void extend(int i,int j) {
if (i>=0 && i< data.x && j>=0 && j< data.y && data.arr[i][j] == 0 && data.arrcopy[i][j] != -1) {
data.arrcopy[i][j] = -1;
extend(i - 1 ,j);
extend(i ,j - 1);
extend(i ,j + 1);
extend(i + 1, j);
} else if ( i>=0 && i< data.x && j>=0 && j< data.y && data.arr[i][j] > 0 ) {
data.arrcopy[i][j] = -1;
}
}
void eventProcess(SDL_Event event) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = 0;
break;
case SDL_MOUSEBUTTONUP:
int x,y;
SDL_GetMouseState(&x,&y);
normalization(&x,&y);
if (event.button.button == SDL_BUTTON_LEFT) {
if (isfirst == 1 && data.arrcopy[x][y] == 0 ) {
getData(&data, x, y);
isfirst = 0;
}
if (data.arr[x][y] == 0 && data.arrcopy[x][y] == 0) {
extend(x ,y );
}
if (data.arrcopy[x][y] == 0) {
data.arrcopy[x][y] = -1;
}
}
if (event.button.button == SDL_BUTTON_RIGHT) {
if (data.arrcopy[x][y] == 0) {
data.arrcopy[x][y] = 1;
} else if (data.arrcopy[x][y] == 1) {
data.arrcopy[x][y] = 0;
}
}
break;
}
}
}
void setColor(Color color) {
SDL_SetRenderDrawColor(game.window.renderer,color.r,color.g,color.b,color.a);
}
SDL_Rect creatRect(int x,int y,Game* game,Data* data) {
SDL_Rect rect = {
x * game->window.width / data->x + 4,
y * game->window.width / data->y+ 4 ,
game->window.width / data->x - 8,
game->window.height / data->y - 8
};
return rect;
}
void drawRect(int x,int y, int kind,int isCilck) {
if (isCilck == 0) {
setColor(RED);
SDL_Rect rect = creatRect(y,x,&game,&data);
SDL_RenderFillRect(game.window.renderer,&rect);
} else if (isCilck == 1) {
SDL_Rect rect = creatRect(y,x,&game,&data);
SDL_RenderCopy(game.window.renderer,img.texture[9],NULL,&rect);
} else if (isCilck == -1) {
SDL_Rect rect = creatRect(y,x,&game,&data);
if (kind > 0) {
SDL_RenderCopy(game.window.renderer,img.texture[kind],NULL,&rect);
} else if (kind == -1) {
SDL_RenderCopy(game.window.renderer,img.texture[0],NULL,&rect);
}
}
}
void drawMap(Data data) {
setColor(BLUE);
SDL_RenderClear(game.window.renderer);
for(int i = 0; i < data.x; i++) {
for(int j = 0; j < data.y; j++) {
drawRect(i, j,data.arr[i][j],data.arrcopy[i][j]);
}
}
}
void moveMouse() {
int x,y;
SDL_GetMouseState(&x,&y);
normalization(&x,&y);
if (data.arrcopy[x][y] == 0 && x > 0 && y > 0 && x < data.x && y < data.y) {
setColor(GREEN);
SDL_Rect rect = creatRect(y,x,&game,&data);
SDL_RenderDrawRect(game.window.renderer,&rect);
}
}
void gameLoop() {
initData(&data,10,10,15);
SDL_Event event;
while (running) {
drawMap(data);
eventProcess(event);
moveMouse();
SDL_RenderPresent(game.window.renderer);
SDL_Delay(6);
}
}

10
src/img.c Normal file
View File

@ -0,0 +1,10 @@
#include "../include/img.h"
#include "SDL_image.h"
void initImg(Img * img ,const char * arr[],int len, SDL_Renderer * renderer){
for(int i = 0 ;i < len; i++) {
SDL_Surface * surf = IMG_Load(arr[i]);
img->texture[i] = SDL_CreateTextureFromSurface(renderer,surf);
SDL_FreeSurface(surf);
}
}

View File

@ -1,17 +1,17 @@
#include "../include/Global.h"
#include "../include/game.h"
#include <SDL.h>
#include <stdio.h>
int main(int argc,char *argv[]) {
#include "../include/data.h"
windowInit(WinWidth,WinHeight);
int main(int, char**) {
initGame("扫雷", 400, 400);
contextInit();
gameLoop();
imgInit();
windowLoop();
windowShutdown();
windowClose();
return 0;
}

View File

@ -1,167 +0,0 @@
#include <SDL_image.h>
#include <stdio.h>
#include "../include/Global.h"
static int flag = 0;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD>Ⱦ<EFBFBD><C8BE>
void windowInit(int width,int height) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
global.window.Window = SDL_CreateWindow("ɨ<EFBFBD><EFBFBD>",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
global.window.Renderer = SDL_CreateRenderer(global.window.Window, -1, SDL_RENDERER_ACCELERATED);
}
// <20><><EFBFBD>ղ<EFBFBD><D5B2>ر<EFBFBD><D8B1><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD>Ⱦ<EFBFBD><C8BE>
void windowShutdown() {
SDL_DestroyRenderer(global.window.Renderer);
SDL_DestroyWindow(global.window.Window);
IMG_Quit();
SDL_Quit();
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
void setColor(Color color) {
SDL_SetRenderDrawColor(global.window.Renderer, color.r, color.g, color.b, color.a);
}
// <20><>һ<EFBFBD><D2BB>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ɫ<EFBFBD>Ŀ<EFBFBD><C4BF>ľ<EFBFBD><C4BE><EFBFBD>
void drawRect(SDL_Rect* rect,Color color) {
setColor(color);
SDL_RenderFillRect(global.window.Renderer, rect);
}
// <20><>ʾͼƬ
void drawImg( int i,int j,SDL_Rect * rect) {
if (global.context.map[i][j] == -1 ) {
SDL_RenderCopy(global.window.Renderer,global.context.imgs[8],NULL,rect);
} else if (global.context.map[i][j] > 0) {
SDL_RenderCopy(global.window.Renderer,global.context.imgs[global.context.map[i][j] - 1 ],NULL,rect);
} else if (global.context.map[i][j] == 0) {
setColor(BACKGROUND);
SDL_RenderFillRect(global.window.Renderer,rect);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void drawMap() {
// <20><><EFBFBD><EFBFBD>
setColor(BACKGROUND);
SDL_RenderClear(global.window.Renderer);
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
SDL_Rect rect = {j * rectWidth + 1,i * rectHeight + 1,rectWidth - 2,rectHeight - 2};
if (global.context.mapcopy[i][j] == 0) {
drawRect(&rect,OVER);
} else if (global.context.mapcopy[i][j] == -1) {
drawImg(i,j,&rect);
} else if (global.context.mapcopy[i][j] == 1) {
SDL_RenderCopy(global.window.Renderer,global.context.imgs[ 9 ],NULL,&rect);
}
}
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void mouseMove() {
int x,y;
SDL_GetMouseState(&x,&y);
SDL_Rect rect = {x / 40 * 40 + 1,y / 40 * 40 + 1,40 - 2,40 - 2};
if (global.context.mapcopy[y/40][x/40] == 0) {
drawRect(&rect,FLOW);
}
}
// <20><>ˮ<EFBFBD><CBAE><EFBFBD><EFBFBD><EFBFBD>
void extend(int i, int j) {
if (i>=0 && j>=0 && i<ROW && j<COL && global.context.map[i][j] == 0 && global.context.mapcopy[i][j] != -1) {
global.context.mapcopy[i][j] = -1;
extend(i - 1,j);
extend(i,j - 1);
extend(i + 1,j);
extend(i,j + 1);
} else if ( i>=0 && j>=0 && i<ROW && j<COL && global.context.map[i][j] > 0) {
global.context.mapcopy[i][j] = -1;
}
}
// <20>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
void eventsProcess() {
while (SDL_PollEvent(&global.window.Event)) {
switch (global.window.Event.type) {
case SDL_QUIT:
windowShutdown();
case SDL_MOUSEBUTTONUP:
int x,y;
SDL_GetMouseState(&x,&y);
if (global.window.Event.button.button == SDL_BUTTON_LEFT) {
if ( global.context.map[y/40][x/40] == 0 && global.context.mapcopy[y/40][x/40] == 0) {
extend(y/40,x/40);
}
if ( global.context.mapcopy[y/40][x/40] == 0) {
global.context.mapcopy[y/40][x/40] = -1;
}
} else if (global.window.Event.button.button == SDL_BUTTON_RIGHT) {
if (global.context.mapcopy[y/40][x/40] == 1) {
global.context.mapcopy[y/40][x/40] = 0;
} else if (global.context.mapcopy[y/40][x/40] == 0) {
global.context.mapcopy[y/40][x/40] = 1;
}
}
}
}
}
// <20><><EFBFBD>ݼ<EFBFBD><DDBC><EFBFBD>
void check() {
int num = BOOM;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (global.context.mapcopy[i][j] == -1 && global.context.map[i][j] == -1) {
flag = -1;
return;
} else if (global.context.mapcopy[i][j] == 1 && global.context.map[i][j] == -1) {
num -= 1;
}
}
}
if (num == 0) {
flag = 1;
}
}
// ʧ<><CAA7>
void defend() {
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
while (1);
}
// <20><>ʤ
void win() {
printf("<EFBFBD><EFBFBD>Ӯ<EFBFBD><EFBFBD>");
while (1);
}
void windowLoop() {
flag = 0;
while ( 1 ) {
if (flag == -1) {
defend();
break;
} else if (flag == 1) {
win();
break;
} else if (flag == 0) {
drawMap();
mouseMove();
SDL_RenderPresent(global.window.Renderer);
eventsProcess();
check();
}
SDL_Delay(6);
}
}