voila un jeu de serpent, comme snake sur les portables.
On peut choisir la vitesse, l'apparence du serpent, le nombre d'obstacle, si on est mort quand on touche un mur on si on reapparait de l'autre cote,...
Source / Exemple :
#include <stdio.h>
#include <conio.c> //OU CONIO.H
#define ROUGE 12
#define JAUNE 14
#define VERT 10
#define VIOLET 13
#define BLANC 15
#define GAUCHE 75
#define DROITE 77
#define HAUT 72
#define BAS 80
#define ENTER 13
#define ESCAPE 27
#define NORMAL 0
#define MORT 1
#define POMME 2
#define MENU 3
#define BONUS 4
typedef struct
{
int X;
int Y;
} POS;
void recta(int A,int B,int X,int Y);
POS hasard();
int main()
{
POS pomme; //POSITION DES POMMES
POS blocs[10]; //POSITION DES OBSTACLES
int bonus; //UTILE POUR LES BONUS
int increment; //UTILE POUR LES BOUCLES
int increment2; //UTILE POUR LES BOUCLES
int obstacle=-1; //NOMBRE D'OBSTACLES (-1 = 0)
int serpent=4; //APPARENCE DU SERPENT
int sortie=2; //POUR LA BOUCLE PRINCIPALE
int longueur; //LONGUEUR DU SERPENT
int direction; //DIRECTION DANS LA QUELLE ON VA
int vitesse=9; //VITESSE DU SERPENT
int action=MENU; //ACTION A FAIRE
int mode=2; //MODE DE JEU
int points; //POINTS
int controle=1; //STYLE DE CONTROLE
int score=0; //MEILLEUR SCORE
int deplacements; //NOMBRES DE DEPLACEMENTS
int cases[80][23]; //TOUTES LES CASES DU JEU
int provi[80][23]; //TOUTES LES CASES DU JEU (PROVISOIRE)
char touche; //POUR CONTENIR LA TOUCHE ENFONCEE
/*BOUCLE PRINCIPALE*/
while (sortie!=0)
{
if (action==MENU)
{
clrscr(); //EFFACE L'ECRAN
/*INITIALISE TOUT*/
longueur=5; //LONGUEUR DE DEPART
bonus=0; //BONUS
action=NORMAL; //ACTION A FAIRE
points=0; //POINTS
sortie=2; //POUR FAIRE UNE PAUSE AU DEBUT DE CHAQUE PARTIE
deplacements=0; //NOMBRE DE DEPLACEMENTS
direction=2; //DIRECTION DE DEPART
/*INITIALISATION DES CASES*/
for (increment=0 ; increment<23 ; increment++)
{
for (increment2=0 ; increment2<80 ; increment2++) cases[increment2][increment]=0;
}
/*POSITION DE LA PREMIERE POMME*/
pomme=hasard();
cases[pomme.X][pomme.Y]=-2;
/*INITIALISATION DU SERPENT*/
cases[21][10]=-1;
for (increment=1 ; increment<=longueur ; increment++) cases[21-increment][10]=increment;
/*ECRIS LE MENU DU DEBUT*/
gotoxy(1,1);
textcolor(ROUGE); //ECRIS EN ROUGE
printf("-----SNAKE-----\n\n");
textcolor(JAUNE); //ECRIS EN JAUNE
printf("Le but du jeu est d'attraper les pastilles vertes.\n");
printf("Plus vous prenez de pastilles plus vous \x88tes long et plus c'est difficile.\n");
/*ON CHOISIT LA RAPIDITE DU JEU*/
do
{
gotoxy(1,6);
textcolor(JAUNE);
printf("rapidit\x82 du jeu : ");
textcolor(VERT);
printf("%d",vitesse);
touche=getch();
switch (touche)
{
case GAUCHE: vitesse--;
if (vitesse==0) vitesse=9;
break;
case DROITE: vitesse++;
if (vitesse==10) vitesse=1;
break;
}
} while (touche!=ENTER);
/*ON CHOISIT LE MODE DE JEU*/
do
{
gotoxy(1,7);
textcolor(VERT);
if (mode==1) printf("On meurt");
else printf("On r\x82 \bapparait de l'autre c\x93t\x82");
textcolor(JAUNE);
printf(" quand on touche le bord ");
touche=getch();
if ((touche==GAUCHE)||(touche==DROITE))
{
if (mode==1) mode=2;
else mode=1;
}
} while (touche!=ENTER);
/*ON CHOISIT L'APPARENCE DU SERPENT*/
do
{
gotoxy(1,8);
textcolor(JAUNE);
printf("apparence du serpent : ");
textcolor(VERT);
switch (serpent)
{
case 1: printf("\xDB\xDB\xDB\xDB\xDB\x10");
break;
case 2: printf("\xC4\xC4\xC4\xC4\xC4\x1A");
break;
case 3: printf("\xCD\xCD\xCD\xCD\xCD\x10");
break;
case 4: printf("\xC4\xC4\xC4\xC4\xC4\x10");
break;
}
touche=getch();
switch (touche)
{
case GAUCHE: serpent--;
if (serpent==0) serpent=4;
break;
case DROITE: serpent++;
if (serpent==5) serpent=1;
break;
}
} while (touche!=ENTER);
/*ON CHOISIT LE MODE DE CONTROLE*/
do
{
gotoxy(1,9);
textcolor(JAUNE);
printf("Mode de controle : ");
textcolor(VERT);
if (controle==1) printf("normal ");
else printf("2 boutons");
touche=getch();
if ((touche==GAUCHE)||(touche==DROITE))
{
if (controle==1) controle=2;
else controle=1;
}
} while (touche!=ENTER);
/*ON CHOISIT LE NOMBRE D'OBSTACLES*/
do
{
gotoxy(1,10);
textcolor(JAUNE);
printf("Nombre d'obstacle : ");
textcolor(VERT);
printf("%-2d",obstacle+1);
touche=getch();
switch (touche)
{
case GAUCHE: obstacle--;
if (obstacle==-2) obstacle=9;
break;
case DROITE: obstacle++;
if (obstacle==10) obstacle=-1;
break;
}
} while (touche!=ENTER);
/*INITIALISE LES OBSTACLES*/
if (obstacle>-1)
{
textcolor(JAUNE);
do
{
gotoxy(1,12);
printf("Initialisation bloc 1...");
blocs[0]=hasard();
} while ((blocs[0].X==0)||(blocs[0].X==79)||(blocs[0].Y==0)||(blocs[0].Y==22));
for (increment=1 ; increment<=obstacle ; increment++)
{
do
{
gotoxy(1,12+increment);
printf("Initialisation bloc %d...",increment+1);
blocs[increment]=hasard();
} while (((blocs[increment].X==blocs[increment-1].X)&&(blocs[increment].Y==blocs[increment-1].Y))
|| ((blocs[increment].X==0)||(blocs[increment].X==79)||(blocs[increment].Y==0)||(blocs[increment].Y==22)));
}
for (increment=0 ; increment<=obstacle ; increment++)
{
cases[blocs[increment].X][blocs[increment].Y]=-3;
}
}
/*INITIALISE LE TABLEAU PROVISOIRE*/
for (increment=0 ; increment<23 ; increment++)
{
for (increment2=0 ; increment2<80 ; increment2++) provi[increment2][increment]=cases[increment2][increment];
}
clrscr(); //EFFACE L'ECRAN
/*DESSINE LA LIGNE SEPARATRICE ROUGE*/
gotoxy(1,24);
textcolor(ROUGE);
for (increment=1 ; increment<=80 ; increment++) printf("\xC4");
}
/*DEBUT DU JEU*/
action=NORMAL; //AUCUNE ACTION SPECIALE A FAIRE
/*AFFICHE TOUTES LES CASES*/
for (increment=0 ; increment<23 ; increment++)
{
for (increment2=0 ; increment2<80 ; increment2++)
{
textcolor(VERT); //ECRIS EN VERT
if (cases[increment2][increment]<0)
{
/*EFFACE DERNIERE CASE DE LA QUEUE DU SERPENT*/
if (cases[increment2][increment]==-1)
{
gotoxy(increment2+1,increment+1);
printf(" ");
cases[increment2][increment]=0;
}
/*UNIQUEMENT POUR VERIFIER SI IL Y A DES POMMES*/
if (cases[increment2][increment]==-2) action=POMME;
/*DESSINE LES OBSTACLES*/
if (cases[increment2][increment]==-3)
{
gotoxy(increment2+1,increment+1);
textcolor(VIOLET);
printf("\xDB");
}
}
/*DESSINE LE SERPENT*/
else if (cases[increment2][increment]!=0)
{
gotoxy(increment2+1,increment+1);
/*TETE DU SERPENT*/
if (cases[increment2][increment]==1)
{
if (((cases[0][increment]==1)&&(cases[79][increment]==2))
|| ((cases[increment2+1][increment]==2)&&(increment2!=79)))
{
if (serpent==1) printf("\x11");
if (serpent==2) printf("\x1B");
if (serpent==3) printf("\x11");
if (serpent==4) printf("\x11");
}
if (((cases[79][increment]==1)&&(cases[0][increment]==2))
|| ((cases[increment2-1][increment]==2)&&(increment2!=0)))
{
if (serpent==1) printf("\x10");
if (serpent==2) printf("\x1A");
if (serpent==3) printf("\x10");
if (serpent==4) printf("\x10");
}
if (((cases[increment2][22]==1)&&(cases[increment2][0]==2))
|| ((cases[increment2][increment+1]==2)&&(increment!=22)))
{
if (serpent==1) printf("\x1E");
if (serpent==2) printf("\x18");
if (serpent==3) printf("\x1E");
if (serpent==4) printf("\x1E");
}
if (((cases[increment2][0]==1)&&(cases[increment2][22]==2))
|| ((cases[increment2][increment-1]==2)&&(increment!=0)))
{
if (serpent==1) printf("\x1F");
if (serpent==2) printf("\x19");
if (serpent==3) printf("\x1F");
if (serpent==4) printf("\x1F");
}
}
/*CORPS DU SERPENT*/
else
{
if (((cases[increment2+1][increment]==cases[increment2][increment]-1)
&& (cases[increment2][increment+1]==cases[increment2][increment]+1))
|| ((cases[increment2+1][increment]==cases[increment2][increment]+1)
&& (cases[increment2][increment+1]==cases[increment2][increment]-1)))
{
if (serpent==1) printf("\xDB");
if (serpent==2) printf("\xDA");
if (serpent==3) printf("\xC9");
if (serpent==4) printf("\xDA");
}
else
{
if (((cases[increment2-1][increment]==cases[increment2][increment]-1)
&& (cases[increment2][increment+1]==cases[increment2][increment]+1))
|| ((cases[increment2-1][increment]==cases[increment2][increment]+1)
&& (cases[increment2][increment+1]==cases[increment2][increment]-1)))
{
if (serpent==1) printf("\xDB");
if (serpent==2) printf("\xBF");
if (serpent==3) printf("\xBB");
if (serpent==4) printf("\xBF");
}
else
{
if (((cases[increment2+1][increment]==cases[increment2][increment]-1)
&& (cases[increment2][increment-1]==cases[increment2][increment]+1))
|| ((cases[increment2+1][increment]==cases[increment2][increment]+1)
&& (cases[increment2][increment-1]==cases[increment2][increment]-1)))
{
if (serpent==1) printf("\xDB");
if (serpent==2) printf("\xC0");
if (serpent==3) printf("\xC8");
if (serpent==4) printf("\xC0");
}
else
{
if (((cases[increment2-1][increment]==cases[increment2][increment]-1)
&& (cases[increment2][increment-1]==cases[increment2][increment]+1))
|| ((cases[increment2-1][increment]==cases[increment2][increment]+1)
&& (cases[increment2][increment-1]==cases[increment2][increment]-1)))
{
if (serpent==1) printf("\xDB");
if (serpent==2) printf("\xD9");
if (serpent==3) printf("\xBC");
if (serpent==4) printf("\xD9");
}
else
{
if ((cases[increment2+1][increment]==cases[increment2][increment]+1)
|| (cases[increment2+1][increment]==cases[increment2][increment]-1)
|| (cases[increment2-1][increment]==cases[increment2][increment]-1)
|| (cases[increment2-1][increment]==cases[increment2][increment]+1))
{
if (serpent==1) printf("\xDB");
if (serpent==2) printf("\xC4");
if (serpent==3) printf("\xCD");
if (serpent==4) printf("\xC4");
}
else if ((cases[increment2][increment+1]==cases[increment2][increment]+1)
|| (cases[increment2][increment+1]==cases[increment2][increment]-1)
|| (cases[increment2][increment-1]==cases[increment2][increment]-1)
|| (cases[increment2][increment-1]==cases[increment2][increment]+1))
{
if (serpent==1) printf("\xDB");
if (serpent==2) printf("\xB3");
if (serpent==3) printf("\xBA");
if (serpent==4) printf("\xB3");
}
}
}
}
}
}
}
}
}
/*POSITIONNE LES POMMES*/
for (increment=1 ; increment<=20 ; increment++)
{
for (increment2=0 ; increment2<=100 ; increment2++)
{
/*SI DEPLACEMENTS = 500 POSE UN BONUS QUI RESTE PENDANT 100 DEPLACEMENTS*/
if (deplacements==(increment*500)+increment2)
{
if ((bonus==1)&&(deplacements==(increment*500))) bonus=0;
else if (bonus==0)
{
gotoxy(pomme.X+1,pomme.Y+1);
textcolor(ROUGE);
printf("\x02");
increment=21;
increment2=101;
}
}
/*SINON POSE UNE POMME*/
else if ((increment==20)&&(increment2==100))
{
gotoxy(pomme.X+1,pomme.Y+1);
textcolor(JAUNE);
printf("\x0F");
}
}
}
/*FAIS UNE PAUSE AU DEBUT DE CHAQUE NOUVELLE PARTIE*/
if (sortie==2)
{
textcolor(BLANC);
gotoxy(1,25);
printf("Appuyez sur une touche pour commencer");
getch();
sortie=1;
}
/*SI N'A PAS TROUVE UNE SEULE POMME SUR LE TABLEAU*/
if (action!=POMME)
{
/*INITIALISE UNE NOUVELLE POSITION DE POMME*/
do
{
pomme=hasard();
} while (cases[pomme.X][pomme.Y]!=0);
provi[pomme.X][pomme.Y]=-2;
/*VERIFIE SI ON A GAGNE UN BONUS*/
for (increment=1 ; increment<=20 ; increment++)
{
for (increment2=0 ; increment2<=100 ; increment2++)
{
if ((deplacements==(increment*500)+increment2)&&(bonus==0))
{
increment=21;
increment2=101;
action=BONUS;
}
}
}
/*SI C'EST UN BONUS*/
if (action==BONUS)
{
points=points+(10*vitesse);
longueur=longueur+8;
bonus=1;
}
/*SINON*/
else
{
points=points+vitesse;
longueur=longueur+4;
}
/*ACTUALISE LE MEILLEUR SCORE*/
if (points>score) score=points;
/*AUCUNE ACTION SPECIALE A FAIRE => CONTINUE NORMALEMENT*/
action=NORMAL;
}
/*SI UNE TOUCHE EST PRESSE*/
if (kbhit())
{
touche=getch(); //PREND LA TOUCHE
/*FAIS UNE ACTION SELON LA TOUCHE*/
switch (touche)
{
case GAUCHE: if (controle==2)
{
switch (direction)
{
case 1: direction=4;
break;
case 2: direction=3;
break;
case 3: direction=1;
break;
case 4: direction=2;
break;
}
}
else if (direction!=2) direction=1;
break;
case DROITE: if (controle==2)
{
switch (direction)
{
case 1: direction=3;
break;
case 2: direction=4;
break;
case 3: direction=2;
break;
case 4: direction=1;
break;
}
}
else if (direction!=1) direction=2;
break;
case HAUT: if ((direction!=4)&&(controle==1)) direction=3;
break;
case BAS: if ((direction!=3)&&(controle==1)) direction=4;
break;
case ESCAPE: sortie=0;
break;
case ENTER: gotoxy(1,25);
textcolor(BLANC);
printf(" -------------------------------JEU MIS EN PAUSE-------------------------------");
touche=0;
while (touche!=ENTER) touche=getch();
break;
}
/*SI UNE AUTRE TOUCHE EST PRESSE (PERMET DE POUVOIR PRESSER DEUX TOUCHE
DANS UN SEULE BOUCLE ET AINSI AUGMENTER LA MOBILITE DU SERPENT*/
if (kbhit())
{
touche=getch();//REPREND LA TOUCHE
/*FAIS UNE ACTION SELON LA TOUCHE*/
switch (touche)
{
case GAUCHE: if (controle==2)
{
switch (direction)
{
case 1: direction=4;
break;
case 2: direction=3;
break;
case 3: direction=1;
break;
case 4: direction=2;
break;
}
}
else if (direction!=2) direction=1;
break;
case DROITE: if (controle==2)
{
switch (direction)
{
case 1: direction=3;
break;
case 2: direction=4;
break;
case 3: direction=2;
break;
case 4: direction=1;
break;
}
}
else if (direction!=1) direction=2;
break;
case HAUT: if ((direction!=4)&&(controle==1)) direction=3;
break;
case BAS: if ((direction!=3)&&(controle==1)) direction=4;
break;
case ESCAPE: sortie=0;
break;
case ENTER: gotoxy(1,25);
textcolor(BLANC);
printf(" -------------------------------JEU MIS EN PAUSE-------------------------------");
touche=0;
while (touche!=ENTER) touche=getch();
break;
}
}
}
/*REPOSITIONNE LE SERPENT*/
for (increment=0 ; increment<23 ; increment++)
{
for (increment2=0 ; increment2<80 ; increment2++)
{
/*SI TOUCHE UN OBSTACLE*/
if (cases[increment2][increment]==-3)
{
if ((cases[increment2-1][increment]==1)
&& (direction==2)) action=MORT;
if ((cases[increment2+1][increment]==1)
&& (direction==1)) action=MORT;
if ((cases[increment2][increment-1]==1)
&& (direction==4)) action=MORT;
if ((cases[increment2][increment+1]==1)
&& (direction==3)) action=MORT;
}
/*REPOSITIONNE LA TETE DU SERPENT*/
if (cases[increment2][increment]==1)
{
switch (direction)
{
case 1: if (increment2==0)
{
if (mode==1) action=MORT;
if (mode==2) provi[79][increment]=1;
}
else
{
if (cases[increment2-1][increment]<=0)
{
provi[increment2-1][increment]=1;
}
else action=MORT;
}
break;
case 2: if (increment2==79)
{
if (mode==1) action=MORT;
if (mode==2) provi[0][increment]=1;
}
else
{
if (cases[increment2+1][increment]<=0)
{
provi[increment2+1][increment]=1;
}
else action=MORT;
}
break;
case 3: if (increment==0)
{
if (mode==1) action=MORT;
if (mode==2) provi[increment2][22]=1;
}
else
{
if (cases[increment2][increment-1]<=0)
{
provi[increment2][increment-1]=1;
}
else action=MORT;
}
break;
case 4: if (increment==22)
{
if (mode==1) action=MORT;
if (mode==2) provi[increment2][0]=1;
}
else
{
if (cases[increment2][increment+1]<=0)
{
provi[increment2][increment+1]=1;
}
else action=MORT;
}
break;
}
provi[increment2][increment]=2;
}
/*REPOSITIONNE LE CORPS DU SERPENT*/
else if (cases[increment2][increment]>0)
{
/*SI EST A LA FIN DU SERPENT*/
if (cases[increment2][increment]==longueur) provi[increment2][increment]=-1;
/*SINON*/
else provi[increment2][increment]++;
}
/*SI LE SERPENT EST MORT*/
if (action==MORT)
{
textcolor(ROUGE);
printf("\a");
increment2=80;
increment=23;
if (points>score) score=points;
recta(30,10,27,6);
gotoxy(31,11);
textcolor(JAUNE);
printf("Vous avez perdu !");
gotoxy(31,12);
printf("Longueur du serpent : %-3d",longueur);
gotoxy(31,13);
printf("Votre score : %-4d",points);
gotoxy(31,14);
printf("Meilleur score : %-4d",score);
while (touche!=ENTER) touche=getch();
action=MENU;
}
}
}
/*ATTEND UN CERTAIN TEMPS SELON LA VITESSE CHOISIE*/
sleep(100-(9*vitesse));
/*REINITIALISE LES CASES AVEC LE TABLEAU PROVISOIRE*/
for (increment=0 ; increment<23 ; increment++)
{
for (increment2=0 ; increment2<80 ; increment2++) cases[increment2][increment]=provi[increment2][increment];
}
deplacements++; //AUGMENTE LE DEPLACEMENTS DE 1
/*ECRIS LE NOMBRE DE DEPLACEMENTS, DE POINTS ET LE MEILLEUR SCORE*/
gotoxy(1,25);
textcolor(BLANC);
printf(" nombre de d\x82placements : %-6d meilleur score : %-4d points : %-4d ",deplacements,score,points);
}
return 0;
}
/*POUR DESSINER UN RECTANGLE*/
void recta(int A,int B,int X,int Y)
{
int increment;
int increment2;
gotoxy(A,B);
printf("\xDA"); //COIN HAUT GAUCHE
for (increment=1 ; increment <= (X-2) ; increment++) printf("\xC4"); //LIGNE
printf("\xBF"); //COIN HAUT DROITE
for (increment=1 ; increment <= (Y-2) ; increment++)
{
gotoxy(A,B+increment);
printf("\xB3"); //LIGNE VERTICALE
for (increment2=1 ; increment2 <= (X-2) ; increment2++) printf("\x20"); //ESPACES
printf("\xB3"); //LIGNE VERTICALE
}
gotoxy(A,B+Y-1);
printf("\xC0"); //COIN BAS GAUCHE
for (increment=1 ; increment <= (X-2) ; increment++) printf("\xC4"); //LIGNE
printf("\xD9"); //COIN BAS DROITE
}
/*GENERE UNE POSITION AU HASARD*/
POS hasard()
{
POS nombre;
srand(time(NULL));
nombre.X=rand() % 80;
nombre.Y=rand() % 23;
return nombre;
}
Conclusion :
voila ! il y a encore quelques bugs, qui arrivent tres rarement, que je n'ai pas reussi a corriger :
_parfois quand on commence une nouvelle partie on meurt tout de suite si on choisit de commencer a gauche, il faut faire haut ou bas comme tout premier mouvement pour ne pas mourir.
_parfois, mais c'est extremement rare, lorsque l'on touche le bord en haut on n'apparait pas de l'auter cote et on continue comme ça a l'infini. C'est le bug le plus grave mais il est tres rare
Il y a un autre petit probleme, pas tres important, c'est que quelques fois quand on prend une pomme le jeu se stoppe 1/2 secondes, puis continue normalement. Quand il se passe ça c'est que la nouvelle position de la pomme est tombee sur une position deja occupe (par le serpent ou un obstacle), alors le pc genere une autre position et c'est ça qui ralentit (plus le serpent est long plus il y a des chances que ce bug arrive). Sinon c'est tout, je n'ai pas trouve d'autres bugs.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.