Labyrinthe

Contenu du snippet

un simple jeu de labyrinthe ki se joue a 2 joueurs
le joueur 1 fait le labyrinthe (il fait le tracé puis appuie sur espace pour valider celui ci) et le joueur 2 doit trouver la sortie.
a votre disposition 2 niveaux de difficultés:
-facile: qd on se heurte a un mur celui ci s'affiche.
-difficile: les murs ne s'affiche pas.
voila c mon premier jeu en c.
(dsl pas de commentaire)

Source / Exemple :


#include<stdio.h>
#include<graphics.h>

int mur_V[20][3];
int mur_H[20][3];

void init() {
int i,j,g,h;
g=50; h=100;
setbkcolor(9);
for(i=0;i<4;i++){
setcolor(4);
rectangle(g,50,h,250);
rectangle(50,g,250,h);
g=g+50;
h=h+50;
}
setcolor(9);
line(50,50,50,100);
line(250,250,250,200);
}

void coord(int a,int i,int x2,int y2) {
int k,m;
m=1;
if(a==0) {
for(k=0;k<i;k++)
{
if((x2==mur_H[k][0]) && (y2==mur_H[k][1]))
{
m=0;
k=i;
}
}
if(m==1) {
mur_H[i][0]=x2;
mur_H[i][1]=y2;
}
}
if(a==1) {
for(k=0;k<i;k++) {
if((x2==mur_V[k][0]) && (y2==mur_V[k][1]))
{
m=0;
k=i;
}
}
if(m==1) {
mur_V[i][0]=x2;
mur_V[i][1]=y2;
}
}
}

void ligne(int coul,int x1,int y1,int x2,int y2) {
setcolor(coul);
line(x1,y1,x2,y2);
}

void curseur(int x2,int y2,int coul) {
setcolor(coul);
circle(x2,y2,5);
}

int deplacement(int d,int x2,int y2) {
curseur(x2,y2,4);

if(d==0) {
curseur(x2,y2,9);
y2=y2-50;
return y2;
}

if(d==1) {
curseur(x2,y2,9);
y2=y2+50;
return y2;
}

if(d==2) {
curseur(x2,y2,9);
x2=x2-50;
return x2;
}

if(d==3) {
curseur(x2,y2,9);
x2=x2+50;
return x2;
}
}

void init_2() {
setbkcolor(9);
setcolor(4);
line(50,50,250,50);
line(50,100,50,250);
line(50,250,250,250);
line(250,50,250,200);
}

int jeu_1(int i,int j) {
int k,m,c,x2,y2,tmp,mode,pilote;
detectgraph(&pilote,&mode);
initgraph(&pilote,&mode,"c:\\tc");
x2=75;
y2=75;
tmp=0;
init_2();
curseur(x2,y2,4);

while((x2!=225) || (y2!=225)) {
curseur(x2,y2,4);
c=getch();
m=0;

if(c==72) {
if(y2>75) {
for(k=0;k<i;k++) {
if((x2==mur_H[k][0]) && (y2-25==mur_H[k][1])) {
y2=deplacement(0,x2,y2);
k=i;
m=1;
}
}
if(m==0)
ligne(4,x2-25,y2-25,x2+25,y2-25);
}
tmp++;
}

if(c==75) {
if(x2>75)
{
for(k=0;k<j;k++) {
if((y2==mur_V[k][0]) && (x2-25==mur_V[k][1])) {
x2=deplacement(2,x2,y2);
k=j;
m=1;
}
}
if(m==0)
ligne(4,x2-25,y2-25,x2-25,y2+25);
}
tmp++;
}

if(c==77) {
if((x2<225) || (y2==225))
{
for(k=0;k<j;k++) {
if((y2==mur_V[k][0]) && (x2+25==mur_V[k][1])) {
x2=deplacement(3,x2,y2);
k=j;
m=1;
}
}
if(m==0)
ligne(4,x2+25,y2-25,x2+25,y2+25);
}
tmp++;
}

if(c==80) {
if(x2<=225) {
for(k=0;k<i;k++) {
if((x2==mur_H[k][0]) && (y2+25==mur_H[k][1])) {
y2=deplacement(1,x2,y2);
k=i;
m=1;
}
}
if(m==0)
ligne(4,x2-25,y2+25,x2+25,y2+25);
}
tmp++;
}
}
cleardevice();
setcolor(4);
outtextxy(225,250,"VOUS AVEZ R‚USSI !!!");
system("pause");
closegraph();
return tmp;
}

