Serpent

Description

  • Comme je n'ai pas de Nokia j'ai voulu faire un jeu comme celui qui se

trouve sur le nokia mais un peu plus sympa :).
  • Pour tout ceux que ?a interesse, ce jeu est ecrit en 'language C' et utilise

les pointeurs et aussi le Mode graphique .
Appuyez sur une touche pour retourner au Menu ..."

Source / Exemple :


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

   struct els
{
   int abs,crd;

 els * suiv,* prec;

};
   els * tete,* queue , * temp ;
   int niveau,taille,ajout;
   long score;
   char une_fois;

void menu(void);
void jouer(void);

void carre(int x1,int y1,int cl)
{
   setcolor(cl);
   rectangle(x1*20+1  ,y1*20 + 1  +60 ,x1*20 + 19  ,y1*20+19 +60);
   rectangle(x1*20+3  ,y1*20 + 3  +60 ,x1*20 + 17  ,y1*20+17 +60);
   rectangle(x1*20+5  ,y1*20 + 5  +60 ,x1*20 + 15  ,y1*20+15 +60);
   rectangle(x1*20+7  ,y1*20 + 7  +60 ,x1*20 + 13  ,y1*20+13 +60);

}

void new_point(void)
{  int a,b;
   a= random(28) +1;
   b= random(18)+1;
   a= random(28) +1;
   b= random(18)+1;

   while(getpixel(a*20 +1,b*20 +1+60))
   {
   a= random(28) +1;
   b= random(18)+1;
   }
   carre(a,b,4);
}

void calcul()
{
   if (ajout >4)
   { ajout=0;
	niveau++ ;
	if (niveau > 10) niveau--;
	sound(10);
	delay(100);
	nosound();
   }
   score +=   niveau*10;
   gotoxy(5,4);
   textcolor(2);
   setcolor(2);
   printf("score : %d",score);
   gotoxy(20,4);
   printf("Niveau : %d", niveau);

}
void perdu()
{
 gotoxy(32,4);
			   textcolor(5);
			   printf("  Vous avez Perdu !!!!,");
			   gotoxy(55,4);
			   textcolor(5);
			   printf("Rejouer ? (o/n) : ");
			   for(int i =getch();(i!='o')&&(i!='n')&&(i!='O')&&(i!='N')&&(i!=27)&&(i!=13); i=getch());
			   if ((i=='o')||(i=='O')||(i==13)) jouer();
			   else {    closegraph(); menu();}
			   gotoxy(30,4);
			   textcolor(5);
			   printf("                        ");
			   gotoxy(60,4);
			   textcolor(5);
			   printf("                        ");

}
void liberer()
{
  for(temp =tete;temp;temp=tete)
  {
  tete=(*temp).prec;
  delete temp;

  }

}

void positionner (int abs1, int crd1 )
{
		if ((abs1>30) || (abs1<1) || (crd1<1) || (crd1>19))
		  {
		    if (une_fois)
			{
			  une_fois=0;
			  liberer();
			  perdu();
			}
			else {une_fois=1; delay(50);}
		  }
		else
		switch (getpixel(abs1*20+1 , crd1*20+1+60))
		{
		case 3:

			if (une_fois)
			{
			  une_fois=0;
			  liberer();
			  perdu();
			}
			else {une_fois=1;delay(50);}
			break;

		case 4:
			carre(abs1,crd1,6);
			carre((*tete).abs,(*tete).crd,3);
			temp = new els;
			(*temp).abs =abs1;
			(*temp).crd =crd1;

			(*temp).suiv = NULL;
			(*temp).prec = tete;
			(*tete).suiv=temp;
			tete=temp;
			ajout++;
			taille++;
			calcul();
			new_point();
			une_fois=0;
			break;

		default :
		temp = new els;
		(*temp).abs =abs1;
		(*temp).crd =crd1;

		(*temp).suiv = NULL;
		(*temp).prec = tete;
		(*tete).suiv=temp;
		tete=temp;

		temp = queue;
		(*(*temp).suiv).prec= NULL;
		queue=(*temp).suiv;

		carre((*tete).abs,(*tete).crd,6) ;
		carre((*temp).abs,(*temp).crd,0);
		carre((*((*tete).prec)).abs,(*((*tete).prec)).crd,3);
		delete temp;
		break;
		}

}

