Bonjour, pour un projet scolaire j'ai codé un jeu Snake sur console en langage c.
mon programme fonctionne bien mais il me reste un petit bug que je n'arrive pas a régler malgré de très nombreux essais et recherches.
Lorsque je lance mon jeu la console s'ouvre, un menu apparait, on séletionne une otption (sortit ou choix niveau) on arrive ensuite dans un deuxieme menu qui permet de choisir le niveaux, en fonction du choix, le jeux se lance en conséquence. lorsque l'on perde une page de "game over" apparait puis l'on revient alors au menu.
de retour au menu on choisi a nouveau si l'on souhaite sortir ou sélectionner un niveau, si l'on choisis de sortir la console se ferme, si on entre dans le le menu des niveaux, tous s'affiche correctement mais si 'lon choisis un niveaux le jeu ne se lance pas mais on revient au menu principale.

pour réafficher le menu lorsque l'on perd j'ai utilisé un while dans le main qui lance le menu lorsque d(une variable) est égale a 0.
d=0 si le nombre de vie de mon snake==0 (donc si l'on perd)

je pense que le probleme vient du fait que d ne passe pas a 1 mais reste a 0 apres avoir choisis les niveaux (mais alors comment se lance t'il la premier fois ?).

voici le code et merci d'avances pour vos réponses et pour le temps passé dessus (je code depuis code blocks)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
 
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
 
int x, y, posx_pomme, posy_pomme, score;
int corpsx[100], corpsy[100];
int ncorps;
enum Direction
{ STOP = 0, GAUCHE, DROITE, HAUT, BAS,ARRET };
int dir = STOP;
 
 
//Définition des constantes
const int LARG = 50;     //Taille de l'aire de jeu
const int HAUTEUR = 20;
const char contour=178,tete=254,fruit=157;
 
 
 
 
 
//Affichage du Menu que verra le joueur
 
int Menu()
{
 
 
 
  int choix;
 
  printf("\n\n\n\n\n");
  printf("           |=========================================================| \n");
  printf("           |                       MENU                              | \n");
  printf("           |                       ----                              | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |          Quel est votre choix? taper 1 ou 2             | \n");
  printf("           |                                                         | \n");
  printf("           |          1- commencer une nouvelle partie               | \n");
  printf("           |          2- sortir                                      | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |=========================================================| \n");
 
 
  scanf("%d", &choix);
 
  printf("\n");
  system("cls");
  switch (choix)
  {
 
 
    case 1:
        {
        printf("\n\n\n\n\n");
  printf("           |=========================================================| \n");
  printf("           |                       MENU                              | \n");
  printf("           |                       ----                              | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |          vous voulez commencer une nouvelle partie      | \n");
  printf("           |                                                         | \n");
  printf("           |          1- niveau facile                               | \n");
  printf("           |          2- niveau moyen                                | \n");
  printf("           |          3- niveau difficile                            | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |                                                         | \n");
  printf("           |=========================================================| \n");
 
       int Lvl;
 
        scanf("%d", &Lvl);
       switch(Lvl){
        case 1:
 
 
            return(Lvl);
            break;
        case 2:
            printf("choix 2 du sous menu 1\n\n");
 
            return(Lvl);
 
            break;
 
        case 3:
            printf("choix 3 du sous menu 1 \n\n");
 
            break;
 
 
       }
       break;
      }
 
    case 2:
 
     exit(0);
     break;
 
       }
 
return 1;
 
  }
 
 
 
 
 
 
 
 
//Appartion de tous les élements visuels de l'aire de jeu
void TerrainEtSnake()
{
    system("cls");
    for (int i = 0; i < LARG+2; i++)
        printf( "%c", contour );
    printf( "\n" );
 
    for (int i = 0; i < HAUTEUR; i++)
    {
        for (int j = 0; j < LARG; j++)
        {
            if (j == 0)
            {
                printf( "%c",contour  );
            }
            if (i == y && j == x)
            {
                printf( "%c", tete);
            }
            else if (i == posy_pomme && j == posx_pomme)
            {
                printf( "%c", fruit );
            }
            else
            {
                bool print = 0;
                for (int k = 0; k < ncorps; k++)
                {
                    if (corpsx[k] == j && corpsy[k] == i)
                    {
                        printf( "o" );
                        print = 1;
                    }
                }
                if (!print)
                {
                    printf( " " );
                }
            }
 
 
            if (j == LARG - 1)
            {
                printf( "%c", contour );
            }
        }
        printf( "\n" );
    }
 
    for (int i = 0; i < LARG+2; i++)
    {
        printf( "%c", contour );
    }
    printf( "\n" );
    printf( "score:%d\n", score );
}
 
 
 
 
 
 
 
 
 
// Le système qui gère quand le joueur perd une vie
 
int vies(int vie)
{
    for (int i = 0; i < ncorps; i++)
        if (corpsx[i] == x && corpsy[i] == y){
            vie--;
            x = LARG /2 ;
            y = HAUTEUR /2 ;
        }
 
        if (x > LARG || x < 0 || y > HAUTEUR || y < 0){
            vie--;
            x = LARG /2 ;
            y = HAUTEUR /2 ;}
printf("vies:%d", vie);
 
return vie;
}
 
 
 
 
 
 
 
 
 
 
//Gère quand le joueur perd
 
int perte(int nb_vies)
{
 
int gameOver=0;
if(nb_vies == 0)
    {
            system("cls");
            printf("\n\n\n\n\n\n\n");
            printf("          |-----|   |------      |-----|     ____                        \n ");
            printf("         |     |   |            |     |    |    |     |        |        \n ");
            printf("         |-----|   |            |_____|    |     |    |        |        \n ");
            printf("         |         |-----       |   )      |      |   |        |        \n ");
            printf("         |         |            |    )     |      |   |        |        \n ");
            printf("         |         |            |     )    |     /    |        |        \n ");
            printf("         |         |-------     |      )   |____/     |--------|        \n ");
            printf("\n\n\n");
            printf("               dommage vous avez perdu avec un score de :%d \n",score );
            system("pause");
 
            system("cls");
            gameOver=1;
            }
 return (gameOver);
 
}
 
 
int vitesse()
{
    int ralentisement=50;
    if(score>10){
        ralentisement=(ralentisement-10);
 
     if(score>30){
        ralentisement=(ralentisement-10);
 
     if(score>70){
        ralentisement=(ralentisement-10);
 
     if(score>150){
        ralentisement=(ralentisement-10);
 
        if(score>310){
        ralentisement=(ralentisement-10);
        }
       }
      }
     }
    }
 
    return ralentisement;
}
 
 
 
 
 
 
 
 
// Gestion de l'action du joeur sur les touches
 
void Entree()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'q':
            dir = GAUCHE;
            break;
        case 'd':
            dir = DROITE;
            break;
        case 'z':
            dir = HAUT;
            break;
        case 's':
            dir = BAS;
            break;
        case 'x':
            dir = ARRET;
            break;
        }
    }
}
 
 
 
 
 
 
 
 
 
 
// Gestion du mouvement pour le niveau 1
 
