Intégration des mouvements du serpent ( à améliorer )

This commit is contained in:
Vincent TEISSIER 2023-11-27 16:17:11 +01:00
parent bfa12491ac
commit 766b98d8dd
3 changed files with 90 additions and 72 deletions

63
main.c

@ -13,12 +13,16 @@
#define DECALEMENT 30 #define DECALEMENT 30
#define delta 1000000L #define delta 100000L
void Affichertimer(int n) /*Afficher le temps passé*/ void Affichertimer(int n) /*Afficher le temps passé*/
{ {
char buf[100]; char buf[100];
couleur j;
j=CouleurParNom("yellow");
ChoisirCouleurDessin(j);
ChoisirEcran(1); ChoisirEcran(1);
CopierZone(2,1,0,0,930,710,0,0); CopierZone(2,1,0,0,930,710,0,0);
snprintf(buf,100,"temps : %05d",n); snprintf(buf,100,"temps : %05d",n);
@ -28,30 +32,57 @@ void Affichertimer(int n) /*Afficher le temps passé*/
CopierZone(1,0,0,0,930,710,0,0); CopierZone(1,0,0,0,930,710,0,0);
} }
void DeplacementSerpent(int direction ,int *serpent, int longueur) void DeplacementSerpent(int direction ,int *serpent, int longueur)
{ {
int i = 0; int tempX = serpent[0];
int tempY = serpent[1];
int i;
couleur g;
couleur j;
ChoisirEcran(2);
j=CouleurParNom("yellow");
g=CouleurParNom("lightgreen");
ChoisirCouleurDessin(g);
RemplirRectangle(serpent[longueur-2],serpent[longueur-1],T_PIXEL,T_PIXEL);
for (i = 2; i < longueur; i += 2) {
int tempX2 = serpent[i];
int tempY2 = serpent[i + 1];
serpent[i] = tempX;
serpent[i + 1] = tempY;
tempX = tempX2;
tempY = tempY2;
}
ChoisirCouleurDessin(j);
if(direction == 0) /* Direction vers la gauche */ if(direction == 0) /* Direction vers la gauche */
{ {
printf("Gauche %d\n",direction); printf("Gauche %d\n",direction);
serpent[0]-=T_PIXEL;
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
printf("Longueur = %d | x %d | y %d",longueur,serpent[longueur-1],serpent[longueur-2]);
} }
else if(direction == 1) /* Direction vers le haut */ else if(direction == 1) /* Direction vers le haut */
{ {
printf("Haut %d\n",direction); printf("Haut %d\n",direction);
serpent[1]-=T_PIXEL;
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
} }
else if(direction == 2) /* Direction vers la droite */ else if(direction == 2) /* Direction vers la droite */
{ {
printf("Droite %d\n",direction); printf("Droite %d\n",direction);
serpent[0]+=T_PIXEL;
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
} }
else if(direction == 3) /* Direction vers le bas */ else if(direction == 3) /* Direction vers le bas */
{ {
printf("Bas %d\n",direction); printf("Bas %d\n",direction);
serpent[1]+=T_PIXEL;
RemplirRectangle(serpent[0],serpent[1],T_PIXEL,T_PIXEL);
} }
} }
@ -107,7 +138,7 @@ void gen_pastille(int nb_pastille, int *p_pastilles) /* Ajout des pointeurs pour
p_pastilles[i+1] = y_pastille ; p_pastilles[i+1] = y_pastille ;
printf("x : %d ; y : %d\n",p_pastilles[i],p_pastilles[i+1]); printf("x : %d ; y : %d\n",p_pastilles[i],p_pastilles[i+1]);
ChoisirEcran(2);
ChoisirCouleurDessin(r); ChoisirCouleurDessin(r);
RemplirRectangle(x_pastille,y_pastille,T_PIXEL,T_PIXEL); RemplirRectangle(x_pastille,y_pastille,T_PIXEL,T_PIXEL);
} }
@ -128,13 +159,13 @@ void DessinerScene(int* pastilles) /* Dessine la scène */
void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */ void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
{ {
int x_millieu = 0, y_millieu = 0 , compteur = 0; int x_millieu = 0, y_millieu = 0 , compteur = 0;
couleur o;
couleur j; couleur j;
o=CouleurParNom("Gold"); int i = 0;
j=CouleurParNom("yellow"); j=CouleurParNom("yellow");
ChoisirCouleurDessin(o); ChoisirCouleurDessin(j);
x_millieu = T_PIXEL*30; /* 30 = 60 colonnes divisé par 2*/ x_millieu = T_PIXEL*30; /* 30 = 60 colonnes divisé par 2*/
y_millieu = T_PIXEL*20; /* 20 = 40 colonnes divisé par 2*/ y_millieu = T_PIXEL*20; /* 20 = 40 colonnes divisé par 2*/
ChoisirEcran(2);
RemplirRectangle(x_millieu,y_millieu,T_PIXEL,T_PIXEL); RemplirRectangle(x_millieu,y_millieu,T_PIXEL,T_PIXEL);
/* printf("x_millieu : %d | y_millieu : %d\n",x_millieu,y_millieu); DEBUG*/ /* printf("x_millieu : %d | y_millieu : %d\n",x_millieu,y_millieu); DEBUG*/
@ -143,7 +174,6 @@ void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
p_serpent[1] = y_millieu; p_serpent[1] = y_millieu;
for(compteur=0;compteur<=18;compteur+=2) /* Commence par 1 car p_serpent index 0 initialisées pour la tête et 2 + 2x9 = 10 couples de coordonnées 2D */ for(compteur=0;compteur<=18;compteur+=2) /* Commence par 1 car p_serpent index 0 initialisées pour la tête et 2 + 2x9 = 10 couples de coordonnées 2D */
{ {
ChoisirCouleurDessin(j);
p_serpent[compteur+2] = p_serpent[compteur]+T_PIXEL; p_serpent[compteur+2] = p_serpent[compteur]+T_PIXEL;
p_serpent[compteur+3] = p_serpent[compteur+1]; p_serpent[compteur+3] = p_serpent[compteur+1];
@ -151,6 +181,11 @@ void InitialisationDuSerpent(int *p_serpent) /* L'initialisation du serpent */
RemplirRectangle(p_serpent[compteur+2],p_serpent[compteur+3],T_PIXEL,T_PIXEL); RemplirRectangle(p_serpent[compteur+2],p_serpent[compteur+3],T_PIXEL,T_PIXEL);
} }
printf("Avant \n");
for (i = 0; i < 20; i += 2) {
printf("(%d, %d) ", p_serpent[i], p_serpent[i + 1]);
}
printf("\n");
} }
@ -190,19 +225,15 @@ int main()
{ {
case XK_Up: case XK_Up:
direction = 1; direction = 1;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break; break;
case XK_Down: case XK_Down:
direction = 3; direction = 3;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break; break;
case XK_Left: case XK_Left:
direction = 0; direction = 0;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break; break;
case XK_Right: case XK_Right:
direction = 2; direction = 2;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break; break;
default: default:
@ -216,7 +247,7 @@ int main()
{ {
n++; n++;
Affichertimer(n); Affichertimer(n);
suivant=Microsecondes()+delta; suivant=Microsecondes()+delta;
DeplacementSerpent(direction,serpent,longueur_serpent); DeplacementSerpent(direction,serpent,longueur_serpent);
} }

BIN
prog

Binary file not shown.

99
test.c

@ -1,58 +1,45 @@
#include<stdio.h> #include <stdio.h>
#include<stdlib.h>
#include<graph.h> #define TAILLE 20
#define delta 1000000L
/* Fonction pour afficher un tableau de coordonnées */
void DessinerScene(int sprite,int x,int y,int n) void afficherSerpent(int serpent[], int taille) {
{ int i = 0;
char buf[100]; for (i = 0; i < taille; i += 2) {
ChoisirEcran(1); printf("(%d, %d) ", serpent[i], serpent[i + 1]);
CopierZone(2,1,0,0,800,500,0,0); }
snprintf(buf,100,"temps : %05d",n); printf("\n");
EcrireTexte(10,20,buf,0);
AfficherSprite(sprite,x,y);
CopierZone(1,0,0,0,800,500,0,0);
} }
int main() /* Fonction pour décaler chaque paire d'éléments (x, y) du tableau vers la droite */
{ void decalerSerpent(int serpent[], int taille) {
int pingouin; int tempX = serpent[0];
int x,y,old_x,old_y; int tempY = serpent[1];
int n; int i;
unsigned long suivant;
int go_on=1; for (i = 2; i < taille; i += 2) {
int tempX2 = serpent[i];
InitialiserGraphique(); int tempY2 = serpent[i + 1];
CreerFenetre(0,0,800,500);
ChoisirEcran(2); serpent[i] = tempX;
ChargerImageFond("./images/arctis2.jpg"); serpent[i + 1] = tempY;
pingouin=ChargerSprite("./images/walk-0.png");
x=y=old_x=old_y=10; tempX = tempX2;
n=0; tempY = tempY2;
suivant=Microsecondes()+delta; }
}
while(go_on)
{ int main() {
if (SourisCliquee()) go_on=0; int serpent[TAILLE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
else
{ printf("Serpent initial : ");
if (Microsecondes()>suivant) afficherSerpent(serpent, TAILLE);
{
n++; /* Décaler le serpent d'une paire d'éléments vers la droite */
DessinerScene(pingouin,x,y,n); decalerSerpent(serpent, TAILLE);
suivant=Microsecondes()+delta;
} printf("Serpent après décalage : ");
SourisPosition(); afficherSerpent(serpent, TAILLE);
x=_X;
y=_Y; return 0;
if (x!=old_x || y!=old_y)
{
DessinerScene(pingouin,x,y,n);
old_x=x;
old_y=y;
}
}
}
FermerGraphique();
} }