Ajout Menu Start, Pause

This commit is contained in:
Vincent TEISSIER 2023-12-19 17:23:38 +01:00
parent 7c0621f36a
commit 114f3dbb39
10 changed files with 146 additions and 82 deletions

18
GUI.c Normal file

@ -0,0 +1,18 @@
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
int Menu()
{
ChargerImage("./images/Menu.png",0,0,0,0,930,710);
}
void Pause()
{
ChargerImage("./images/Pause.png",0,0,0,0,930,710);
}
void Reinitialiser()
{
CopierZone(8,0,0,0,930,710,0,0);
}

8
GUI.h Normal file

@ -0,0 +1,8 @@
#ifndef GUI_H
#define GUI_H
void Pause();
void Reinitialiser();
int AfficherGUI(unsigned char menu);
#endif

@ -3,7 +3,7 @@ CFLAGS = -Wall -Wextra
all: prog
prog: main.o evenement.o
prog: main.o evenement.o GUI.o
$(CC) -o $@ $^ -lgraph
main.o: main.c evenement.h
@ -12,5 +12,8 @@ main.o: main.c evenement.h
evenement.o: evenement.c evenement.h
$(CC) $(CFLAGS) -c $< -o $@
GUI.o: GUI.c GUI.h
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o prog

@ -1,8 +1,9 @@
#include <graph.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "main.h"
#include <time.h>
#include "GUI.h"
void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes) /*Afficher le temps passé et le score*/
{
@ -79,16 +80,10 @@ void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur)
bg=CouleurParComposante(171, 204, 104);
ChoisirCouleurDessin(bg);
if(serpent[longueur-1].x > 900 || serpent[longueur-1].x < 0)
{
serpent[longueur-1].x = 30;
serpent[longueur-1].y = 710; /* Valeurs toujours de couleurs verte */
}
RemplirRectangle(serpent[longueur-1].x,serpent[longueur-1].y,T_PIXEL,T_PIXEL);
for (i = 1; i <= longueur; i++) {
for (i = 1; i <= longueur; i++)
{
int tempX2 = serpent[i].x;
int tempY2 = serpent[i].y;
@ -99,6 +94,7 @@ void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur)
tempY = tempY2;
}
if(direction == 0) /* Direction vers la gauche */
{
serpent[0].x-=T_PIXEL;
@ -119,4 +115,19 @@ void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur)
serpent[0].y+=T_PIXEL;
ChargerImage("./images/SnakePart.png",serpent[0].x,serpent[0].y,0,0,T_PIXEL,T_PIXEL);
}
}
int Serpent(PIXELS *serpent,PIXELS *pastilles,unsigned long *score,int *longueur_serpent,unsigned long int *vitesse,int direction)
{
if(MourrirSerpent(serpent,*longueur_serpent) == 1)
{
return 2;
}
DeplacementSerpent(direction,serpent,*longueur_serpent);
if(MangerPastille(serpent,pastilles,score,*longueur_serpent,vitesse) == 1)
{
*longueur_serpent+=2;
return 1;
}
return 0;
}

@ -15,5 +15,7 @@ int PastilleSurSerpent(PIXELS pastille, PIXELS *serpent, int longueur_serpent);
int MourrirSerpent(PIXELS *serpent, int longueur_serpent);
int Serpent(PIXELS *serpent,PIXELS *pastilles,unsigned long *score,int *longueur_serpent,unsigned long int *vitesse,int direction);
#endif

BIN
images/Menu.png Normal file

Binary file not shown.

After

(image error) Size: 40 KiB

BIN
images/PERDU.png Normal file

Binary file not shown.

After

(image error) Size: 38 KiB

BIN
images/Pause.png Normal file

Binary file not shown.

After

(image error) Size: 41 KiB

166
main.c