void jouer()
{
   int i;

   tete = new els;
   (*tete).abs = 10;
   (*tete).crd = 10;
   (*tete).suiv = NULL;
   (*tete).prec = NULL;

   temp = new els;
   (*temp).abs = 9;
   (*temp).crd = 10;
   (*temp).prec = NULL;

   (*temp).suiv = tete;
   (*tete).prec = temp;
   queue=temp;

   temp = new els;
   (*temp).abs = 8;
   (*temp).crd = 10;
   (*temp).prec = NULL;

   (*temp).suiv = queue;
   (*queue).prec = temp;
   queue = temp;

   temp = new els;
   (*temp).abs = 7;
   (*temp).crd = 10;
   (*temp).prec = NULL;

   (*temp).suiv = queue;
   (*queue).prec = temp;
   queue = temp;

   int gdriver = DETECT, gmode, errorcode;
   int left, top, right, bottom;

   initgraph(&gdriver, &gmode, "c:\\borlandc\\bgi");

   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }

   taille = 0;
   score=0;
   ajout=0;
   setcolor(7);
   rectangle(20,10,620,78);
   setcolor(1);
   rectangle(17,77,623,463);
   rectangle(18,78,622,462);
   rectangle(19,79,621,461);
   rectangle(20,80,620,460);
   gotoxy(30,3);
   textcolor(9);
   setcolor(9);
   printf(" THE SNAKE  V 1.0 ");

   carre (7,7,4);
   temp=tete;
   gotoxy(5,4);
   textcolor(2);
   setcolor(2);
   printf("Score : %d", score);
   gotoxy(20,4);
   printf("Niveau : %d", niveau);
   carre((*temp).abs,(*temp).crd,6);
   for (;(*temp).prec; )
   {  temp=(*temp).prec;
      carre((*temp).abs,(*temp).crd,3);
      }
   getch();

   int code,car;
   int direction;
   direction =2;

   while (1==1)
   {

   delay(500 / niveau);

	code = kbhit();
	if (code)
	{

	car=getch();
	if (car==0) car = getch();
	switch (car)
	{
	  case 27 :    closegraph();
			menu();
			break;
	  case 32 :gotoxy(40,4);
		   textcolor(5);
		   printf("PAUSE");
		   for(i=getch();(i!=32)&&(i!=72)&&(i!=77)&&(i!=75)&&(i!=80);i=getch());
		   gotoxy(40,4);
		   textcolor(5);
		   printf("      ");
		   break;

	  case 72 : if (direction!=3) direction=1; break;
	  case 77 : if (direction!=4) direction=2; break;
	  case 80 : if (direction!=1) direction=3; break;
	  case 75 : if (direction!=2) direction=4;break;
	}
	 }

   switch (direction)
   {

      case  1 : positionner((*tete).abs,(*tete).crd-1 );
		break;
      case  2 : positionner((*tete).abs +1,(*tete).crd );
		break;
      case  3 : positionner((*tete).abs,(*tete).crd+1 );
		break;
      case  4 : positionner((*tete).abs-1,(*tete).crd );
		break;
   }

   }

   closegraph();
}
void description(void)
{
	clrscr();

	gotoxy(30,1);
	textcolor(9);
	cprintf(" THE SNAKE  V 1.0 ");
	gotoxy(30,2);
	textcolor(15);
	cprintf(" ^^^^^^^^^^^^^^^^ ");
	gotoxy(50,3);
	textcolor(2);
	cprintf(" By : ALI");
	gotoxy(50,4);
	textcolor(2);
	cprintf(" Url : http://ali25.8m.com ");
	gotoxy(50,5);
	textcolor(2);
	cprintf(" Mail : coper23@yahoo.fr ");

	gotoxy(2,7);
	textcolor(2);
	cprintf(" Description : ");
	textcolor(7);
	gotoxy(2,8);
	cprintf(" *  Comme je n'ai pas de Nokia j'ai voulu faire un jeu comme celui qui se");
	gotoxy(1,9);
	cprintf(" trouve sur le nokia mais un peu plus sympa :).  ");
	gotoxy(2,10);
	cprintf(" *  Pour tout ceux que ?a interesse, ce jeu est ecrit en 'language C' et utilise");
	gotoxy(1,11);
	cprintf(" les pointeurs et aussi le Mode graphique .");
	gotoxy(20,23);
	cprintf("Appuyez sur une touche pour retourner au Menu ...");
	getch();
	menu();

}
void affiche_niveau(int n1)
{
gotoxy(6,11);
cprintf("                    ");
for (int i1=1;i1<=n1;i1++)
{
gotoxy(6+i1,11);
	cprintf("°");

}

}
void f_niveau()
{
char car;
clrscr();
	gotoxy(30,1);
	textcolor(9);
	cprintf(" THE SNAKE  V 1.0 ");
	gotoxy(30,2);
	textcolor(15);
	cprintf(" ^^^^^^^^^^^^^^^^ ");
	gotoxy(2,6);
	cprintf("utiliser les fl?ches sur le clavier  <-- & --> !!!");
	gotoxy(2,8);
	cprintf("Niveau : %d  ",niveau);
	affiche_niveau(niveau);
	for (car=getch();(car != 27)&&(car!=13) ;car=getch())
	{
	if (car == 77) niveau++;
	if (niveau>10) niveau--;
	if (car==75)niveau--;
	if (niveau<1) niveau++;

	gotoxy(2,8);
	cprintf("Niveau : %d  ",niveau);

	affiche_niveau(niveau);

	}
	menu();

}
void f_score()
{
clrscr();
	gotoxy(30,1);
	textcolor(9);
	cprintf(" THE SNAKE  V 1.0 ");
	gotoxy(30,2);
	textcolor(15);
	cprintf(" ^^^^^^^^^^^^^^^^ ");
	gotoxy(2,6);
	cprintf("Cette partie sera disponible bient?t  :)");
	for (int car=getch();(car != 27)&&(car!=13) ;car=getch());
	menu();

}
void f_source()
{

clrscr();
	gotoxy(30,1);
	textcolor(9);
	cprintf(" THE SNAKE  V 1.0 ");
	gotoxy(30,2);
	textcolor(15);
	cprintf(" ^^^^^^^^^^^^^^^^ ");
	gotoxy(2,6);
	textcolor(7);
	cprintf("Le source de ce jeu et beaucoup d'autres programmes sont");
	cprintf("\n\n%c disponiblent sur :",13);
	textcolor(2);
	gotoxy(30,8);
	cprintf("http://ali25.8m.com");
	textcolor(7);
	gotoxy(2,10);
	cprintf("Mon mail         :");
	gotoxy(30,10);
	textcolor(2);
	cprintf("coper23@yahoo.fr");
	textcolor(7);
	for (int car=getch();(car != 27)&&(car!=13) ;car=getch());
	menu();

}

