Timer,arret du jeu,image

This commit is contained in:
2023-12-12 12:24:31 +01:00
parent a39ed7f18d
commit 31cab42c8b
2 changed files with 217 additions and 418 deletions

View File

@@ -1,277 +0,0 @@
#include <stdlib.h>
#include <graph.h>
#include <time.h>
#include <stdio.h>
#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
#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;
}
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())
{
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<nbr_pastille;i++){
ChoisirCouleurDessin(CouleurParNom("red"));
RemplirArc(pomme[i].x, pomme[i].y, TAILLE_CASE, TAILLE_CASE,0,360);
}
}
}
deplacerSerpent(&longueurSerpent, serpent, direction);
for (i = 0; i < NBR_PASTILLE_INITIAL; i++)
{
/*Mise a zero du score*/
ChoisirCouleurDessin(CouleurParNom("blue"));
sprintf(texte, "Score : %3d", score);
EcrireTexte(825, 685, texte, 2);
if (serpent[i].x == pomme[i].x && serpent[i].y == pomme[i].y)
{
longueurSerpent += 1;
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);
/*+1 au score a chaque pomme*/
score = score + 1;
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(825, 655, 150, 40);
ChoisirCouleurDessin(CouleurParNom("blue"));
sprintf(texte, "Score : %3d", score);
EcrireTexte(825, 685, texte, 2);
}
}
printf("La longueur d'un serpent est de : %d\n", longueurSerpent);
/*Delimitation du terrain de jeu */
if (serpent[0].x < 5 * TAILLE_CASE || serpent[0].x >= 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);
}
}
free(pomme);
free(serpent);
Touche();
FermerGraphique();
return EXIT_SUCCESS;
}

320
main.c
View File

@@ -1,89 +1,131 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <time.h>
#define CYCLE 10000L
#include <stdio.h>
#define LARGEUR_FENETRE 1050
#define HAUTEUR_FENETRE 750
#define TAILLE_CASE 15
#define NBR_COLONNE 60
#define NBR_LIGNE 40
#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
#define CYCLE 1000000L
#define MAX_LONGUEUR 100
typedef struct {
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) {
void afficherSerpent(Segment serpent[], int taille)
{
int i;
couleur couleurSerpent = CouleurParNom("yellow");
ChoisirCouleurDessin(couleurSerpent);
for (i = 0; i < 10; i++) {
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) {
int x,y,i;
if(z>0){
couleur rouge = CouleurParNom("red");
x = rand() % 60 * TAILLE_CASE + 5*15;
y = rand() % 40 * TAILLE_CASE + 5*15;
ChoisirCouleurDessin(rouge);
RemplirRectangle(x, y, TAILLE_CASE, TAILLE_CASE);
afficherPastilleAleatoire(z-1);
}
}
/*Fonction initialisant la fenetre de jeux en vert*/
void EcranJeu(){
int case_colone,case_ligne;
couleur v;
v = CouleurParNom("light green");
for (case_ligne = 2; case_ligne < NBR_LIGNE+5; case_ligne++) {
for (case_colone = 5; case_colone < NBR_COLONNE+5; case_colone++) {
ChoisirCouleurDessin(v);
RemplirRectangle(case_colone * TAILLE_CASE, case_ligne * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
}
}
}
void deplacerSerpent(int longueurSerpent, Segment serpent[], int direction){
int i;
couleur c;
couleur v;
couleur s;
c = CouleurParNom("black");
s = CouleurParNom("yellow");
v = CouleurParNom("light green");
for(i=0;i<longueurSerpent;i++){
ChoisirCouleurDessin(v);
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);
}
}
for(i=longueurSerpent-1;i>0;i--){
serpent[i] = serpent[i-1];
/*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){
switch (direction)
{
case 1:
serpent[0].x += TAILLE_CASE;
break;
@@ -96,80 +138,65 @@ void deplacerSerpent(int longueurSerpent, Segment serpent[], int direction){
case 4:
serpent[0].y += TAILLE_CASE;
break;
}
for (i = 0; i < *longueurSerpent; i++)
{
for(i=0;i<longueurSerpent;i++){
ChoisirCouleurDessin(s);
ChoisirCouleurDessin(CouleurParNom("yellow"));
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
}
Attendre(100);
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));
unsigned long Microsecondes() {
return 0;
}
void ConvertirMicrosecondes(unsigned long microsecondes, int *minutes, int *secondes) {
*minutes = microsecondes / 60000000;
*secondes = (microsecondes % 60000000) / 1000000;
}
void AttendreMicrosecondes(unsigned long microsecondes) {
clock_t debut = clock();
while ((clock() - debut) * 1000000 / CLOCKS_PER_SEC < microsecondes);
}
int main() {
int touche,i, j,x=0,direction=1,longueurSerpent=10;
Segment serpent[10];
couleur c;
couleur v;
couleur s;
if (pomme == NULL || serpent == NULL)
{
printf("Erreur d'allocation de mémoire.\n");
return EXIT_FAILURE;
}
InitialiserGraphique();
CreerFenetre(400, 250, LARGEUR_FENETRE, HAUTEUR_FENETRE);
CreerFenetre(350, 100, LARGEUR_FENETRE, HAUTEUR_FENETRE);
srand(time(NULL));
c = CouleurParNom("black");
s = CouleurParNom("yellow");
v = CouleurParNom("green");
EffacerEcran(c);
EffacerEcran(CouleurParNom("black"));
EcranJeu();
afficherPastilleAleatoire(5);
afficherPastilleAleatoire(NBR_PASTILLE_INITIAL, pomme);
afficherSerpent(serpent, longueurSerpent);
const unsigned long CYCLE = 1000000L;
unsigned long suivant = Microsecondes() + CYCLE;
while (1) {
if (Microsecondes() > suivant) {
int minutes, secondes;
ConvertirMicrosecondes(Microsecondes(), &minutes, &secondes);
printf("Temps écoulé : %02d:%02d\n", minutes, secondes);
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);
}
AttendreMicrosecondes(1000);
}
while(1){
int touche;
if(ToucheEnAttente()){
touche = Touche();
switch(touche){
/*Temps ecoulé fin */
if (ToucheEnAttente())
{
switch (Touche())
{
case XK_Right:
direction = 1;
break;
@@ -182,19 +209,68 @@ int main() {
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<nbr_pastille;i++){
ChoisirCouleurDessin(CouleurParNom("red"));
RemplirArc(pomme[i].x, pomme[i].y, TAILLE_CASE, TAILLE_CASE,0,360);
}
}
}
deplacerSerpent(longueurSerpent, serpent, direction);
deplacerSerpent(&longueurSerpent, serpent, direction);
for (i = 0; i < NBR_PASTILLE_INITIAL; i++)
{
/*Mise a zero du score*/
ChoisirCouleurDessin(CouleurParNom("blue"));
sprintf(texte, "Score : %3d", score);
EcrireTexte(825, 685, texte, 2);
if (serpent[i].x == pomme[i].x && serpent[i].y == pomme[i].y)
{
longueurSerpent += 1;
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);
/*+1 au score a chaque pomme*/
score = score + 1;
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(825, 655, 150, 40);
ChoisirCouleurDessin(CouleurParNom("blue"));
sprintf(texte, "Score : %3d", score);
EcrireTexte(825, 685, texte, 2);
}
}
printf("La longueur d'un serpent est de : %d\n", longueurSerpent);
/*Delimitation du terrain de jeu */
if (serpent[0].x < 5 * TAILLE_CASE || serpent[0].x >= 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);
}
}
free(pomme);
free(serpent);
Touche();
FermerGraphique();
return EXIT_SUCCESS;