progprinc.c~ 7.84 KB
#include <stdio.h>
#include <stdlib.h>
#include <libFile.h>
#include <libgraph.h>
#include <unistd.h>
#include <time.h>
#include <SDL/SDL.h>
#define MAX_ELEMENTS 50

typedef struct {
  char pseudo[MAX_ELEMENTS];
  char score;
}scorejoueur;

typedef struct {
  scorejoueur classement[10];
}classement;

typedef struct {
  int x;
  int y;
}position;

typedef struct {
  position contenu[MAX_ELEMENTS];
  int nombre;
  } liste_poly;
  
    
void creationgrille(){
int i;
for(i = 0 ; i < 20; i++)
	    {
	    rectanglePlein(i*50,0,1,1000,5);			//pose de la grille
	    rectanglePlein(0,i*50,1000,1,5);
	    } }
	   

void affichage(element_t e)
{ 
  position *p = e;
  printf("%d %d \n", p->x, p->y);
}


void creerchampignons(int n,liste_poly *muschrooms,int champi){
      position poschampi;
      int i,w;
      int test=0;
      for(w=0;w<n;w++) 
	    {
	    poschampi.x = (rand()%20)*50;			//fonction de pose des champignons
	    poschampi.y = (rand()%20)*50;
	    for(i=0; i<w; i++){
		if(muschrooms->contenu[i].x == poschampi.x && muschrooms->contenu[i].y == poschampi.y ) test=1;
	    }
	    if((poschampi.y > 570 || poschampi.y < 430) && test==0) 
		{
		muschrooms->contenu[muschrooms->nombre++]=poschampi;
		afficherLutin(champi,(muschrooms->contenu[w].x)+1,(muschrooms->contenu[w].y)+6);
		}
	    else w--;
	    test = 0;
	    }
}


void creationcenti(Liste *liste,int Teted, int Corpsd) {
      int x,y=500;
      for(x = 300 ; x <= 450 ; x=x+50) {     // fonction pour la creation centipede liste chainée
      insertion(liste, x, y);
      afficherLutin(Corpsd,x,y); }
      x = 500; 
      afficherLutin(Teted,x,y);
      insertion(liste, x, y);
}


