-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
205 lines (171 loc) · 5.32 KB
/
game.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "game.h"
#include "save.h"
#include <iostream>
#include <unistd.h>
#include <iomanip>
Game::Game(int width, int height)
{
isFail = false;
score = 0;
FPS = 2;
this->width = width;
this->height = height;
toClose = false;
grid = new Grid(width, height);
snake = new Player(*grid);
controls = new Controls();
Save::record *highRec = Save::readHighScore();
high = highRec->score;
highName = highRec->username;
delete highRec;
username = getlogin();
}
Game::~Game()
{
delete grid;
delete snake;
delete controls;
}
void Game::Update()
{
std::string options = "| H-help | L-leaders | N-new name | C-close";
directions dir = NONE;
Point fruit = newFruit();
if (highName.size() == 0)
{
highName = username;
}
do
{
// очистка терминала
printf("\033c");
directions new_dir = controls->Input();
switch (controls->todo)
{
case EXIT:
toClose = true;
controls->EchoOn();
return;
break;
case LEADERS:
{
std::vector<Save::record> leaders = Save::listScores();
if (leaders.size() == 0)
{
std::cout << "It seems there is no leaders yet, be the first!\n";
}
else
{
std::cout << "Current leaderboard:\n";
for (Save::record rec : leaders)
{
std::cout << std::setw(4) << rec.score << std::setw(0) << " - " << rec.username << std::endl;
}
}
std::cout << "\nPress SPACE to continue...\n";
while (char c = getchar() != ' ');
printf("\033c");
controls->todo = DEFAULT;
}
break;
case CHUSER:
{
std::cout << "Enter you name: (SPACE to cancel)\n";
controls->BufferOff();
char buf[16];
*buf = getchar();
if (buf[0] == ' ')
{
controls->EchoOff();
controls->todo = DEFAULT;
break;
}
scanf("%s", &buf[1]);
username = buf;
controls->EchoOff();
controls->todo = DEFAULT;
}
break;
case HELP:
{
std::string helpMsg = "Use WASD or arrows to control the snake. Eat fruits to grow and increase your score. You also can't move backwards. If you bite your tail or bump into a wall, you lose.\n\nWritten by NetherQuartz, follow me on Github: https://github.com/NetherQuartz.";
std::cout << helpMsg << std::endl;
std::cout << "\nPress SPACE to continue...\n";
while (char c = getchar() != ' ');
printf("\033c");
controls->todo = DEFAULT;
}
break;
}
printf("\033c");
char buf[50];
sprintf(buf, "\033[100;37mYour score: %d\033[0m", score);
std::string yourScore = buf;
sprintf(buf, "High score: %d (%s)", high, highName.c_str());
std::string highScore = buf;
std::cout << yourScore;
// чтобы влезла надпись your score
for (int i = 0; i < grid->width * 2 - yourScore.size() + 17 - highScore.size(); i++)
{
std::cout << "\033[100m \033[0m";
}
std::cout << "\033[100;37m" << highScore << "\033[0m" << std::endl;
if (new_dir != NONE)
{
dir = new_dir;
}
bool movement = snake->Move(dir);
if (!movement)
{
isFail = true;
}
if (snake->InTail({snake->x, snake->y}, 1))
{
isFail = true;
}
if (fruit.x == snake->x && fruit.y == snake->y)
{
fruit = newFruit();
snake->Grow();
FPS += 1;
score++;
}
snake->Print();
grid->PlaceFruit(fruit.x, fruit.y);
grid->Print();
if (!isFail)
{
std::cout << "\033[100;37m" << username << "\033[0m";
// чтобы влезла надпись options
for (int i = 0; i < grid->width * 2 - options.size() + 4 - username.size(); i++)
{
std::cout << "\033[100;37m \033[0m";
}
std::cout << "\033[100;37m" << options << "\033[0m" << std::endl;
}
else
{
std::string gameOver = "\033[100;37mGAME OVER!!!\033[0m";
std::cout << gameOver;
Save::writeHighScore(username, score);
// чтобы влезли надписи game over и options
for (int i = 0; i < grid->width * 2 - 8; i++)
{
std::cout << "\033[100;37m \033[0m";
}
std::cout << std::endl;
}
usleep(1E6 / FPS);
} while (!isFail);
}
Point Game::newFruit()
{
Point fruit;
do
{
int x = rand() % grid->width;
int y = rand() % grid->height;
fruit = {x, y};
} while (snake->InTail(fruit));
return fruit;
}