t
This commit is contained in:
parent
c6b09bb5ca
commit
cd7b7af74c
221
serpent.c
221
serpent.c
@ -12,97 +12,111 @@
|
||||
#define CYCLE 100000L
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
int x;
|
||||
int y;
|
||||
} Position;
|
||||
|
||||
typedef struct {
|
||||
Position* corps;
|
||||
int longueur;
|
||||
Position* corps;
|
||||
int longueur;
|
||||
} Serpent;
|
||||
|
||||
typedef struct PommeNode {
|
||||
Position position;
|
||||
struct PommeNode* next;
|
||||
Position position;
|
||||
struct PommeNode* next;
|
||||
} PommeNode;
|
||||
|
||||
typedef struct {
|
||||
PommeNode* head;
|
||||
int nombre;
|
||||
PommeNode* head;
|
||||
int nombre;
|
||||
} Pommes;
|
||||
|
||||
typedef struct {
|
||||
Position* positions;
|
||||
int nombre;
|
||||
Position* positions;
|
||||
int nombre;
|
||||
} Obstacles;
|
||||
|
||||
void Attendre(unsigned int millisecondes) {
|
||||
usleep(millisecondes * 1000);
|
||||
usleep(millisecondes * 1000);
|
||||
}
|
||||
|
||||
void LibererPommes(Pommes* pommes) {
|
||||
PommeNode* current = pommes->head;
|
||||
while (current != NULL) {
|
||||
PommeNode* next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
pommes->head = NULL;
|
||||
PommeNode* current = pommes->head;
|
||||
while (current != NULL) {
|
||||
PommeNode* next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
pommes->head = NULL;
|
||||
}
|
||||
|
||||
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;
|
||||
serpent->longueur = 10;
|
||||
serpent->corps = malloc(sizeof(Position) * serpent->longueur);
|
||||
pommes->nombre = NOMBRE_POMMES;
|
||||
obstacles->nombre = NOMBRE_OBSTACLES;
|
||||
|
||||
srand(time(NULL));
|
||||
srand(time(NULL));
|
||||
|
||||
serpent->corps[0].x = LARGEUR / 2 * TAILLE_CASE;
|
||||
serpent->corps[0].y = HAUTEUR / 2 * TAILLE_CASE;
|
||||
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;
|
||||
}
|
||||
/* 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 GenererObstacles(Obstacles* obstacles) {
|
||||
couleur couleurObstacle = CouleurParComposante(0, 0, 255);
|
||||
for (int i = 0; i < obstacles->nombre; i++) {
|
||||
ChoisirCouleurDessin(couleurObstacle);
|
||||
RemplirRectangle(obstacles->positions[i].x * TAILLE_CASE, obstacles->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
}
|
||||
couleur couleurObstacle = CouleurParComposante(0, 0, 255);
|
||||
for (int i = 0; i < obstacles->nombre; i++) {
|
||||
ChoisirCouleurDessin(couleurObstacle);
|
||||
RemplirRectangle(obstacles->positions[i].x * TAILLE_CASE, obstacles->positions[i].y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
}
|
||||
}
|
||||
|
||||
void Graphique() {
|
||||
InitialiserGraphique();
|
||||
CreerFenetre(1, 1, 1240, 940);
|
||||
EcrireTexte(500, 400, "Le jeu va commencer !", 2);
|
||||
Attendre(1000);
|
||||
EffacerEcran(CouleurParComposante(0, 0, 0));
|
||||
InitialiserGraphique();
|
||||
CreerFenetre(1, 1, 1240, 940);
|
||||
EcrireTexte(500, 400, "Le jeu va commencer !", 2);
|
||||
Attendre(1000);
|
||||
EffacerEcran(CouleurParComposante(0, 0, 0));
|
||||
}
|
||||
|
||||
void AffichageBasique() {
|
||||
ChoisirCouleurDessin(CouleurParComposante(111, 255, 94));
|
||||
RemplirRectangle(0, 0, 1140, 760);
|
||||
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);
|
||||
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 LibererMemoire(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
||||
free(serpent->corps);
|
||||
LibererPommes(pommes);
|
||||
free(obstacles->positions);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
int PauseJeu() {
|
||||
while (1) {
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
while (1) {
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
if (touche == XK_space) {
|
||||
return 1; // La barre d'espace a été pressée, reprendre le jeu
|
||||
}
|
||||
@ -110,22 +124,6 @@ int PauseJeu() {
|
||||
Attendre(100);
|
||||
}
|
||||
}
|
||||
|
||||
void LibererMemoire(Serpent* serpent, Pommes* pommes, Obstacles* obstacles) {
|
||||
free(serpent->corps);
|
||||
LibererPommes(pommes);
|
||||
free(obstacles->positions);
|
||||
}
|
||||
|
||||
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 AfficherSerpent(Serpent* serpent) {
|
||||
couleur couleurSerpent = CouleurParComposante(255, 255, 0);
|
||||
ChoisirCouleurDessin(couleurSerpent);
|
||||
@ -152,7 +150,7 @@ void GenererPommes(Pommes* pommes) {
|
||||
pommes->head = nouvellePomme;
|
||||
|
||||
ChoisirCouleurDessin(couleurPommes);
|
||||
RemplirRectangle(nouvellePomme->position.x * TAILLE_CASE, nouvellePomme->position.y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
RemplirArc(nouvellePomme->position.x * TAILLE_CASE, nouvellePomme->position.y * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE,360,360);
|
||||
}
|
||||
}
|
||||
void AjouterPomme(Pommes* pommes, int x, int y) {
|
||||
@ -176,7 +174,7 @@ void GenererNouvellePomme(Pommes* pommes) {
|
||||
AjouterPomme(pommes, newX, newY);
|
||||
|
||||
ChoisirCouleurDessin(couleurPommes);
|
||||
RemplirRectangle(newX * TAILLE_CASE, newY * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
||||
RemplirArc(newX * TAILLE_CASE, newY * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE,360,360);
|
||||
}
|
||||
|
||||
int CollisionAvecPomme(Serpent* serpent, Pommes* pommes, int* score) {
|
||||
@ -311,48 +309,55 @@ int main() {
|
||||
while (perdu != -1) {
|
||||
if (!jeuEnPause) {
|
||||
if (Microsecondes() > suivant) {
|
||||
suivant = Microsecondes() + CYCLE;
|
||||
seconde_actuel = (suivant / 1000000) % 10;
|
||||
if (seconde_actuel != old_seconde) {
|
||||
old_seconde = seconde_actuel;
|
||||
if ((temps[1] + 1) == 60) {
|
||||
temps[0]++;
|
||||
temps[1] = 0;
|
||||
} else {
|
||||
temps[1]++;
|
||||
}
|
||||
suivant = Microsecondes() + CYCLE;
|
||||
seconde_actuel = (suivant / 1000000) % 10;
|
||||
if (seconde_actuel != old_seconde) {
|
||||
old_seconde = seconde_actuel;
|
||||
if ((temps[1] + 1) == 60) {
|
||||
temps[0]++;
|
||||
temps[1] = 0;
|
||||
} else {
|
||||
temps[1]++;
|
||||
}
|
||||
AfficheTemps(temps[0], temps[1]);
|
||||
}
|
||||
}
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
switch (touche) {
|
||||
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:
|
||||
jeuEnPause = 1;
|
||||
break;
|
||||
case XK_Escape:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
switch (touche) {
|
||||
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:
|
||||
jeuEnPause = 1;
|
||||
break;
|
||||
case XK_Escape:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
DeplacerSerpent(&serpent, &direction);
|
||||
perdu = GestionCollision(&serpent, &pommes, &obstacles, perdu, &score);
|
||||
} else {
|
||||
jeuEnPause = PauseJeu();
|
||||
} else {
|
||||
if (ToucheEnAttente()) {
|
||||
int touche = Touche();
|
||||
if (touche == XK_space) {
|
||||
if (touche == XK_Escape) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
jeuEnPause = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LibererMemoire(&serpent, &pommes, &obstacles);
|
||||
LibererMemoire(&serpent, &pommes, &obstacles);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user