89 lines
1.7 KiB
C
89 lines
1.7 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <graph.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include "fonction.h"
|
|
|
|
/*Input Serpent*/
|
|
void Controle(){
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*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);
|
|
pommey[p] = ((rand() % (34)+1)*20);
|
|
}
|
|
}
|
|
|
|
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]);
|
|
}
|
|
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
|
|
RemplirRectangle(0,0,20,20);
|
|
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];
|
|
}
|
|
}
|
|
|
|
/*Fonction pour détécter si le serpent se touche lui même*/
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|