2023-12-01 13:32:18 +01:00
|
|
|
#include <graph.h>
|
2023-12-21 21:45:49 +01:00
|
|
|
#include "serpent.h"
|
2023-12-01 13:32:18 +01:00
|
|
|
|
2023-12-20 21:26:34 +01:00
|
|
|
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[], int* direction){
|
2023-12-19 14:52:23 +01:00
|
|
|
int i = 0;
|
|
|
|
for (i=1 ; i<segment ; i++){
|
|
|
|
pos_x[i]=old_x[i-1];
|
|
|
|
pos_y[i]=old_y[i-1];
|
|
|
|
}
|
2023-12-20 21:26:34 +01:00
|
|
|
if (*direction == 1){
|
|
|
|
pos_y[0]=old_y[0]-20;
|
2023-12-21 16:07:38 +01:00
|
|
|
ChargerImage("../img/tete_up.png", pos_x[0], pos_y[0], 0,0,20,20);
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
|
|
|
if (*direction == 2) {
|
|
|
|
pos_y[0]=old_y[0]+20;
|
2023-12-21 16:07:38 +01:00
|
|
|
ChargerImage("../img/tete_down.png", pos_x[0], pos_y[0], 0,0,20,20);
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
|
|
|
if (*direction == 3) {
|
|
|
|
pos_x[0]=old_x[0]-20;
|
2023-12-21 16:07:38 +01:00
|
|
|
ChargerImage("../img/tete_left.png", pos_x[0], pos_y[0], 0,0,20,20);
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
|
|
|
if (*direction == 4) {
|
|
|
|
pos_x[0]=old_x[0]+20;
|
2023-12-21 16:07:38 +01:00
|
|
|
ChargerImage("../img/tete_right.png", pos_x[0], pos_y[0], 0,0,20,20);
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
2023-12-21 14:55:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void dessinerSerpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){
|
|
|
|
int i;
|
2023-12-20 21:26:34 +01:00
|
|
|
for (i=0 ; i<segment ; i++){
|
2023-12-21 16:07:38 +01:00
|
|
|
ChargerImage("../img/serpent.png", old_x[i], old_y[i],0,0, 20,20);
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
|
|
|
for (i=0 ; i<segment ; i++){
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(218,209,77));
|
2023-12-21 14:55:45 +01:00
|
|
|
RemplirRectangle(old_x[segment-1], old_y[segment-1], 20,20);
|
2023-12-20 21:26:34 +01:00
|
|
|
|
2023-12-19 14:52:23 +01:00
|
|
|
old_x[i]=pos_x[i];
|
|
|
|
old_y[i]=pos_y[i];
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
2023-12-18 14:59:07 +01:00
|
|
|
}
|
2023-12-19 14:52:23 +01:00
|
|
|
void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){
|
2023-12-18 14:59:07 +01:00
|
|
|
int i;
|
2023-12-19 14:52:23 +01:00
|
|
|
/*Serpent contre coté*/
|
2023-12-21 14:55:45 +01:00
|
|
|
if (pos_x[0] >1160 || pos_x[0]<=0){
|
2023-12-19 14:52:23 +01:00
|
|
|
*go_on=0;
|
|
|
|
}
|
|
|
|
/*Serpent contre coté*/
|
2023-12-21 14:32:41 +01:00
|
|
|
if (pos_y[0]<20 || pos_y[0] >=720){
|
2023-12-19 14:52:23 +01:00
|
|
|
*go_on=0;
|
|
|
|
}
|
|
|
|
/*Serpent contre Serpent*/
|
|
|
|
for (i = 1; i < segment; i++) {
|
2023-12-20 21:26:34 +01:00
|
|
|
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]){
|
2023-12-19 14:52:23 +01:00
|
|
|
*go_on = 0;
|
2023-12-20 21:26:34 +01:00
|
|
|
}
|
2023-12-19 14:52:23 +01:00
|
|
|
}
|
|
|
|
/*Serpent contre mur*/
|
|
|
|
for(i=0; i<30;i++){
|
|
|
|
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
|
|
|
|
*go_on=0;
|
|
|
|
}
|
2023-12-14 15:00:02 +01:00
|
|
|
}
|
2023-12-21 14:55:45 +01:00
|
|
|
return;
|
2023-12-04 17:27:48 +01:00
|
|
|
}
|
2023-12-20 21:26:34 +01:00
|
|
|
|
2023-12-21 12:29:12 +01:00
|
|
|
void Controle(int *direction, int last_direction, int *go_on, int *pause) {
|
2023-12-19 14:52:23 +01:00
|
|
|
int t;
|
2023-12-14 15:00:02 +01:00
|
|
|
while(ToucheEnAttente()) {
|
|
|
|
t = Touche();
|
|
|
|
switch(t) {
|
|
|
|
case XK_Left:
|
|
|
|
if (last_direction != 4) {
|
|
|
|
*direction = 3;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Right:
|
|
|
|
if (last_direction != 3) {
|
|
|
|
*direction = 4;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Up:
|
|
|
|
if (last_direction != 2) {
|
|
|
|
*direction = 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Down:
|
|
|
|
if (last_direction != 1) {
|
|
|
|
*direction = 2;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Escape:
|
|
|
|
*direction = 0;
|
|
|
|
*go_on = 0;
|
|
|
|
return;
|
2023-12-21 12:29:12 +01:00
|
|
|
case XK_space:
|
|
|
|
if (*pause == 0 ){
|
|
|
|
*pause == 1;
|
|
|
|
}else{
|
|
|
|
*pause = 0;
|
|
|
|
ChargerImage("../img/PAUSE.png", 400,200,0,0,350,300);
|
|
|
|
}
|
2023-12-14 15:00:02 +01:00
|
|
|
}
|
2023-12-04 17:27:48 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 14:52:23 +01:00
|
|
|
void Serpent(int pos_x[], int pos_y[], int old_x[], int old_y[], int *segment, int murx[], int mury[], int *go_on, int *direction) {
|
|
|
|
int serpent;
|
2023-12-20 21:26:34 +01:00
|
|
|
int i = 0;
|
2023-12-21 16:07:38 +01:00
|
|
|
/*Position initial serpent*/
|
2023-12-20 21:26:34 +01:00
|
|
|
pos_x[0] = 600;
|
|
|
|
pos_y[0] = 400;
|
2023-12-19 14:52:23 +01:00
|
|
|
for (i = 0; i < *segment; i++){
|
2023-12-20 21:26:34 +01:00
|
|
|
pos_x[i]= pos_x[0];
|
|
|
|
pos_y[i]= pos_y[0];
|
2023-12-19 14:52:23 +01:00
|
|
|
old_x[i]=pos_x[i];
|
|
|
|
old_y[i]=pos_y[i];
|
2023-12-04 17:27:48 +01:00
|
|
|
}
|
|
|
|
}
|