2023-12-04 17:01:52 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2023-12-07 14:02:18 +01:00
|
|
|
#include "fonction.h"
|
2023-12-04 17:01:52 +01:00
|
|
|
|
|
|
|
/*Input Serpent*/
|
|
|
|
void Controle(){
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Avancement automatique du serpent en fonction de la direction*/
|
|
|
|
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);
|
2023-12-10 12:09:56 +01:00
|
|
|
pommey[p] = ((rand() % (34)+1)*20);
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Update_Serpent();
|
|
|
|
Terrain();
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*fonction pour mettre à jour la position du serpent*/
|
|
|
|
void Update_Serpent(){
|
|
|
|
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]);
|
|
|
|
}
|
2023-12-10 12:27:36 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
|
|
|
|
RemplirRectangle(0,0,20,20);
|
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-07 14:02:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|