Des beugs paramètre dans les fonctions

This commit is contained in:
2023-12-14 15:00:02 +01:00
parent 90fdcd7036
commit e5def4ff96
15 changed files with 330 additions and 213 deletions

View File

@@ -1,70 +1,135 @@
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
#include "serpent.h"
#include "terrain.h"
#include "../fichier.h/serpent.h"
#include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
Serpent serpent;
int direction;
void initialiserSerpent() {
serpent.longueur = 1;
serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur);
serpent.corps[0].x = LARGEUR_FENETRE / 2;
serpent.corps[0].y = HAUTEUR_FENETRE / 2;
}
void dessinerSerpent() {
int i;
for (i = 0; i < serpent.longueur; i++) {
if (i % 2 == 0) {
ChoisirCouleurDessin(CouleurParNom("yellow")); /*JAUNE*/
}
RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE);
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];
}
}
void deplacerSerpent() {
int i;
/* Déplacer le corps du serpent*/
for (i = serpent.longueur - 1; i > 0; i--) {
serpent.corps[i] = serpent.corps[i - 1];
}
/* Déplacer la tête du serpent en fonction de la direction*/
switch (direction) {
case 0:
serpent.corps[0].x += TAILLE_CELLULE;
break;
case 1:
serpent.corps[0].y += TAILLE_CELLULE;
break;
case 2:
serpent.corps[0].x -= TAILLE_CELLULE;
break;
case 3:
serpent.corps[0].y -= TAILLE_CELLULE;
break;
}
}
int collisionAvecSerpent() {
int i;
for (i = 1; i < serpent.longueur; i++) {
if (serpent.corps[0].x == serpent.corps[i].x && serpent.corps[0].y == serpent.corps[i].y) {
return 1;
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;
}
}
return 0;
}
int collisionAvecBordures() {
return (serpent.corps[0].x < 0 || serpent.corps[0].x >= LARGEUR_FENETRE ||
serpent.corps[0].y < 0 || serpent.corps[0].y >= HAUTEUR_FENETRE);
}
void gestionCollision() {
if (collisionAvecSerpent() || collisionAvecBordures()) {
FermerGraphique();
exit(EXIT_SUCCESS);
int Collision_Serpent(int* go_on){
int pos_x[60];
int pos_y[60];
int murx[50];
int mury[50];
int segment = 10;
int i;
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
return *go_on= 0;
} else{
return 1;
}
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
return *go_on= 0;
} else{
return 1;
}
/*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;
}
}
/*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;
}
}
}
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 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;
Controle(pointeur_direction, go_on);
Update_Serpent(segment, direction, pointeur_pos_x, pointeur_pos_y );
Collision_Serpent(go_on);
}