From c105dcc6e4983cc39de12416143778c088f631e5 Mon Sep 17 00:00:00 2001 From: stiti Date: Sat, 2 Dec 2023 15:18:40 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20d'un=20serpent=20de=2010=20segm?= =?UTF-8?q?ents=20+=20d=C3=A9placement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/snake.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/src/snake.c b/src/snake.c index 13c1539..b23b7da 100644 --- a/src/snake.c +++ b/src/snake.c @@ -1,7 +1,69 @@ -#include -#include -#include +#include +#include +#include +#include -void creer_snake(){ - serpent[1] +#define TAILLE_CASE 20 +#define TEMPS_ATTENTE 100000 + +typedef struct { + int x, y; +} Segment; + +int creationSnake() { + int touche,i,j,longueur,direction_x,direction_y; + /*A SUPPRIMER : (uniquement pour tester le serpent)*/ + InitialiserGraphique(); + CreerFenetre(0, 0, 1200, 1000); + + + Segment serpent[10]; + longueur = 10; + for (i = 0; i < longueur; ++i) { + serpent[i].x = 5 + i; + serpent[i].y = 5; + } + + direction_x = 1; + direction_y = 0; + + while (1) { + + for (i = longueur - 1; i > 0; --i) { + serpent[i] = serpent[i - 1]; + } + serpent[0].x += direction_x; + serpent[0].y += direction_y; + + for (i = 0; i < longueur; ++i) { + ChoisirCouleurDessin(CouleurParNom("yellow")); + RemplirRectangle(serpent[i].x * TAILLE_CASE, serpent[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); + } + + + + + usleep(TEMPS_ATTENTE); /*Attendre 100 millisecondes -> ça nous permet de "gérer" la vitesse du serpent*/ + + + if (ToucheEnAttente()) { + touche = Touche(); + if (touche == XK_Up) { + direction_x = 0; + direction_y = -1; + } else if (touche == XK_Down) { + direction_x = 0; + direction_y = 1; + } else if (touche == XK_Left) { + direction_x = -1; + direction_y = 0; + } else if (touche == XK_Right) { + direction_x = 1; + direction_y = 0; + } + } + } + + FermerGraphique(); + return 0; } \ No newline at end of file