séparation fichier seulement .c

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

View File

@ -1,32 +1,41 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "../fichier.h/time.h" #include <graph.h>
#include "../fichier.h/terrain.h" #include "serpent.h"
#include "../fichier.h/pastille.h" #include "pommes.h"
#include "../fichier.h/serpent.h" #include "time.h"
#include "../fichier.h/menu.h"
#include "../fichier.h/ajouter_menu_fin.h"
int main(void){ #define CYCLE 10000L
int go_on = 1;
int segment = 10; int main(void) {
int* pointeur_segment = &segment; int segment = 10;
int* pointeur_go_on= &go_on; int go_on = 1;
int seconde_actuel; int direction = 4;
int old_seconde; int pos_x[60];
unsigned long int suivant; int pos_y[60];
int old_x[60];
int old_y[60];
int pommex[5];
int pommey[5];
int murx[30];
int mury[30];
int *pointeur_segment = &segment;
int *pointeur_go_on = &go_on;
int *pointeur_direction = &direction;
InitialiserGraphique(); InitialiserGraphique();
CreerFenetre(350,100,1200,900); CreerFenetre(350,100,1200,900);
EffacerEcran(CouleurParComposante(0,0,0)); EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE; suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10; old_seconde=(suivant/1000000)%10;
DessinerScene(); DessinerScene(murx, mury);
usleep(1000); InitialiserPommes(pommex, pommey, segment);
while(go_on){ while(go_on){
Timer(); Timer();
Score(); Score(*pointeur_segment);
Serpent(); Controle(pointeur_direction, 0, pointeur_go_on);
Pomme(); Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, pointeur_go_on, pointeur_direction);
Pomme(pos_x, pos_y, pommex, pommey, pointeur_segment);
} }
FermerGraphique(); FermerGraphique();
} return EXIT_SUCCESS;
}

View File

@ -1,23 +1,30 @@
#include <stdlib.h>
#include <graph.h> #include <graph.h>
#include "../fichier.h/pastille.h" #include <stdlib.h>
#include "../fichier.h/serpent.h" #include "pommes.h"
#include "../fichier.h/pastille.h"
void Pomme(){ void InitialiserPommes(int pommex[], int pommey[], int segment) {
int pos_x[60]; int p;
int pos_y[60]; int pomme;
int segment = 10; pomme = ChargerSprite("../img/pomme.png");
int p, pp; for (p = 0; p < 5; p++) {
int pomme, pommex[5], pommey[5]; pommex[p] = ((rand() % (55) + 1) * 20);
for(p=0; p<6; p++){ pommey[p] = ((rand() % (35) + 1) * 20);
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){ AfficherSprite(pomme, pommex[p], pommey[p]);
segment+=2; }
}
void Pomme(int pos_x[], int pos_y[], int pommex[], int pommey[], int *segment) {
int p;
int pomme;
pomme=ChargerSprite("../img/pomme.png");
for (p = 0; p < 5; p++) {
AfficherSprite(pomme, pommex[p],pommey[p]);
}
for(p=0; p<5; p++){
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
(*segment) +=2;
pommex[p] = ((rand() % (60)+1)*20); pommex[p] = ((rand() % (60)+1)*20);
pommey[p] = ((rand() % (27)+1)*20); pommey[p] = ((rand() % (27)+1)*20);
} }
} }
for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
} }

View File

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

View File

@ -6,51 +6,17 @@
#include "../fichier.h/main.h" #include "../fichier.h/main.h"
#include "../fichier.h/pastille.h" #include "../fichier.h/pastille.h"
void DessinerScene(void){ void DessinerScene(int murx[30], int mury[30]){
/*Temps*/
int minute = 0;
int seconde = 0;
char timer[6];
/*Mur*/
int murx[50];
int mury[50];
int mur; int mur;
/*Serpent*/ int i;
int serpent; int fond;
int segment=10;
int x = 600;
int y = 400;
int i;
int pos_x[60];
int pos_y[60];
int old_x[60];
int old_y[60];
int fond;
int p=0;
int pp=0;
int pomme, pommex[5], pommey[5];
snprintf(timer,6,"%02d:%02d", minute ,seconde); snprintf(timer,6,"%02d:%02d", minute ,seconde);
ChoisirCouleurDessin(CouleurParComposante(91,222,122)); ChoisirCouleurDessin(CouleurParComposante(91,222,122));
RemplirRectangle(20,20,1160,700); RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255)); ChoisirCouleurDessin(CouleurParComposante(255,255,255));
serpent=ChargerSprite("../img/serpent.png");
fond = ChargerSprite("../img/fond.png"); fond = ChargerSprite("../img/fond.png");
mur = ChargerSprite("../img/mur.png"); mur = ChargerSprite("../img/mur.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];
}
srand(time(NULL)); srand(time(NULL));
pomme=ChargerSprite("../img/pomme.png");
for (p = 0; p < 5; p++) {
pommex[p] = ((rand() % (55)+1)*20);
pommey[p] = ((rand() % (35)+1)*20);
AfficherSprite(pomme, pommex[p],pommey[p]);
}
for(i=0; i<30; i++){ for(i=0; i<30; i++){
murx[i] = ((rand() % (55)+1)*20); murx[i] = ((rand() % (55)+1)*20);
mury[i] = ((rand() % (35)+1)*20); mury[i] = ((rand() % (35)+1)*20);