2023-11-30 09:54:00 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#define CYCLE 10000L
|
|
|
|
|
2023-11-30 09:59:11 +01:00
|
|
|
/*timer*/
|
|
|
|
int seconde=0;
|
|
|
|
int minute=0;
|
|
|
|
int seconde_actuel=0;
|
|
|
|
int old_seconde=0;
|
2023-11-30 09:54:00 +01:00
|
|
|
char timer[6];
|
|
|
|
unsigned long int suivant;
|
2023-11-30 09:59:11 +01:00
|
|
|
/*serpent*/
|
2023-11-30 09:54:00 +01:00
|
|
|
int serpent;
|
|
|
|
int x = 600;
|
|
|
|
int y = 400;
|
2023-11-30 09:59:11 +01:00
|
|
|
int direction = 4; /*1 : vers le haut , 2 : vers le bas; 3 : vers la gauche, 4 : vers la droite*/
|
2023-11-30 09:54:00 +01:00
|
|
|
int t;
|
2023-11-30 09:59:11 +01:00
|
|
|
int segment=10;
|
|
|
|
int i=0;
|
2023-11-30 09:54:00 +01:00
|
|
|
int pos_x[60];
|
|
|
|
int pos_y[60];
|
|
|
|
int old_x[60];
|
|
|
|
int old_y[60];
|
2023-11-30 09:59:11 +01:00
|
|
|
/*variable pomme*/
|
|
|
|
int p=0;
|
|
|
|
int pp=0;
|
|
|
|
int pomme, pommex[5], pommey[5];
|
2023-12-04 17:01:52 +01:00
|
|
|
int fond, Nbr;
|
|
|
|
char score[4];
|
|
|
|
|
|
|
|
/*fin de jeu*/
|
|
|
|
int go_on=1;
|
2023-11-30 09:59:11 +01:00
|
|
|
|
|
|
|
/*Fonction Pour créer la première scene du jeu*/
|
|
|
|
void DessinerScene(){
|
|
|
|
snprintf(timer,6,"%02d:%02d", minute, seconde);
|
2023-12-04 17:01:52 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(5,130,4));
|
2023-11-30 09:59:11 +01:00
|
|
|
RemplirRectangle(20,20,1160,700);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
EcrireTexte(10,760,timer,2);
|
2023-12-04 17:01:52 +01:00
|
|
|
serpent=ChargerSprite("IMG/serpent.png");
|
|
|
|
fond=ChargerSprite("IMG/fond.png");
|
2023-11-30 09:59:11 +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];
|
|
|
|
}
|
|
|
|
srand(time(NULL));
|
2023-12-04 17:01:52 +01:00
|
|
|
pomme=ChargerSprite("IMG/pomme.png");
|
2023-11-30 09:59:11 +01:00
|
|
|
for (p = 0; p < 5; p++) {
|
|
|
|
pommex[p] = ((rand() % (58)+1)*20);
|
|
|
|
pommey[p] = ((rand() % (35)+1)*20);
|
|
|
|
AfficherSprite(pomme, pommex[p], pommey[p]);
|
2023-11-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
void Terrain(){
|
|
|
|
if (pos_x[0] >1140 || pos_x[0]<=20)
|
|
|
|
go_on=0;
|
|
|
|
if (pos_y[0]<40 || pos_y[0] >=680)
|
|
|
|
go_on=0;
|
2023-11-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
/*Fonction pour calculer le temps*/
|
|
|
|
void Timer(){
|
|
|
|
if(Microsecondes()> suivant){
|
|
|
|
suivant = Microsecondes()+CYCLE;
|
|
|
|
seconde_actuel = (suivant/1000000)%10;
|
|
|
|
if(seconde_actuel !=old_seconde){
|
|
|
|
old_seconde = seconde_actuel;
|
|
|
|
if(seconde == 59){
|
|
|
|
minute=minute+1;
|
|
|
|
seconde=0;
|
|
|
|
Update_Timer();
|
|
|
|
}else{
|
|
|
|
seconde = seconde+1;
|
|
|
|
Update_Timer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
/*Fonction pour mettre à jour unuquement le timer*/
|
|
|
|
void Update_Timer(void){
|
|
|
|
snprintf(timer,6,"%02d:%02d", minute, seconde);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
|
|
|
|
RemplirRectangle(0,700,1200,800);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
EcrireTexte(10,750,timer,2);
|
2023-11-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
void Score(){
|
|
|
|
Nbr=(segment-10)*10;
|
|
|
|
snprintf(score,4,"%04d0", Nbr);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
|
|
|
|
RemplirRectangle(1100,730,1200,800);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
EcrireTexte(1000,750,"Score :",2);
|
|
|
|
EcrireTexte(1100,750,score,2);
|
2023-11-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:59:11 +01:00
|
|
|
/*Input Serpent*/
|
|
|
|
void Controle(){
|
2023-12-04 17:01:52 +01:00
|
|
|
while(ToucheEnAttente()){
|
|
|
|
t = Touche();
|
|
|
|
switch(t){
|
|
|
|
case XK_Left :
|
|
|
|
direction=3;
|
|
|
|
break;
|
|
|
|
case XK_Right:
|
|
|
|
direction=4;
|
|
|
|
break;
|
|
|
|
case XK_Up:
|
|
|
|
direction=1;
|
|
|
|
break;
|
|
|
|
case XK_Down:
|
|
|
|
direction=2;
|
|
|
|
break;
|
|
|
|
case XK_Escape:
|
|
|
|
go_on=0;
|
|
|
|
break;
|
|
|
|
case XK_p:
|
|
|
|
direction=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-11-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:59:11 +01:00
|
|
|
/*Avancement automatique du serpent en fonction de la direction*/
|
2023-12-04 17:01:52 +01:00
|
|
|
void Serpent(){
|
|
|
|
if (direction == 1){
|
|
|
|
pos_y[0]=old_y[0]-20;
|
|
|
|
}
|
|
|
|
if (direction == 2){
|
|
|
|
pos_y[0]=old_y[0]+20;
|
|
|
|
}
|
|
|
|
if (direction == 3){
|
|
|
|
pos_x[0]=old_x[0]-20;
|
|
|
|
}
|
|
|
|
if (direction == 4){
|
|
|
|
pos_x[0]=old_x[0]+20;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(p=0; p<5; p++){
|
|
|
|
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
|
|
|
|
segment+=2;
|
|
|
|
pommex[p] = ((rand() % (58)+1)*20);
|
|
|
|
pommey[p] = ((rand() % (35)+1)*20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Update_Serpent();
|
|
|
|
Terrain();
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collision(){
|
|
|
|
if(seconde!=0 || minute!=0){
|
|
|
|
for(i=1; i<segment;i++){
|
|
|
|
if (pos_x[0]==pos_x[i] && pos_y[0]==pos_y[i]){
|
|
|
|
go_on=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*fonction pour mettre à jour la position du serpent*/
|
|
|
|
void Update_Serpent(void){
|
|
|
|
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
|
|
|
|
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-30 09:54:00 +01:00
|
|
|
}
|
|
|
|
|
2023-11-30 09:59:11 +01:00
|
|
|
/*Apparition aléatoire des pommes*/
|
|
|
|
void Pomme(){
|
2023-11-30 09:54:00 +01:00
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
for (pp = 0; pp < 5; ++pp) {
|
|
|
|
AfficherSprite(pomme, pommex[pp], pommey[pp]);
|
|
|
|
}
|
|
|
|
|
2023-11-30 09:59:11 +01:00
|
|
|
}
|
2023-11-30 09:54:00 +01:00
|
|
|
|
2023-11-30 09:59:11 +01:00
|
|
|
/*Fonction Principale*/
|
|
|
|
int main(){
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
/* paramétrage de la fenêtre + chargement première scène */
|
2023-11-30 09:54:00 +01:00
|
|
|
InitialiserGraphique();
|
2023-11-30 09:59:11 +01:00
|
|
|
CreerFenetre(350,100,1200,800);
|
|
|
|
EffacerEcran(CouleurParComposante(0,0,0));
|
2023-12-04 17:01:52 +01:00
|
|
|
suivant=Microsecondes()+CYCLE;
|
2023-11-30 09:59:11 +01:00
|
|
|
old_seconde=(suivant/1000000)%10;
|
2023-11-30 09:54:00 +01:00
|
|
|
DessinerScene();
|
2023-11-30 09:59:11 +01:00
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
/*Boucle Principale du Programme*/
|
2023-11-30 09:59:11 +01:00
|
|
|
while(go_on){
|
|
|
|
Timer();
|
2023-12-04 17:01:52 +01:00
|
|
|
Score();
|
|
|
|
Collision();
|
2023-11-30 09:59:11 +01:00
|
|
|
Controle();
|
|
|
|
Serpent();
|
|
|
|
Pomme();
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
/* fermeture de la fenêtre si ECHAP pressé*/
|
|
|
|
usleep(3000000);
|
|
|
|
EcrireTexte(000,000,"Game Over", 000);
|
2023-11-30 09:54:00 +01:00
|
|
|
FermerGraphique();
|
2023-11-30 09:59:11 +01:00
|
|
|
return EXIT_SUCCESS;
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|