2023-11-23 11:08:18 +01:00
# include <stdio.h>
# include <stdlib.h>
# include <graph.h>
2023-12-01 15:55:06 +01:00
# include <time.h>
2023-12-01 15:35:48 +01:00
# define CYCLE 10000L
2023-12-01 15:05:42 +01:00
# define LARGEUR_FENETRE 1050
# define HAUTEUR_FENETRE 750
2023-11-23 11:08:18 +01:00
# define TAILLE_CASE 15
# define NBR_COLONNE 60
2023-12-01 15:05:42 +01:00
# define NBR_LIGNE 40
typedef struct {
int x ;
int y ;
} Segment ;
/*Fonction permettant d'afficher le serpent*/
void afficherSerpent ( Segment serpent [ ] , int taille ) {
int i ;
couleur couleurSerpent = CouleurParNom ( " yellow " ) ;
ChoisirCouleurDessin ( couleurSerpent ) ;
for ( i = 0 ; i < 10 ; 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 ) ;
RemplirRectangle ( serpent [ i ] . x , serpent [ i ] . y , TAILLE_CASE , TAILLE_CASE ) ;
}
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 ( s ) ;
RemplirRectangle ( serpent [ i ] . x , serpent [ i ] . y , TAILLE_CASE , TAILLE_CASE ) ;
}
Attendre ( 100 ) ;
}
2023-12-01 15:35:48 +01:00
unsigned long Microsecondes ( ) {
2023-12-01 15:55:06 +01:00
return 0 ;
2023-12-01 15:05:42 +01:00
}
2023-11-23 11:08:18 +01:00
2023-12-01 15:35:48 +01:00
void ConvertirMicrosecondes ( unsigned long microsecondes , int * minutes , int * secondes ) {
2023-12-01 15:55:06 +01:00
* minutes = microsecondes / 60000000 ;
* secondes = ( microsecondes % 60000000 ) / 1000000 ;
}
void AttendreMicrosecondes ( unsigned long microsecondes ) {
clock_t debut = clock ( ) ;
while ( ( clock ( ) - debut ) * 1000000 / CLOCKS_PER_SEC < microsecondes ) ;
2023-12-01 15:35:48 +01:00
}
2023-11-23 11:08:18 +01:00
int main ( ) {
2023-12-01 15:05:42 +01:00
int touche , i , j , x = 0 , direction = 1 , longueurSerpent = 10 ;
Segment serpent [ 10 ] ;
2023-11-23 11:08:18 +01:00
couleur c ;
2023-12-01 15:05:42 +01:00
couleur v ;
couleur s ;
2023-11-23 11:08:18 +01:00
InitialiserGraphique ( ) ;
2023-12-01 15:05:42 +01:00
CreerFenetre ( 400 , 250 , LARGEUR_FENETRE , HAUTEUR_FENETRE ) ;
srand ( time ( NULL ) ) ;
c = CouleurParNom ( " black " ) ;
s = CouleurParNom ( " yellow " ) ;
v = CouleurParNom ( " green " ) ;
2023-11-23 11:08:18 +01:00
EffacerEcran ( c ) ;
2023-12-01 15:05:42 +01:00
EcranJeu ( ) ;
afficherPastilleAleatoire ( 5 ) ;
afficherSerpent ( serpent , longueurSerpent ) ;
2023-12-01 15:55:06 +01:00
const unsigned long CYCLE = 1000000L ;
2023-12-01 15:35:48 +01:00
unsigned long suivant = Microsecondes ( ) + CYCLE ;
2023-12-01 15:55:06 +01:00
while ( 1 ) {
2023-12-01 15:35:48 +01:00
if ( Microsecondes ( ) > suivant ) {
2023-12-01 15:55:06 +01:00
2023-12-01 15:35:48 +01:00
int minutes , secondes ;
ConvertirMicrosecondes ( Microsecondes ( ) , & minutes , & secondes ) ;
printf ( " Temps <20> coul<75> : %02d:%02d \n " , minutes , secondes ) ;
suivant = Microsecondes ( ) + CYCLE ;
}
2023-12-01 15:55:06 +01:00
AttendreMicrosecondes ( 1000 ) ;
2023-12-01 15:35:48 +01:00
}
2023-12-01 15:55:06 +01:00
2023-12-01 15:35:48 +01:00
2023-12-01 15:05:42 +01:00
while ( 1 ) {
int touche ;
if ( ToucheEnAttente ( ) ) {
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 ;
}
2023-11-23 11:08:18 +01:00
}
2023-12-01 15:05:42 +01:00
deplacerSerpent ( longueurSerpent , serpent , direction ) ;
}
2023-11-23 11:08:18 +01:00
Touche ( ) ;
FermerGraphique ( ) ;
return EXIT_SUCCESS ;
2023-12-01 15:05:42 +01:00
}