Pacman

Contenu du snippet

pacman est un jeux developper en language turbo c++
bon exemple pour vous initialiser a la programmation orineté objet

Source / Exemple :


#include<stdlib.h>
#include<graphics.h>
#include<math.h>
#include<stdio.H>
#include<string.h>
#include<conio.h>
#include<dos.h>

#define  ESC   0X1B
#define  CR    0XD
#define  HAUT  0X48
#define  BAS   0X50
#define  DROIT 0X4D
#define  GAUCH 0X4B

#define pi 3.1429654

#define  SUP       420
#define  INF       80
#define lin 70
#define col 70
int vrai=1;
int cpt=0;
int mat[18][18];
int u;
void affiche_stage(int mat[18][18]);
void point_stage(int mat[18][18]);
void Remplir_mat(int mat[18][18]);

class Virus
 {
     public :
		Virus(int,int,int,int);
		void Affiche(int);
		friend void Score();
		friend void demo_music_laser();
		void Cache();
		void Cache1();
		void Command(Virus,int);
		void Command2(Virus,int);

		friend void Command1(Virus,Virus,Virus,Virus);

		void Affiche1(char);
		void Life();
		void Add();
		void vieADD();
		void vieSUB();
		void Compteur();
		void Modifier(int,int);
		int lecX(){return x;}
		int lecY(){return y;}
		int lecVie(){return vie;}

     private :
       int x,y;int c,vie;

 };

//-----------------------------------
Virus::Virus(int x1,int y1,int c1,int v)
{
x=x1;
y=y1;
c=c1;
vie=v;

}
//-----------------------------------
void demo_music_laser()
{
    for(int count=0;count<10;count++)
	{sound(count*100);delay(10);}
     nosound();

}
void demo_music_ambilance()
{
     for(int count=0;count<2;count++)
	{sound(4000);delay(5);sound(7000);delay(5);}
     nosound();

    }

//------------------------------------

void Virus::Compteur()
{
cpt+=1;
gotoxy(1,1);printf("SCORE : %d",cpt);
	 sound(7000);
	 delay(50);
	 nosound();

}
//-----------------------------------
//-----------------------------------
void Virus::Modifier(int x1,int y1)
{
x=(x+x1) ;
y=(y+y1) ;

}
void Virus::vieADD()
{
vie+=1;
}
void Virus::vieSUB()
{
vie-=1;
}
//-----------------------------------
void Virus::Affiche(int n)
{
setcolor(c);
  //arc (x,y-1,0,180,8);      //tete
  arc (x,y+4,-160,-20,4);      //bouche
  arc (x,y+3,35,145,10);
  setfillstyle(SOLID_FILL,15);
  setcolor(15);
  pieslice(x-4,y,0,360,3);           //lunette
  pieslice(x+4,y,0,360,3);

  setfillstyle(SOLID_FILL,0);       //
  setcolor(0);

  switch(n){
    case 0:   pieslice(x-3,y,0,360,2);
	      pieslice(x+7,y,0,360,2);
	      break;
    case 1:   pieslice(x-7,y,0,360,2);
	      pieslice(x+3,y,0,360,2);
	      break;
    case 2:   pieslice(x-5,y+3,0,360,2);
	      pieslice(x+5,y+3,0,360,2);
	      break;
    case 3:   pieslice(x-5,y-3,0,360,2);
	      pieslice(x+5,y-3,0,360,2);
	      break;
	  }

 }
//------------------------------------------
void Virus::Affiche1(char n)
{
setcolor(c);
setfillstyle(1,15);
pieslice(lecX(),lecY(),0, 360, 8);
floodfill(lecX(),lecY(),15);
delay(10);
setcolor(0);setfillstyle(1,0);
   switch(n){

   case DROIT  :pieslice(lecX(),lecY(),0,30,8);break;
   case HAUT   : pieslice(lecX(),lecY(),70,110,8);break;
   case BAS    : pieslice(lecX(),lecY(),-80,-120,8);break;
   case GAUCH  :  pieslice(lecX(),lecY(),160,210,8);break;
    }
}
//""""""""""""""""""""""""""""""""""
void Virus::Life()
{
int ii,jj;
//fillellipse(210,240,8,8);
setcolor(4);
setfillstyle(1,4);
randomize();
ii=rand() % 18;
jj=rand() % 18;

if (getpixel(40*ii+col+10,40*jj+lin+10)!=YELLOW)
   if (( 40*ii+col+10 < SUP-20)&&(40*jj+lin+10 < SUP-20))
	     fillellipse(40*ii+col+10,40*jj+lin+10,8,8);

}
//""""""""""""""""""""""""""""""
void Virus::Add()
{
setcolor(14);
    setfillstyle(SOLID_FILL,14);
    pieslice(520,40+vie*30,20,350,10);
}

