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*/
|
|
|
|
void Controle(){
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Avancement automatique du serpent en fonction de la direction*/
|
2023-12-18 13:36:17 +01:00
|
|
|
void Serpent(){
|
|
|
|
int p=0;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11 14:42:47 +01:00
|
|
|
if (vitesse>20000){
|
|
|
|
vitesse=vitesse-1500;
|
|
|
|
}
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Update_Serpent();
|
|
|
|
Terrain();
|
2023-12-12 15:00:17 +01:00
|
|
|
Attendre(vitesse);
|
2023-12-04 17:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*fonction pour mettre à jour la position du serpent*/
|
|
|
|
void Update_Serpent(){
|
2023-12-18 13:36:17 +01:00
|
|
|
int i=1;
|
2023-12-04 17:01:52 +01:00
|
|
|
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
|
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-07 14:02:18 +01:00
|
|
|
void Collision(){
|
2023-12-18 13:36:17 +01:00
|
|
|
int i=1;
|
2023-12-07 14:02:18 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|