-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.cpp
148 lines (131 loc) · 4.65 KB
/
root.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
#include <chrono>
#include <thread>
#include <atomic>
#include <iostream>
#include <string>
#include "GameLogic/GameController.h"
#include "GameLogic/Input/Keylistener.h"
#include "Utilities/AnsiEscape.h"
#include "GameLogic/Highscores/Highscore.h"
#include "Utilities/MemoryLeakDetection.h"
#include "Utilities/Utilities.h"
#ifdef _WIN32
#include <windows.h>
#endif
/* Zeigt das Hauptmenü mit verschiedenen Optionen */
int ShowStartMenuSelect()
{
moveTo(0,0);
clearScreen();
std::string selection;
std::cout <<"Welcome to our Tetris project" << std::endl
<<"Select what you want to do" << std::endl
<<"1:\t Start a new game" << std::endl
<<"2:\t View the highscores" << std::endl
<<"0:\t Close the application" << std::endl;
std::cin >> selection;
auto res = -1;
try
{
res = std::stoi(selection.c_str());
}
catch(...)
{
}
return res;
}
void AnimateString(std::string str, bool linebreak, int delay)
{
for (int i = 0; i < str.length(); i++) {
std::cout << str[i] << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
if (linebreak) {
std::cout << std::endl;
}
}
/* GameOver-Screen mit Informationen über das Spiel */
void ShowGameOver(const int score, const std::string name, const int level)
{
const auto tempScore = "Score: " + std::to_string(score);
const auto tempLevel = "Level: " + std::to_string(level);
const auto tempName = "Name: " + name;
const auto* chName = tempName.c_str();
const auto* chScore = tempScore.c_str();
const auto* chLevel = tempLevel.c_str();
clearScreen();
moveTo(0,0);
AnimateString(std::string("Game Over"), true, 200);
AnimateString(std::string(chName), true, 50);
AnimateString(std::string(chLevel), true, 50);
AnimateString(std::string(chScore), true, 50);
std::this_thread::sleep_for(std::chrono::milliseconds(4000));
}
int main()
{
setupConsole();
restoreConsole();
#ifdef _WIN32
// Make utf8 vaible
SetConsoleOutputCP(CP_UTF8);
#endif
while(const auto selection = ShowStartMenuSelect())
{
switch (selection)
{
case 1:{
AnimateString(std::string("Insert your Name: "), true, 100);
std::string name;
std::cin >> name;
replaceAll(name,",","");
replaceAll(name," ","");
auto* controller = new GameController();
auto* listener = new Keylistener();
listener->Stop();
listener->RegisterHandler(100, [controller]() {controller->RightKeyPressed();}); // d
listener->RegisterHandler(-39, [controller]() {controller->RightKeyPressed();}); // right
listener->RegisterHandler(97, [controller]() {controller->LeftKeyPressed();}); // a
listener->RegisterHandler(-37, [controller]() {controller->LeftKeyPressed();}); // left
listener->RegisterHandler(119, [controller]() {controller->UpKeyPressed();}); // w
listener->RegisterHandler(-38, [controller]() {controller->UpKeyPressed();}); // up
listener->RegisterHandler(98, [controller]() {controller->BKeyPressed();}); // b
listener->RegisterHandler(115, [controller]() {controller->DownKeyPressed();}); // s
listener->RegisterHandler(-40, [controller]() {controller->DownKeyPressed();}); // down
listener->RegisterHandler(32, [controller]() {controller->SpaceKeyPressed();}); // space
listener->StartMultithreaded();
std::thread game([controller, listener, name]()
{
controller->Start();
while (controller->IsGameRunning())
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
controller->Update();
}
});
game.join();
listener->Stop();
controller->Stop();
const auto score = controller->GetScore();
const auto level = controller->GetLevel();
AddHighscore(score, name);
ShowGameOver(score, name, level);
showCursor();
delete controller;
delete listener;
break;
}
case 2:
clearScreen();
moveTo(0,0);
ShowHighscore();
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
break;
default:
std::cout << "This is not a valid option\n";
break;
}
}
#ifdef debug
_CrtDumpMemoryLeaks();
#endif
}