From 82b8b9a8795f2804993db85e585b1a34190e41e2 Mon Sep 17 00:00:00 2001 From: aissi Date: Fri, 1 Dec 2023 20:11:24 +0100 Subject: [PATCH] Ajout jeu qui marche --- SAE_semestre1/src/game.c | 209 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 SAE_semestre1/src/game.c diff --git a/SAE_semestre1/src/game.c b/SAE_semestre1/src/game.c new file mode 100644 index 0000000..e9ecb2e --- /dev/null +++ b/SAE_semestre1/src/game.c @@ -0,0 +1,209 @@ +#include +#include +#include + +#define LARGEUR_FENETRE 1250 +#define HAUTEUR_FENETRE 750 +#define TAILLE_CELLULE 25 +#define DELAI_MILLISECONDES 100 +#define ESPACE_NOIR 100 +#define CYCLE 100000L + + + int j,i; + int secondes = 0; + int minutes = 0; + int seconde_actuel = 0; + int old_seconde = 0; + char timer[6]; + unsigned long int suivant; + +typedef struct { + int x, y; +} Position; + +typedef struct { + Position *corps; + int longueur; +} Serpent; +Position pomme; +Serpent serpent; +int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/ + +void initialiserSerpent() { + serpent.longueur = 10; + serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur); + serpent.corps[0].x = LARGEUR_FENETRE / 2; + serpent.corps[0].y = HAUTEUR_FENETRE / 2; +} + +void dessinerSerpent() { + for (i = 0; i < serpent.longueur; i++) { + if (i % 2 == 0) { + ChoisirCouleurDessin(CouleurParComposante(0,0,0)); /*JAUNE*/ + } + RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE); + } +} + +void deplacerSerpent() { + /* Déplacer le corps du serpent*/ + for (i = serpent.longueur - 1; i > 0; i--) { + serpent.corps[i] = serpent.corps[i - 1]; + } + + /* Déplacer la tête du serpent en fonction de la direction*/ + switch (direction) { + case 0: + serpent.corps[0].x += TAILLE_CELLULE; + break; + case 1: + serpent.corps[0].y += TAILLE_CELLULE; + break; + case 2: + serpent.corps[0].x -= TAILLE_CELLULE; + break; + case 3: + serpent.corps[0].y -= TAILLE_CELLULE; + break; + } +} + int i; +void genererPomme() { + pomme.x = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; + pomme.y = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; +} +void dessinerPomme() { + ChoisirCouleurDessin(CouleurParComposante(255, 0, 0)); + RemplirRectangle(pomme.x, pomme.y, TAILLE_CELLULE, TAILLE_CELLULE); + +} +int collisionAvecPomme() { + return (serpent.corps[0].x == pomme.x && serpent.corps[0].y == pomme.y); +} + +int collisionAvecSerpent() { + for (i = 1; i < serpent.longueur; i++) { + if (serpent.corps[0].x == serpent.corps[i].x && serpent.corps[0].y == serpent.corps[i].y) { + return 1; + } + } + return 0; +} + +int collisionAvecBordures() { + return (serpent.corps[0].x < 0 || serpent.corps[0].x >= LARGEUR_FENETRE || + serpent.corps[0].y < 0 || serpent.corps[0].y >= HAUTEUR_FENETRE); +} + +void dessinerDamier() { + int cell_largeur = LARGEUR_FENETRE / TAILLE_CELLULE; + int cell_hauteur = HAUTEUR_FENETRE / TAILLE_CELLULE; + for (i = 0; i < cell_hauteur; i++) { + for (j = 0; j < cell_largeur; j++) { + if ((i + j) % 2 == 0) { + ChoisirCouleurDessin(CouleurParComposante(144, 238, 144)); + } else { + ChoisirCouleurDessin(CouleurParComposante(143, 218, 143)); + } + RemplirRectangle(j * TAILLE_CELLULE, i * TAILLE_CELLULE, TAILLE_CELLULE, TAILLE_CELLULE); + } + } +} +void Update_Timer(){ + snprintf(timer,6, "%02d:%02d", minutes,secondes); + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(0, 1200, 1250, 800); + ChoisirCouleurDessin(CouleurParNom("white")); + EcrireTexte(10,800,timer,2); +} +void Timer(){ + if(Microsecondes()> suivant){ + suivant = Microsecondes()+CYCLE; + seconde_actuel = ((suivant/1000)%10); + if (seconde_actuel != old_seconde){ + old_seconde = seconde_actuel; + if(secondes == 59){ + minutes = minutes++; + secondes = 0; + Update_Timer(); + }else{ + secondes = secondes++; + Update_Timer(); + } + } + } +} + +void gestionCollision() { + if (collisionAvecPomme()) { + serpent.longueur++; + serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur); + genererPomme(); + } + + if (collisionAvecSerpent() || collisionAvecBordures()) { + FermerGraphique(); + exit(EXIT_SUCCESS); + } +} + +void attente(int milliseconds) { + clock_t start_time = clock(); + while ((clock() - start_time) * 1000 / CLOCKS_PER_SEC < milliseconds) { + /* Attente*/ + } +} + +void jeu() { + InitialiserGraphique(); + CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR); + ChoisirCouleurDessin(CouleurParComposante(0, 0, 0)); + EffacerEcran(CouleurParComposante(0, 0, 0)); + + initialiserSerpent(); + genererPomme(); + + while (1) { + dessinerDamier(); /* Dessiner le damier en fond*/ + Timer(); + deplacerSerpent(); + + gestionCollision(); + + dessinerSerpent(); + dessinerPomme(); + + attente(DELAI_MILLISECONDES); /* Attente pour ralentir le serpent*/ + + if (SourisCliquee()) { + FermerGraphique(); + exit(EXIT_SUCCESS); + } + + if (ToucheEnAttente()) { + int touche = Touche(); + switch (touche) { + case XK_Right: + direction = 0; + break; + case XK_Down: + direction = 1; + break; + case XK_Left: + direction = 2; + break; + case XK_Up: + direction = 3; + break; + case XK_Escape: + FermerGraphique(); + } + } + } +} + +int main() { + jeu(); + return 0; +}