-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
163 lines (137 loc) · 5.71 KB
/
main.cpp
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
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include "tetrimino.h"
#include "userAction.h"
enum class Scene {
START, GAME, END, PAUSE
};
class Game : public olc::PixelGameEngine {
public:
Game() {
sAppName = "Tetris";
}
public:
bool OnUserCreate() override {
// Called once at the start, so create things here
return true;
}
bool OnUserUpdate(float fElapsedTime) override {
// called once per frame
Clear(olc::BLACK);
switch (scene) {
case Scene::GAME: {
timeSinceLastTick += fElapsedTime;
if (GetKey(olc::Key::W).bPressed || GetKey(olc::Key::UP).bPressed) mTet.Flip(&playfield);
if (GetKey(olc::Key::SPACE).bPressed) mTet.HardDrop(&playfield);
if (GetKey(olc::R).bReleased) StartGame();
if (GetKey(olc::ESCAPE).bPressed) scene = Scene::PAUSE;
for (auto const&[key, val] : buttonAction) {
if (GetKey(key).bHeld || GetKey(key).bPressed || GetKey(key).bReleased)
PrepareUserAction(GetKey(key), val, fElapsedTime);
}
if (timeSinceLastTick >= playfield.getTick()) {
mTet.MoveDown(&playfield, false);
timeSinceLastTick -= playfield.getTick();
}
playfield.Draw(this);
mTet.Draw(this, &playfield);
mTet.DrawGhost(this, &playfield);
std::string sScore = "Score: " + std::to_string(playfield.getScore());
std::string sLevel = "Level: " + std::to_string(playfield.getLevel());
std::string sLC = "LC: " + std::to_string(playfield.getLinesCleared());
DrawString(olc::vi2d(10, 20), sScore);
DrawString(olc::vi2d(10, 35), sLevel);
DrawString(olc::vi2d(10, 50), sLC);
DrawString({10, 70}, "Next:");
mTetNext.Draw(this, &playfield, {10, 80});
if (mTet.isInFinalPosition) {
for (int i = 0; i < 10; i++)
if (playfield.IsOccupied({i, 1}))
GameOver();
playfield.CheckForFullLines();
mTet = mTetNext;
mTetNext = Tetrimino::random();
}
break;
}
case Scene::START: {
DrawCenteredString("START", {ScreenWidth() / 2, ScreenHeight() / 2});
if (GetKey(olc::ENTER).bPressed) StartGame();
break;
}
case Scene::END: {
DrawCenteredString("GAME OVER", {ScreenWidth() / 2, 50});
DrawCenteredString("SCORE: " + std::to_string(playfield.getScore()), {ScreenWidth() / 2, 65});
DrawCenteredString("Press Enter to play again.", {ScreenWidth() / 2, ScreenHeight() / 2});
if (GetKey(olc::ENTER).bPressed) StartGame();
break;
}
case Scene::PAUSE: {
DrawCenteredString("PAUSED", {ScreenWidth() / 2, ScreenHeight() / 2});
if (GetKey(olc::ESCAPE).bPressed) scene = Scene::GAME;
break;
}
}
return true;
}
void StartGame() {
scene = Scene::GAME;
srand(time(NULL));
playfield = Playfield(olc::vi2d((ScreenWidth() / 2) + 25, ScreenHeight() / 2));
mTet = Tetrimino::random();
mTetNext = Tetrimino::random();
}
void GameOver() {
scene = Scene::END;
}
void PrepareUserAction(olc::HWButton button, UserAction action, float fElapsedTime) {
if (button.bReleased)
buttonHeldTime[action] = 0.0f;
if (button.bPressed)
PerformUserAction(action);
if (button.bHeld) {
if (buttonHeldTime[action] >= (mMoveSpeed - (float(playfield.getLevel()) * mMoveSpeed / 4))) {
PerformUserAction(action);
buttonHeldTime[action] -= (mMoveSpeed - (float(playfield.getLevel()) * mMoveSpeed / 4));
} else
buttonHeldTime[action] += fElapsedTime;
}
}
void PerformUserAction(UserAction action) {
switch (action) {
case UserAction::LEFT :
mTet.MoveLeft(&playfield);
break;
case UserAction::RIGHT :
mTet.MoveRight(&playfield);
break;
case UserAction::DOWN:
mTet.MoveDown(&playfield);
}
}
void DrawCenteredString(const std::string& text, const olc::vi2d& pos) {
DrawString({pos.x - ((int)text.length() * 4), pos.y - (3)}, text);
}
private:
Tetrimino mTet;
Tetrimino mTetNext;
float timeSinceLastTick = 0.0f;
float mMoveSpeed = 0.15f;
std::map<UserAction, float> buttonHeldTime = {{UserAction::LEFT, 0.0f},
{UserAction::RIGHT, 0.0f},
{UserAction::DOWN, 0.0f}};
std::map<olc::Key, UserAction> buttonAction = {{olc::Key::A, UserAction::LEFT},
{olc::Key::LEFT, UserAction::LEFT},
{olc::Key::D, UserAction::RIGHT},
{olc::Key::RIGHT, UserAction::RIGHT},
{olc::Key::S, UserAction::DOWN},
{olc::Key::DOWN, UserAction::DOWN}};
Playfield playfield;
Scene scene = Scene::START;
};
int main() {
Game game;
if (game.Construct(256, 240, 4, 4))
game.Start();
return 0;
}