From 4c58ebf0ba9f82f9bac61d74e8998406c59454d1 Mon Sep 17 00:00:00 2001 From: Yi Jianlong Date: Thu, 18 Jan 2024 10:44:05 +0800 Subject: [PATCH] add a raylib template: lifegame (#176) --- .../templates/raylib-lifegame/info.template | 20 +++ .../raylib-lifegame/raylib-lifegame.ico | Bin 0 -> 12862 bytes .../raylib-lifegame/raylib_lifegame_cpp.txt | 128 ++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 platform/windows/templates/raylib-lifegame/info.template create mode 100644 platform/windows/templates/raylib-lifegame/raylib-lifegame.ico create mode 100644 platform/windows/templates/raylib-lifegame/raylib_lifegame_cpp.txt diff --git a/platform/windows/templates/raylib-lifegame/info.template b/platform/windows/templates/raylib-lifegame/info.template new file mode 100644 index 00000000..7e3ce68a --- /dev/null +++ b/platform/windows/templates/raylib-lifegame/info.template @@ -0,0 +1,20 @@ +[Template] +ver=2 +Name=snake +Name[zh_CN]=生命游戏 +Icon=raylib-lifegame.ico +Description=snake game using raylib ( https://raylib.com ) +Description[zh_CN]=基于raylib的生命游戏( https://raylib.com ) +Category=Game +Category[zh_CN]=游戏 + +[Unit0] +CName=main.cpp +C=raylib_lifegame_cpp.txt + +[Project] +UnitCount=1 +Type=1 +IsCpp=0 +linker=-lraylib -lopengl32 -lgdi32 -lwinmm +ExecEncoding=UTF-8 diff --git a/platform/windows/templates/raylib-lifegame/raylib-lifegame.ico b/platform/windows/templates/raylib-lifegame/raylib-lifegame.ico new file mode 100644 index 0000000000000000000000000000000000000000..c7fdfaad6e0404b1ce268eed8458194b82b3fe14 GIT binary patch literal 12862 zcmeH~T~fm^41~=k7@qsoN2Xk$r{NqN9jTc~tS`f6()^57Y-sUjEor@$3zYI^U-x^l z_m6VDmGWLnx!TJfl^=Wg^T+rce$?-mYS{A%U3}U1*YrJuSiA*JRjlHgLb~YFX-$I8 z4EE_l$OHIv+OB`HZs{n!-_i~=jE0jZD{qF|*@#1KxQ-Rf~z#_JW_1yFVB9DfM)ljDbD^dw6h5s%x^U(gc&$h47xv`8dx-qw6q-Kr!5YJy8gidYrts5afcNO z#iD^aQPQf-Qe@h@>%yb|@BvUSK8{r~y8eedi$}TTbY9v2)R%YZ|6g@*EARe6sgPDf z^e~`~1&dffEEZ5uW-wS(0mNbr5eukeYqp-x1%pKuKrGe}v4A=jZ0Z|~y#T@%KrG$@ ukr_~A7L=b${Ot^=V^^`r3@B0ws_P%kF)PNoP391W11`k6(S7_+2Yvy2CLL%1 literal 0 HcmV?d00001 diff --git a/platform/windows/templates/raylib-lifegame/raylib_lifegame_cpp.txt b/platform/windows/templates/raylib-lifegame/raylib_lifegame_cpp.txt new file mode 100644 index 00000000..a2b1d345 --- /dev/null +++ b/platform/windows/templates/raylib-lifegame/raylib_lifegame_cpp.txt @@ -0,0 +1,128 @@ +/** + * Raylib实现的生命游戏: + * 使用鼠标设置初始状态,回车:开始演化(S:单步演化), R:复位 + * 生命游戏,又称康威生命游戏,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机 + */ +#include +#include + +class LifeGame { + public: + LifeGame(int fps = 10, std::string title = "Life Game") { + InitWindow(width, height, title.c_str()); + SetTargetFPS(fps); + } + ~LifeGame() { CloseWindow(); } + void run() { + while (!WindowShouldClose()) { + processKeyEvt(); // 处理键盘 + processMouseEvt(); // 处理鼠标 + + BeginDrawing(); + + drawFrame(); // 绘制当前帧 + + EndDrawing(); + + updateCellStates(); // 更新细胞状态 + } + } + + private: + // + // @brief 处理键盘事件 + // + void processKeyEvt() { + if (IsKeyPressed(KEY_ENTER)) { // 回车-运行 + running = true; + } + if (IsKeyPressed(KEY_R)) { // R - 复位 + running = false; + clearStates(); + } + if (IsKeyPressed(KEY_S)) { // S - 单步执行 + updateCellStates(true); + } + } + + // + // @brief 处理鼠标事件:单击改变颜色 + // + void processMouseEvt() { + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + if (!running) { + Vector2 vec = GetMousePosition(); + int x = vec.x / cell; + int y = vec.y / cell; + state[state_index][x + 1][y + 1] = (state[state_index][x + 1][y + 1] == 1) ? 0 : 1; + } + } + } + + // + // @brief 绘制当前帧 + // + void drawFrame() { + for (int i = 0; i < 60; i++) { + for (int j = 0; j < 40; j++) { + Color c = state[state_index][i + 1][j + 1] ? ORANGE : DARKGRAY; + DrawRectangle(cell * i, cell * j, cell, cell, c); + DrawRectangleLines(cell * i, cell * j, cell, cell, GREEN); + } + } + } + + // + // @brief 更新细胞存活状态 + // + void updateCellStates(bool force = false) { + if (!force) { + if (!running) return; // 没有运行,直接返回 + } + int last = state_index; + state_index = last == 1 ? 0 : 1; + + for (int i = 1; i <= cell_col; i++) { + for (int j = 1; j <= cell_row; j++) { + int sum = state[last][i - 1][j - 1] + state[last][i - 1][j] + state[last][i - 1][j + 1] + + state[last][i][j - 1] + state[last][i][j + 1] + state[last][i + 1][j - 1] + state[last][i + 1][j] + + state[last][i + 1][j + 1]; + if (state[last][i][j]) { + state[state_index][i][j] = (sum < 2 || sum > 3) ? 0 : 1; + } else { + state[state_index][i][j] = (sum == 3) ? 1 : 0; + } + } + } + } + // + // @brief 清除状态 + // + void clearStates() { + state_index = 0; + for (int i = 1; i <= cell_col; i++) { + for (int j = 1; j <= cell_row; j++) { + state[state_index][i][j] = 0; + } + } + } + + private: + static constexpr float width = 1200; + static constexpr float height = 800; + static constexpr float cell = 25; + static constexpr int cell_col = width / cell; + static constexpr int cell_row = height / cell; + + char state[2][cell_col + 2][cell_row + 2] = {0}; + int state_index = 0; + + bool running = false; +}; + +int main(void) { + LifeGame game; + game.run(); + + return 0; +} \ No newline at end of file