diff --git a/Image/Pomme.png b/Image/Pomme.png new file mode 100644 index 0000000..6a24741 Binary files /dev/null and b/Image/Pomme.png differ diff --git a/Image/Serpent_Corps_Horizontal.png b/Image/Serpent_Corps_Horizontal.png new file mode 100644 index 0000000..85e1b63 Binary files /dev/null and b/Image/Serpent_Corps_Horizontal.png differ diff --git a/Image/Serpent_Corps_Vertical.png b/Image/Serpent_Corps_Vertical.png new file mode 100644 index 0000000..64111b3 Binary files /dev/null and b/Image/Serpent_Corps_Vertical.png differ diff --git a/Image/Serpent_Queu_Bas.png b/Image/Serpent_Queu_Bas.png new file mode 100644 index 0000000..1f23c2c Binary files /dev/null and b/Image/Serpent_Queu_Bas.png differ diff --git a/Image/Serpent_Queu_Droite.png b/Image/Serpent_Queu_Droite.png new file mode 100644 index 0000000..48ab1b0 Binary files /dev/null and b/Image/Serpent_Queu_Droite.png differ diff --git a/Image/Serpent_Queu_Gauche.png b/Image/Serpent_Queu_Gauche.png new file mode 100644 index 0000000..389f551 Binary files /dev/null and b/Image/Serpent_Queu_Gauche.png differ diff --git a/Image/Serpent_Queu_Haut.png b/Image/Serpent_Queu_Haut.png new file mode 100644 index 0000000..d554fa6 Binary files /dev/null and b/Image/Serpent_Queu_Haut.png differ diff --git a/Image/Serpent_Tete_Bas.png b/Image/Serpent_Tete_Bas.png new file mode 100644 index 0000000..59dbd41 Binary files /dev/null and b/Image/Serpent_Tete_Bas.png differ diff --git a/Image/Serpent_Tete_Droite.png b/Image/Serpent_Tete_Droite.png new file mode 100644 index 0000000..b349b2d Binary files /dev/null and b/Image/Serpent_Tete_Droite.png differ diff --git a/Image/Serpent_Tete_Gauche.png b/Image/Serpent_Tete_Gauche.png new file mode 100644 index 0000000..cb7ebab Binary files /dev/null and b/Image/Serpent_Tete_Gauche.png differ diff --git a/Image/Serpent_Tete_Haut.png b/Image/Serpent_Tete_Haut.png new file mode 100644 index 0000000..cea03e3 Binary files /dev/null and b/Image/Serpent_Tete_Haut.png differ diff --git a/Image/gameover.png b/Image/gameover.png new file mode 100644 index 0000000..ccd3550 Binary files /dev/null and b/Image/gameover.png differ diff --git a/Image/pause.png b/Image/pause.png new file mode 100644 index 0000000..f53f783 Binary files /dev/null and b/Image/pause.png differ diff --git a/Image/start.png b/Image/start.png new file mode 100644 index 0000000..71c5b1e Binary files /dev/null and b/Image/start.png differ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d859696 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +but : snake + +OFILES = main.o \ + serpent.o \ + pastille.o \ + menu_terrain.o\ + temps_score.o + +CC = gcc + +CFLAGS = -Wall -ansi -pedantic -g + +serpent.o : serpent.h menu_terrain.h temps_score.h + +pastille.o : pastille.h menu_terrain.h serpent.h + +main.o : serpent.h pastille.h temps_score.h menu_terrain.h + +temps_score.o : temps_score.h + +menu_terrain.o : menu_terrain.h + +snake : $(OFILES) + $(CC) $(CFLAGS) -o snake $(OFILES) -lgraph + +run : snake + ./snake + +clean : + -rm -f $(OFILES) snake + +.PHONY : but clean diff --git a/Snake_MarvinAubert_PatrickFelix-Vimalaratnam.pdf b/Snake_MarvinAubert_PatrickFelix-Vimalaratnam.pdf new file mode 100644 index 0000000..465b93a Binary files /dev/null and b/Snake_MarvinAubert_PatrickFelix-Vimalaratnam.pdf differ diff --git a/main.c b/main.c index cc0691a..4db02b8 100644 --- a/main.c +++ b/main.c @@ -2,276 +2,111 @@ #include #include #include -#define LARGEUR_FENETRE 1050 -#define HAUTEUR_FENETRE 750 -#define TAILLE_CASE 15 -#define ESPACE_BAS 75 -#define ESPACE_HAUT 75 -#define NBR_PASTILLE_INITIAL 5 -#define COLONNES 60 -#define LIGNES 40 -#define LARGEUR_ECRAN_JEU 900 -#define HAUTEUR_ECRAN_JEU 600 -#define TAILLE_CARRE 120 +#include "menu_terrain.h" +#include "temps_score.h" +#include "serpent.h" +#include "pastille.h" #define CYCLE 1000000L #define MAX_LONGUEUR 100 -typedef struct -{ - int x; - int y; -} Segment; - -typedef struct -{ - int x; - int y; -} Pastille; - - -void Attendre(unsigned int millisecondes) -{ - unsigned long debut = Microsecondes(); - unsigned long attente = millisecondes * 1000; - while (Microsecondes() - debut < attente) - { - } -} -/*Fonction Perdu !!!*/ -void perdu(char *texte, int score, int minutes, int secondes, int pour_l_instant_t_a_pas_encore_perdue) -{ - - ChargerImage("perdu-removebg-preview.png", 300, 150, 30, 30, 900, 900); - /*Afficher le score sur l'image*/ - ChoisirCouleurDessin(CouleurParNom("black")); - sprintf(texte, "%3d", score); - EcrireTexte(455, 364, texte, 2); - /*affichage du timer au menu*/ - sprintf(texte, "%02d:%02d", minutes, secondes); - EcrireTexte(660, 364, texte, 2); - if (Touche() == XK_Escape) - { - pour_l_instant_t_a_pas_encore_perdue = 0; - } - else if (Touche() == XK_Return) - { - main(); - } -} - -void effacerSerpent(Segment serpent[], int longueurSerpent) -{ - int i; - ChoisirCouleurDessin(CouleurParComposante(126, 217, 87)); - for (i = 0; i < longueurSerpent; i++) - { - RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); - } -} -/*Fonction permettant d'afficher le serpent*/ -void afficherSerpent(Segment serpent[], int taille) -{ - int i; - couleur couleurSerpent = CouleurParNom("yellow"); - ChoisirCouleurDessin(couleurSerpent); - - for (i = 0; i < taille; i++) - { - serpent[i].x = LARGEUR_FENETRE / 2 + i * TAILLE_CASE; - serpent[i].y = HAUTEUR_FENETRE / 2; - } - for (i = 0; i < taille; i++) - { - RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); - } -} -/*Fonction recursive permettant d'afficher au moment de l'initialisation un nombre donnée en parametre de pastille Rouge (oui j'en suis fiere meme si c'est pas incroyable)*/ -void afficherPastilleAleatoire(int z, Pastille pomme[]) -{ - int i; - for (i = 0; i < z; i++) - { - ; - pomme[i].x = rand() % (COLONNES - 10) * TAILLE_CASE + 5 * TAILLE_CASE; - pomme[i].y = rand() % (LIGNES - 5) * TAILLE_CASE + (ESPACE_HAUT - 3 * TAILLE_CASE); - ChoisirCouleurDessin(CouleurParNom("red")); - RemplirArc(pomme[i].x, pomme[i].y, TAILLE_CASE, TAILLE_CASE, 0, 360); - } -} - -/*Fonction initialisant la fenetre de jeux en vert*/ -void EcranJeu() -{ - int i, j; - - for (i = 2; i < LIGNES + 2; i++) - { - for (j = 5; j < COLONNES + 5; j++) - { - ChoisirCouleurDessin(CouleurParComposante(126, 217, 87)); - RemplirRectangle(j * TAILLE_CASE, i * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); - } - } -} -void deplacerSerpent(int *longueurSerpent, Segment serpent[], int direction) -{ - int i; - - effacerSerpent(serpent, *longueurSerpent); - for (i = *longueurSerpent - 1; i > 0; i--) - { - serpent[i] = serpent[i - 1]; - } - - switch (direction) - { - case 1: - serpent[0].x += TAILLE_CASE; - break; - case 2: - serpent[0].x -= TAILLE_CASE; - break; - case 3: - serpent[0].y -= TAILLE_CASE; - break; - case 4: - serpent[0].y += TAILLE_CASE; - break; - } - for (i = 0; i < *longueurSerpent; i++) - { - - ChoisirCouleurDessin(CouleurParNom("yellow")); - RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); - } - Attendre(75); -} - -int main() -{ - unsigned long debut, suivant; - int n = 0, minutes, secondes, score = 0; - char texte[20]; - int nbr_pastille = 5, touche, pour_l_instant_t_a_pas_encore_perdue = 1, i, j, x = 0, direction = 1, verifSerpent = 0, longueurSerpent = 10; - Pastille *pomme = (Pastille *)malloc(nbr_pastille * sizeof(Pastille)); - Segment *serpent = (Segment *)malloc(MAX_LONGUEUR * sizeof(Segment)); - - if (pomme == NULL || serpent == NULL) - { - printf("Erreur d'allocation de mémoire.\n"); - return EXIT_FAILURE; - } - +int main(){ + int jouer = 2, rejouer; InitialiserGraphique(); CreerFenetre(350, 100, LARGEUR_FENETRE, HAUTEUR_FENETRE); srand(time(NULL)); - EffacerEcran(CouleurParNom("black")); - EcranJeu(); - afficherPastilleAleatoire(NBR_PASTILLE_INITIAL, pomme); - afficherSerpent(serpent, longueurSerpent); - - while (pour_l_instant_t_a_pas_encore_perdue) - { - if (longueurSerpent >= MAX_LONGUEUR){ - serpent = (Segment*)realloc(serpent,(MAX_LONGUEUR+MAX_LONGUEUR) * sizeof(Segment)); - } - /*Temps ecoulé debut*/ - if (Microsecondes() > suivant) - { - - suivant = Microsecondes() + CYCLE; - n++; - minutes = n / 60; - secondes = n % 60; - ChoisirCouleurDessin(CouleurParNom("black")); - RemplirRectangle(75, 650, 250, 40); - ChoisirCouleurDessin(CouleurParNom("blue")); - printf("Temps : %d secondes\n", n); - sprintf(texte, "Temps : %02d : %02d", minutes, secondes); - EcrireTexte(85, 685, texte, 2); - } - - /*Temps ecoulé fin */ - if (ToucheEnAttente()) - { - switch (Touche()) + Start(&jouer); + if (jouer){ + do{ + unsigned long suivant; + unsigned int vitesse_serpent = VITESSE_INITIAL; + int n = 0, minutes, secondes, score = 0, continuer; + char texte[20]; + int nbr_pastille = 5, gameover, i, direction = 1, direction_avant = 1, longueurSerpent = TAILLE_SERPENT_INITIAL, max_longuer = MAX_LONGUEUR; + Pastille *pomme = (Pastille *) malloc(nbr_pastille * sizeof(Pastille)); + Segment *serpent = (Segment *) malloc(MAX_LONGUEUR * sizeof(Segment)); + if (pomme == NULL || serpent == NULL) { - 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: - - ChargerImage("pause.png", 300, 150, 30, 30, 900, 900); - while (Touche() != XK_space) - { - - Attendre(10); - } - EcranJeu(); - for(i=0;i= LARGEUR_ECRAN_JEU + 5 * TAILLE_CASE || serpent[0].y < ESPACE_HAUT - 3 * TAILLE_CASE || serpent[0].y >= HAUTEUR_ECRAN_JEU + 2 * TAILLE_CASE) - { - - ChoisirCouleurDessin(CouleurParNom("black")); - RemplirRectangle(serpent[0].x, serpent[0].y, TAILLE_CASE, TAILLE_CASE); - Attendre(1000); - /*Execution de la fontion perdu*/ - perdu(texte, score, minutes, secondes, pour_l_instant_t_a_pas_encore_perdue = 0); - } + rejouer = 2; + EffacerEcran(CouleurParNom("black")); + Terrain(); + Creation_Serpent(serpent, longueurSerpent); + Afficher_Serpent(serpent, longueurSerpent, direction); + Creation_Pastille(NBR_PASTILLE_INITIAL, pomme, longueurSerpent, serpent); + Afficher_Pastille(NBR_PASTILLE_INITIAL, pomme); + do{ + gameover = 2; + if (longueurSerpent >= MAX_LONGUEUR){ + serpent = (Segment*) realloc(serpent,(max_longuer+MAX_LONGUEUR)); + max_longuer += MAX_LONGUEUR; + } + /*Temps ecoul� debut*/ + if (Microsecondes() > suivant){ + suivant = Microsecondes() + CYCLE; + n++; + minutes = n / 60; + secondes = n % 60; + Afficher_Temps(minutes, secondes, texte); + } + /*Temps ecoul� fin */ + if (ToucheEnAttente()){ + switch (Touche()){ + case XK_Right: + direction = 1; + break; + case XK_Left: + direction = -1; + break; + case XK_Up: + direction = 2; + break; + case XK_Down: + direction = -2; + break; + case XK_space: + continuer = 2; + Pause(texte, minutes, secondes, score, &continuer); + if (continuer == 0){ + FermerGraphique(); + return EXIT_SUCCESS; + } + Afficher_Pastille(NBR_PASTILLE_INITIAL, pomme); + Afficher_Serpent(serpent, longueurSerpent, direction); + } + } + if (direction==direction_avant*(-1)){ + direction=direction_avant; + }else{ + direction_avant=direction; + } + Deplacer_Serpent(&longueurSerpent, serpent, direction, vitesse_serpent); + for (i = 0; i < NBR_PASTILLE_INITIAL; i++){ + /*Mise a zero du score*/ + ChoisirCouleurDessin(CouleurParNom("white")); + sprintf(texte, "Score : %3d", score); + EcrireTexte(825, 685, texte, 2); + if (serpent[i].x == pomme[i].x && serpent[i].y == pomme[i].y) + { + longueurSerpent += 2; + Validation_Coordonne( NBR_PASTILLE_INITIAL, pomme, longueurSerpent, serpent, &pomme[i]); + score = score + 5; + if (score % 40 == 0 && vitesse_serpent > 10U){ + vitesse_serpent -= 5U; + } + Afficher_Pastille(NBR_PASTILLE_INITIAL, pomme); + Afficher_Score(score, texte); + } + } + Contoure_Terrain(); + Mort_Serpent( longueurSerpent, serpent, score, minutes, secondes, texte, &rejouer, &gameover); + }while (gameover); + free(pomme); + free(serpent); + }while(rejouer); } - - - free(pomme); - free(serpent); - Touche(); FermerGraphique(); return EXIT_SUCCESS; } diff --git a/menu_terrain.c b/menu_terrain.c new file mode 100644 index 0000000..9a06bf6 --- /dev/null +++ b/menu_terrain.c @@ -0,0 +1,119 @@ +#include +#include +#include +#include "menu_terrain.h" + +/*Fonction initialisant le terrain de jeux*/ +void Terrain(){ + int i, j; + + for (i = CONTOURE_H; i < LIGNES + CONTOURE_H; i++) + { + for (j = CONTOURE_L; j < COLONNES + CONTOURE_L; j++){ + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(j * TAILLE_CASE, i * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); + } + } +} + +/*Fonction dessinnant la bordure du terrain en noir pour enlever les block vert qui apparaissent*/ +void Contoure_Terrain(){ + int i, j; + for (i = 0; i < CONTOURE_H; i++){ + for (j = 0; j < COLONNES; j++){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(j * TAILLE_CASE, i * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); + } + } + for (j = 0; j < CONTOURE_L; j++){ + for (i = 0; i < LIGNES; i++){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(j * TAILLE_CASE, i * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); + } + } + for (j = COLONNES + CONTOURE_L; j < CONTOURE_L; j++){ + for (i = 0; i < LIGNES; i++){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(j * TAILLE_CASE, i * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE); + } + } + +} + +/*Fonction qui affiche le menu de début*/ +void Start(int* jouer){ + EffacerEcran(CouleurParNom("black")); + ChargerImage("Image/start.png", 75, 50, 0, 0, 1000, 1000); + while(*jouer == 2){ + if (ToucheEnAttente()){ + switch(Touche()){ + case XK_Return : + *jouer = 1; + break; + case XK_Escape : + *jouer = 0; + break; + } + } + } +} + +/*Fonction qui affiche le menu pause*/ +void Pause(char* texte, int minutes, int secondes, int score, int* continuer){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(825, 655, 150, 50); + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(75, 650, 250, 50); + ChargerImage("Image/pause.png", 75, 50, 0, 0, 1000, 1000); + ChoisirCouleurDessin(CouleurParNom("black")); + sprintf(texte, "%3d", score); + EcrireTexte(550, 420, texte, 2); + /*affichage du timer au menu*/ + sprintf(texte, "%02d:%02d", minutes, secondes); + EcrireTexte(550, 485, texte, 2); + while(*continuer == 2){ + if (ToucheEnAttente()){ + switch(Touche()){ + case XK_space : + *continuer = 1; + break; + case XK_Escape : + *continuer = 0; + break; + } + } + } + Terrain(); + ChoisirCouleurDessin(CouleurParNom("white")); + sprintf(texte, "Temps : %02d : %02d", minutes, secondes); + EcrireTexte(85, 685, texte, 2); + ChoisirCouleurDessin(CouleurParNom("white")); + sprintf(texte, "Score : %3d", score); + EcrireTexte(825, 685, texte, 2); +} + +/*Fonction qui affiche le menu perdu*/ +void Perdu(char *texte, int score, int minutes, int secondes, int* rejouer) +{ + EffacerEcran(CouleurParNom("black")); + ChargerImage("Image/gameover.png", 75, 50, 0, 0, 1000, 1000); + /*Afficher le score sur l'image*/ + ChoisirCouleurDessin(CouleurParNom("black")); + sprintf(texte, "%3d", score); + EcrireTexte(550, 420, texte, 2); + /*affichage du timer au menu*/ + sprintf(texte, "%02d:%02d", minutes, secondes); + EcrireTexte(550, 485, texte, 2); + while(*rejouer == 2){ + if (ToucheEnAttente()){ + switch(Touche()){ + case XK_Return : + *rejouer = 1; + break; + case XK_Escape : + *rejouer = 0; + break; + } + } + } +} \ No newline at end of file diff --git a/menu_terrain.h b/menu_terrain.h new file mode 100644 index 0000000..db5498d --- /dev/null +++ b/menu_terrain.h @@ -0,0 +1,18 @@ +#ifndef MENU_TERRAIN_H +#define MENU_TERRAIN_H + +void Terrain(); +void Contoure_Terrain(); +void Start(int* jouer); +void Pause(char* texte, int minutes, int secondes, int score, int* continuer); +void Perdu(char *texte, int score, int minutes, int secondes, int* rejouer); + +#define LARGEUR_FENETRE 1050 +#define HAUTEUR_FENETRE 750 +#define TAILLE_CASE 15 +#define COLONNES 60 +#define LIGNES 40 +#define CONTOURE_H 2 +#define CONTOURE_L 5 + +#endif /* MENU_TERRAIN_H */ diff --git a/pastille.c b/pastille.c new file mode 100644 index 0000000..db65242 --- /dev/null +++ b/pastille.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include "pastille.h" +#include "menu_terrain.h" +#include "serpent.h" + +/*Fonction permettant l'initialisation d'un nombre donnée en parametre de pastille*/ +void Creation_Pastille(int z, Pastille pomme[], int longueurSerpent, Segment serpent[]) +{ + int i; + for (i = 0; i < z; i++) + { + pomme[i].x = rand() % (COLONNES) * TAILLE_CASE + CONTOURE_L * TAILLE_CASE; + pomme[i].y = rand() % (LIGNES) * TAILLE_CASE + (CONTOURE_H * TAILLE_CASE); + Validation_Coordonne( i, pomme, longueurSerpent, serpent, &pomme[i]); + } +} + +/*Fonction validant les coordonnées des pastilles*/ +void Validation_Coordonne(int z, Pastille pomme[], int longueurSerpent, Segment serpent[], Pastille* indice_pomme){ + int coordonee_ok, j; + Pastille nouv_pomme; + do{ + coordonee_ok = 0; + nouv_pomme.x = (rand() % COLONNES) * TAILLE_CASE + (CONTOURE_L * TAILLE_CASE); + nouv_pomme.y = (rand() % LIGNES) * TAILLE_CASE + (CONTOURE_H * TAILLE_CASE); + for (j = 0; j < longueurSerpent; j++){ + if (nouv_pomme.x == serpent[j].x && nouv_pomme.y == serpent[j].y){ + coordonee_ok = 1; + break; + } + } + if (coordonee_ok == 0){ + for (j = 0; j < z; j++){ + if (nouv_pomme.x == pomme[j].x && nouv_pomme.y == pomme[j].y){ + coordonee_ok = 1; + break; + } + } + } + }while (coordonee_ok); + indice_pomme->x = nouv_pomme.x; + indice_pomme->y = nouv_pomme.y; +} + +/*Fonction permettant l'affichage d'un nombre donnée de pastilles*/ +void Afficher_Pastille(int z, Pastille pomme[]){ + int i; + for (i = 0; i < z; i++){ + ChargerImage("Image/Pomme.png", pomme[i].x, pomme[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } +} \ No newline at end of file diff --git a/pastille.h b/pastille.h new file mode 100644 index 0000000..446ec61 --- /dev/null +++ b/pastille.h @@ -0,0 +1,17 @@ +#include "serpent.h" +#ifndef PASTILLE_H +#define PASTILLE_H + +typedef struct +{ + int x; + int y; +} Pastille; + +void Creation_Pastille(int z, Pastille pomme[], int longueurSerpent, Segment serpent[]); +void Afficher_Pastille(int z, Pastille pomme[]); +void Validation_Coordonne(int z, Pastille pomme[], int longueurSerpent, Segment serpent[], Pastille* indice_pomme); + +#define NBR_PASTILLE_INITIAL 5 + +#endif /* PASTILLE_H */ diff --git a/serpent.c b/serpent.c new file mode 100644 index 0000000..8213c86 --- /dev/null +++ b/serpent.c @@ -0,0 +1,206 @@ +#include +#include +#include +#include "serpent.h" +#include "menu_terrain.h" +#include "temps_score.h" + +/*Fonction permettant de créer un serpent de base avec une taille donnée*/ +void Creation_Serpent(Segment serpent[], int taille){ + int i; + for (i = 0; i < taille; i++) + { + serpent[i].x = (COLONNES / 2) * TAILLE_CASE; + serpent[i].y = (LIGNES / 2) * TAILLE_CASE; + } +} + +/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le haut de l'écran*/ +void Afficher_Serpent_Haut(Segment serpent[], int taille, int rotation){ + int i; + if (rotation == 2){ + ChargerImage("Image/Serpent_Tete_Haut.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + for (i = 1; i < taille-1; i++){ + if (serpent[i-1].y == serpent[i].y){ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else{ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE); + if (serpent[taille-2].x == serpent[taille-1].x){ + if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Haut.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + }else if(serpent[taille-2].y == serpent[taille-1].y){ + if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + } +} + +/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le bas de l'écran*/ +void Afficher_Serpent_Bas(Segment serpent[], int taille, int rotation){ + int i; + if(rotation == -2){ + ChargerImage("Image/Serpent_Tete_Bas.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + for (i = 1; i < taille-1; i++){ + if (serpent[i-1].y == serpent[i].y){ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else{ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE); + if (serpent[taille-2].x == serpent[taille-1].x){ + if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Haut2.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + }else if(serpent[taille-2].y == serpent[taille-1].y){ + if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + } +} + +/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le droite de l'écran*/ +void Afficher_Serpent_Droite(Segment serpent[], int taille, int rotation){ + int i; + if (rotation == 1){ + ChargerImage("Image/Serpent_Tete_Droite.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + for (i = 1; i < taille-1; i++){ + if (serpent[i-1].x == serpent[i].x){ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else{ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE); + if (serpent[taille-2].x == serpent[taille-1].x){ + if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Haut.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + }else if(serpent[taille-2].y == serpent[taille-1].y){ + if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + } +} + +/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le gauche de l'écran*/ +void Afficher_Serpent_Gauche(Segment serpent[], int taille, int rotation){ + int i; + if (rotation == -1){ + ChargerImage("Image/Serpent_Tete_Gauche.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + for (i = 1; i < taille-1; i++){ + if (serpent[i-1].x == serpent[i].x){ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else{ + RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE); + ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE); + if (serpent[taille-2].x == serpent[taille-1].x){ + if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){ + ChargerImage("Image/Serpent_Queu_Haut.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + }else if(serpent[taille-2].y == serpent[taille-1].y){ + if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + }else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){ + ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE); + } + } + } +} + +/*Fonction permettant d'afficher le serpent avec une taille donnée*/ +void Afficher_Serpent(Segment serpent[], int taille, int rotation) +{ + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(serpent[0].x, serpent[0].y, TAILLE_CASE, TAILLE_CASE); + RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE); + Afficher_Serpent_Haut( serpent, taille, rotation); + Afficher_Serpent_Bas( serpent, taille, rotation); + Afficher_Serpent_Droite( serpent, taille, rotation); + Afficher_Serpent_Gauche( serpent, taille, rotation); +} + + +/*Fonction permettant au serpent de se déplacer*/ +void Deplacer_Serpent(int *longueurSerpent, Segment serpent[], int direction, unsigned int vitesse_serpent){ + int i; + ChoisirCouleurDessin(CouleurParComposante(0, 217, 87)); + RemplirRectangle(serpent[*longueurSerpent-1].x, serpent[*longueurSerpent-1].y, TAILLE_CASE, TAILLE_CASE); + for (i = *longueurSerpent - 1; i > 0; i--) + { + serpent[i] = serpent[i - 1]; + } + + switch (direction) + { + case 1: + serpent[0].x += TAILLE_CASE; + break; + case -1: + serpent[0].x -= TAILLE_CASE; + break; + case 2: + serpent[0].y -= TAILLE_CASE; + break; + case -2: + serpent[0].y += TAILLE_CASE; + break; + } + Afficher_Serpent(serpent, *longueurSerpent, direction); + Attendre(vitesse_serpent); +} + +/*Fonction l'emplacement de la tête du serpent pour la mort*/ +void Mort_Serpent(int longueurSerpent, Segment serpent[], int score, int minutes, int secondes, char texte[], int* rejouer, int* gameover){ + int i; + if (serpent[0].x < 5 * TAILLE_CASE || serpent[0].x >= (COLONNES * TAILLE_CASE) + 5 * TAILLE_CASE || serpent[0].y < CONTOURE_H * TAILLE_CASE || serpent[0].y >= (LIGNES * TAILLE_CASE) + 2 * TAILLE_CASE){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(serpent[0].x, serpent[0].y, TAILLE_CASE, TAILLE_CASE); + Attendre(1000); + *gameover = 0; + /*Execution de la fontion perdu*/ + Perdu(texte, score, minutes, secondes, rejouer); + } + for (i = 1; i < longueurSerpent; i++){ + if (serpent[0].x == serpent[i].x && serpent[0].y == serpent[i].y){ + Attendre(1000); + *gameover = 0; + Perdu(texte, score, minutes, secondes, rejouer); + } + } +} \ No newline at end of file diff --git a/serpent.h b/serpent.h new file mode 100644 index 0000000..a6c20db --- /dev/null +++ b/serpent.h @@ -0,0 +1,23 @@ +#ifndef SERPENT_H +#define SERPENT_H + +typedef struct +{ + int x; + int y; +} Segment; + + +void Creation_Serpent(Segment serpent[], int taille); +void Afficher_Serpent(Segment serpent[], int taille, int rotation); +void Deplacer_Serpent(int *longueurSerpent, Segment serpent[], int direction, unsigned int vitesse_serpent); +void Mort_Serpent(int longueurSerpent, Segment serpent[], int score, int minutes, int secondes, char texte[], int* rejouer, int* gameover); +void Afficher_Serpent_Gauche(Segment serpent[], int taille, int rotation); +void Afficher_Serpent_Droite(Segment serpent[], int taille, int rotation); +void Afficher_Serpent_Droitefficher_Serpent_Bas(Segment serpent[], int taille, int rotation); +void Afficher_Serpent_Haut(Segment serpent[], int taille, int rotation); + +#define TAILLE_SERPENT_INITIAL 10 +#define VITESSE_INITIAL 70U + +#endif /* SERPENT_H */ diff --git a/snake b/snake new file mode 100755 index 0000000..89266a5 Binary files /dev/null and b/snake differ diff --git a/temps_score.c b/temps_score.c new file mode 100644 index 0000000..c77f930 --- /dev/null +++ b/temps_score.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include "temps_score.h" + +void Attendre(unsigned int millisecondes){ + unsigned long debut = Microsecondes(); + unsigned long attente = millisecondes * 1000; + while (Microsecondes() - debut < attente){ + } +} + +void Afficher_Temps(int minutes, int secondes, char texte[]){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(75, 650, 250, 40); + ChoisirCouleurDessin(CouleurParNom("white")); + sprintf(texte, "Temps : %02d : %02d", minutes, secondes); + EcrireTexte(85, 685, texte, 2); +} + +void Afficher_Score(int score, char texte[]){ + ChoisirCouleurDessin(CouleurParNom("black")); + RemplirRectangle(825, 655, 150, 40); + ChoisirCouleurDessin(CouleurParNom("white")); + sprintf(texte, "Score : %3d", score); + EcrireTexte(825, 685, texte, 2); +} + diff --git a/temps_score.h b/temps_score.h new file mode 100644 index 0000000..a95c8e9 --- /dev/null +++ b/temps_score.h @@ -0,0 +1,8 @@ +#ifndef TEMPS_SCORE_H +#define TEMPS_SCORE_H + +void Attendre(unsigned int millisecondes); +void Afficher_Temps(int minutes, int secondes, char texte[]); +void Afficher_Score(int score, char texte[]); + +#endif /* TEMPS_SCORE_H */