2023-11-22 13:36:41 +01:00
|
|
|
#include<stdlib.h>
|
2023-11-22 19:53:24 +01:00
|
|
|
#include <stdio.h>
|
2023-11-22 13:36:41 +01:00
|
|
|
#include<graph.h>
|
2023-11-24 14:58:27 +01:00
|
|
|
#define CYCLE 10000L
|
2023-11-22 21:31:33 +01:00
|
|
|
|
2023-11-22 21:51:51 +01:00
|
|
|
/*variables*/
|
2023-11-22 21:31:33 +01:00
|
|
|
int seconde=0;
|
|
|
|
int minute=0;
|
2023-11-24 14:58:27 +01:00
|
|
|
int seconde_actuel=0;
|
|
|
|
int old_seconde=0;
|
|
|
|
char timer[6];
|
|
|
|
unsigned long int suivant;
|
2023-11-22 21:51:51 +01:00
|
|
|
int go_on=1;
|
2023-11-23 17:19:35 +01:00
|
|
|
int serpent;
|
2023-11-24 15:20:01 +01:00
|
|
|
int x = 600;
|
|
|
|
int y = 400;
|
2023-11-23 17:19:35 +01:00
|
|
|
int direction = 4; /*1 : vers le haut , 2 : vers le bas; 3 : vers la gauche, 4 : vers la droite*/
|
2023-11-23 18:33:14 +01:00
|
|
|
int t;
|
2023-11-23 17:19:35 +01:00
|
|
|
|
2023-11-24 14:58:27 +01:00
|
|
|
/*Fonction Pour créer la première scene du jeu*/
|
2023-11-22 21:31:33 +01:00
|
|
|
void DessinerScene(){
|
2023-11-24 14:58:27 +01:00
|
|
|
snprintf(timer,6,"%02d:%02d", minute, seconde);
|
2023-11-22 21:31:33 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(141,199,63));
|
2023-11-23 17:19:35 +01:00
|
|
|
RemplirRectangle(20,20,1160,700);
|
2023-11-22 21:31:33 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
EcrireTexte(10,760,timer,2);
|
2023-11-23 17:19:35 +01:00
|
|
|
serpent=ChargerSprite("serpent.png");
|
|
|
|
AfficherSprite(serpent, x, y);
|
2023-11-22 21:31:33 +01:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:58:27 +01:00
|
|
|
/*Fonction pour mettre à jour unuquement le timer*/
|
|
|
|
void Update_Timer(){
|
|
|
|
snprintf(timer,6,"%02d:%02d", minute, seconde);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
|
2023-11-24 15:20:01 +01:00
|
|
|
RemplirRectangle(0,700,1200,800);
|
2023-11-24 14:58:27 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
EcrireTexte(10,760,timer,2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update_Serpent(){
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(141,199,63));
|
|
|
|
RemplirRectangle(20,20,1160,700);
|
|
|
|
AfficherSprite(serpent, x, y);
|
2023-11-24 15:31:25 +01:00
|
|
|
printf("%d %d\n", x, y);
|
2023-11-24 14:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Fonction pour calculer le temps*/
|
2023-11-22 21:31:33 +01:00
|
|
|
void Timer(){
|
2023-11-24 14:58:27 +01:00
|
|
|
if(Microsecondes()> suivant){
|
|
|
|
suivant = Microsecondes()+CYCLE;
|
|
|
|
seconde_actuel = (suivant/1000000)%10;
|
|
|
|
if(seconde_actuel !=old_seconde){
|
|
|
|
old_seconde = seconde_actuel;
|
|
|
|
if(seconde == 60){
|
|
|
|
minute=minute+1;
|
|
|
|
seconde=0;
|
|
|
|
Update_Timer();
|
|
|
|
}else{
|
|
|
|
seconde = seconde+1;
|
|
|
|
Update_Timer();
|
|
|
|
}
|
|
|
|
}
|
2023-11-22 21:31:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:58:27 +01:00
|
|
|
/*Input Serpent*/
|
2023-11-23 18:33:14 +01:00
|
|
|
void Controle(){
|
2023-11-23 21:06:18 +01:00
|
|
|
while(ToucheEnAttente()){
|
|
|
|
t = Touche();
|
|
|
|
switch(t){
|
2023-11-23 18:33:14 +01:00
|
|
|
case XK_Left :
|
|
|
|
direction=3;
|
|
|
|
break;
|
|
|
|
case XK_Right:
|
|
|
|
direction=4;
|
|
|
|
break;
|
|
|
|
case XK_Up:
|
|
|
|
direction=1;
|
|
|
|
break;
|
|
|
|
case XK_Down:
|
|
|
|
direction=2;
|
2023-11-24 15:20:01 +01:00
|
|
|
break;
|
|
|
|
case XK_Escape:
|
|
|
|
go_on=0;
|
2023-11-23 21:06:18 +01:00
|
|
|
break;
|
|
|
|
break;
|
2023-11-23 18:33:14 +01:00
|
|
|
}
|
2023-11-23 21:06:18 +01:00
|
|
|
}
|
2023-11-23 18:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-24 14:58:27 +01:00
|
|
|
/*Avancement automatique du serpent en fonction de la direction*/
|
2023-11-23 21:06:18 +01:00
|
|
|
void Serpent(){
|
2023-11-23 17:19:35 +01:00
|
|
|
if (direction == 1){
|
2023-11-23 21:06:18 +01:00
|
|
|
y=y-20;
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
|
|
|
if (direction == 2){
|
2023-11-23 21:06:18 +01:00
|
|
|
y=y+20;
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
|
|
|
if (direction == 3){
|
|
|
|
x=x-20;
|
|
|
|
}
|
|
|
|
if (direction == 4){
|
|
|
|
x=x+20;
|
|
|
|
}
|
2023-11-24 14:58:27 +01:00
|
|
|
Update_Serpent();
|
|
|
|
sleep(1);
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
|
|
|
|
2023-11-22 21:31:33 +01:00
|
|
|
/*Fonction Principale*/
|
2023-11-22 13:36:41 +01:00
|
|
|
int main(){
|
|
|
|
|
2023-11-24 14:58:27 +01:00
|
|
|
/* paramétrage de la fenêtre + charment première scène */
|
2023-11-22 13:36:41 +01:00
|
|
|
InitialiserGraphique();
|
2023-11-22 19:53:24 +01:00
|
|
|
CreerFenetre(350,100,1200,800);
|
2023-11-24 14:58:27 +01:00
|
|
|
EffacerEcran(CouleurParComposante(0,0,0));
|
|
|
|
suivant = Microsecondes()+CYCLE;
|
|
|
|
old_seconde=(suivant/1000000)%10;
|
2023-11-22 21:31:33 +01:00
|
|
|
DessinerScene();
|
2023-11-23 18:33:14 +01:00
|
|
|
|
2023-11-22 21:31:33 +01:00
|
|
|
/*Boucle Principale du Programme*/
|
2023-11-22 21:51:51 +01:00
|
|
|
while(go_on){
|
|
|
|
Timer();
|
2023-11-23 18:33:14 +01:00
|
|
|
Controle();
|
2023-11-24 14:58:27 +01:00
|
|
|
Serpent();
|
2023-11-22 19:53:24 +01:00
|
|
|
}
|
2023-11-24 14:58:27 +01:00
|
|
|
|
|
|
|
/* fermeture de la fenêtre si ECHAP pressé*/
|
2023-11-22 13:36:41 +01:00
|
|
|
FermerGraphique();
|
2023-11-22 13:41:41 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|