初具模型的扫雷
This commit is contained in:
57
src/context.c
Normal file
57
src/context.c
Normal file
@ -0,0 +1,57 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user