//-----------------------------------
void Virus::Cache()
{
int note=0;
//pieslice(x,y,30, 360, 9);
if ((getpixel(x,y)==9)||(getpixel(x,y)==15)){
if (getpixel(x,y)==15) note=1;
setfillstyle(SOLID_FILL, 0);
setcolor(0);
fillellipse(x,y,9,8);
setcolor(9);setfillstyle(SOLID_FILL,9);
if (note) {setcolor(15);setfillstyle(1,15);pieslice(x,y,0,360,3);}
pieslice(x,y,0,360,2);
}

else
{setfillstyle(SOLID_FILL, 0);
setcolor(0);
fillellipse(x,y,9,8);}

}
//-----------------------------------------
void Virus::Cache1()
{
setfillstyle(SOLID_FILL, 0);
setcolor(0);
//pieslice(x,y,30, 360, 9);
fillellipse(x,y,9,8);

}

//-------------------------------------------
//-----------------------------------
void Virus::Command(Virus p,int n)
{

int choix;

 if (( x != p.lecX() ) || ( y != p.lecY() ))

    {if (x != p.lecX())
	 {if (x < p.lecX()) {if (getpixel(x+12,y)!=YELLOW) choix=0;else if (getpixel(x,y-12)!=YELLOW) choix=3;}
	 else {if (getpixel(x-12,y)!=YELLOW) choix=1;else if(getpixel(x,y+12)!=YELLOW) choix=2;}}

    else {if (y < p.lecY()) {if(getpixel(x,y+12)!=YELLOW) choix=2;else if (getpixel(x+12,y)!=YELLOW) choix=0;}
    else {if (getpixel(x,y-12)!=YELLOW) choix=3;else if (getpixel(x-12,y)!=YELLOW) choix=1;}}

for (int l=1;l<=n;l++){
if (vrai){
  switch(choix) {
		   case 0:         Cache();
				   x+=10;
				   Affiche(0);
				   break;
		   case 1 	:  Cache();

				   x-=10;

				   Affiche(1);
				   break;

		   case 2      :   Cache();

				   y+=10;

				   Affiche(2);
				   break;

		   case 3 	:  Cache();

				   y-=10;

				   Affiche(3);
				   break;
		 }

		 delay(4);
	     }
    } }
    else {vrai=0; demo_music_laser();}
}

