séparation fichier seulement .c

This commit is contained in:
2023-12-19 14:52:23 +01:00
parent 80893815ff
commit 3e87799671
4 changed files with 122 additions and 145 deletions

View File

@@ -1,38 +1,54 @@
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
#include "../fichier.h/serpent.h"
#include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
#include "serpent.h"
void InitialiserSerpent(Serpent *serpent, int x, int y) {
serpent->longueur = 10;
serpent->directionX = 1;
serpent->directionY = 0;
serpent->corps[0].x = x;
serpent->corps[0].y = y;
}
void DeplacerSerpent(Serpent *serpent) {
int i;
for ( i = serpent->longueur - 1; i > 0; i--) {
serpent->corps[i] = serpent->corps[i - 1];
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[]){
int i = 0;
int serpent;
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]);
}
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];
}
serpent->corps[0].x += serpent->directionX * 20;
serpent->corps[0].y += serpent->directionY * 20;
}
void Controle(int* direction , int* go_on) {
int t;
int last_direction = 0;
void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){
int mur;
int i;
mur = ChargerSprite("../img/mur.png");
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
*go_on=0;
}
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
*go_on=0;
}
/*Serpent contre Serpent*/
for (i = 1; i < segment; i++) {
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i])
*go_on = 0;
}
/*Serpent contre mur*/
for(i=0; i<30;i++){
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
*go_on=0;
}
}
}
void Controle(int *direction, int last_direction, int *go_on) {
int t;
while(ToucheEnAttente()) {
t = Touche();
switch(t) {
case XK_Left:
if (last_direction != 4) {
*direction = 3;
printf("gauche\n");
}
return;
case XK_Right:
@@ -59,53 +75,32 @@ void Controle(int* direction , int* go_on) {
}
}
}
int VerifierCollision(const Serpent *serpent) {
int teteX = serpent->corps[0].x;
int teteY = serpent->corps[0].y;
int i;
if (teteX < 0 || teteX >= 1200 || teteY < 0 || teteY >= 900-80) {
return 1;
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;
int x = 600;
int y = 400;
int i = 0;
serpent=ChargerSprite("../img/serpent.png");
for (i = 0; i < *segment; i++){
AfficherSprite(serpent, x-(i*20), y);
pos_x[i]=x-(i*20);
pos_y[i]=y;
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
for (i = 1; i < serpent->longueur; i++) {
if (teteX == serpent->corps[i].x && teteY == serpent->corps[i].y) {
return 1;
}
if (*direction == 1 && *direction != 2 ){
pos_y[0]=old_y[0]-20;
}
return 0;
}
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);
}
void DessinerSerpent(const Serpent *serpent) {
int i;
couleur couleurSerpent = CouleurParComposante(34, 139, 34);
ChoisirEcran(1);
EffacerEcran(CouleurParNom("grey"));
ChoisirCouleurDessin(couleurSerpent);
/* Afficher la tête du serpent */
RemplirRectangle(serpent->corps[0].x, serpent->corps[0].y, 20, 20);
for (i = 0; i < serpent->longueur; i++) {
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, 20, 20);
if (*direction == 2 && *direction != 1) {
pos_y[0]=old_y[0]+20;
}
ChoisirEcran(0);
AfficherFenetre();
CopierZone(1, 0, 0, 0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0, 0);
if (*direction == 3 & *direction != 4) {
pos_x[0]=old_x[0]-20;
}
if (*direction == 4 && *direction != 3) {
pos_x[0]=old_x[0]+20;
}
Update_Serpent(pos_x, pos_y, *segment, old_x, old_y);
Collision_Serpent(pos_x, pos_y, *segment, murx, mury, go_on);
usleep(100000);
}