NEW一个FINDBOOM
This commit is contained in:
@ -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
|
||||
|
||||
15
include/data.h
Normal file
15
include/data.h
Normal 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
|
||||
29
include/game.h
Normal file
29
include/game.h
Normal 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
11
include/img.h
Normal 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
|
||||
59
src/data.c
Normal file
59
src/data.c
Normal 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
171
src/game.c
Normal 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
10
src/img.c
Normal 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);
|
||||
}
|
||||
}
|
||||
18
src/main.c
18
src/main.c
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user