1
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 673 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.0 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+106
-38
@@ -41,22 +41,22 @@ public:
|
||||
_spr.CancelRef();
|
||||
break;
|
||||
case MapNodeState::FOOD:
|
||||
_spr = g_factory->CreateSpriteDefault(ColorDef::RED);
|
||||
_spr = g_factory->CreateSprite("apple");
|
||||
_spr->SetOrder(0.8f);
|
||||
// 使用共享的Coor
|
||||
_spr->SetCoor(_coor);
|
||||
// 设置精灵的大小和位置
|
||||
Position pos = toPosition(_xy * MAP_NODE_SIZE);
|
||||
_spr->SetQuad(createQuad(pos, pos + Position{10.0f,10.0f}));
|
||||
_spr->SetQuad(createQuad(pos, pos + Position{12.0f,12.0f}));
|
||||
break;
|
||||
case MapNodeState::BODY:
|
||||
_spr = g_factory->CreateSpriteDefault(ColorDef::YELLOW);
|
||||
_spr = g_factory->CreateSprite("body");
|
||||
_spr->SetOrder(0.8f);
|
||||
// 使用共享的Coor
|
||||
_spr->SetCoor(_coor);
|
||||
// 设置精灵的大小和位置
|
||||
Position posBody = toPosition(_xy * MAP_NODE_SIZE);
|
||||
_spr->SetQuad(createQuad(posBody, posBody + Position{10.0f,10.0f}));
|
||||
_spr->SetQuad(createQuad(posBody, posBody + Position{12.0f,12.0f}));
|
||||
break;
|
||||
case MapNodeState::WALL:
|
||||
_spr = g_factory->CreateSpriteDefault(ColorDef::GRAY);
|
||||
@@ -65,7 +65,7 @@ public:
|
||||
_spr->SetCoor(_coor);
|
||||
// 设置精灵的大小和位置
|
||||
Position posWall = toPosition(_xy * MAP_NODE_SIZE);
|
||||
_spr->SetQuad(createQuad(posWall, posWall + Position{10.0f,10.0f}));
|
||||
_spr->SetQuad(createQuad(posWall, posWall + Position{12.0f,12.0f}));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -154,13 +154,32 @@ public:
|
||||
return MapNodeState::PASS;
|
||||
}
|
||||
|
||||
void RenderMap() {
|
||||
void RenderMap(const std::vector<PointU>& snakeBody) {
|
||||
for (size_t i = 0; i < _vec.size(); ++i)
|
||||
{
|
||||
_vec[i].RenderNode();
|
||||
// 不渲染蛇头的位置,留待Snake::Render()渲染
|
||||
bool isHead = false;
|
||||
if (!snakeBody.empty()) {
|
||||
PointU head = snakeBody.front();
|
||||
// 检查head坐标是否有效
|
||||
if (head.x < MAP_SIZE[0] && head.y < MAP_SIZE[1]) {
|
||||
unsigned idx = MAP_SIZE[0] * head.y + head.x;
|
||||
if (idx == i) {
|
||||
isHead = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isHead) {
|
||||
_vec[i].RenderNode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
Coor* GetCoor() const {
|
||||
return _coor;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<MapNode> _vec;
|
||||
Coor* _coor; // 所有精灵共享的Coor
|
||||
@@ -235,19 +254,21 @@ public:
|
||||
}
|
||||
|
||||
// 检查是否吃到食物
|
||||
if (map.GetNodeState(head) == MapNodeState::FOOD) {
|
||||
_grow = true;
|
||||
score += 10; // 增加分数
|
||||
// 创建新食物
|
||||
PointU foodPos;
|
||||
do {
|
||||
foodPos = {
|
||||
Random::Uint(1, MAP_SIZE[0] - 2),
|
||||
Random::Uint(1, MAP_SIZE[1] - 2)
|
||||
};
|
||||
} while (map.GetNodeState(foodPos) != MapNodeState::PASS);
|
||||
map.CreateFood(foodPos);
|
||||
} else {
|
||||
if (map.GetNodeState(head) == MapNodeState::FOOD) {
|
||||
_grow = true;
|
||||
score += 10; // 增加分数
|
||||
// 播放吃到食物的音效
|
||||
snd_play("eat");
|
||||
// 创建新食物
|
||||
PointU foodPos;
|
||||
do {
|
||||
foodPos = {
|
||||
Random::Uint(1, MAP_SIZE[0] - 2),
|
||||
Random::Uint(1, MAP_SIZE[1] - 2)
|
||||
};
|
||||
} while (map.GetNodeState(foodPos) != MapNodeState::PASS);
|
||||
map.CreateFood(foodPos);
|
||||
} else {
|
||||
// 移除尾部
|
||||
if (!_grow) {
|
||||
PointU tail = _body.back();
|
||||
@@ -265,10 +286,52 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
void Render() {
|
||||
// 渲染蛇身
|
||||
for (const auto& bodyPart : _body) {
|
||||
// 蛇身已经通过Map渲染,这里可以添加特殊效果
|
||||
void Render(Coor* coor) {
|
||||
// 渲染蛇头
|
||||
if (!_body.empty() && coor) {
|
||||
PointU head = _body.front();
|
||||
gcSprite headSprite = g_factory->CreateSprite("right");
|
||||
if (headSprite) {
|
||||
headSprite->SetOrder(0.7f);
|
||||
|
||||
// 使用精灵自身的AddCoor方法创建坐标系
|
||||
headSprite->SetCoor(coor);
|
||||
Coor* headCoor = headSprite->AddCoor();
|
||||
if (headCoor) {
|
||||
// 计算蛇头的位置
|
||||
Position pos = toPosition(head * MAP_NODE_SIZE);
|
||||
|
||||
// 根据移动方向旋转蛇头(弧度制)
|
||||
float rotate = 0.0f;
|
||||
switch (_dir) {
|
||||
case Direction::UP:
|
||||
rotate = 3.1415926535f / 2.0f; // 90度
|
||||
// 调整位置,使旋转后蛇头保持在正确位置
|
||||
headCoor->SetPosition(pos + Position{12.0f, 0.0f});
|
||||
break;
|
||||
case Direction::DOWN:
|
||||
rotate = 3.0f * 3.1415926535f / 2.0f; // 270度
|
||||
// 调整位置,使旋转后蛇头保持在正确位置
|
||||
headCoor->SetPosition(pos + Position{0.0f, 12.0f});
|
||||
break;
|
||||
case Direction::LEFT:
|
||||
rotate = 3.1415926535f; // 180度
|
||||
// 调整位置,使旋转后蛇头保持在正确位置
|
||||
headCoor->SetPosition(pos + Position{12.0f, 12.0f});
|
||||
break;
|
||||
case Direction::RIGHT:
|
||||
rotate = 0.0f; // 0度
|
||||
// 不需要调整位置
|
||||
headCoor->SetPosition(pos);
|
||||
break;
|
||||
}
|
||||
|
||||
headSprite->SetQuad(createQuad(Position{0.0f, 0.0f}, Position{12.0f, 12.0f}));
|
||||
headCoor->SetRotate(rotate);
|
||||
|
||||
headSprite->Render();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,6 +393,9 @@ public:
|
||||
} while (_map.GetNodeState(foodPos) != MapNodeState::PASS);
|
||||
_map.CreateFood(foodPos);
|
||||
|
||||
// 播放游戏开始音效
|
||||
snd_play("click");
|
||||
|
||||
_gameOver = false;
|
||||
_gameStarted = true;
|
||||
}
|
||||
@@ -380,18 +446,20 @@ public:
|
||||
// 更新游戏逻辑
|
||||
if (!_gameOver) {
|
||||
if (!_snake.Update(g_time->GetDelta(), _map, _score)) {
|
||||
_gameOver = true;
|
||||
// 显示游戏结束信息
|
||||
_txtGameOver = g_factory->CreateTextDefault();
|
||||
_txtGameOver->SetFontSize(36);
|
||||
_txtGameOver->SetString("Game Over!");
|
||||
_txtGameOver->SetOrder(0.4f);
|
||||
|
||||
_txtRestart = g_factory->CreateTextDefault();
|
||||
_txtRestart->SetFontSize(24);
|
||||
_txtRestart->SetString("Press R to restart");
|
||||
_txtRestart->SetOrder(0.4f);
|
||||
}
|
||||
_gameOver = true;
|
||||
// 播放游戏结束音效
|
||||
snd_play("end");
|
||||
// 显示游戏结束信息
|
||||
_txtGameOver = g_factory->CreateTextDefault();
|
||||
_txtGameOver->SetFontSize(36);
|
||||
_txtGameOver->SetString("Game Over!");
|
||||
_txtGameOver->SetOrder(0.4f);
|
||||
|
||||
_txtRestart = g_factory->CreateTextDefault();
|
||||
_txtRestart->SetFontSize(24);
|
||||
_txtRestart->SetString("Press R to restart");
|
||||
_txtRestart->SetOrder(0.4f);
|
||||
}
|
||||
} else {
|
||||
if (_txtGameOver) {
|
||||
// 每次渲染时重新计算位置,确保在屏幕中间
|
||||
@@ -410,8 +478,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
_map.RenderMap();
|
||||
_snake.Render();
|
||||
_map.RenderMap(_snake.GetBody());
|
||||
_snake.Render(_map.GetCoor());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ int Main()
|
||||
log_msg("snake start");
|
||||
|
||||
g_factory->SetPath("img/","model/","out/","temp/");
|
||||
|
||||
// 设置音频路径
|
||||
g_audio->SetPath("sound/");
|
||||
|
||||
g_system->SetFuncInit(Init);
|
||||
g_system->SetFuncRun(Run);
|
||||
|
||||
Reference in New Issue
Block a user