#include #include #include #include #include namespace snake { int score; } // namespace snake namespace snake::scene { Vector2 gridPos = { 16, 54 }; constexpr int CELL_SIZE = 24; constexpr int MAX_CELLX = 32; constexpr int MAX_CELLY = 22; float speed; bool isDead = false; enum SnakeDir { Up, Down, Left, Right, }; struct SnakeSegment { Vector2 cell; Color color; }; struct Snake { std::vector body; SnakeDir dir; SnakeDir nextDir; }; struct Food { Vector2 cell; Color color; }; Snake snek = {}; Food food = { 0 }; int prevGridX = 0; int prevGridY = 0; void placeFood() { food.cell = { static_cast(GetRandomValue(0, MAX_CELLX - 1)), static_cast(GetRandomValue(0, MAX_CELLY - 1)), }; Color foodCol; foodCol.r = GetRandomValue(100, 255); foodCol.g = GetRandomValue(100, 255); foodCol.b = GetRandomValue(100, 255); foodCol.a = 255; food.color = foodCol; } bool selfCollide() { const Vector2 &head = snek.body[0].cell; int headX = static_cast(head.x); int headY = static_cast(head.y); for (size_t i = 1; i < snek.body.size(); ++i) { int bodyX = static_cast(snek.body[i].cell.x); int bodyY = static_cast(snek.body[i].cell.y); if (headX == bodyX && headY == bodyY) return true; } return false; } bool GameMain::Init() { score = 0; speed = 2.5f; snek.body.clear(); snek.body.push_back({ { 10.f, 10.f }, RED }); snek.body.push_back({ { 9.f, 10.f }, GREEN }); snek.body.push_back({ { 8.f, 10.f }, BLUE }); snek.dir = Right; snek.nextDir = snek.dir; prevGridX = 10; prevGridY = 10; placeFood(); return true; } Vector2 oldPos; bool GameMain::Update() { float deltaTime = GetFrameTime() * speed; if (deltaTime > 1.f / 5) deltaTime = 1.f / 5; if (snek.dir == Left || snek.dir == Right) { if (IsKeyPressed(KEY_UP)) snek.nextDir = Up; else if (IsKeyPressed(KEY_DOWN)) snek.nextDir = Down; } else { if (IsKeyPressed(KEY_LEFT)) snek.nextDir = Left; else if (IsKeyPressed(KEY_RIGHT)) snek.nextDir = Right; } if ((snek.nextDir == Left && snek.dir == Right) || (snek.nextDir == Right && snek.dir == Left) || (snek.nextDir == Up && snek.dir == Down) || (snek.nextDir == Down && snek.dir == Up)) { snek.nextDir = snek.dir; } oldPos = snek.body[0].cell; if (snek.dir == Left) snek.body[0].cell.x -= 1 * deltaTime; else if (snek.dir == Right) snek.body[0].cell.x += 1 * deltaTime; else if (snek.dir == Up) snek.body[0].cell.y -= 1 * deltaTime; else if (snek.dir == Down) snek.body[0].cell.y += 1 * deltaTime; int curGridX = static_cast(snek.body[0].cell.x); int curGridY = static_cast(snek.body[0].cell.y); if ((curGridX != prevGridX) || (curGridY != prevGridY)) { snek.dir = snek.nextDir; for (size_t i = snek.body.size() - 1; i > 0; --i) snek.body[i].cell = snek.body[i - 1].cell; snek.body[1].cell = oldPos; prevGridX = curGridX; prevGridY = curGridY; if (selfCollide()) { snake::PlaySFX(snake::Die, 1.f); snake::Close(); snake::s = snake::GameOver; snake::Init(); return true; } } // フードを食べたかどうか確認 if ((curGridX == static_cast(food.cell.x)) && (curGridY == static_cast(food.cell.y))) { score += 100; speed += .5f; SnakeSegment seg = snek.body.back(); seg.color = food.color; snek.body.push_back(seg); snake::PlaySFX(snake::Food, 1.f); placeFood(); } // 壁を打ったかどうか確認 if ((snek.body[0].cell.x < 0 || snek.body[0].cell.x > MAX_CELLX) || (snek.body[0].cell.y < 0 || snek.body[0].cell.y > MAX_CELLY)) { snake::PlaySFX(snake::Die, 1.f); snake::Close(); snake::s = snake::GameOver; snake::Init(); return true; } ClearBackground(BLACK); // グリッド int cX = static_cast(gridPos.x); int cY = static_cast(gridPos.y); for (int h = 0; h <= MAX_CELLY; ++h) { int y = cY + h * CELL_SIZE; DrawLine(cX, y, cX + MAX_CELLX * CELL_SIZE, y, RAYWHITE); } for (int w = 0; w <= MAX_CELLX; ++w) { int x = cX + w * CELL_SIZE; DrawLine(x, cY, x, cY + MAX_CELLY * CELL_SIZE, RAYWHITE); } // プレイヤー for (const auto &seg : snek.body) { int pX = static_cast(seg.cell.x) * CELL_SIZE; int pY = static_cast(seg.cell.y) * CELL_SIZE; DrawRectangle(cX + pX, cY + pY, CELL_SIZE, CELL_SIZE, seg.color); } // フード int fX = static_cast(food.cell.x) * CELL_SIZE; int fY = static_cast(food.cell.y) * CELL_SIZE; DrawRectangle(cX + fX, cY + fY, CELL_SIZE, CELL_SIZE, food.color); // スコア DrawText(TextFormat("SCORE: %d", score), 10, 10, 20, GREEN); return true; } void GameMain::Close() { } } // namespace snake::scene