2023-12-04 17:01:52 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
2023-12-07 14:02:18 +01:00
|
|
|
#include "fonction.h"
|
2023-12-04 17:01:52 +01:00
|
|
|
|
|
|
|
/*Input Serpent*/
|
2023-12-19 11:52:06 +01:00
|
|
|
int Controle(int direction){
|
2023-12-18 13:36:17 +01:00
|
|
|
int t;
|
2023-12-04 17:01:52 +01:00
|
|
|
while(ToucheEnAttente()){
|
|
|
|
t = Touche();
|
2023-12-10 17:09:46 +01:00
|
|
|
switch(t){
|
2023-12-04 17:01:52 +01:00
|
|
|
case XK_Left :
|
2023-12-10 17:09:46 +01:00
|
|
|
if (direction != 4){
|
|
|
|
direction=3;
|
|
|
|
}
|
2023-12-18 17:05:44 +01:00
|
|
|
break;
|
2023-12-04 17:01:52 +01:00
|
|
|
case XK_Right:
|
2023-12-10 17:09:46 +01:00
|
|
|
if (direction != 3){
|
|
|
|
direction=4;
|
|
|
|
}
|
2023-12-18 17:05:44 +01:00
|
|
|
break;
|
2023-12-04 17:01:52 +01:00
|
|
|
case XK_Up:
|
2023-12-10 17:09:46 +01:00
|
|
|
if(direction !=2){
|
|
|
|
direction=1;
|
|
|
|
}
|
2023-12-18 17:05:44 +01:00
|
|
|
break;
|
2023-12-04 17:01:52 +01:00
|
|
|
case XK_Down:
|
2023-12-10 17:09:46 +01:00
|
|
|
if (direction !=1){
|
|
|
|
direction=2;
|
|
|
|
}
|
2023-12-18 17:05:44 +01:00
|
|
|
break;
|
2023-12-17 12:58:38 +01:00
|
|
|
case XK_Escape:
|
|
|
|
go_on=0;
|
|
|
|
go_menu=0;
|
2023-12-18 17:05:44 +01:00
|
|
|
break;
|
2023-12-17 12:58:38 +01:00
|
|
|
case XK_space:
|
|
|
|
if (pause==0){
|
|
|
|
pause=1;
|
|
|
|
}else{
|
|
|
|
pause=0;
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
EcrireTexte(500,750,"Jeu en Pause",2);
|
|
|
|
}
|
2023-12-18 17:05:44 +01:00
|
|
|
break;
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 11:52:06 +01:00
|
|
|
return direction;
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*Avancement automatique du serpent en fonction de la direction*/
|
2023-12-19 15:44:28 +01:00
|
|
|
void Serpent(int direction, int segment, int tete_up, int tete_down, int tete_left, int tete_right, int serpent, int fond){
|
2023-12-04 17:01:52 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-12-19 15:44:28 +01:00
|
|
|
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
|
|
|
|
Update_Serpent(direction, segment, tete_up, tete_down, tete_right, tete_left, serpent);
|
2023-12-19 12:46:10 +01:00
|
|
|
Terrain();
|
|
|
|
}
|
2023-12-04 17:01:52 +01:00
|
|
|
|
2023-12-19 12:46:10 +01:00
|
|
|
/*augmentation de la vitesse en fonction des pommes mangées*/
|
|
|
|
long int Update_Vitesse(long int vitesse){
|
|
|
|
int p=0;
|
2023-12-04 17:01:52 +01:00
|
|
|
for(p=0; p<5; p++){
|
|
|
|
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
|
2023-12-11 14:42:47 +01:00
|
|
|
if (vitesse>20000){
|
|
|
|
vitesse=vitesse-1500;
|
2023-12-19 12:46:10 +01:00
|
|
|
}
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 12:46:10 +01:00
|
|
|
return vitesse;
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 13:09:01 +01:00
|
|
|
int Update_Segment(int segment){
|
|
|
|
int p=0;
|
|
|
|
for(p=0; p<5; p++){
|
|
|
|
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
|
|
|
|
segment=segment+2;
|
|
|
|
pommex[p] = ((rand() % (58)+1)*20);
|
|
|
|
pommey[p] = ((rand() % (34)+1)*20);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return segment;
|
|
|
|
}
|
|
|
|
|
2023-12-04 17:01:52 +01:00
|
|
|
/*fonction pour mettre à jour la position du serpent*/
|
2023-12-19 15:44:28 +01:00
|
|
|
void Update_Serpent(int direction, int segment, int tete_up, int tete_down, int tete_left, int tete_right, int serpent){
|
2023-12-18 13:36:17 +01:00
|
|
|
int i=1;
|
2023-12-19 15:44:28 +01:00
|
|
|
|
2023-12-10 17:55:25 +01:00
|
|
|
/*affichage de la tete en fonction de la direction du serpent*/
|
|
|
|
if (direction==1){
|
|
|
|
AfficherSprite(tete_up, pos_x[0], pos_y[0]);
|
|
|
|
}
|
|
|
|
if (direction==2){
|
|
|
|
AfficherSprite(tete_down, pos_x[0], pos_y[0]);
|
|
|
|
}
|
|
|
|
if (direction==3){
|
|
|
|
AfficherSprite(tete_left, pos_x[0], pos_y[0]);
|
|
|
|
}
|
|
|
|
if (direction==4){
|
|
|
|
AfficherSprite(tete_right, pos_x[0], pos_y[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*affichage du reste du corps*/
|
2023-12-04 17:01:52 +01:00
|
|
|
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]);
|
|
|
|
}
|
2023-12-10 17:55:25 +01:00
|
|
|
|
2023-12-10 12:27:36 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
|
|
|
|
RemplirRectangle(0,0,20,20);
|
2023-12-10 17:55:25 +01:00
|
|
|
|
|
|
|
/*Décalage des positions du serpent*/
|
2023-12-04 17:01:52 +01:00
|
|
|
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-12-10 12:27:36 +01:00
|
|
|
/*Fonction pour détécter si le serpent se touche lui même*/
|
2023-12-19 13:09:01 +01:00
|
|
|
void Collision(int segment){
|
2023-12-18 13:36:17 +01:00
|
|
|
int i=1;
|
2023-12-21 11:33:41 +01:00
|
|
|
for(i=1; i<segment;i++){
|
|
|
|
if (pos_x[0]==pos_x[i] && pos_y[0]==pos_y[i]){
|
|
|
|
go_on=0;
|
2023-12-07 14:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|