56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
#ifndef GAME_H
|
||
#define GAME_H
|
||
|
||
#include <Arduino.h>
|
||
#include "ui.h" // for DisplayList and colors
|
||
|
||
namespace esp32 {
|
||
namespace game {
|
||
|
||
using namespace esp32::ui;
|
||
|
||
// 棋盘尺寸(经典 10x20)
|
||
static const int COLS = 10;
|
||
static const int ROWS = 20;
|
||
|
||
enum class State : uint8_t { Playing = 0, GameOver = 1 };
|
||
|
||
struct Piece {
|
||
uint8_t type; // 0..6
|
||
int8_t x; // 左上角/参考点
|
||
int8_t y;
|
||
// 仅使用一个朝向(简化:不提供旋转)
|
||
int8_t blocks[4][2]; // 4 个方块偏移
|
||
uint16_t color;
|
||
};
|
||
|
||
struct Tetris {
|
||
uint8_t board[ROWS][COLS]; // 0=空,其它=颜色索引或标记
|
||
uint16_t colors[8]; // 颜色表(index->RGB565)
|
||
Piece current;
|
||
uint32_t lastDropMs = 0;
|
||
uint32_t dropInterval = 700; // ms
|
||
State state = State::Playing;
|
||
uint32_t score = 0;
|
||
uint32_t rng = 1;
|
||
};
|
||
|
||
void init(Tetris& g);
|
||
void reset(Tetris& g);
|
||
void update(Tetris& g, uint32_t nowMs);
|
||
|
||
// 输入
|
||
void moveLeft(Tetris& g);
|
||
void moveRight(Tetris& g);
|
||
void hardDrop(Tetris& g);
|
||
|
||
// UI 构建
|
||
void buildScene(const Tetris& g, DisplayList& out,
|
||
int16_t screenW, int16_t screenH,
|
||
int16_t cell, int16_t originX, int16_t originY);
|
||
|
||
} // namespace game
|
||
} // namespace esp32
|
||
|
||
#endif // GAME_H
|