int mouvement_case_par_case()
{
 
    int frappe;
 
{
    frappe=getch();
    if(frappe=='q'){
        x=x-1;
    }
       if(frappe=='d'){
        x=x+1;
       }
 
       if(frappe=='z'){
        y=y-1;
       }
 
       if(frappe=='s'){
        y=y+1;
       }
 
       if (frappe=='x'){
        return 1;
       }
        if (x == posx_pomme && y == posy_pomme)
    {
        score += 10;
        posx_pomme = rand() % LARG;
        posy_pomme = rand() % HAUTEUR;
        ncorps++;
    }
 
 
    }
      return 0;
}
 
 
 
 
 
 
 
 
// Gestion du mouvement pour le niveau 2
 
 
int mouvement()
{
 
         switch (dir)
         {
    case GAUCHE:
        x--;
        break;
    case DROITE:
        x++;
        break;
    case HAUT:
        y--;
        break;
    case BAS:
        y++;
        break;
    case ARRET:
        return 1;
    default:
        break;
    }
 
 
    if (x == posx_pomme && y == posy_pomme)
    {
        score += 10;
        posx_pomme = rand() % LARG;
        posy_pomme = rand() % HAUTEUR;
        ncorps++;
    }
     return 0;
}
 
 
 
 
 
 
 
 
 
 
 
// Le grandissement du corps du Snake
 
 
void grandir()
{
    int prevX = corpsx[0];
    int prevY = corpsy[0];
    int prev2X, prev2Y;
    corpsx[0] = x;
    corpsy[0] = y;
    for (int i = 1; i < ncorps; i++)
    {
        prev2X = corpsx[i];
        prev2Y = corpsy[i];
        corpsx[i] = prevX;
        corpsy[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
 
         putch (' ');
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
int main()
{   int gameOver=0;
    x = LARG /2 ;
    y = HAUTEUR /2 ;
    posx_pomme = rand() % LARG;
    posy_pomme = rand() % HAUTEUR;
    int nb_vies=3;
    int d;
 
 
 system("COLOR 81");
if (gameOver==1){
    d=0;
}
 
while(d==0){
int choix = Menu();
 
 
 
 
 if (choix ==1)
 {
 
    while (gameOver!=1)
    {
 
 
        TerrainEtSnake();
        Entree();
        grandir();
        gameOver=mouvement_case_par_case();
        nb_vies=vies(nb_vies);
        gameOver=perte(nb_vies);
 
 
        }
 }
 
 
 
 if(choix==2)
 {
     while (gameOver!=1)
     {
 
     TerrainEtSnake();
        Entree();
        grandir();
        gameOver=mouvement();
        nb_vies=vies(nb_vies);
        gameOver=perte(nb_vies);
        Sleep(vitesse());
 
      }
 
 }
     }
return 0;
 
 
}