SAE11_2023/SAE_semestre1/src/serpent.c

135 lines
3.5 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
#include "../fichier.h/serpent.h"
#include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
void Update_Serpent(int segment, int direction, int pos_x[60], int pos_y[60]){
int old_x[60];
int old_y[60];
int x = 600;
int y = 400;
int t, fond, i = 0, serpent;
fond = ChargerSprite("../img/fond.png");
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
AfficherSprite(serpent, pos_x[0], pos_y[0]);
if (direction == 1 && direction != 2 ){
pos_y[0]=old_y[0]-20;
}
if (direction == 2 && direction != 1) {
pos_y[0]=old_y[0]+20;
}
if (direction == 3 & direction != 4) {
pos_x[0]=old_x[0]-20;
}
if (direction == 4 && direction != 3) {
pos_x[0]=old_x[0]+20;
}
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]);
}
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-04 17:27:48 +01:00
}
void Controle(int* direction , int* go_on) {
int t;
int last_direction = 0;
while(ToucheEnAttente()) {
t = Touche();
switch(t) {
case XK_Left:
if (last_direction != 4) {
*direction = 3;
printf("gauche\n");
}
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;
case XK_p:
*direction = 0;
}
2023-12-04 17:27:48 +01:00
}
}
int Collision_Serpent(int* go_on){
int pos_x[60];
int pos_y[60];
int murx[50];
int mury[50];
int segment = 10;
2023-12-07 14:32:42 +01:00
int i;
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
return *go_on= 0;
} else{
return 1;
2023-12-04 17:27:48 +01:00
}
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
return *go_on= 0;
} else{
return 1;
2023-12-04 17:27:48 +01:00
}
/*Serpent contre Serpent*/
for (i = 1; i < segment; i++) {
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]){
return *go_on = 0;
} else {
return 1;
2023-12-04 17:27:48 +01:00
}
}
/*Serpent contre mur*/
for(i=0; i<30;i++){
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
return *go_on= 0;
}else{
return 1;
}
}
2023-12-07 14:32:42 +01:00
}
void Score(int segment){
int nombre;
char score[4];
nombre = (segment-10)*10;
snprintf(score,4,"%04d0", nombre);
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(1100,700,1200,800);
ChoisirCouleurDessin(CouleurParNom("white"));
EcrireTexte(1000,760,"Score: ",2);
EcrireTexte(1100,760,score,2);
2023-12-07 14:32:42 +01:00
}
void Serpent(int* go_on) {
int direction=0;
int pos_x[60];
int pos_y[60];
int* pointeur_pos_x= pos_x;
int* pointeur_pos_y= pos_y;
int* pointeur_direction=&direction;
2023-12-07 14:32:42 +01:00
Controle(pointeur_direction, go_on);
Update_Serpent(segment, direction, pointeur_pos_x, pointeur_pos_y );
Collision_Serpent(go_on);
2023-12-04 17:27:48 +01:00
}