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 delta 1000000L
#define delta 100000L
void Affichertimer(int n) /*Afficher le temps passé*/
{
char buf[100];
couleur j;
j=CouleurParNom("yellow");
ChoisirCouleurDessin(j);
ChoisirEcran(1);
CopierZone(2,1,0,0,930,710,0,0);
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);
}
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 */
{
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 */
{
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 */
{
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 */
{
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 ;
printf("x : %d ; y : %d\n",p_pastilles[i],p_pastilles[i+1]);
ChoisirEcran(2);
ChoisirCouleurDessin(r);
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 */
{
int x_millieu = 0, y_millieu = 0 , compteur = 0;
couleur o;
couleur j;
o=CouleurParNom("Gold");
int i = 0;
j=CouleurParNom("yellow");
ChoisirCouleurDessin(o);
ChoisirCouleurDessin(j);
x_millieu = T_PIXEL*30; /* 30 = 60 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);
/* 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;
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+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);
}
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:
direction = 1;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break;
case XK_Down:
direction = 3;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break;
case XK_Left:
direction = 0;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break;
case XK_Right:
direction = 2;
DeplacementSerpent(direction ,serpent, longueur_serpent);
break;
default:
@ -216,7 +247,7 @@ int main()
{
n++;
Affichertimer(n);
suivant=Microsecondes()+delta;
suivant=Microsecondes()+delta;
DeplacementSerpent(direction,serpent,longueur_serpent);
}

BIN
prog

Binary file not shown.

99
test.c

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