#include #include #include #include #define CYCLE 10000L int seconde=0; int minute=0; int seconde_actuel=0; int old_seconde=0; unsigned long int suivant; char timer[6]; void DessinerScene(int murx[30], int mury[30]){ int mur; int i; int fond; snprintf(timer,6,"%02d:%02d", minute ,seconde); ChoisirCouleurDessin(CouleurParComposante(91,222,122)); RemplirRectangle(20,20,1160,700); ChoisirCouleurDessin(CouleurParComposante(255,255,255)); fond = ChargerSprite("../img/fond.png"); mur = ChargerSprite("../img/mur.png"); srand(time(NULL)); for(i=0; i<30; i++){ murx[i] = ((rand() % (55)+1)*20); mury[i] = ((rand() % (35)+1)*20); AfficherSprite(mur, murx[i], mury[i]); } } void Score(int segment){ int nombre; char score[4]; nombre= (segment-10)*10; snprintf(score,4,"%04d0", nombre); ChoisirCouleurDessin(CouleurParNom("black")); RemplirRectangle(1100,700,1200,800); ChoisirCouleurDessin(CouleurParNom("white")); EcrireTexte(1000,760,"Score: ",2); EcrireTexte(1100,760,score,2); } void Update_Timer(){ snprintf(timer,6,"%02d:%02d", minute, seconde); ChoisirCouleurDessin(CouleurParComposante(0,0,0)); RemplirRectangle(10,700,12000,800); ChoisirCouleurDessin(CouleurParComposante(255,255,255)); EcrireTexte(50,760,"time: ",2); EcrireTexte(120,760,timer,2); } void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){ int i = 0; int serpent; AfficherSprite(serpent, pos_x[0], pos_y[0]); for (i=1 ; i1160 || pos_x[0]<=20){ *go_on=0; } /*Serpent contre coté*/ if (pos_y[0]<40 || pos_y[0] >=700){ *go_on=0; } /*Serpent contre Serpent*/ for (i = 1; i < segment; i++) { if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]) *go_on = 0; } /*Serpent contre mur*/ for(i=0; i<30;i++){ if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){ *go_on=0; } } } void Timer(){ if(Microsecondes()> suivant){ suivant = Microsecondes()+CYCLE; seconde_actuel = (suivant/1000000)%10; if(seconde_actuel !=old_seconde){ old_seconde = seconde_actuel; if(seconde == 59){ minute=minute+1; seconde=0; Update_Timer(); }else;{ seconde = seconde+1; Update_Timer(); } } } } void Controle(int *direction, int last_direction, int *go_on) { int t; while(ToucheEnAttente()) { t = Touche(); switch(t) { case XK_Left: if (last_direction != 4) { *direction = 3; } return; case XK_Right: if (last_direction != 3) { *direction = 4; } return; case XK_Up: if (last_direction != 2) { *direction = 1; } return; case XK_Down: if (last_direction != 1) { *direction = 2; } return; case XK_Escape: *direction = 0; *go_on = 0; return; case XK_p: *direction = 0; } } } void Serpent(int pos_x[], int pos_y[], int old_x[], int old_y[], int *segment, int murx[], int mury[], int *go_on, int *direction) { int serpent; int x = 600; int y = 400; int i = 0; serpent=ChargerSprite("../img/serpent.png"); for (i = 0; i < *segment; i++){ AfficherSprite(serpent, x-(i*20), y); pos_x[i]=x-(i*20); pos_y[i]=y; old_x[i]=pos_x[i]; old_y[i]=pos_y[i]; } if (*direction == 1 && *direction != 2 ){ pos_y[0]=old_y[0]-20; } if (*direction == 2 && *direction != 1) { pos_y[0]=old_y[0]+20; } if (*direction == 3 & *direction != 4) { pos_x[0]=old_x[0]-20; } if (*direction == 4 && *direction != 3) { pos_x[0]=old_x[0]+20; } Update_Serpent(pos_x, pos_y, *segment, old_x, old_y); Collision_Serpent(pos_x, pos_y, *segment, murx, mury, go_on); usleep(100000); } void InitialiserPommes(int pommex[], int pommey[], int segment) { int p; int pomme; pomme = ChargerSprite("../img/pomme.png"); for (p = 0; p < 5; p++) { pommex[p] = ((rand() % (55) + 1) * 20); pommey[p] = ((rand() % (35) + 1) * 20); AfficherSprite(pomme, pommex[p], pommey[p]); } } void Pomme(int pos_x[], int pos_y[], int pommex[], int pommey[], int *segment){ int p; int pomme; pomme=ChargerSprite("../img/pomme.png"); for (p = 0; p < 5; p++) { AfficherSprite(pomme, pommex[p],pommey[p]); } for(p=0; p<5; p++){ if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){ (*segment) +=2; pommex[p] = ((rand() % (60)+1)*20); pommey[p] = ((rand() % (27)+1)*20); } } } int main(void){ int segment = 10; int go_on = 1; int direction = 4; int pos_x[60]; int pos_y[60]; int old_x[60]; int old_y[60]; int pommex[5]; int pommey[5]; int murx[30]; int mury[30]; int *pointeur_segment = &segment; int *pointeur_go_on = &go_on; int *pointeur_direction = &direction; InitialiserGraphique(); CreerFenetre(350,100,1200,900); EffacerEcran(CouleurParComposante(0,0,0)); suivant = Microsecondes()+CYCLE; old_seconde=(suivant/1000000)%10; DessinerScene(murx, mury); InitialiserPommes(pommex, pommey, segment); while(go_on){ Timer(); Score(*pointeur_segment); Controle(pointeur_direction, 0, pointeur_go_on); Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, pointeur_go_on, pointeur_direction); Pomme(pos_x, pos_y, pommex, pommey, pointeur_segment); } FermerGraphique(); return EXIT_SUCCESS; }