本ゲームプレイループ

守矢諏訪子 <suwako@076.moe>、2026-07-11 18:04
コミット 3db2ba1d849d1e4b1556a2440d671e79ddb59574 ・親 3e5068a ・ファイルを見る

4個のファイル、+235 -17

@@ -11,8 +11,13 @@
1111 void Close();
1212
1313 private:
14+ void spawn();
15+ void lock();
1416 void updateBoardLayout();
1517 void drawNextPiece();
18+ void drawHoldPiece();
19+ void handleInput();
20+ bool isValidPos(i8 piece, i8 rot, i8 x, i8 y);
1621
1722 private:
1823 u8 m_Score = 0;
@@ -23,5 +28,18 @@
2328 u8 m_HSize = 10;
2429 u8 m_VSize = 20;
2530 u8 m_SquareSize = 20;
31+
32+ i8 m_Hold = -1;
33+
34+ i8 m_CurPiece = 0;
35+ i8 m_CurRot = 0;
36+ i8 m_CurX = 0;
37+ i8 m_CurY = 0;
38+
39+ f32 m_FallTimer = 0.f;
40+ f32 m_FallSpeed = .8f;
41+
42+ bool m_GameOver = false;
43+ bool m_CanHold = true;
2644 }; // struct GameMain
2745 } // namespace tetris::scene
@@ -2,7 +2,9 @@
22
33 namespace tetris {
44 enum class Scene {
5+ Title,
56 GameMain,
7+ GameOver,
68 }; // enum class Scene
79
810 struct Game {
@@ -15,12 +15,33 @@
1515 SKYBLUE,
1616 };
1717
18+ // [ピース][回転][4ブロック][x,y]
19+ static const i32 tetrominoShapes[7][4][4][2] = {
20+ // I
21+ {{{0,0},{1,0},{2,0},{3,0}}, {{0,0},{0,1},{0,2},{0,3}}, {{0,0},{1,0},{2,0},{3,0}}, {{0,0},{0,1},{0,2},{0,3}}},
22+ // O
23+ {{{0,0},{1,0},{0,1},{1,1}}, {{0,0},{1,0},{0,1},{1,1}}, {{0,0},{1,0},{0,1},{1,1}}, {{0,0},{1,0},{0,1},{1,1}}},
24+ // S
25+ {{{1,0},{2,0},{0,1},{1,1}}, {{0,0},{0,1},{1,1},{1,2}}, {{1,0},{2,0},{0,1},{1,1}}, {{0,0},{0,1},{1,1},{1,2}}},
26+ // Z
27+ {{{0,0},{1,0},{1,1},{2,1}}, {{1,0},{0,1},{1,1},{0,2}}, {{0,0},{1,0},{1,1},{2,1}}, {{1,0},{0,1},{1,1},{0,2}}},
28+ // T
29+ {{{1,0},{0,1},{1,1},{2,1}}, {{1,0},{1,1},{1,2},{0,1}}, {{0,1},{1,1},{2,1},{1,0}}, {{1,0},{1,1},{1,2},{2,1}}},
30+ // J
31+ {{{0,0},{0,1},{1,1},{2,1}}, {{0,0},{1,0},{1,1},{1,2}}, {{0,1},{1,1},{2,1},{2,0}}, {{1,0},{1,1},{1,2},{0,2}}},
32+ // L
33+ {{{2,0},{0,1},{1,1},{2,1}}, {{0,0},{0,1},{0,2},{1,2}}, {{0,0},{1,0},{2,0},{0,1}}, {{1,0},{1,1},{1,2},{2,0}}}
34+ };
35+
1836 Vector2 boardPos = { 0, 0 };
1937
2038 i8 nextPiece = 0;
21- Vector2 nextPos = {};
39+ Vector2 nextPos = { 0, 0 };
2240 f32 nextSquareSize = 0.f;
2341
42+ Vector2 holdPos = { 0, 0 };
43+ f32 holdSquareSize;
44+
2445 std::vector<std::vector<i32>> board = {};
2546
2647 void GameMain::Init() {
@@ -31,18 +52,40 @@
3152
3253 board.assign(m_VSize, std::vector<i32>(m_HSize, 0));
3354 nextPiece = GetRandomValue(0, 6);
55+ spawn();
3456 updateBoardLayout();
3557 }
3658
3759 void GameMain::FuncUpdate() {
60+ if (m_GameOver) {
61+ // TODO: ゲームオーバー
62+ return;
63+ }
3864 updateBoardLayout();
65+ handleInput();
66+
67+ m_FallTimer += GetFrameTime();
68+
69+ f32 curFallSpeed = m_FallSpeed;
70+ if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) {
71+ curFallSpeed = .05f;
72+ }
73+
74+ if (m_FallTimer >= curFallSpeed) {
75+ if (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) {
76+ m_CurY++;
77+ } else {
78+ lock();
79+ }
80+
81+ m_FallTimer = 0.f;
82+ }
3983 }
4084
4185 void GameMain::DrawUpdate() {
4286 i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f));
4387 DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE);
4488
45- // Draw the game board border
4689 Rectangle rect = {
4790 .x = boardPos.x - 5,
4891 .y = boardPos.y - 5,
@@ -64,6 +107,22 @@
64107 }
65108 }
66109
110+ // 現在のピース
111+ Color pieceCol = tetrominoColors[m_CurPiece];
112+ for (i8 i = 0; i < 4; ++i) {
113+ i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0];
114+ i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1];
115+
116+ if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) {
117+ f32 x = boardPos.x + px * m_SquareSize;
118+ f32 y = boardPos.y + py * m_SquareSize;
119+
120+ DrawRectangle(x, y, m_SquareSize, m_SquareSize, pieceCol);
121+ DrawRectangleLines(x, y, m_SquareSize, m_SquareSize, BLACK);
122+ }
123+ }
124+
125+ // 次のピース
67126 Rectangle nextPanel = {
68127 .x = nextPos.x - 10,
69128 .y = nextPos.y - 10,
@@ -71,16 +130,29 @@
71130 .height = 4 * nextSquareSize + 20,
72131 };
73132
74- // 次のピース
75133 DrawRectangleRec(nextPanel, DARKGRAY);
76134 DrawRectangleLinesEx(nextPanel, 3, LIGHTGRAY);
77135 DrawText("NEXT", nextPos.x + 10, nextPos.y - 30, 20, WHITE);
78136
79137 drawNextPiece();
80138
81- f32 uiX = nextPos.x;
82- f32 uiY = nextPos.y + 180;
139+ // 持ち
140+ Rectangle holdPanel = {
141+ .x = holdPos.x - 10,
142+ .y = holdPos.y - 10,
143+ .width = 4 * holdSquareSize + 20,
144+ .height = 4 * holdSquareSize + 20,
145+ };
83146
147+ DrawRectangleRec(holdPanel, DARKGRAY);
148+ DrawRectangleLinesEx(holdPanel, 3, LIGHTGRAY);
149+ DrawText("HOLD", holdPos.x + 10, holdPos.y - 45, 20, WHITE);
150+
151+ drawHoldPiece();
152+
153+ f32 uiX = holdPos.x;
154+ f32 uiY = holdPos.y + 120;
155+
84156 DrawText(TextFormat("SCORE"), uiX, uiY, 24, WHITE);
85157 DrawText(TextFormat("%08d", m_Score), uiX, uiY + 35, 28, YELLOW);
86158
@@ -97,6 +169,31 @@
97169
98170 //////////
99171
172+ void GameMain::spawn() {
173+ m_CurPiece = nextPiece;
174+ nextPiece = GetRandomValue(0, 6);
175+
176+ m_CurRot = 0;
177+ m_CurX = m_HSize / 2 - 2;
178+ m_CurY = 0;
179+ m_CanHold = true;
180+
181+ if (!isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY)) {
182+ m_GameOver = true;
183+ }
184+ }
185+
186+ void GameMain::lock() {
187+ for (i8 i = 0; i < 4; ++i) {
188+ i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0];
189+ i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1];
190+ if (py >= 0) board[py][px] = m_CurPiece + 1;
191+ }
192+
193+ // TODO: クリアでチェック
194+ spawn();
195+ }
196+
100197 void GameMain::updateBoardLayout() {
101198 i32 screenW = GetScreenWidth();
102199 i32 screenH = GetScreenHeight();
@@ -115,26 +212,99 @@
115212 .x = boardPos.x + m_HSize * m_SquareSize + 80,
116213 .y = boardPos.y + 60,
117214 };
118- }
119215
120- void GameMain::drawNextPiece() {
121- const u32 shapes[7][4][2] = {
122- {{0,1},{1,1},{2,1},{3,1}}, // I
123- {{0,0},{0,1},{1,0},{1,1}}, // O
124- {{0,1},{1,1},{2,1},{2,0}}, // S
125- {{0,0},{1,0},{1,1},{2,1}}, // Z
126- {{1,0},{0,1},{1,1},{2,1}}, // T
127- {{0,1},{1,1},{2,1},{2,2}}, // J
128- {{2,1},{1,1},{0,1},{0,2}} // L
216+ holdSquareSize = m_SquareSize * .75f;
217+ holdPos = {
218+ .x = boardPos.x + m_HSize * m_SquareSize + 80,
219+ .y = boardPos.y + 220,
129220 };
221+ }
130222
223+ void GameMain::drawNextPiece() {
131224 Color col = tetrominoColors[nextPiece];
132225
133226 for (int i = 0; i < 4; ++i) {
134- float x = nextPos.x + shapes[nextPiece][i][0] * nextSquareSize;
135- float y = nextPos.y + shapes[nextPiece][i][1] * nextSquareSize;
227+ float x = nextPos.x + tetrominoShapes[nextPiece][0][i][0] * nextSquareSize;
228+ float y = nextPos.y + tetrominoShapes[nextPiece][0][i][1] * nextSquareSize;
136229 DrawRectangle(x, y, nextSquareSize, nextSquareSize, col);
137230 DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK);
138231 }
232+ }
233+
234+ void GameMain::drawHoldPiece() {
235+ if (m_Hold == -1) return;
236+
237+ Color col = tetrominoColors[m_Hold];
238+
239+ for (int i = 0; i < 4; ++i) {
240+ float x = holdPos.x + tetrominoShapes[m_Hold][0][i][0] * holdSquareSize;
241+ float y = holdPos.y + tetrominoShapes[m_Hold][0][i][1] * holdSquareSize;
242+ DrawRectangle(x, y, holdSquareSize, holdSquareSize, col);
243+ DrawRectangleLines(x, y, holdSquareSize, holdSquareSize, BLACK);
244+ }
245+ }
246+
247+ void GameMain::handleInput() {
248+ if (m_GameOver) return;
249+
250+ if ((IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) || (IsKeyPressed(KEY_A) || IsKeyPressedRepeat(KEY_A))) {
251+ if (isValidPos(m_CurPiece, m_CurRot, m_CurX - 1, m_CurY)) {
252+ m_CurX--;
253+ }
254+ }
255+
256+ if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) || (IsKeyPressed(KEY_D) || IsKeyPressedRepeat(KEY_D))) {
257+ if (isValidPos(m_CurPiece, m_CurRot, m_CurX + 1, m_CurY)) {
258+ m_CurX++;
259+ }
260+ }
261+
262+ if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) {
263+ while (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) {
264+ m_CurY++;
265+ m_Score += 2;
266+ }
267+ lock();
268+ m_FallTimer = 0.f;
269+ return;
270+ }
271+
272+ if (IsKeyPressed(KEY_SPACE)) {
273+ u8 newRot = (m_CurRot + 1) % 4;
274+ if (isValidPos(m_CurPiece, newRot, m_CurX, m_CurY)) {
275+ m_CurRot = newRot;
276+ } else if (isValidPos(m_CurPiece, newRot, m_CurX - 1, m_CurY)) {
277+ m_CurX--;
278+ m_CurRot = newRot;
279+ } else if (isValidPos(m_CurPiece, newRot, m_CurX + 1, m_CurY)) {
280+ m_CurX++;
281+ m_CurRot = newRot;
282+ }
283+ }
284+
285+ if ((IsKeyPressed(KEY_LEFT_CONTROL) || IsKeyPressed(KEY_RIGHT_CONTROL)) && m_CanHold) {
286+ if (m_Hold == -1) {
287+ m_Hold = m_CurPiece;
288+ spawn();
289+ } else {
290+ std::swap(m_CurPiece, m_Hold);
291+ m_CurRot = 0;
292+ m_CurX = m_HSize / 2 - 2;
293+ m_CurY = 0;
294+ }
295+
296+ m_CanHold = false;
297+ }
298+ }
299+
300+ bool GameMain::isValidPos(i8 piece, i8 rot, i8 x, i8 y) {
301+ for (i8 i = 0; i < 4; ++i) {
302+ i8 px = x + tetrominoShapes[piece][rot][i][0];
303+ i8 py = y + tetrominoShapes[piece][rot][i][1];
304+
305+ if (px < 0 || px >= m_HSize || py >= m_VSize) return false;
306+ if (py >= 0 && board[py][px] != 0) return false;
307+ }
308+ return true;
139309 }
140310 } // namespace tetris::scene
変更 src/tetris.cc
@@ -1,40 +1,68 @@
11 #include <tetris/tetris.hh>
2+//#include <tetris/scene/title.hh>
23 #include <tetris/scene/gamemain.hh>
4+//#include <tetris/scene/gameover.hh>
35
46 namespace tetris {
7+ //scene::Title g_Title;
58 scene::GameMain g_GameMain;
9+ //scene::GameOver g_GameOver;
610
711 void Game::Init() {
812 isRunning = true;
913 isChangeScene = false;
1014
1115 switch (nextScene) {
16+ case Scene::Title:
17+ //g_Title.Init();
18+ break;
1219 case Scene::GameMain:
1320 g_GameMain.Init();
1421 break;
22+ case Scene::GameOver:
23+ //g_GameOver.Init();
24+ break;
1525 }
1626 }
1727
1828 void Game::FuncUpdate() {
1929 switch (curScene) {
30+ case Scene::Title:
31+ //g_Title.FuncUpdate();
32+ break;
2033 case Scene::GameMain:
2134 g_GameMain.FuncUpdate();
2235 break;
36+ case Scene::GameOver:
37+ //g_GameOver.FuncUpdate();
38+ break;
2339 }
2440 }
2541
2642 void Game::DrawUpdate() {
2743 switch (curScene) {
44+ case Scene::Title:
45+ //g_Title.DrawUpdate();
46+ break;
2847 case Scene::GameMain:
2948 g_GameMain.DrawUpdate();
3049 break;
50+ case Scene::GameOver:
51+ //g_GameOver.DrawUpdate();
52+ break;
3153 }
3254 }
3355
3456 void Game::Close() {
3557 switch (curScene) {
58+ case Scene::Title:
59+ //g_Title.Close();
60+ break;
3661 case Scene::GameMain:
3762 g_GameMain.Close();
63+ break;
64+ case Scene::GameOver:
65+ //g_GameOver.Close();
3866 break;
3967 }
4068