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-24 17:09:15 +01:00
|
|
|
|
|
|
|
/*timer*/
|
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-24 17:09:15 +01:00
|
|
|
|
|
|
|
/*fin de jeu*/
|
2023-11-22 21:51:51 +01:00
|
|
|
int go_on=1;
|
2023-11-24 17:09:15 +01:00
|
|
|
|
|
|
|
/*serpent*/
|
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 17:09:15 +01:00
|
|
|
int segment=5;
|
|
|
|
int i=0;
|
|
|
|
int pos_x[5];
|
|
|
|
int pos_y[5];
|
|
|
|
int old_x[5];
|
|
|
|
int old_y[5];
|
|
|
|
|
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");
|
2023-11-24 17:09:15 +01:00
|
|
|
|
|
|
|
for (i=0 ; i<segment ; i++){
|
|
|
|
AfficherSprite(serpent, x-(i*20), y);
|
|
|
|
pos_x[i]=x-(i*20);
|
|
|
|
pos_y[i]=y;
|
|
|
|
old_y[i]=pos_y[i];
|
|
|
|
old_x[i]=pos_x[i];
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-11-24 16:08:20 +01:00
|
|
|
/*fonction pour mettre à jour la position du serpent*/
|
2023-11-24 14:58:27 +01:00
|
|
|
void Update_Serpent(){
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(141,199,63));
|
|
|
|
RemplirRectangle(20,20,1160,700);
|
2023-11-24 17:09:15 +01:00
|
|
|
AfficherSprite(serpent, pos_x[0], pos_y[0]);
|
|
|
|
for (i=1 ; i<segment ; i++){
|
|
|
|
pos_x[i]=old_x[i-1];
|
|
|
|
pos_y[i]=old_y[i-1];
|
|
|
|
AfficherSprite(serpent, pos_x[i], pos_y[i]);
|
|
|
|
}
|
|
|
|
old_x[0]=pos_x[0];
|
|
|
|
old_y[0]=pos_y[0];
|
|
|
|
for (i=1 ; i<segment ; i++){
|
|
|
|
old_x[i]=pos_x[i];
|
|
|
|
old_y[i]=pos_y[i];
|
|
|
|
}
|
2023-11-24 16:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Terrain(){
|
2023-11-24 17:09:15 +01:00
|
|
|
if (pos_x[0] >1180 || pos_x[0]<20)
|
2023-11-24 16:08:20 +01:00
|
|
|
go_on=0;
|
2023-11-24 17:09:15 +01:00
|
|
|
if (pos_y[0]<0 || pos_y[0] >700)
|
2023-11-24 16:08:20 +01:00
|
|
|
go_on=0;
|
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;
|
2023-11-24 16:08:20 +01:00
|
|
|
if(seconde == 59){
|
2023-11-24 14:58:27 +01:00
|
|
|
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-24 17:09:15 +01:00
|
|
|
pos_y[0]=old_y[0]-20;
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
|
|
|
if (direction == 2){
|
2023-11-24 17:09:15 +01:00
|
|
|
pos_y[0]=old_y[0]+20;
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
|
|
|
if (direction == 3){
|
2023-11-24 17:09:15 +01:00
|
|
|
pos_x[0]=old_x[0]-20;
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
|
|
|
if (direction == 4){
|
2023-11-24 17:09:15 +01:00
|
|
|
pos_x[0]=old_x[0]+20;
|
2023-11-23 17:19:35 +01:00
|
|
|
}
|
2023-11-24 14:58:27 +01:00
|
|
|
Update_Serpent();
|
2023-11-24 16:08:20 +01:00
|
|
|
Terrain();
|
2023-11-24 14:58:27 +01:00
|
|
|
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 16:08:20 +01:00
|
|
|
/* paramétrage de la fenêtre + chargement 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;
|
|
|
|
}
|