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

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);
}
}