-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayableSnake.cpp
More file actions
342 lines (282 loc) · 7.35 KB
/
Copy pathPlayableSnake.cpp
File metadata and controls
342 lines (282 loc) · 7.35 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <iostream>
#include <windows.h> //Pour le buffer/la fenêtre
#include <vector> //Pour les objets snake, fruit, obstacle etc
#include <thread> //Pour "mettre en pause" le jeu afin d'avoir un framerate ~ constant
#include <chrono> //pour thread
#include <cstdlib> //pour un générateur de nombre aléatoire
#include <ctime> //pour initialiser le générateur de nombre aléatoire
int nScreenWidth = 120;
int nScreenHeight = 30;
bool bGameOver = 0;
bool bSnakeGrew = 0;
int score = 0;
std::vector<int> coordFruit;
bool bFruit = true;
bool bDroite = false;
bool bGauche = false;
bool bDroitePrec = false;
bool bGauchePrec = false;
std::vector<std::vector<int>> snake(0);
int tailleSnake = 4;
char direction = 'W';
//Créé une zone de jeu plus réduite que l'écran
void Setup(wchar_t* screen)
{
bGameOver = 0;
tailleSnake = 4;
char word[] = { 'S','N','A','K','E',' ','G','A','M','E' };
//Bordures
for (int i = 0; i < nScreenHeight * nScreenWidth - 1; i++)
{
if (i < nScreenWidth || (2 * nScreenWidth <= i && i < 3 * nScreenWidth))
{
screen[i] = '=';
}
else
{
screen[i] = ' ';
}
}
//Titre du jeu, Score
for (int i = 0; i <= 9; i++)
{
screen[nScreenWidth + 54 + i] = word[i];
};
//(Re-)Initialise la position de Snake et sa direction
std::vector<std::vector<int>> newSnake(1);
snake = newSnake;
std::vector<int> tete(2, 55);
tete[1] = 20;
snake[0] = tete;
std::vector<int> corp1(2, 56);
corp1[1] = 20;
snake.push_back(corp1);
std::vector<int> corp2(2, 57);
corp2[1] = 20;
snake.push_back(corp2);
std::vector<int> corp3(2, 58);
corp3[1] = 20;
snake.push_back(corp3);
direction = 'W';
//Dessine Snake
for (int i = 0; i < tailleSnake; i++)
{
int coord = snake[i][0] + nScreenWidth * snake[i][1];
if (i == 0)
{
screen[coord] = '@';
}
else
{
screen[coord] = 'O';
}
}
//(Re-)Génère le premier fruit
if (bFruit)
{
std::vector<int> newFruit;
int x = 1 + (int)(nScreenWidth * rand() / (RAND_MAX + 1.0));
int y = 4 + (int)((nScreenHeight - 3) * rand() / (RAND_MAX + 1.0));
coordFruit = newFruit;
coordFruit.push_back(x);
coordFruit.push_back(y);
bFruit = false;
}
}
//Ecran de fin de partie
void EndScreen(wchar_t* screen)
{
char word1[] = { 'G','A','M', 'E', ' ', 'O', 'V', 'E', 'R' };
char word2[] = { 'P','O','U','R',' ','R','E','J','O','U','E','R',' ','T','A','P','E','R',' ','E','S','P','A','C','E' };
for (int i = 0; i < nScreenHeight * nScreenWidth; i++)
{
screen[i] = ' ';
}
for (int i = 0; i < 9; i++)
{
screen[10 * nScreenWidth + 55 + i] = word1[i];
}
for (int i = 0; i < 25; i++)
{
screen[12 * nScreenWidth + 50 + i] = word2[i];
}
}
//Action en fonction de l'état du buffer
void Logic(wchar_t* screen)
{
//detection des collisions avec les bordures
if (snake[0][1] < 4 || snake[0][1] > nScreenHeight || snake[0][0] < 0 || snake[0][0] > nScreenWidth)
{
bGameOver = true;
}
//detection des collisions avec le reste du snake
for (int i = 1; i < tailleSnake; i++)
{
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
{
bGameOver = true;
}
}
//detection d'un fruit mangé, action sur le buffer en conséquence
if (snake[0][0] == coordFruit[0] && snake[0][1] == coordFruit[1])
{
bSnakeGrew = 1;
score++;
tailleSnake++;
std::vector<int> nouv;
switch (direction)
{
case 'W':
nouv.push_back(snake[0][0] - 1);
nouv.push_back(snake[0][1]);
snake.insert(snake.begin(), nouv); break;
case 'N':
nouv.push_back(snake[0][0]);
nouv.push_back(snake[0][1] - 1);
snake.insert(snake.begin(), nouv); break;
case 'E':
nouv.push_back(snake[0][0] + 1);
nouv.push_back(snake[0][1]);
snake.insert(snake.begin(), nouv); break;
case 'S':
nouv.push_back(snake[0][0]);
nouv.push_back(snake[0][1] + 1);
snake.insert(snake.begin(), nouv); break;
}
bFruit = true;
}
//Génère un nouveau fruit si besoin
if (bFruit)
{
int x = 1 + (int)((float)nScreenWidth * rand() / (RAND_MAX + 1.0));
int y = 4 + (int)(((float)nScreenHeight - 3) * rand() / (RAND_MAX + 1.0));
coordFruit[0] = x;
coordFruit[1] = y;
bFruit = false;
}
}
//fait avancer snake en fonction de sa direction, le dessine, dessine le fruit
void UpdateSnake(wchar_t* screen)
{
//Avance Snake dans le buffer en fonction de la position
if (direction == 'W')
{
snake.pop_back();
std::vector<int> h;
h.push_back(snake[0][0] - 1);
h.push_back(snake[0][1]);
snake.insert(snake.begin(), h);
}
if (direction == 'E')
{
snake.pop_back();
std::vector<int> h;
h.push_back(snake[0][0] + 1);
h.push_back(snake[0][1]);
snake.insert(snake.begin(), h);
}
if (direction == 'N')
{
snake.pop_back();
std::vector<int> h;
h.push_back(snake[0][0]);
h.push_back(snake[0][1] - 1);
snake.insert(snake.begin(), h);
}
if (direction == 'S')
{
snake.pop_back();
std::vector<int> h;
h.push_back(snake[0][0]);
h.push_back(snake[0][1] + 1);
snake.insert(snake.begin(), h);
}
bGauchePrec = false;
bDroitePrec = false;
//Nettoie la zone de jeu
for (int i = nScreenWidth * 3; i < nScreenHeight * nScreenWidth - 1; i++)
{
screen[i] = ' ';
}
//Dessine le nouveau snake
for (int i = 0; i < tailleSnake; i++)
{
int coord = snake[i][0] + nScreenWidth * snake[i][1];
if (i == 0)
{
screen[coord] = '@';
}
else
{
screen[coord] = 'O';
}
}
//Dessine le fruit
screen[coordFruit[1] * nScreenWidth + coordFruit[0]] = '%';
}
//changement de direction en fonction des inputs (gauche/droite)
void Input(wchar_t* screen)
{
//stockage de l'état des touches gauche droite dans des bools
bDroite = (0x8000 & GetAsyncKeyState((unsigned char)('\x27'))) != 0;
bGauche = (0x8000 & GetAsyncKeyState((unsigned char)('\x25'))) != 0;
//changement des directions
if (bDroite && (bDroite != bDroitePrec))
{
switch (direction)
{
case 'W': direction = 'N'; break;
case 'N': direction = 'E'; break;
case 'E': direction = 'S'; break;
case 'S': direction = 'W'; break;
}
bDroitePrec = bDroite;
}
if (bGauche && (bGauchePrec != bGauche))
{
switch (direction)
{
case 'S': direction = 'E'; break;
case 'W': direction = 'S'; break;
case 'N': direction = 'W'; break;
case 'E': direction = 'N'; break;
}
bGauchePrec = bGauche;
}
}
int main()
{
//Créé un buffer pour l'écran
wchar_t* screen = new wchar_t[nScreenWidth * nScreenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
//boucle de jeu
while (1)
{
//initalise le générateur de nombre aléatoire au début de chaque partie
srand(time(NULL));
//Ecran de début de partie
Setup(screen);
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
while (!bGameOver)
{
auto t0 = std::chrono::system_clock::now();
while (std::chrono::system_clock::now() - t0 < std::chrono::microseconds(200))
{
Input(screen);
}
Logic(screen);
UpdateSnake(screen);
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
while ((0x8000 & GetAsyncKeyState((unsigned char)('\x20'))) == 0)
{
EndScreen(screen);
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);
}
}
return 0;
}