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

View File

@ -1,23 +1,30 @@
#include <stdlib.h>
#include <graph.h>
#include "../fichier.h/pastille.h"
#include "../fichier.h/serpent.h"
#include "../fichier.h/pastille.h"
#include <stdlib.h>
#include "pommes.h"
void Pomme(){
int pos_x[60];
int pos_y[60];
int segment = 10;
int p, pp;
int pomme, pommex[5], pommey[5];
for(p=0; p<6; p++){
void InitialiserPommes(int pommex[], int pommey[], int segment) {
int p;
int pomme;
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]);
}
}
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;
(*segment) +=2;
pommex[p] = ((rand() % (60)+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 "../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 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]);
}
void DeplacerSerpent(Serpent *serpent) {
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 Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){
int mur;
int i;
for ( i = serpent->longueur - 1; i > 0; i--) {
serpent->corps[i] = serpent->corps[i - 1];
mur = ChargerSprite("../img/mur.png");
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
*go_on=0;
}
serpent->corps[0].x += serpent->directionX * 20;
serpent->corps[0].y += serpent->directionY * 20;
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
*go_on=0;
}
void Controle(int* direction , int* go_on) {
/*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;
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:
@ -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;
}
if (*direction == 2 && *direction != 1) {
pos_y[0]=old_y[0]+20;
}
return 0;
if (*direction == 3 & *direction != 4) {
pos_x[0]=old_x[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);
if (*direction == 4 && *direction != 3) {
pos_x[0]=old_x[0]+20;
}
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);
AfficherFenetre();
CopierZone(1, 0, 0, 0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0, 0);
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/pastille.h"
void DessinerScene(void){
/*Temps*/
int minute = 0;
int seconde = 0;
char timer[6];
/*Mur*/
int murx[50];
int mury[50];
void DessinerScene(int murx[30], int mury[30]){
int mur;
/*Serpent*/
int serpent;
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);
ChoisirCouleurDessin(CouleurParComposante(91,222,122));
RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
serpent=ChargerSprite("../img/serpent.png");
fond = ChargerSprite("../img/fond.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));
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++){
murx[i] = ((rand() % (55)+1)*20);
mury[i] = ((rand() % (35)+1)*20);