2023-11-26 13:05:24 +01:00
|
|
|
#include<stdlib.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<graph.h>
|
|
|
|
#include<time.h>
|
|
|
|
|
|
|
|
#define HAUTEUR 40
|
|
|
|
#define LARGEUR 60
|
|
|
|
|
|
|
|
#define CYCLE 10000L
|
|
|
|
|
2023-11-26 18:10:32 +01:00
|
|
|
#define SEGMENT 25
|
2023-11-26 13:05:24 +01:00
|
|
|
|
2023-11-26 18:10:32 +01:00
|
|
|
enum Direction { UP=2, DOWN=3, LEFT=0, RIGHT=1 };
|
2023-11-26 13:05:24 +01:00
|
|
|
|
2023-11-26 18:10:32 +01:00
|
|
|
void graphique(){
|
2023-11-26 13:05:24 +01:00
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(1700,950,1700,950);
|
|
|
|
couleur c, b;
|
|
|
|
b=CouleurParComposante(0,0,0);
|
|
|
|
ChoisirCouleurDessin(b);
|
|
|
|
RemplirRectangle(0,0,1750,950);
|
|
|
|
c=CouleurParComposante(111,255,94);
|
|
|
|
ChoisirCouleurDessin(c);
|
2023-11-26 18:10:32 +01:00
|
|
|
RemplirRectangle(100,100,LARGEUR * SEGMENT,HAUTEUR * SEGMENT);
|
2023-11-26 13:05:24 +01:00
|
|
|
FermerGraphique();
|
|
|
|
};
|
|
|
|
|
|
|
|
void serpent(int tab[][LARGEUR],int taille) {
|
|
|
|
int posx_s,posy_s,i;
|
|
|
|
for(i=0;i<taille;i++){
|
|
|
|
posx_s = tab[i][0] * SEGMENT;
|
|
|
|
posy_s = tab[i][1] * SEGMENT;
|
|
|
|
couleur s = CouleurParComposante(255,255,0);
|
|
|
|
ChoisirCouleurDessin(s);
|
2023-11-26 18:10:32 +01:00
|
|
|
RemplirRectangle(posx_s, posy_s,SEGMENT, SEGMENT);
|
2023-11-26 13:05:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-26 18:10:32 +01:00
|
|
|
void mouvement(int tab[][LARGEUR],int *taille,int *dir){
|
2023-11-26 13:05:24 +01:00
|
|
|
for (int i = *taille - 1; i>0;i--){
|
|
|
|
tab[i][0] = tab[i - 1][0];
|
|
|
|
tab[i][1] = tab[i - 1][1];
|
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
if(ToucheEnAttente()){
|
|
|
|
if (Touche() ==XK_Left && *dir !=RIGHT){
|
|
|
|
*dir= LEFT;
|
2023-11-26 13:05:24 +01:00
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
else if(Touche()==XK_Right && *dir != LEFT){
|
|
|
|
*dir= RIGHT;
|
2023-11-26 13:05:24 +01:00
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
else if(Touche()==XK_Up && *dir != DOWN){
|
|
|
|
*dir= UP;
|
2023-11-26 13:05:24 +01:00
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
else if(Touche()==XK_Down && *dir != UP){
|
|
|
|
*dir= DOWN;
|
2023-11-26 13:05:24 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
if (*dir== LEFT){
|
|
|
|
tab[0][0] -=1;
|
|
|
|
}
|
|
|
|
else if(*dir== RIGHT){
|
|
|
|
tab[0][0] +=1;
|
|
|
|
}
|
|
|
|
else if(*dir== UP){
|
|
|
|
tab[0][1] -=1;
|
|
|
|
}
|
|
|
|
else if(*dir== DOWN){
|
|
|
|
tab[0][1] +=1;
|
|
|
|
}
|
|
|
|
}
|
2023-11-26 13:05:24 +01:00
|
|
|
int main() {
|
2023-11-26 18:10:32 +01:00
|
|
|
int dir= RIGHT;
|
|
|
|
int tab[HAUTEUR * LARGEUR][2];
|
2023-11-26 13:05:24 +01:00
|
|
|
unsigned long suivant;
|
2023-11-26 18:10:32 +01:00
|
|
|
int g=0;
|
2023-11-26 13:05:24 +01:00
|
|
|
suivant=Microsecondes()+CYCLE;
|
|
|
|
int taille =3;
|
2023-11-26 18:10:32 +01:00
|
|
|
|
2023-11-26 13:05:24 +01:00
|
|
|
for(int i = 0;i < taille; i++){
|
|
|
|
tab[i][0] = LARGEUR / 2;
|
|
|
|
tab[i][1] = HAUTEUR / 2+1;
|
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
graphique();
|
|
|
|
|
2023-11-26 13:05:24 +01:00
|
|
|
while(1){
|
|
|
|
if (Microsecondes()>suivant){
|
|
|
|
g++;
|
2023-11-26 18:10:32 +01:00
|
|
|
graphique();
|
2023-11-26 13:05:24 +01:00
|
|
|
suivant=Microsecondes()+CYCLE;
|
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
mouvement(tab,&taille,&dir);
|
|
|
|
|
|
|
|
for(int i = 0; i < taille; i++){
|
|
|
|
if (tab[i][0] < 0) tab [i][0] = LARGEUR - 1;
|
|
|
|
if (tab[i][0] >= LARGEUR) tab [i][0] = 0;
|
|
|
|
if (tab[i][1] < 0) tab [i][1] = HAUTEUR - 1;
|
|
|
|
if (tab[i][1] >= HAUTEUR) tab [i][1] = 0;
|
2023-11-26 13:05:24 +01:00
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
serpent(tab,taille);
|
|
|
|
}
|
2023-11-26 13:05:24 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
}
|
2023-11-26 18:10:32 +01:00
|
|
|
|