int jeu_2(int i,int j) {
int k,c,x2,y2,tmp,pilote,mode;
detectgraph(&pilote,&mode);
initgraph(&pilote,&mode,"c:\\tc");
x2=75;
y2=75;
tmp=0;
init_2();
curseur(x2,y2,4);

while((x2!=225) || (y2!=225)) {
curseur(x2,y2,4);
c=getch();

if(c==72) {
if(y2>75) {
for(k=0;k<i;k++) {
if((x2==mur_H[k][0]) && (y2-25==mur_H[k][1])) {
y2=deplacement(0,x2,y2);
k=i;

}
}
}
tmp++;
}

if(c==75) {
if(x2>75)
{
for(k=0;k<j;k++) {
if((y2==mur_V[k][0]) && (x2-25==mur_V[k][1])) {
x2=deplacement(2,x2,y2);
k=j;

}
}
}
tmp++;
}

if(c==77) {
if((x2<225) || (y2==225))
{
for(k=0;k<j;k++) {
if((y2==mur_V[k][0]) && (x2+25==mur_V[k][1])) {
x2=deplacement(3,x2,y2);
k=j;

}
}
}
tmp++;
}

if(c==80) {
if(x2<=225) {
for(k=0;k<i;k++) {
if((x2==mur_H[k][0]) && (y2+25==mur_H[k][1])) {
y2=deplacement(1,x2,y2);
k=i;

}
}
}
tmp++;
}
}
cleardevice();
setcolor(4);
outtextxy(225,250,"VOUS AVEZ R‚USSI !!!");
system("pause");
closegraph();
return tmp;
}

void laby() {
int pilote,mode,x1,y1,x2,y2,c,i,j,v,tmp_2;
i=0;
j=0;
v=0;
x2=75;
y2=75;
detectgraph(&pilote,&mode);
initgraph(&pilote,&mode,"c:\\tc");
setcolor(4);
outtextxy(250,250,"JOUEUR 1 FAIT LE LABY");
system("pause");
cleardevice();
init();
c=1;

while(c!=32) {
curseur(x2,y2,4);
c=getch();
if(c==0)
c=getch();

if((c==72) && (y2>75)) {
coord(0,i,x2,y2-25);
ligne(9,x2-25,y2-25,x2+25,y2-25);
y2=deplacement(0,x2,y2);
i++;
}

if((c==75) && (x2>75)) {
coord(1,j,y2,x2-25);
ligne(9,x2-25,y2-25,x2-25,y2+25);
x2=deplacement(2,x2,y2);
j++;
}

if((c==77) && (x2<225)) {
coord(1,j,y2,x2+25);
ligne(9,x2+25,y2-25,x2+25,y2+25);
x2=deplacement(3,x2,y2);
j++;
}

if((c==80) && (y2<225)) {
coord(0,i,x2,y2+25);
ligne(9,x2-25,y2+25,x2+25,y2+25);
y2=deplacement(1,x2,y2);
i++;
}
}

cleardevice();
closegraph();
while((v!=1) && (v!=2)) {
clrscr();
printf("CHOIX DU NIVEAU DE JEU\n");
printf("NIVEAU FACILE : TAPER 1\n");
printf("NIVEAU DIFFICILE : TAPER 2\n");
scanf("%d",&v);
}
if(v==1)
tmp_2=jeu_1(i,j);
else {
if(v==2)
tmp_2=jeu_2(i,j);
}
printf("VOTRE SCORE EST : %d\n",tmp_2);
system("pause");
}

main() {
laby();
}

A voir également

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.