软件

SNAKE_esp.1200258

本页内容由 AI 翻译,可能与原文有出入
游戏娱乐

无

1233

// SPDX-License-Identifier: Apache-2.0 #pragma once

#define GAME_NAME "snake" /* A 20x20 grid fills most of the square RGB565 display. */ enum { GAME_WIDTH = 480, GAME_HEIGHT = 480, GAME_COLUMNS = 20, GAME_ROWS = 20, GAME_TICK_HZ = 30, GAME_TICKS_PER_CELL = 6, }; #define GAME_RANDOM_SEED 0x534e414bU enum { GAME_ACTION_PAUSE = 3, GAME_ACTION_RESET = 4 };


### `components/app_game/include/app_game.h`

```c
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define APP_GAME_MAX_CELLS 400U

typedef struct app_game *app_game_handle_t;

typedef enum {
    APP_GAME_OK = 0,
    APP_GAME_INVALID_ARGUMENT,
    APP_GAME_NO_MEMORY,
} app_game_err_t;

typedef enum {
    APP_GAME_UP = 0,
    APP_GAME_RIGHT,
    APP_GAME_DOWN,
    APP_GAME_LEFT,
} app_game_direction_t;

typedef enum {
    APP_GAME_RUNNING = 0,
    APP_GAME_PAUSED,
    APP_GAME_OVER,
    APP_GAME_WON,
} app_game_phase_t;

typedef struct {
    uint16_t columns;
    uint16_t rows;
    uint16_t ticks_per_cell;
    uint32_t seed;
} app_game_config_t;

typedef struct {
    uint32_t tick;
    uint32_t score;
    uint16_t length;
    uint16_t snake[APP_GAME_MAX_CELLS]; /**< Head first, row-major cell indices. */
    uint16_t food;
    app_game_direction_t direction;
    app_game_phase_t phase;
    bool tilt_calibrated;
} app_game_snapshot_t;

app_game_err_t app_game_create(const app_game_config_t *config, app_game_handle_t *out_handle);
void app_game_destroy(app_game_handle_t *handle);
void app_game_reset(app_game_handle_t handle);
void app_game_toggle_pause(app_game_handle_t handle);
void app_game_set_direction(app_game_handle_t handle, app_game_direction_t direction);
void app_game_set_tilt(app_game_handle_t handle, float x_g, float y_g, float z_g);
void app_game_update(app_game_handle_t handle);
app_game_snapshot_t app_game_read(app_game_handle_t handle);

#ifdef __cplusplus
}
#endif

components/app_game/app_game.c

// SPDX-License-Identifier: Apache-2.0 #include <stdlib.h> #include "include/app_game.h"

#define TILT_DEADZONE_G 0.28f #define IMU_MIN_MAGNITUDE_SQUARED 0.42f #define IMU_MAX_MAGNITUDE_SQUARED 2.10f

struct app_game { app_game_config_t config; app_game_snapshot_t state; uint32_t rng; uint16_t move_counter; app_game_direction_t queued_direction; bool turn_queued; bool tilt_calibrated; float neutral_x; float neutral_y; float neutral_z; };

static uint32_t next_random(app_game_handle_t game) { uint32_t value = game->rng; value ^= value << 13; value ^= value >> 17; value ^= value << 5; game->rng = value ? value : 0x6d2b79f5U; return game->rng; }

static bool is_opposite(app_game_direction_t first, app_game_direction_t second) { return ((uint32_t)first + 2U) % 4U == (uint32_t)second; }

static bool cell_is_occupied(app_game_handle_t game, uint16_t cell) { for (uint16_t index = 0; index < game->state.length; ++index) { if (game->state.snake[index] == cell) return true; } return false; }

static bool place_food(app_game_handle_t game) { const uint16_t total = (uint16_t)(game->config.columns * game->config.rows); if (game->state.length >= total) return false; uint16_t candidate = (uint16_t)(next_random(game) % total); for (uint16_t checked = 0; checked < total; ++checked) { if (!cell_is_occupied(game, candidate)) { game->state.food = candidate; return true; } candidate = (uint16_t)((candidate + 1U) % total); } return false; }

static void reset_state(app_game_handle_t game) { game->state = (app_game_snapshot_t) { .direction = APP_GAME_RIGHT, .phase = APP_GAME_RUNNING, .length = 3, }; const uint16_t center_x = game->config.columns / 2U; const uint16_t center_y = game->config.rows / 2U; const uint16_t head = (uint16_t)(center_y * game->config.columns + center_x); game->state.snake[0] = head; game->state.snake[1] = (uint16_t)(head - 1U); game->state.snake[2] = (uint16_t)(head - 2U); game->rng = game->config.seed ? game->config.seed : 0x4d4f5341U; game->move_counter = 0; game->queued_direction = APP_GAME_RIGHT; game->turn_queued = false; game->tilt_calibrated = false; game->neutral_x = 0.0f; game->neutral_y = 0.0f; game->neutral_z = 0.0f; (void)place_food(game); }

app_game_err_t app_game_create(const app_game_config_t *config, app_game_handle_t *out_handle) { if (!out_handle) return APP_GAME_INVALID_ARGUMENT; *out_handle = NULL; if (!config || config->columns < 3 || config->rows < 3 || config->ticks_per_cell < 2 || (uint32_t)config->columns * config->rows > APP_GAME_MAX_CELLS) { return APP_GAME_INVALID_ARGUMENT; } app_game_handle_t game = calloc(1, sizeof(*game)); if (!game) return APP_GAME_NO_MEMORY; game->config = *config; reset_state(game); *out_handle = game; return APP_GAME_OK; }

void app_game_destroy(app_game_handle_t *handle) { if (handle) { free(*handle); *handle = NULL; } }

void app_game_reset(app_game_handle_t game) { if (game) reset_state(game); }

void app_game_toggle_pause(app_game_handle_t game) { if (!game) return; if (game->state.phase == APP_GAME_RUNNING) game->state.phase = APP_GAME_PAUSED; else if (game->state.phase == APP_GAME_PAUSED) game->state.phase = APP_GAME_RUNNING; }

void app_game_set_direction(app_game_handle_t game, app_game_direction_t direction) { if (!game || game->state.phase != APP_GAME_RUNNING || (unsigned)direction > APP_GAME_LEFT || game->turn_queued || is_opposite(game->state.direction, direction) || direction == game->state.direction) { return; } game->queued_direction = direction; game->turn_queued = true; }

void app_game_set_tilt(app_game_handle_t game, float x_g, float y_g, float z_g) { if (!game || game->state.phase != APP_GAME_RUNNING || x_g < -1.45f || x_g > 1.45f || y_g < -1.45f || y_g > 1.45f || z_g < -1.45f || z_g > 1.45f) { return; } const float magnitude_squared = x_g * x_g + y_g * y_g + z_g * z_g; if (magnitude_squared < IMU_MIN_MAGNITUDE_SQUARED || magnitude_squared > IMU_MAX_MAGNITUDE_SQUARED) { return; } if (!game->tilt_calibrated) { game->neutral_x = x_g; game->neutral_y = y_g; game->neutral_z = z_g; game->tilt_calibrated = true; game->state.tilt_calibrated = true; return; }

const float delta_x = x_g - game->neutral_x;
const float delta_y = y_g - game->neutral_y;
const float abs_x = delta_x < 0.0f ? -delta_x : delta_x;
const float abs_y = delta_y < 0.0f ? -delta_y : delta_y;
if (abs_x < TILT_DEADZONE_G && abs_y < TILT_DEADZONE_G) return;

if (abs_x > abs_y * 1.15f) {
    app_game_set_direction(game, delta_x > 0.0f ? APP_GAME_RIGHT : APP_GAME_LEFT);
} else if (abs_y > abs_x * 1.15f) {
    app_game_set_direction(game, delta_y > 0.0f ? APP_GAME_DOWN : APP_GAME_UP);
}

}

static void advance_snake(app_game_handle_t game) { const uint16_t columns = game->config.columns; const uint16_t rows = game->config.rows; const uint16_t head = game->state.snake[0]; const uint16_t x = (uint16_t)(head % columns); const uint16_t y = (uint16_t)(head / columns); if (game->turn_queued) game->state.direction = game->queued_direction; game->turn_queued = false;

uint16_t next_head = head;
switch (game->state.direction) {
case APP_GAME_UP:
    if (y == 0) { game->state.phase = APP_GAME_OVER; return; }
    next_head = (uint16_t)(head - columns);
    break;
case APP_GAME_RIGHT:
    if (x + 1U >= columns) { game->state.phase = APP_GAME_OVER; return; }
    next_head = (uint16_t)(head + 1U);
    break;
case APP_GAME_DOWN:
    if (y + 1U >= rows) { game->state.phase = APP_GAME_OVER; return; }
    next_head = (uint16_t)(head + columns);
    break;
case APP_GAME_LEFT:
    if (x == 0) { game->state.phase = APP_GAME_OVER; return; }
    next_head = (uint16_t)(head - 1U);
    break;
default:
    game->state.phase = APP_GAME_OVER;
    return;
}

const bool grows = next_head == game->state.food;
for (uint16_t index = 0; index < game->state.length; ++index) {
    if (!grows && index == game->state.length - 1U) break;
    if (game->state.snake[index] == next_head) {
        game->state.phase = APP_GAME_OVER;
        return;
    }
}

uint16_t next_length = game->state.length;
if (grows && next_length < APP_GAME_MAX_CELLS) ++next_length;
for (uint16_t index = next_length; index > 1U; --index) {
    game->state.snake[index - 1U] = game->state.snake[index - 2U];
}
game->state.snake[0] = next_head;
game->state.length = next_length;
if (grows) {
    ++game->state.score;
    if (!place_food(game)) game->state.phase = APP_GAME_WON;
}

}

void app_game_update(app_game_handle_t game) { if (!game || game->state.phase != APP_GAME_RUNNING) return; ++game->state.tick; uint16_t ticks_per_cell = game->config.ticks_per_cell; const uint32_t speed_steps = game->state.score / 4U; if (speed_steps < ticks_per_cell - 2U) { ticks_per_cell = (uint16_t)(ticks_per_cell - speed_steps); } else { ticks_per_cell = 2U; } if (++game->move_counter >= ticks_per_cell) { game->move_counter = 0; advance_snake(game); } }

app_game_snapshot_t app_game_read(app_game_handle_t game) { return game ? game->state : (app_game_snapshot_t) {0}; }

components/app_game_runtime/include/app_game_runtime.h

// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "raylib_lite_game_module.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Return the immutable Engine ABI v1 descriptor; never NULL, never free it.
 * Thread-safe, nonblocking and allocation-free.
 * Callback lifecycle, return values and timing: ../README.md. */
const raylib_lite_game_module_v1_t *app_game_runtime_module(void);

#ifdef __cplusplus
}
#endif

components/app_game_runtime/app_game_runtime.c

// SPDX-License-Identifier: Apache-2.0 #include <stdio.h> #include "include/app_game_runtime.h" #include "../app_game/include/app_game.h" #include "private/game_config.h" #include "raylib_lite_raylib.h"

#define BOARD_X 40 #define BOARD_Y 64 #define CELL_PX 20

typedef struct { app_game_handle_t game; } host_state_t;

static const Color C_BACKGROUND = {7, 12, 20, 255}; static const Color C_BOARD = {12, 22, 31, 255}; static const Color C_GRID = {28, 44, 55, 255}; static const Color C_SNAKE = {55, 205, 133, 255}; static const Color C_HEAD = {174, 255, 96, 255}; static const Color C_FOOD = {255, 97, 82, 255}; static const Color C_TEXT = {224, 237, 243, 255}; static const Color C_MUTED = {111, 139, 153, 255}; static const Color C_OVERLAY = {5, 11, 18, 232};

static int initialize(void *value, const char *asset_root) { (void)asset_root; host_state_t *state = value; const app_game_config_t config = { .columns = GAME_COLUMNS, .rows = GAME_ROWS, .ticks_per_cell = GAME_TICKS_PER_CELL, .seed = GAME_RANDOM_SEED, }; if (app_game_create(&config, &state->game) != APP_GAME_OK) return -1; InitWindow(GAME_WIDTH, GAME_HEIGHT, GAME_NAME); SetTargetFPS(GAME_TICK_HZ); return 0; }

static void shutdown(void *value) { host_state_t *state = value; app_game_destroy(&state->game); }

static void input(void *value, const raylib_lite_game_input_v1_t *event) { host_state_t *state = value; if (!state || !state->game || !event) return; if (event->type == RAYLIB_LITE_GAME_INPUT_IMU) { app_game_set_tilt(state->game, event->value_x, event->value_y, event->value_z); } else if (event->type == RAYLIB_LITE_GAME_INPUT_POINTER && event->pressed) { const app_game_snapshot_t game = app_game_read(state->game); if (game.phase == APP_GAME_OVER || game.phase == APP_GAME_WON) { app_game_reset(state->game); } else { app_game_toggle_pause(state->game); } } else if (event->type == RAYLIB_LITE_GAME_INPUT_CONTROL) { switch (event->code) { case RAYLIB_LITE_GAME_CONTROL_PAUSE: app_game_toggle_pause(state->game); break; case RAYLIB_LITE_GAME_CONTROL_RESUME: if (app_game_read(state->game).phase == APP_GAME_PAUSED) app_game_toggle_pause(state->game); break; case RAYLIB_LITE_GAME_CONTROL_RESET: app_game_reset(state->game); break; default: break; } } else if (event->type == RAYLIB_LITE_GAME_INPUT_ACTION) { if (event->code == GAME_ACTION_RESET && event->pressed) { app_game_reset(state->game); } else if (event->code == GAME_ACTION_PAUSE && event->pressed) { app_game_toggle_pause(state->game); } else if (event->pressed) { switch (event->code) { case 0: app_game_set_direction(state->game, APP_GAME_LEFT); break; case 1: app_game_set_direction(state->game, APP_GAME_RIGHT); break; case 2: app_game_set_direction(state->game, APP_GAME_UP); break; case 5: app_game_set_direction(state->game, APP_GAME_DOWN); break; default: break; } } } }

static void update(void *value) { app_game_update(((host_state_t *)value)->game); }

static void draw_grid(void) { const int board_size = GAME_COLUMNS * CELL_PX; DrawRectangle(BOARD_X, BOARD_Y, board_size, board_size, C_BOARD); for (int line = 0; line <= GAME_COLUMNS; ++line) { const int offset = line * CELL_PX; DrawLine(BOARD_X + offset, BOARD_Y, BOARD_X + offset, BOARD_Y + board_size, C_GRID); DrawLine(BOARD_X, BOARD_Y + offset, BOARD_X + board_size, BOARD_Y + offset, C_GRID); } }

static void draw_game(const app_game_snapshot_t *game) { char score_text[32]; snprintf(score_text, sizeof(score_text), "SCORE %lu", (unsigned long)game->score); DrawText("SNAKE", 24, 12, 22, C_HEAD); DrawText(score_text, 24, 36, 14, C_TEXT); const char *hint = game->phase == APP_GAME_PAUSED ? "PAUSED" : (game->tilt_calibrated ? "TILT TO STEER" : "HOLD LEVEL"); DrawText(hint, 310, 27, 14, C_MUTED);

draw_grid();
const int food_x = BOARD_X + (game->food % GAME_COLUMNS) * CELL_PX;
const int food_y = BOARD_Y + (game->food / GAME_COLUMNS) * CELL_PX;
DrawCircle(food_x + CELL_PX / 2, food_y + CELL_PX / 2, 7, C_FOOD);
for (uint16_t index = game->length; index > 0; --index) {
    const uint16_t cell = game->snake[index - 1U];
    const int x = BOARD_X + (cell % GAME_COLUMNS) * CELL_PX;
    const int y = BOARD_Y + (cell / GAME_COLUMNS) * CELL_PX;
    const int inset = index == 1U ? 2 : 3;
    DrawRectangle(x + inset, y + inset, CELL_PX - inset * 2,
                  CELL_PX - inset * 2, index == 1U ? C_HEAD : C_SNAKE);
}
DrawText("Tilt left / right / forward / back  |  Tap to pause",
         42, 468, 10, C_MUTED);

if (game->phase == APP_GAME_OVER || game->phase == APP_GAME_WON) {
    DrawRectangle(BOARD_X, BOARD_Y + 152, GAME_COLUMNS * CELL_PX, 96, C_OVERLAY);
    DrawText(game->phase == APP_GAME_WON ? "YOU WIN" : "GAME OVER",
             172, BOARD_Y + 170, 28, game->phase == APP_GAME_WON ? C_HEAD : C_FOOD);
    DrawText("Tap or press Enter to play again", 112, BOARD_Y + 210, 16, C_TEXT);
} else if (game->phase == APP_GAME_PAUSED) {
    DrawRectangle(BOARD_X, BOARD_Y + 168, GAME_COLUMNS * CELL_PX, 64, C_OVERLAY);
    DrawText("TAP TO RESUME", 162, BOARD_Y + 190, 20, C_TEXT);
}

}

static int render(void *value) { const host_state_t *state = value; if (!state || !state->game) return -1; BeginDrawing(); if (!raylib_lite_raylib_frame_available()) { EndDrawing(); return -1; } ClearBackground(C_BACKGROUND); const app_game_snapshot_t game = app_game_read(state->game); draw_game(&game); EndDrawing(); return 0; }

static uint32_t state_hash(const void *value) { const app_game_snapshot_t game = app_game_read(((const host_state_t *)value)->game); uint32_t hash = 2166136261U; hash = (hash ^ game.tick) * 16777619U; hash = (hash ^ game.score) * 16777619U; hash = (hash ^ game.length) * 16777619U; hash = (hash ^ game.food) * 16777619U; hash = (hash ^ (uint32_t)game.direction) * 16777619U; hash = (hash ^ (uint32_t)game.phase) * 16777619U; for (uint16_t index = 0; index < game.length; ++index) hash = (hash ^ game.snake[index]) * 16777619U; return hash; }

static const char *phase_name(app_game_phase_t phase) { switch (phase) { case APP_GAME_RUNNING: return "running"; case APP_GAME_PAUSED: return "paused"; case APP_GAME_OVER: return "game_over"; case APP_GAME_WON: return "won"; default: return "unknown"; } }

static const char *direction_name(app_game_direction_t direction) { static const char *const names[] = {"up", "right", "down", "left"}; return (unsigned)direction < 4U ? names[direction] : "unknown"; }

static int state_json(const void *value, char *output, size_t capacity) { if (!output || !capacity) return -1; const app_game_snapshot_t game = app_game_read(((const host_state_t *)value)->game); const uint16_t head = game.length ? game.snake[0] : 0; const int size = snprintf(output, capacity, "{"phase":"%s","tick":%lu,"score":%lu,"length":%u," ""head_x":%u,"head_y":%u,"food_x":%u,"food_y":%u," ""direction":"%s","tilt_calibrated":%s,"state_hash":"%08lx"}", phase_name(game.phase), (unsigned long)game.tick, (unsigned long)game.score, game.length, head % GAME_COLUMNS, head / GAME_COLUMNS, game.food % GAME_COLUMNS, game.food / GAME_COLUMNS, direction_name(game.direction), game.tilt_calibrated ? "true" : "false", (unsigned long)state_hash(value)); return size < 0 || (size_t)size >= capacity ? -1 : size; }

static const raylib_lite_game_module_v1_t s_module = { .descriptor = { RAYLIB_LITE_GAME_MODULE_ABI_V1, GAME_NAME, GAME_NAME, GAME_WIDTH, GAME_HEIGHT, GAME_TICK_HZ, 1 }, .state_size = sizeof(host_state_t), .initialize = initialize, .shutdown = shutdown, .input = input, .update = update, .render = render, .state_hash = state_hash, .state_json = state_json, };

const raylib_lite_game_module_v1_t *app_game_runtime_module(void) { return &s_module; }

main/game_entry.c

// SPDX-License-Identifier: Apache-2.0
/* The Engine supplies app_main and startup order. This file only selects our
 * application adapter. Use its public header for the Host source-list runner. */
#include "../components/app_game_runtime/include/app_game_runtime.h"

const raylib_lite_game_module_v1_t *raylib_lite_game_module_v1(void)
{
    return app_game_runtime_module();
}

game.sim.json

{
  "schema": "raylib-lite-game-sim/v1",
  "sources": [
    "main/game_entry.c",
    "components/app_game/app_game.c",
    "components/app_game_runtime/app_game_runtime.c"
  ]
}

评论(0)