void menu(void)
{

	char choix;
	clrscr();
	gotoxy(30,1);
	textcolor(9);
	cprintf(" THE SNAKE  V 1.0 ");
	gotoxy(30,2);
	textcolor(15);
	cprintf(" ^^^^^^^^^^^^^^^^ ");
	gotoxy(2,8);
	cprintf("Menu : ");
	gotoxy(6,11);
	cprintf("1) Jouer");
	gotoxy(6,12);
	cprintf("2) Niveau");
	gotoxy(6,13);
	cprintf("3) Meilleurs Scores");
	gotoxy(6,14);
	cprintf("4) Avoir le source du jeu");
	gotoxy(6,15);
	cprintf("5) Description");
	gotoxy(6,16);
	cprintf("6) Quitter 'ou Echap'");

	gotoxy(20,23);
	cprintf("Fa?tes votre choix : ");

	for(choix=getch();(choix<'1')&&(choix>'6')&&(choix!=27);choix=getch());
	switch (choix)
	{
	  case '1': jouer();break;
	  case '2':f_niveau();break;
	  case '3' : f_score();break;
	  case '4' : f_source();break;
	  case '5':description();
	  case '6':exit(1);
	  case 27 : exit(1);
	  default : menu();
	}
}

void main(void)
{
 niveau=6;
 menu();
}

Conclusion :


merci de jetter un coup d'oeil sur http://ali.casa.free.fr

Codes Sources

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.