int main() {
  SDL_keysym* detail;
  srand(time(NULL));
  char touche;
  unsigned char bas;
  int largeur = 1000;
  int hauteur = 1000;
  char *titre = "Le Centipede";
  position *pos;
  position posmillepatte;
  scorejoueur scorefait={.pseudo={'\0'}};
  scorejoueur autres;
  scorejoueur lecture;
  classement ranking;
  
  int champi = chargerLutin("Lutins/millepatte_champi.bmp",0);
  int Teted = chargerLutin("Lutins/millepatte_tete_droite.bmp",0);
  int Teteg = chargerLutin("Lutins/millepatte_tete_gauche.bmp",0);
  int Teteh = chargerLutin("Lutins/millepatte_tete_haut.bmp",0);
  int Teteb = chargerLutin("Lutins/millepatte_tete_bas.bmp",0);
  int Corpsg = chargerLutin("Lutins/millepatte_corps_gauche.bmp",0);
  int Corpsd = chargerLutin("Lutins/millepatte_corps_droite.bmp",0);
  int Corpsh = chargerLutin("Lutins/millepatte_corps_haut.bmp",0);
  int Corpsb = chargerLutin("Lutins/millepatte_corps_bas.bmp",0);
  int gameover = chargerLutin("Lutins/game_over.bmp",0);
  int level1 = chargerLutin("Lutins/level1.bmp",0);
  int level2 = chargerLutin("Lutins/level2.bmp",0);
  int level3 = chargerLutin("Lutins/level3.bmp",0);
  int levelclr = chargerLutin("Lutins/level_cleared.bmp",0);
  int youwin = chargerLutin("Lutins/youwin.bmp",0);
  int Tete;
  int Corps;
  int i;
  int abs, ord, obstacle=0, couleur,level=1,speed=5000,total=0,nombrechampis=6, s,nvx,nvy,abscenti,ordcenti,absqueue,ordqueue,ecriture=1,j,w;
   
  if(champi<0 || Teted<0 || Teteg<0 || Teteh <0 || Teteb<0 || Corpsg<0 || Corpsd<0 || Corpsh<0 || Corpsb<0 || gameover<0 || level1<0 || level2<0 || level3<0 || levelclr<0 || youwin<0){ printf("Image pas là\n"); exit(-1); }
  
  creerSurface(largeur,hauteur,titre);
  majSurface();
  int gomme = creerLutin(0,0,50,50,-1);
 
while(level<=3) {  
  
  liste_poly muschrooms={.nombre=0};
  Liste *maListe = initialisation();
  int w,t,compteur=0,allonge;
  
  rectanglePlein(0,0,1000,1000,1);
  rectanglePlein(0,0,1000,1000,1);
  
  
  if(level==1) { afficherLutin(level1,250,250); majSurface(); sleep(1); }
  if(level==2) { afficherLutin(level2,250,250); majSurface(); sleep(1); }
  if(level==3) { afficherLutin(level3,250,250); majSurface(); sleep(1); }
  
  rectanglePlein(0,0,1000,1000,1);
  rectanglePlein(0,0,1000,1000,1);
  
  creationcenti(maListe,Teted,Corpsd);
  
  creerchampignons(nombrechampis,&muschrooms,champi);
	    
  majSurface();
  
  attendreEvenement();
  
  s = 0;
  t = 0;
  
  while(1){
    
	for(i=0;i<speed;i++)
	{
	  if(lireTouche(&bas,&touche,(void **)&detail) && bas==1) 
	      {
	      if(detail->sym==SDLK_RIGHT) t=0;
	      if(detail->sym==SDLK_UP) t=1;
	      if(detail->sym==SDLK_LEFT) t=2;
	      if(detail->sym==SDLK_DOWN) t=3;	
	      }
 	
	  usleep(1);
	}
	  if((t==0 && s!=2) || s==0) { abs=50; ord=0; Tete = Teted; Corps = Corpsd; s=0; }
	  if((t==1 && s!=3) || s==1) { abs=0; ord=(-50); Tete = Teteh; Corps = Corpsh; s=1; }
	  if((t==2 && s!=0) || s==2) { abs=-50; ord=0; Tete = Teteg; Corps = Corpsg; s=2; }
	  if((t==3 && s!=1) || s==3) { abs=0; ord=50; Tete = Teteb; Corps = Corpsb; s=3; }  
	  
	      abscenti = abstete(maListe);
	      ordcenti = ordtete(maListe);
	      nvx = abscenti+abs;
	      nvy = ordcenti+ord;
	      if(nvx==1000 || nvy==1000 || nvx==-50 || nvy==-50)
		  {
		  afficherLutin(gameover,250,433);
		  majSurface();
		  sleep(5);
		  break;
		  }

	      majSurface();
	      couleur = couleurPixel(nvx+25,nvy+25);
	      if(couleur!=1) obstacle = 1;
	      else obstacle = 0;
	      allonge = 0;
	      
	      
	      if(obstacle==1){
		 for(w=0;w<nombrechampis;w++)
		    {
			if((muschrooms.contenu[w].x) == nvx && (muschrooms.contenu[w].y) == nvy) { allonge = 1; } }
			if(allonge==1){
			afficherLutin(gomme,nvx,nvy);  
			afficherLutin(gomme,abscenti,ordcenti);
			afficherLutin(Corps,abscenti,ordcenti);
			insertion(maListe,nvx,nvy);
			afficherLutin(Tete,nvx,nvy);
			compteur++;
			}
			else {
			afficherLutin(gameover,250,433);
			majSurface();
			sleep(5);
			break;
			}		
	
		
	      }
	      
	      if(obstacle==0){
		afficherLutin(gomme,abscenti,ordcenti);
		afficherLutin(Corps,abscenti,ordcenti);
		absqueue = abscul(maListe);
		ordqueue = ordcul(maListe);
		freelastbloc(maListe);
		insertion(maListe,nvx,nvy);
		afficherLutin(Tete,nvx,nvy);
	        afficherLutin(gomme,absqueue,ordqueue);
	      }
	      
	      majSurface();
	      
	      if(compteur==nombrechampis) break;
    
	      if(touche=='a')
		  {
		  fermerSurface();
		  return 0;    
		  }
	}
	
	
	total = total+compteur;
	if(compteur != nombrechampis) break;
	if(level==3) { afficherLutin(youwin,204,400); majSurface(); sleep(3); }
	else { afficherLutin(levelclr,304,454); majSurface(); sleep(3); }
	compteur = 0;
	nombrechampis = nombrechampis+3;
	speed = speed/2;
	level++;
	freeListe(maListe);
}
  printf("votre score est de %d\n",total);

   for(i=0;i<10;i++) // initialisation de la liste classement
    {
      strcpy(ranking.classement[i].pseudo,"vide");
      ranking.classement[i].score=0;
    }

      fermerSurface();
      printf("Saisissez votre pseudo\n");
      scanf("%s",scorefait.pseudo);
      scorefait.score=total;


    FILE *fichier=fopen("scores.txt","a+");  // Gestion fichier
    fclose(fichier);
    fichier=fopen("scores.txt","r+");
    if(fichier!=NULL){

      while((fread(&autres,sizeof(autres),1,fichier)!=0))
      {
	if(strcmp(autres.pseudo,scorefait.pseudo)==0)
	{
	  if(total>autres.score)
	  {
	    fseek(fichier,-sizeof(scorefait),SEEK_CUR);
	    fwrite(&scorefait,sizeof(scorefait),1,fichier);
	  }
	  ecriture=0;
	  break;
	}
	ecriture=1;
      }
      if(ecriture==1) {fwrite(&scorefait,sizeof(scorefait),1,fichier);}
    }
    rewind(fichier);
    while((fread(&lecture,sizeof(lecture),1,fichier)!=0))
    {
      for(w=0;w<10;w++)
      {
	if(lecture.score>ranking.classement[w].score)
	{
	  for(j=10;j>w;j--)
	  {
	    strcpy(ranking.classement[j].pseudo,ranking.classement[j-1].pseudo);         // gestion et affichage classement
	    ranking.classement[j].score=ranking.classement[j-1].score;
	  }
	  strcpy(ranking.classement[w].pseudo,lecture.pseudo);
	  ranking.classement[w].score=lecture.score;
	  break;
	}
      }
    }
    printf("RANKING :\n");
    for(w=0;w<10;w++) 
    {
      printf("%i : %s , score : %d\n",w+1,ranking.classement[w].pseudo,ranking.classement[w].score);
    }
    fclose(fichier);

  return 0;
}