-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
150 lines (141 loc) · 4.38 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
#include <Windows.h>
#include <iostream>
#include <string>
#include "General.h"
#include "ConsoleDraw.h"
#include "Tetris.h"
#include "InputHandler.h"
#include <chrono>
#include <random>
using namespace std;
using namespace chrono;
int nScreenHeight;
int nScreenWidth;
wchar_t* screen;
int speedLevel = 5;
int main() {
srand(time(0));
nScreenHeight = 44;
nScreenWidth = 30;
// you don't have to understand this yet, it's windows api shit and it's ass
screen = new wchar_t[nScreenHeight * nScreenWidth];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
HWND HWNDConsole = GetConsoleWindow();
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
RECT ConsoleRect;
GetWindowRect(HWNDConsole, &ConsoleRect);
MoveWindow(HWNDConsole, ConsoleRect.left, ConsoleRect.top, (nScreenWidth+1)*16, (nScreenHeight+4)*12, TRUE);
// windows api shit end
int x = 5;
int y = 5;
int tetroRotation = 0;
int messageScore = 0;
int messageTimer = 0;
wstring tetromino;
fillScreen(' ');
tetrisDrawFrame();
fillGameBuffer();
drawGameBuffer(1, 0);
spawnTetromino(x, y, tetromino);
predictTetromino();
auto currentTime = system_clock::now();
auto previousTime = system_clock::now();
int elapsed = duration_cast<milliseconds>(currentTime - previousTime).count();
gameBuffer = L"";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
gameBuffer += L" ";
while (1) {
if (ongoingGame) {
previousTime = currentTime;
currentTime = system_clock::now();
duration<double> delta = currentTime - previousTime;
// GET INPUT
LStickInput = { 0, 0 };
if (getKeyType('A'))
LStickInput.first -= 1;
if (getKeyType('D'))
LStickInput.first += 1;
if (getKeyType('W')) {
tetroRotation += 1;
tetromino = getTetromino(tetrostatus[0], tetroRotation, tetrostatus[1], fillers[tetrostatus[2]]);
if (checkTetrominoCollision(tetromino, x, y))
tetroRotation -= 1;
}
if (getKeyType('S'))
LStickInput.second += 1;
// DRAW
fillScreen(' ');
tetrisDrawFrame(0, 0);
drawGameBuffer(1, 1);
fillImage(tetromino, x, y, 4, 4);
drawPredictionTetromino();
showString(L"Level " + to_wstring(level), gamePosX + gameSizeX + 2, gamePosY + 8);
showString(L"Score " + to_wstring(score), gamePosX + gameSizeX + 2, gamePosY + 10);
tetromino = getTetromino(tetrostatus[0], tetroRotation, tetrostatus[1], fillers[tetrostatus[2]]);
if (messageTimer > 0) {
showScoreMessage(0, messageScore).second;
}
else messageScore = 0;
// GAME UPDATE
moveTetromino(x, y, LStickInput, tetromino);
elapsed += duration_cast<milliseconds>(delta).count();
if (elapsed >= 30 - (level)) {
elapsed = 0;
messageTimer--;
int a = removeClearedLines();
fallTetromino(x, y, tetromino);
checkUpdatedLines();
if (a > 0) {
messageScore = a;
messageTimer = 5;
score += showScoreMessage(0, messageScore).second;
}
levelUp();
}
}
if (getKeyType('F')) {
level++;
}
screen[nScreenWidth * nScreenHeight - 1] = '\0';
WriteConsoleOutputCharacterW(hConsole, screen, nScreenHeight * nScreenWidth, { 0, 0 }, &dwBytesWritten);
}
}