t
This commit is contained in:
parent
75e534f236
commit
6895ca77f7
55
serpent.c
55
serpent.c
@ -236,6 +236,16 @@ int CollisionAvecSerpent(Serpent* serpent) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EstDirectionOpposee(int directionActuelle, int nouvelleDirection) {
|
||||
if ((directionActuelle == 1 && nouvelleDirection == 2) ||
|
||||
(directionActuelle == 2 && nouvelleDirection == 1) ||
|
||||
(directionActuelle == 3 && nouvelleDirection == 4) ||
|
||||
(directionActuelle == 4 && nouvelleDirection == 3)) {
|
||||
return 1; // Les directions sont opposées
|
||||
}
|
||||
return 0; // Les directions ne sont pas opposées
|
||||
}
|
||||
|
||||
int CollisionAvecBordures(Serpent* serpent) {
|
||||
if (serpent->corps[0].x < 0 || serpent->corps[0].x >= LARGEUR * TAILLE_CASE ||
|
||||
serpent->corps[0].y < 0 || serpent->corps[0].y >= HAUTEUR * TAILLE_CASE) {
|
||||
@ -346,27 +356,30 @@ int main() {
|
||||
}
|
||||
}
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
switch (touche) {
|
||||
case XK_Right:
|
||||
direction = 1;
|
||||
break;
|
||||
case XK_Left:
|
||||
direction = 2;
|
||||
break;
|
||||
case XK_Up:
|
||||
direction = 3;
|
||||
break;
|
||||
case XK_Down:
|
||||
direction = 4;
|
||||
break;
|
||||
case XK_space:
|
||||
jeuEnPause = 1;
|
||||
break;
|
||||
case XK_Escape:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
int touche = Touche();
|
||||
int nouvelleDirection;
|
||||
|
||||
// Déterminer la nouvelle direction en fonction de la touche pressée
|
||||
if (touche == XK_Right) {
|
||||
nouvelleDirection = 1;
|
||||
} else if (touche == XK_Left) {
|
||||
nouvelleDirection = 2;
|
||||
} else if (touche == XK_Up) {
|
||||
nouvelleDirection = 3;
|
||||
} else if (touche == XK_Down) {
|
||||
nouvelleDirection = 4;
|
||||
} else if (touche == XK_space) {
|
||||
jeuEnPause = 1;
|
||||
} else if (touche == XK_Escape) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Vérifier si la nouvelle direction n'est pas opposée à la direction actuelle
|
||||
if (!EstDirectionOpposee(direction, nouvelleDirection)) {
|
||||
// Mettre à jour la direction du serpent
|
||||
direction = nouvelleDirection;
|
||||
}
|
||||
}
|
||||
DeplacerSerpent(&serpent, &direction);
|
||||
perdu = GestionCollision(&serpent, &pommes, &obstacles, perdu, &score);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user