@ -4,6 +4,7 @@
#include <time.h>
#include "evenement.h"
#include "main.h"
#include "GUI.h"
#define DELTA 100000L
#define DELTA2 1000000L
@ -110,10 +111,12 @@ int main()
unsigned long score = 0;
unsigned long suivant;
unsigned long suivant2;
int go_on=1;
int game_on=0;
int window_on=1;
int secondes = 0;
int minutes = 0;
unsigned long int vitesse = DELTA;
int valeur_retourne = 0;
size_t longueur_serpent = 10;
size_t longueur_pastilles = PASTILLES;
@ -136,93 +139,112 @@ int main()
CreerFenetre(10,10,W_WINDOW,H_WINDOW); /* Peut être changer cette ligne avec la fonction Maxx et Maxy fournie dans graph.h ??*/
ChoisirTitreFenetre("SNAKE SAE11 IN C");
DessinerScene(pastilles,serpent,longueur_serpent);
while(go_on) /* Lancement du cycle pour les Inputs et le Jeu*/
{
if (ToucheEnAttente() == 1)
while(window_on)
{
ChoisirEcran(0);
Menu();
if (ToucheEnAttente() == 1)
{
switch (Touche())
{
case XK_Up:
direction = 1;
if(direction_davant == 3 && direction == 1)
{
direction = direction_davant;
}
break;
case XK_Down:
direction = 3;
if(direction_davant == 1 && direction == 3)
{
direction = direction_davant; /* Changements de direction du serpent*/
}
break;
case XK_Left:
direction = 0;
if(direction_davant == 2 && direction == 0)
{
direction = direction_davant;
}
break;
case XK_Right:
direction = 2;
if(direction_davant == 0 && direction == 2)
{
direction = direction_davant;
}
break;
case XK_space:
if(pause == 0)
{
pause = 1 ;
} /* Faire pause */
else if(pause == 1)
{
pause = 0;
}
case XK_space:
game_on=1;
DessinerScene(pastilles,serpent,longueur_serpent);
break;
}
}
else
{ if(pause==0)
while(game_on) /* Lancement du cycle pour les Inputs et le Jeu*/
{
if (ToucheEnAttente() == 1)
{
if (Microsecondes()>suivant2)
switch (Touche())
{
secondes++;
if(secondes == 60)
{
minutes++;
secondes = 0;
}
suivant2=Microsecondes()+DELTA2;
case XK_Up:
direction = 1;
if(direction_davant == 3 && direction == 1)
{
direction = direction_davant;
}
break;
case XK_Down:
direction = 3;
if(direction_davant == 1 && direction == 3)
{
direction = direction_davant; /* Changements de direction du serpent*/
}
break;
case XK_Left:
direction = 0;
if(direction_davant == 2 && direction == 0)
{
direction = direction_davant;
}
break;
case XK_Right:
direction = 2;
if(direction_davant == 0 && direction == 2)
{
direction = direction_davant;
}
break;
case XK_space:
if(pause ==0)
{
pause=1;
break;
}
else if(pause == 1)
{
pause=0;
break;
}
}
if (Microsecondes()>suivant)
{
suivant=Microsecondes()+vitesse;
AfficherTimerEtScore(&score,minutes,secondes);
direction_davant = direction; /* Check si le serpent à le droit de changer de direction */
if(MourrirSerpent(serpent,longueur_serpent))
if(pause==1)
{
ChoisirEcran(0);
Pause();
}
}
else
{ if(pause==0)
{
if (Microsecondes()>suivant2)
{
FermerGraphique();
free(serpent);
free(pastilles);
return EXIT_FAILURE;
secondes++;
if(secondes == 60)
{
minutes++;
secondes = 0;
}
suivant2=Microsecondes()+DELTA2;
}
DeplacementSerpent(direction,serpent,longueur_serpent);
if(MangerPastille(serpent,pastilles,&score,longueur_serpent,&vitesse) == 1)
{
longueur_serpent+=2;
serpent = (PIXELS*) realloc(serpent,longueur_serpent * sizeof(PIXELS));
if (Microsecondes()>suivant)
{
suivant=Microsecondes()+vitesse;
AfficherTimerEtScore(&score,minutes,secondes);
direction_davant = direction; /* Check si le serpent à le droit de changer de direction */
valeur_retourne = Serpent(serpent,pastilles,&score,&longueur_serpent,&vitesse,direction);
if(valeur_retourne == 1)
{
serpent = (PIXELS*) realloc(serpent,longueur_serpent * sizeof(PIXELS));
}
else if (valeur_retourne == 2)
{
game_on=0;
}
}
}
}
}
}
FermerGraphique();
return EXIT_SUCCESS;
}

BIN
prog Executable file

Binary file not shown.