void Virus::Command2(Virus p,int n)
{

int choix;

 if ( (x!=p.lecX())||(y != p.lecY()) )
       {if (y != p.lecY())
	 {if (y < p.lecY()) {if (getpixel(x,y+12)!=YELLOW) choix=2;else if (getpixel(x-12,y)!=YELLOW) choix=1;}
	 else {if (getpixel(x,y-12)!=YELLOW) choix=3;else if (getpixel(x+12,y)!=YELLOW) choix=0;}}

	 else {if (x < p.lecX()) {if(getpixel(x+12,y)!=YELLOW) choix=0;else if (getpixel(x,y-12)!=YELLOW) choix=3;}
	 else {if (getpixel(x-12,y)!=YELLOW) choix=1;else if (getpixel(x,y+12)!=YELLOW) choix=2;}}

for (int l=1;l<=n;l++){

if (vrai){
  switch(choix) {
		   case 0:         Cache();
				   x+=10;
				   Affiche(0);
				   break;
		   case 1 	:  Cache();
				   x-=10;
				   Affiche(1);
				   break;

		   case 2      :   Cache();
				   y+=10;
				   Affiche(2);
				   break;

		   case 3 	:  Cache();
				   y-=10;
				   Affiche(3);
				   break;
		 }

		 delay(4);}}
	     }  else {vrai=0; demo_music_laser();}

}
//---------------------------------
void Command1(Virus p1,Virus p2,Virus p3,Virus p4)
{
int c,v;
int slep=0;
char touche;
while((vrai) && (touche!=CR) && (cpt!=70))
{

touche=getch();
if (touche==0X00)
touche=getch();

//for (int t=1;t<=1;t++){
  while(!kbhit()){

	   switch(touche) {

		   case DROIT:    p1.Cache1();
				   if (getpixel(p1.lecX()+12,p1.lecY()) !=YELLOW){
				   if (p1.lecX() != SUP)
				   p1.Modifier(10,0);
				   else if (p1.lecX()==SUP)
				   p1.Modifier(-340,0);}

				   break;

		   case GAUCH 	:  p1.Cache1();
				    if (getpixel(p1.lecX()-12,p1.lecY()) !=YELLOW) {
				   if (p1.lecX() != INF)
				   p1.Modifier(-10,0);
				   else if (p1.lecX()==INF)
				   p1.Modifier(340,0);}

				   break;

		   case BAS      : p1.Cache1();
				   if(getpixel(p1.lecX(),p1.lecY()+12)!=YELLOW){
				   if (p1.lecY() != SUP)
				   p1.Modifier(0,10);
				   else if (p1.lecY()==SUP)
				   p1.Modifier(0,-340);  }

				   break;

		   case HAUT	:  p1.Cache1();
				   if (getpixel(p1.lecX(),p1.lecY()-12)!=YELLOW)  {
				   if (p1.lecY() != INF)
				   p1.Modifier(0,-10);
				   else if (p1.lecY()==INF)
				   p1.Modifier(0,340);  }

				   break;

		    case ESC        :exit(0);

		}if (getpixel(p1.lecX(),p1.lecY())==9) p1.Compteur();
		 if (getpixel(p1.lecX(),p1.lecY())==15) slep=30;
		 if (getpixel(p1.lecX(),p1.lecY())==4) {p1.vieADD();p1.Add();}
		p1.Affiche1(touche);delay(80);
		if(slep==0){
		 p2.Command2(p1,1);p4.Command2(p1,1);p3.Command(p1,1);
		 if (cpt % 1 ==0) p1.Life();
			}  else {demo_music_ambilance();slep-=1;} }

		while (!kbhit())
		  {p2.Command2(p1,1);p4.Command(p1,1);p3.Command2(p1,1);}

	}p1.Cache1();
	p2.Cache();
	p3.Cache();
	p4.Cache();
}
void Remplir_mat(int mat[18][18])
{

for(int i=0;i<18;i++) mat[i][0]=1;

for(int i1=0;i1<18;i1++) mat[0][i1]=1;
mat[0][8]=0;
for(int i2=0;i2<18;i2++) mat[i2][17]=1;
for(int i3=0;i3<18;i3++) mat[17][i3]=1;
mat[17][8]=0;
for(int i4=5;i4<=12;i4++) mat[i4][2]=1;
for(int i5=5;i5<=12;i5++) mat[i5][15]=1;
mat[3][2]=1;mat[3][3]=1;mat[2][3]=1;mat[1][1]=1;

mat[14][2]=1;mat[14][3]=1;mat[15][3]=1;mat[16][1]=1;
mat[5][4]=1;mat[6][4]=1;mat[7][4]=1;
mat[12][4]=1;mat[10][4]=1;mat[11][4]=1;
mat[12][13]=1;mat[10][13]=1;mat[11][13]=1;
mat[5][13]=1;mat[6][13]=1;mat[7][13]=1;
mat[1][5]=1;mat[1][6]=1;mat[1][7]=1;
mat[3][5]=1;mat[3][6]=1;mat[3][7]=1;
mat[14][5]=1;mat[14][6]=1;mat[14][7]=1;
mat[16][5]=1;mat[16][6]=1;mat[16][7]=1;
mat[1][9]=1;mat[1][10]=1;mat[1][11]=1;mat[1][12]=1;
mat[3][9]=1;mat[3][10]=1;mat[3][11]=1;mat[3][12]=1;
mat[14][9]=1;mat[14][10]=1;mat[14][11]=1;mat[14][12]=1;
mat[16][9]=1;mat[16][10]=1;mat[16][11]=1;mat[16][12]=1;
mat[1][16]=1;mat[16][16]=1;mat[8][6]=1;mat[8][11]=1;mat[9][6]=1;mat[9][11]=1;
mat[2][14]=1;mat[3][14]=1;mat[3][15]=1;
mat[15][14]=1;mat[14][14]=1;mat[14][15]=1;
mat[5][6]=1;mat[6][6]=1;mat[5][7]=1;
mat[11][6]=1;mat[12][6]=1;mat[12][7]=1;
mat[5][9]=1;mat[5][10]=1;mat[5][11]=1;mat[6][11]=1;
mat[12][9]=1;mat[12][10]=1;mat[12][11]=1;mat[11][11]=1;

}
void affiche_stage(int mat[18][18])
{ setcolor(14);

setfillstyle(1,14);
   for (int i=0;i<18;i++)
      for (int j=0;j<18;j++)
	if (mat[i][j]==1) bar3d(20*i+col,20*j+lin,20*i+col+20,20*j+lin+20,0,0);

}
void point_stage(int mat[18][18])
{

for (int i=0;i<18;i++)
  for(int j=0;j<18;j++)
     {if (mat[i][j]==0)
     {setcolor(9);setfillstyle(SOLID_FILL,9);pieslice(20*i+col+10,20*j+lin+10,0,360,2);}}
   setcolor(15);
setfillstyle(1,15);
pieslice(120,120,0,360,5);
pieslice(380,120,0,360,5);
pieslice(120,380,0,360,5);
pieslice(380,380,0,360,5);
for (int u=100;u<=160;u+=30 )
{   setcolor(14);
    setfillstyle(SOLID_FILL,14);
    pieslice(520,u,20,350,10);   }

}

//-----------------------------------
void main()
{
//cleardevice();
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "../bgi");
Virus p2(160,380,10,0),p3(340,380,4,0),p4(200,360,9,0);
Virus p1(300,100,15,4);
int u=100;
int mat[18][18]={0};
Remplir_mat(mat);

affiche_stage(mat);
point_stage(mat);

while((cpt!=100)&&(p1.lecVie()!=0))
{
  Command1(p1,p2,p3,p4);
    p1.vieSUB();
    setcolor(0);
    setfillstyle(SOLID_FILL,0);
    pieslice(520,u,20,350,10);
    u+=30;
    vrai=1;
       }

getch();

}

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.