Files
findboom/src/context.c
2025-07-16 18:48:06 +08:00

58 lines
1.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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