2023-11-29 17:29:25 +01:00
|
|
|
#include<stdlib.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<graph.h>
|
|
|
|
#include<time.h>
|
|
|
|
#include<unistd.h>
|
|
|
|
|
|
|
|
#define H 40
|
|
|
|
#define L 60
|
|
|
|
#define DELTA 1000000L
|
|
|
|
#define DELTO 1000000L
|
|
|
|
|
|
|
|
/*recuperation des couleurs en fonction des chiffres dans le tableau*/
|
|
|
|
void AfficheTab(int tab[H][L], int posx, int posy, int i, int j){
|
|
|
|
couleur c;
|
|
|
|
if(tab[i][j]==0){
|
|
|
|
c=CouleurParNom("black");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
RemplirRectangle(posx,posy,20,20);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
6>droite
|
|
|
|
7>gauche
|
|
|
|
8>haut
|
|
|
|
8>bas
|
|
|
|
*/
|
|
|
|
if(tab[i][j]==1||tab[i][j]==6||tab[i][j]==7||tab[i][j]==8||tab[i][j]==9){
|
|
|
|
c=CouleurParNom("dark orange");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
RemplirRectangle(posx,posy,20,20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*affichage du tableau pour rendu graphique*/
|
|
|
|
void Affiche(int tab[H][L]){
|
|
|
|
int i,j,posx=0,posy=0;
|
|
|
|
for(i=0;i<H;i++){
|
|
|
|
for(j=0;j<L;j++){
|
|
|
|
AfficheTab(tab, posx, posy, i, j);
|
|
|
|
posx=posx+20;
|
|
|
|
}
|
|
|
|
posx=0;
|
|
|
|
posy=posy+20;
|
|
|
|
}
|
|
|
|
posx=0;
|
|
|
|
posy=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*recuperation de la direction en fonction des touches*/
|
|
|
|
int CliqueTouche(int tab[H][L],int direction){
|
|
|
|
switch(Touche()){
|
|
|
|
case XK_Up:
|
|
|
|
if(direction!=0){
|
|
|
|
printf("aaaaaaa");
|
|
|
|
direction=2;
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
case XK_Down:
|
|
|
|
if(direction!=2){
|
|
|
|
printf("zzzzzzz");
|
|
|
|
direction=0;
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
case XK_Right:
|
|
|
|
if(direction!=1){
|
|
|
|
printf("eeeeeeeee");
|
|
|
|
direction=3;
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
case XK_Left:
|
|
|
|
if(direction!=3){
|
|
|
|
printf("rrrrrrrr");
|
|
|
|
direction=1;
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-07 12:34:24 +01:00
|
|
|
int DepXTete(int* sxmax,int* symax,int direction,int tab[H][L]){
|
2023-11-29 17:29:25 +01:00
|
|
|
/*haut*/
|
|
|
|
if(direction==2){
|
2023-12-07 12:34:24 +01:00
|
|
|
*sxmax=*sxmax-1;
|
|
|
|
tab[*sxmax][*symax]=1;
|
|
|
|
return *sxmax;
|
2023-11-29 17:29:25 +01:00
|
|
|
}
|
|
|
|
/*bas*/
|
|
|
|
if(direction==0){
|
2023-12-07 12:34:24 +01:00
|
|
|
*sxmax=*sxmax+1;
|
|
|
|
tab[*sxmax][*symax]=1;
|
|
|
|
return *sxmax;
|
2023-11-29 17:29:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-07 12:34:24 +01:00
|
|
|
int DepYTete(int* sxmax,int* symax,int* direction,int tab[H][L]){
|
2023-11-29 17:29:25 +01:00
|
|
|
/*droite*/
|
|
|
|
if(direction==3){
|
|
|
|
symax=symax+1;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
return symax;
|
|
|
|
}
|
|
|
|
/*gauche*/
|
|
|
|
if(direction==1){
|
|
|
|
symax=symax-1;
|
|
|
|
tab[sxmax][symax]=1;
|
|
|
|
return symax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*mvmt du serpent*/
|
2023-11-30 15:12:15 +01:00
|
|
|
void Serpent(int tab[H][L], int* psxmax, int* psxmin, int* psymax, int* psymin, int* pdirection){
|
2023-11-29 17:29:25 +01:00
|
|
|
unsigned long suivant2=Microsecondes()+DELTO;
|
|
|
|
|
2023-11-30 15:12:15 +01:00
|
|
|
*pdirection = CliqueTouche(tab,pdirection);
|
2023-11-29 17:29:25 +01:00
|
|
|
|
|
|
|
/*deplacement tete*/
|
|
|
|
if(Microsecondes()>=suivant2){
|
2023-12-07 12:34:24 +01:00
|
|
|
if(*pdirection==0||*pdirection==2){
|
|
|
|
*psxmax=DepXTete(psxmax,psymax,pdirection,tab);
|
2023-11-29 17:29:25 +01:00
|
|
|
}
|
2023-12-07 12:34:24 +01:00
|
|
|
else if(*pdirection==1||*pdirection==3){
|
|
|
|
*psymax=DepYTete(psxmax,psymax,pdirection,tab);
|
2023-11-29 17:29:25 +01:00
|
|
|
}
|
|
|
|
suivant2=Microsecondes()+DELTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*affichage du timer*/
|
|
|
|
void DessinerTimer(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*initialisation de la grille et de la fenetre*/
|
|
|
|
void init(int tab[H][L]){
|
|
|
|
/*creation page*/
|
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(10,10,1700,1000);
|
|
|
|
couleur c;
|
|
|
|
c=CouleurParNom("green");
|
|
|
|
ChoisirCouleurDessin(c);
|
|
|
|
int i,j;
|
|
|
|
/*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[0][1]=0;
|
|
|
|
tab[10][1]=1;
|
|
|
|
for(i=0;i<10;i++){
|
|
|
|
tab[i][1]=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void){
|
|
|
|
int tab[H][L];
|
|
|
|
int n=0,fin=0;
|
|
|
|
int sxmax=10,sxmin=1,symin=1,symax=1,direction=0;
|
|
|
|
int* psxmax = &sxmax, psxmin = &sxmin, psymin = &symin, psymax = &symax, pdirection = &direction;
|
|
|
|
unsigned long suivant;
|
|
|
|
suivant=Microsecondes()+DELTA;
|
|
|
|
|
|
|
|
init(tab);
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
Affiche(tab);
|
2023-11-30 15:12:15 +01:00
|
|
|
Serpent(tab, *psxmax, *psxmin, *psymax,*psymin,*pdirection);
|
2023-11-29 17:29:25 +01:00
|
|
|
|
|
|
|
/*timer*/
|
|
|
|
if(Microsecondes()>suivant){
|
|
|
|
n++;
|
|
|
|
DessinerTimer(n);
|
|
|
|
suivant=Microsecondes()+DELTA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fin==1){
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-07 12:34:24 +01:00
|
|
|
|