-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
166 lines (138 loc) · 3.08 KB
/
Copy pathMain.cpp
File metadata and controls
166 lines (138 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <array>
#include <string>
#include <cstddef>
#include <raylib.h>
#include "Board.h"
#include "Block.h"
#include "Renderer.h"
constexpr int screenWidth = 600;
constexpr int screenHeight = 800;
constexpr int targetFps = 60;
constexpr float moveInterval = 0.1f;
constexpr float normalFallInterval = 0.5f;
constexpr float softDropInterval = 0.05f;
using BlockBag = std::array<BlockType, 7>;
namespace {
Block createNextBlock(BlockBag& bag, std::size_t& bagIndex)
{
if (bagIndex >= bag.size())
{
bag = Block::createBlockBag();
bagIndex = 0;
}
return Block{ bag[bagIndex++] };
}
}
int main() {
InitWindow(screenWidth, screenHeight, "Jetris");
SetTargetFPS(targetFps);
BlockBag blockBag = Block::createBlockBag();
std::size_t bagIndex = 0;
Block block = createNextBlock(blockBag, bagIndex);
Block nextBlock = createNextBlock(blockBag, bagIndex);
float falltimer = 0.0f;
float moveTimer = 0.0f;
bool gameOver = false;
Board board;
while (!WindowShouldClose())
{
if (gameOver && IsKeyPressed(KEY_R))
{
board = Board{};
blockBag = Block::createBlockBag();
bagIndex = 0;
block = createNextBlock(blockBag, bagIndex);
nextBlock = createNextBlock(blockBag, bagIndex);
falltimer = 0.0f;
moveTimer = 0.0f;
gameOver = false;
}
if (!gameOver)
{
int movedir = 0;
if (IsKeyDown(KEY_LEFT))
{
movedir = -1;
}
if (IsKeyDown(KEY_RIGHT))
{
movedir = 1;
}
const bool firstPress =
IsKeyPressed(KEY_LEFT) ||
IsKeyPressed(KEY_RIGHT);
if (firstPress)
{
const int newX = block.x + movedir;
if (board.canPlace(block, newX, block.y))
{
block.x = newX;
}
moveTimer = 0.0f;
}
else if (movedir != 0)
{
moveTimer += GetFrameTime();
if (moveTimer >= moveInterval)
{
const int newX = block.x + movedir;
if (board.canPlace(block, newX, block.y))
{
block.x = newX;
}
moveTimer = 0.0f;
}
}
else
{
moveTimer = 0.0f;
}
if (IsKeyPressed(KEY_UP))
{
board.rotateCells(block);
}
if (IsKeyPressed(KEY_SPACE))
{
while (board.canPlace(block, block.x, block.y+1))
{
block.y++;
}
board.placeBlock(block, block.x, block.y);
block = nextBlock;
nextBlock = createNextBlock(blockBag, bagIndex);
if (!board.canPlace(block, block.x, block.y))
{
gameOver = true;
}
falltimer = 0.0f;
}
const float currentFallInterval =
IsKeyDown(KEY_DOWN)
? softDropInterval
: normalFallInterval;
falltimer += GetFrameTime();
if (falltimer >= currentFallInterval)
{
falltimer = 0.0f;
if (board.canPlace(block, block.x, block.y + 1))
{
block.y++;
}
else
{
board.placeBlock(block, block.x, block.y);
block = nextBlock;
nextBlock = createNextBlock(blockBag, bagIndex);
// 생성 위치에 놓을 수 없다면 게임 오버
if (!board.canPlace(block, block.x, block.y))
{
gameOver = true;
}
}
}
}
int currentScore = board.getScore();
Renderer::drawGame(currentScore, board, block, gameOver, nextBlock);
}
CloseWindow();
}