更名FINDBOOM为MineSweepe

正式初步完成了MineSweepe
This commit is contained in:
wlx
2025-07-19 15:37:01 +08:00
parent a262e8d508
commit a3b2d6c7c8
30 changed files with 278 additions and 468 deletions

View File

@ -1,36 +1,37 @@
cmake_minimum_required(VERSION 3.31)
cmake_minimum_required(VERSION 3.30.1)
project(
saolei
MineSweeper
VERSION 0.1.0
LANGUAGES C
)
set(CMAKE_C_STANDARD 11)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
set(CMAKE_PREFIX_PATH ./lib/SDL/cmake)
find_package(SDL2 REQUIRED)
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)
file(COPY ${res} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/res)
add_executable(saolei
file(GLOB lib lib/SDL/x86_64-w64-mingw32/bin/**)
file(COPY ${lib} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
add_executable(MineSweeper
src/main.c
src/game.c
include/game.h
src/data.c
include/data.h
src/img.c
include/img.h
src/over.c
src/resource/logo.rc
)
target_link_libraries(saolei
target_link_libraries(MineSweeper
${SDL2_LIBRARIES}
SDL2_mixer::SDL2_mixer
SDL2_ttf::SDL2_ttf
SDL2_image::SDL2_image
)
target_link_options(saolei PRIVATE -Wl,-subsystem,console)

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# MineSweeper
这一个初学者对c语言的初次探索在mingw的环境下基于SDL2实现的一个有着可视化窗口的扫雷小游戏。
## 使用Cmkae构建
```shell
cmake -B build -G "MingGW Makefiles"
```
```shell
cmake --build build --config Release -j18
```

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

View File

@ -9,7 +9,5 @@ typedef struct Data {
void initData(Data *data, int x, int y ,int boom);
void getData(Data *data, int x, int y);
void showData(Data *data);
int checkData(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

View File

@ -18,12 +18,14 @@ 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}
#define SQUARE (Color){0xC0,0XC0,0XC0,255}
#define BACKGROUND (Color){0x24,0x49,0X5E,255}
#define BUTTON (Color){0X2c,0X3E,0X50,255}
#define FLOW (Color){0xD9,0xD9,0xD9}
void initGame(const char* name, int width, int height);
void gameLoop();
void setColor(Color color);
void windowClose();
#endif //RUNNING_H

22
include/over.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef WIN_H
#define WIN_H
#include "game.h"
#include <SDL_ttf.h>
typedef struct Font {
TTF_Font * fontKind;
SDL_Texture* texture;
int w, h;
} Font;
typedef struct Button {
int x,y,w,h;
} Button;
typedef struct Over {
int nameH,buttonH,w;
Button botton;
} Over;
void initOver(Game * game, Over * over);
void drawOver(int fontSizeName, int fontSizeButton,Font * font, const char * nameText, Color buttonColor, SDL_Renderer * renderer , Over* over);
#endif //WIN_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,65 +0,0 @@
#!/bin/sh
# Get the canonical path of the folder containing this script
bindir=`cd -P -- "\`dirname -- "$0"\`" && printf '%s\n' "\`pwd -P\`"`
# Calculate the canonical path of the prefix, relative to the folder of this script
prefix=`cd -P -- "$bindir/.." && printf '%s\n' "\`pwd -P\`"`
exec_prefix=${prefix}/bin
exec_prefix_set=no
libdir=${prefix}/lib
#usage="\
#Usage: $0 [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--cflags] [--libs]"
usage="\
Usage: $0 [--prefix[=DIR]] [--exec-prefix[=DIR]] [--version] [--cflags] [--libs] [--static-libs]"
if test $# -eq 0; then
echo "${usage}" 1>&2
exit 1
fi
while test $# -gt 0; do
case "$1" in
-*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
*) optarg= ;;
esac
case $1 in
--prefix=*)
prefix=$optarg
if test $exec_prefix_set = no ; then
exec_prefix=$optarg
fi
;;
--prefix)
echo $prefix
;;
--exec-prefix=*)
exec_prefix=$optarg
exec_prefix_set=yes
;;
--exec-prefix)
echo $exec_prefix
;;
--version)
echo 2.32.8
;;
--cflags)
echo -I${prefix}/include/SDL2 -Dmain=SDL_main
;;
--libs)
echo -L${prefix}/lib -lmingw32 -lSDL2main -lSDL2 -mwindows
;;
--static-libs)
# --libs|--static-libs)
sdl_static_libs=$(echo "-lmingw32 -lSDL2main -lSDL2 -mwindows -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid" | sed -E "s#-lSDL2[ $]#$libdir/libSDL2.a #g")
echo -L${prefix}/lib $sdl_static_libs
;;
*)
echo "${usage}" 1>&2
exit 1
;;
esac
shift
done

BIN
res/1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 456 B

BIN
res/2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 653 B

BIN
res/3.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 675 B

BIN
res/4.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 659 B

After

Width:  |  Height:  |  Size: 680 B

BIN
res/5.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 606 B

After

Width:  |  Height:  |  Size: 719 B

BIN
res/6.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 710 B

After

Width:  |  Height:  |  Size: 830 B

BIN
res/7.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 528 B

After

Width:  |  Height:  |  Size: 603 B

BIN
res/8.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 804 B

After

Width:  |  Height:  |  Size: 880 B

BIN
res/Minecraft.ttf Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 578 B

After

Width:  |  Height:  |  Size: 629 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 221 B

After

Width:  |  Height:  |  Size: 224 B

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

View File

@ -3,22 +3,28 @@
#include <stdlib.h>
#include <string.h>
#include <rend.h>
#include <stdio.h>
#include <time.h>
#include "../include/over.h"
static int isArrInint = 1;
void initData(Data *data ,int x, int y, int boom) {
data->x = x;
data->y = y;
if (isArrInint) {
data->arrcopy = (int **)malloc( x * sizeof(int*));
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));
}
isArrInint = 0;
}
for (int i = 0; i < x; i++) {
memset(data->arr[i], 0, y * sizeof(int));
memset(data->arrcopy[i], 0, y * sizeof(int));
}
data->boom = boom;
}
@ -28,7 +34,7 @@ void getData(Data *data, int x, int y) {
char i,j;
i = rand() % data->x;
j = rand() % data->y;
if (i != x && j != y && data->arr[i][j] != -1) {
if ((i != x || j != y) && data->arr[i][j] != -1) {
data->arr[i][j] = -1;
n++;
}
@ -49,11 +55,18 @@ void getData(Data *data, int x, int y) {
}
}
void showData(Data *data){
int checkData(Data *data) {
int num = 1;
for (int i = 0; i < data->x; i++) {
for (int j = 0; j < data->y; j++) {
printf("%3d", data->arrcopy[i][j]);
if ( data->arr[i][j] == -1 && data->arrcopy[i][j] == -1 ) {
return -1;
}
printf("\n");
if (data->arr[i][j] != -1 && data->arrcopy[i][j] >= 0 && num) {
num =0;
}
}
}
return num;
}

View File

@ -1,16 +1,24 @@
#include "../include/game.h"
#include "../include/data.h"
#include "../include/img.h"
#include "../include/over.h"
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <stdio.h>
Game game;
Data data;
Img img;
Over over;
Font font;
static int running = 1;
static int isfirst = 1;
static int flag = 0;
static int isInit = 1;
static int isOverInit = 0;
const char *imgArr[] = {
"./res/boom.png",
"./res/1.png",
@ -22,16 +30,22 @@ const char * imgArr[] = {
"./res/7.png",
"./res/8.png",
"./res/flag.png"
};
const char *winTitle = "WC NB";
const char *failureTitle = "G G";
TTF_Font *fontKind = NULL;
void initGame(const char *name, int width, int height) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG );
TTF_Init();
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.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);
}
@ -39,12 +53,13 @@ void initGame(const char* name,int width,int height) {
void windowClose() {
SDL_DestroyRenderer(game.window.renderer);
SDL_DestroyWindow(game.window.window);
IMG_Quit();
SDL_Quit();
}
void normalization(int *x, int *y) {
*x = *x / (game.window.width / data.x);
*y = *y / (game.window.height / data.y);
*x = *x / (game.window.width / data.y);
*y = *y / (game.window.height / data.x);
*x = *x ^ *y;
*y = *y ^ *x;
@ -63,7 +78,7 @@ void extend(int i,int j) {
}
}
void eventProcess(SDL_Event event) {
void runningEventProcess(SDL_Event event) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
@ -72,9 +87,7 @@ void eventProcess(SDL_Event event) {
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);
@ -87,7 +100,6 @@ void eventProcess(SDL_Event event) {
data.arrcopy[x][y] = -1;
}
}
if (event.button.button == SDL_BUTTON_RIGHT) {
if (data.arrcopy[x][y] == 0) {
data.arrcopy[x][y] = 1;
@ -105,26 +117,25 @@ void setColor(Color color) {
}
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
x * game->window.width / data->y + 4,
y * game->window.height / data->x + 4,
game->window.width / data->y - 8,
game->window.height / data->x - 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);
setColor(SQUARE);
SDL_Rect rect = creatRect(x, y, &game, &data);
SDL_RenderFillRect(game.window.renderer, &rect);
} else if (isCilck == 1) {
SDL_Rect rect = creatRect(y,x,&game,&data);
SDL_Rect rect = creatRect(x, y, &game, &data);
SDL_RenderCopy(game.window.renderer, img.texture[9],NULL, &rect);
} else if (isCilck == -1) {
SDL_Rect rect = creatRect(y,x,&game,&data);
SDL_Rect rect = creatRect(x, y, &game, &data);
if (kind > 0) {
SDL_RenderCopy(game.window.renderer, img.texture[kind],NULL, &rect);
} else if (kind == -1) {
@ -134,11 +145,11 @@ void drawRect(int x,int y, int kind,int isCilck) {
}
void drawMap(Data data) {
setColor(BLUE);
setColor(BACKGROUND);
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]);
drawRect(j, i, data.arr[i][j], data.arrcopy[i][j]);
}
}
}
@ -147,24 +158,92 @@ 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);
if (data.arrcopy[x][y] == 0 && x >= 0 && y >= 0 && x < data.x && y < data.y) {
setColor(FLOW);
SDL_Rect rect = creatRect(y, x, &game, &data);
SDL_RenderDrawRect(game.window.renderer,&rect);
SDL_RenderFillRect(game.window.renderer, &rect);
}
}
void showAll(Data *data) {
for (int i = 0; i < data->x; i++) {
memset(data->arrcopy[i], -1, data->y * sizeof(int));
}
}
void overEventProcess(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);
if (event.button.button == SDL_BUTTON_LEFT && x > over.botton.x && y > over.botton.y && x < (
over.botton.x + over.botton.w) && y < (over.botton.y + over.botton.h)) {
setColor(BACKGROUND);
SDL_RenderClear(game.window.renderer);
flag = 0;
isInit = 1;
isfirst = 1;
}
}
}
}
void gameLoop() {
initData(&data,10,10,15);
int time = 0;
SDL_Event event;
while (running) {
switch (flag) {
case 0:
if (isInit) {
initData(&data, 20, 20, 88);
isInit = 0;
}
runningEventProcess(event);
drawMap(data);
eventProcess(event);
moveMouse();
flag = checkData(&data);
if (flag != 0) {
isOverInit = 1;
time = 0;
}
break;
case 1:
if (time < 1000) {
time += 6;
if (isOverInit) {
initOver(&game, &over);
isOverInit = 0;
}
} else {
setColor(BACKGROUND);
SDL_RenderClear(game.window.renderer);
drawOver(102,22 ,&font, winTitle, SQUARE, game.window.renderer, &over);
overEventProcess(event);
}
break;
case -1:
if (time < 1000) {
time += 6;
showAll(&data);
drawMap(data);
if (isOverInit) {
initOver(&game, &over);
isOverInit = 0;
}
} else {
setColor(BACKGROUND);
SDL_RenderClear(game.window.renderer);
drawOver(102, 22, &font,failureTitle, SQUARE, game.window.renderer, &over);
overEventProcess(event);
}
break;
default:
return;
}
SDL_RenderPresent(game.window.renderer);
SDL_Delay(6);
}

View File

@ -1,11 +1,8 @@
#include "../include/game.h"
#include <SDL.h>
#include <stdio.h>
#include "../include/data.h"
int main(int, char**) {
initGame("扫雷", 400, 400);
initGame("MineSweeper", 800, 800);
gameLoop();

56
src/over.c Normal file
View File

@ -0,0 +1,56 @@
#include "../include/over.h"
#include "../include/game.h"
#include <stdlib.h>
#include <string.h>
const char *ttf_path = "../res/Minecraft.ttf";
const char *buttonText = "PLAY GAME AGAIN";
static SDL_Color fontColor = {0xE7, 0X4C, 0X3C, 255};
static SDL_Color buttonFontColor = { 0x3C, 0X4E, 0X60 };
void initOver(Game * game, Over * over) {
over->nameH = game->window.height * 7 / 10;
over->buttonH = game->window.height - over->nameH;
over->w = game->window.width;
over->botton.h = over->buttonH * 1 / 4;
over->botton.w = over->w * 3 / 5;
over->botton.x= ( over->w - over->botton.w ) / 2;
over->botton.y = ( over->buttonH - over->botton.h ) / 2 + over->nameH;
}
void initFont(int fontSize,Font * font, const char * text, SDL_Color color, SDL_Renderer * renderer) {
font->fontKind = TTF_OpenFont(ttf_path, fontSize);
SDL_Surface* Surface = TTF_RenderText_Blended(font->fontKind, text, color);
font->texture = SDL_CreateTextureFromSurface(renderer, Surface);
SDL_FreeSurface(Surface);
int width, height;
SDL_QueryTexture(font->texture, NULL, NULL, &width, &height);
font->w = width;
font->h = height;
}
void drawFont(Font * font, int x, int y, int width, int height, SDL_Renderer * renderer) {
SDL_Rect rect = { (width - font->w) / 2 + x, (height - font->h) / 2 + y, font->w, font->h };
SDL_RenderCopy(renderer, font->texture, NULL, &rect);
SDL_DestroyTexture(font->texture);
}
void drawButton(Font * font ,Over * over, SDL_Renderer * renderer ,Color color) {
setColor(color);
SDL_Rect rect = { over->botton.x, over->botton.y, over->botton.w, over->botton.h };
SDL_RenderFillRect(renderer, &rect);
drawFont(font, rect.x, rect.y, rect.w, rect.h, renderer);
}
void drawOver(int fontSizeName, int fontSizeButton, Font * font,const char * nameText, Color buttonColor, SDL_Renderer * renderer , Over* over) {
initFont(fontSizeName, font, nameText, fontColor, renderer);
drawFont(font, 0,0,over->w,over->nameH, renderer);
drawButton(font,over,renderer, buttonColor);
initFont(fontSizeButton, font, buttonText , buttonFontColor, renderer);
drawFont(font,over->botton.x,over->botton.y, over->botton.w, over->botton.h, renderer);
}

1
src/resource/logo.rc Normal file
View File

@ -0,0 +1 @@
id ICON "logo.ico"

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