2023-12-15 09:15:22 +01:00
|
|
|
#include "graphique.h"
|
|
|
|
#include "serpent.h"
|
|
|
|
#include "pommes.h"
|
|
|
|
#include "obstacles.h"
|
|
|
|
#include <time.h>
|
|
|
|
void Attendre(unsigned int millisecondes) {
|
2023-12-21 15:01:03 +01:00
|
|
|
clock_t start_time = clock();
|
|
|
|
while ((clock() - start_time) * 1000 / CLOCKS_PER_SEC < millisecondes){
|
|
|
|
}
|
2023-12-15 09:15:22 +01:00
|
|
|
}
|
|
|
|
void InitialiserJeu(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
|
|
|
serpent->longueur = 10;
|
|
|
|
serpent->corps = malloc(sizeof(Position) * serpent->longueur);
|
|
|
|
pommes->nombre = NOMBRE_POMMES;
|
|
|
|
obstacles->nombre = NOMBRE_OBSTACLES;
|
|
|
|
srand(time(NULL));
|
|
|
|
serpent->corps[0].x = LARGEUR / 2 * TAILLE_CASE;
|
|
|
|
serpent->corps[0].y = HAUTEUR / 2 * TAILLE_CASE;
|
|
|
|
/* Initialisation des obstacles*/
|
|
|
|
obstacles->positions = malloc(sizeof(Position) * obstacles->nombre);
|
|
|
|
for (int i = 0; i < obstacles->nombre; i++) {
|
|
|
|
obstacles->positions[i].x = rand() % LARGEUR;
|
|
|
|
obstacles->positions[i].y = rand() % HAUTEUR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void AfficherSerpent(Serpent* serpent) {
|
|
|
|
couleur couleurSerpent = CouleurParComposante(255, 255, 0);
|
|
|
|
ChoisirCouleurDessin(couleurSerpent);
|
|
|
|
for (int i = 0; i < serpent->longueur; i++) {
|
|
|
|
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void LibererMemoire(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
|
|
|
free(serpent->corps);
|
|
|
|
LibererPommes(pommes);
|
|
|
|
free(obstacles->positions);
|
|
|
|
}
|
|
|
|
void Graphique() {
|
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(0, 0, 1240, 940);
|
|
|
|
EcrireTexte(500, 400, "Le jeu va commencer !", 2);
|
2023-12-21 15:01:03 +01:00
|
|
|
Attendre(2000);
|
2023-12-15 09:15:22 +01:00
|
|
|
EffacerEcran(CouleurParComposante(0, 0, 0));
|
|
|
|
}
|
|
|
|
void AffichageBasique() {
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(111, 255, 94));
|
|
|
|
RemplirRectangle(0, 0, 1140, 760);
|
|
|
|
}
|
|
|
|
void AfficherScore(int score) {
|
|
|
|
char scoreText[20];
|
|
|
|
snprintf(scoreText, 20, "Score: %d", score);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
|
|
|
|
RemplirRectangle(20, 820, 150, 40);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
EcrireTexte(20, 850, scoreText, 2);
|
|
|
|
}
|
|
|
|
void AfficheTemps(int minute, int seconde) {
|
|
|
|
char temps[6];
|
|
|
|
snprintf(temps, 6, "%02d:%02d", minute, seconde);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
|
|
|
|
RemplirRectangle(20, 870, 70, 40);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
EcrireTexte(20, 900, temps, 2);
|
|
|
|
}
|
|
|
|
void AfficherEcranFin(int score) {
|
|
|
|
EcrireTexte(500, 300, "Game Over!", 2);
|
|
|
|
char scoreText[20];
|
|
|
|
snprintf(scoreText, 20, "Score: %d", score);
|
|
|
|
EcrireTexte(500, 400, scoreText, 2);
|
2023-12-18 14:51:12 +01:00
|
|
|
|
2023-12-15 09:15:22 +01:00
|
|
|
}
|
2023-12-18 14:51:12 +01:00
|
|
|
int PauseJeu(int perdu) {
|
|
|
|
while (perdu != -1) {
|
2023-12-15 09:15:22 +01:00
|
|
|
if (ToucheEnAttente()) {
|
|
|
|
int touche = Touche();
|
|
|
|
if (touche == XK_space) {
|
|
|
|
return 1; // La barre d'espace a été pressée, reprendre le jeu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Attendre(100);
|
|
|
|
}
|
|
|
|
}
|