2023-11-21 12:58:14 +01:00
|
|
|
#include<stdlib.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<graph.h>
|
|
|
|
#include<time.h>
|
|
|
|
|
|
|
|
#define H 40
|
|
|
|
#define L 60
|
|
|
|
#define DELTA 1000000L
|
|
|
|
|
|
|
|
void Affiche(int tab[H][L], int posx, int posy, int i, int j){
|
|
|
|
couleur c;
|
|
|
|
if(tab[i][j]==0){
|
|
|
|
c=CouleurParNom("green");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
RemplirRectangle(posx,posy,20,20);
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:05:08 +01:00
|
|
|
if(tab[i][j]==1||tab[i][j]==6||tab[i][j]==7||tab[i][j]==8||tab[i][j]==9){
|
2023-11-21 12:58:14 +01:00
|
|
|
c=CouleurParNom("yellow");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
RemplirRectangle(posx,posy,20,20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DessinerScene(int n)
|
|
|
|
{
|
|
|
|
couleur c;
|
|
|
|
char buf[100];
|
|
|
|
c=CouleurParNom("white");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
RemplirRectangle(0,850,200,100);
|
|
|
|
c=CouleurParNom("black");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
snprintf(buf,100,"time : %d",n);
|
|
|
|
EcrireTexte(50,900,buf,2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void){
|
|
|
|
/*partie graphique*/
|
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(10,10,1700,1000);
|
|
|
|
couleur c;
|
|
|
|
c=CouleurParNom("green");
|
|
|
|
ChoisirCouleurDessin(c);
|
2023-11-27 15:05:08 +01:00
|
|
|
int tab[H][L], haut=0,gauche=0,droite=0,bas=1;
|
2023-11-21 12:58:14 +01:00
|
|
|
int i,j,posx=0,posy=0,n=0,sxmin=0,symin=1,sxmax=10,symax=1;
|
|
|
|
unsigned long suivant,suivant2;
|
|
|
|
suivant2=Microsecondes()+DELTA;
|
|
|
|
suivant=Microsecondes()+DELTA;
|
|
|
|
|
|
|
|
|
2023-11-27 15:05:08 +01:00
|
|
|
|
|
|
|
|
2023-11-21 12:58:14 +01:00
|
|
|
/*initialisation de la grille a 0(vert)*/
|
|
|
|
for(i=0;i<H;i++){
|
|
|
|
for(j=0;j<L;j++){
|
|
|
|
tab[i][j]=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*initialisation du serpent*/
|
|
|
|
tab[sxmin][symin]=0;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
for(i=0;i<10;i++){
|
|
|
|
tab[i][1]=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1){
|
2023-11-27 15:05:08 +01:00
|
|
|
|
|
|
|
/*affichage du tableau pour rendu graphique*/
|
|
|
|
for(i=0;i<H;i++){
|
|
|
|
for(j=0;j<L;j++){
|
|
|
|
Affiche(tab, posx, posy, i, j);
|
|
|
|
posx=posx+20;
|
|
|
|
}
|
|
|
|
posx=0;
|
|
|
|
posy=posy+20;
|
|
|
|
}
|
|
|
|
posx=0;
|
|
|
|
posy=0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-21 12:58:14 +01:00
|
|
|
/*timer*/
|
|
|
|
if(Microsecondes()>suivant){
|
|
|
|
n++;
|
|
|
|
DessinerScene(n);
|
|
|
|
suivant=Microsecondes()+DELTA;
|
|
|
|
}
|
2023-11-27 15:05:08 +01:00
|
|
|
if(Microsecondes()>suivant2){
|
|
|
|
if(bas==1){
|
2023-11-21 17:03:42 +01:00
|
|
|
sxmax=sxmax+1;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
tab[sxmin][symin]=0;
|
|
|
|
sxmin=sxmin+1;
|
|
|
|
}
|
2023-11-27 15:05:08 +01:00
|
|
|
if(droite==1){
|
|
|
|
symax=symax+1;
|
|
|
|
tab[sxmin][symin]=0;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
sxmin=sxmin+1;
|
2023-11-21 12:58:14 +01:00
|
|
|
}
|
2023-11-27 15:05:08 +01:00
|
|
|
if(gauche==1){
|
|
|
|
symax=symax-1;
|
|
|
|
tab[sxmin][symin]=0;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
sxmin=sxmin-1;
|
|
|
|
}
|
|
|
|
if(haut==1){
|
|
|
|
sxmax=sxmax+1;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
tab[sxmin][symin]=0;
|
|
|
|
sxmin=sxmin+1;
|
|
|
|
}
|
|
|
|
suivant2=Microsecondes()+DELTA;
|
2023-11-21 12:58:14 +01:00
|
|
|
}
|
2023-11-27 15:05:08 +01:00
|
|
|
/*deplacements du serpent*/
|
|
|
|
if(ToucheEnAttente()){
|
|
|
|
if(Touche()==XK_Right){
|
|
|
|
printf("droite");
|
|
|
|
droite=1;
|
|
|
|
bas=0;
|
|
|
|
haut=0;
|
|
|
|
gauche=0;
|
|
|
|
}
|
|
|
|
if(Touche()==XK_Down){
|
|
|
|
printf("bas");
|
|
|
|
bas=1;
|
|
|
|
droite=0;
|
|
|
|
gauche=0;
|
|
|
|
haut=0;
|
|
|
|
}
|
|
|
|
if(Touche()==XK_Left){
|
|
|
|
printf("gauche");
|
|
|
|
bas=1;
|
|
|
|
droite=0;
|
|
|
|
gauche=0;
|
|
|
|
haut=0;
|
|
|
|
}
|
|
|
|
if(Touche()==XK_Up){
|
|
|
|
printf("haut");
|
|
|
|
bas=1;
|
|
|
|
droite=0;
|
|
|
|
gauche=0;
|
|
|
|
haut=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 12:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-11-21 17:03:42 +01:00
|
|
|
|