SAE11_2023/main.c

111 lines
4.5 KiB
C
Raw Permalink Blame History

#include <stdlib.h>
#include <graph.h>
#include <time.h>
#include <stdio.h>
#include "terrain.h"
#include "temps_et_score.h"
#include "serpent.h"
#include "pomme.h"
#define CYCLE 1000000L
#define MAX_LONGUEUR 100
int main(){
int rejouer;
InitialiserGraphique();
CreerFenetre(350, 100, LARGEUR_FENETRE, HAUTEUR_FENETRE);
srand(time(NULL));
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, longSerpent = 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)
{
printf("Erreur d'allocation de m<>moire.\n");
return EXIT_FAILURE;
}
rejouer = 2;
EffacerEcran(CouleurParNom("black"));
Terrain();
Creation_Serpent(serpent, longSerpent);
Afficher_Serpent(serpent, longSerpent, direction);
Creation_Pastille(NBR_PASTILLE_INITIAL, pomme, longSerpent, serpent);
Afficher_Pastille(NBR_PASTILLE_INITIAL, pomme);
do{
gameover = 2;
if (longSerpent >= MAX_LONGUEUR){
serpent = (Segment*) realloc(serpent,(max_longuer+MAX_LONGUEUR));
max_longuer += MAX_LONGUEUR;
}
/*Temps ecoul<75> debut*/
if (Microsecondes() > suivant){
suivant = Microsecondes() + CYCLE;
n++;
minutes = n / 60;
secondes = n % 60;
Afficher_Temps(minutes, secondes, texte);
}
/*Temps ecoul<75> 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, longSerpent, direction);
}
}
if (direction==direction_avant*(-1)){
direction=direction_avant;
}else{
direction_avant=direction;
}
Deplacer_Serpent(&longSerpent, 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)
{
longSerpent += 2;
Validation_Coordonne( NBR_PASTILLE_INITIAL, pomme, longSerpent, 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( longSerpent, serpent, score, minutes, secondes, texte, &rejouer, &gameover);
}while (gameover);
free(pomme);
free(serpent);
}while(rejouer);
FermerGraphique();
return EXIT_SUCCESS;
}