Des beugs paramètre dans les fonctions

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

View File

@ -1,11 +1,25 @@
### VARIABLES ###
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -std=c99 CFLAGS = -lgraph -ansi
LIBS = -lgraph -lm SRCDIR = ./src
HDIR = ./fichier.h
all: main ODIR = ./out
OFILES = $(subst src/,out/,$(subst .c,.o,$(shell find $(SRCDIR)/ -type f)))
main: main.c jeu.c serpent.c pomme.c EXE = game
$(CC) $(CFLAGS) -o main main.c jeu.c serpent.c pomme.c $(LIBS) ### BUT PAR DEFAUT ###
but : $(EXE)
clean: ### REGLES ESSENTIELLES ###
rm main $(ODIR)/%.o : $(SRCDIR)/%.c
@mkdir -p $(@D)
$(CC) -c $< -o $@
$(EXE) : $(OFILES)
$(CC) $(CFLAGS) -o $(EXE) $(OFILES)
### REGLES OPTIONNELLES ###
run : $(EXE)
./$(EXE)
clean :
-rm -rf $(ODIR)
mrproper :
clean but
### BUTS FACTICES ###
.PHONY : but run clean mrproper

View File

@ -1,5 +1,6 @@
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
void main(void);
#endif #endif

View File

@ -1,6 +1,6 @@
#ifndef MENU_H #ifndef MENU_H
#define MENU_H #define MENU_H
void menu(void) void menu(void);
#endif #endif

View File

@ -1,8 +1,6 @@
#ifndef PASTILLE_H #ifndef PASTILLE_H
#define PASTILLE_H #define PASTILLE_H
void genererPomme(); void Pomme();
void dessinerPomme();
int collisionAvecPomme();
#endif #endif

View File

@ -1,21 +1,12 @@
#ifndef SERPENT_H #ifndef SERPENT_H
#define SERPENT_H #define SERPENT_H
#include "../fichier.h/terrain.h" #include "../fichier.h/terrain.h"
typedef struct {
Position *corps;
int longueur;
} Serpent;
extern Serpent serpent; void Update_Serpent(int, int,int*, int*);
extern int direction; void Controle(int*, int*);
void Serpent(int*);
void initialiserSerpent(); int Collision_Serpent(int*);
void dessinerSerpent(); void Score(int*);
void deplacerSerpent();
int collisionAvecSerpent();
int collisionAvecBordures();
void gestionCollision();
#endif #endif

View File

@ -1,12 +1,6 @@
#ifndef TERRAIN_H #ifndef TERRAIN_H
#define TERRAIN_H #define TERRAIN_H
#define LARGEUR_FENETRE 1250 void DessinerScene();
#define HAUTEUR_FENETRE 750
#define TAILLE_CELLULE 25
#define DELAI_MILLISECONDES 100
#define ESPACE_NOIR 100
void jeu();
#endif #endif

View File

@ -3,6 +3,6 @@
#define CYCLE 10000L #define CYCLE 10000L
void Timer(unsigned long int suivant); void Timer();
#endif #endif

20
SAE_semestre1/out/test.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
int test(int nombre)
{
return nombre;
}
int main(void)
{
int toto = 5;
printf("%d",test(toto));
return EXIT_SUCCESS;
}

View File

@ -162,25 +162,21 @@ void Controle() {
case XK_Left: case XK_Left:
if (last_direction != 4) { if (last_direction != 4) {
direction = 3; direction = 3;
last_direction = 3;
} }
break; break;
case XK_Right: case XK_Right:
if (last_direction != 3) { if (last_direction != 3) {
direction = 4; direction = 4;
last_direction = 4;
} }
break; break;
case XK_Up: case XK_Up:
if (last_direction != 2) { if (last_direction != 2) {
direction = 1; direction = 1;
last_direction = 1;
} }
break; break;
case XK_Down: case XK_Down:
if (last_direction != 1) { if (last_direction != 1) {
direction = 2; direction = 2;
last_direction = 2;
} }
break; break;
case XK_Escape: case XK_Escape:

View File

@ -1,6 +1,31 @@
#include <stdlib.h>
#include <stdio.h>
#include "../fichier.h/time.h"
#include "../fichier.h/terrain.h" #include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
#include "../fichier.h/serpent.h"
#include "../fichier.h/menu.h"
int main() { int main(void){
jeu(); int go_on = 1;
return 0; int segment = 10;
int* pointeur_segment = &segment;
int* pointeur_go_on= &go_on;
int seconde_actuel;
int old_seconde;
unsigned long int suivant;
InitialiserGraphique();
CreerFenetre(350,100,1200,900);
EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10;
DessinerScene();
usleep(1000);
while(go_on){
Timer();
Score(pointeur_segment);
Serpent(&go_on);
Pomme();
}
FermerGraphique();
} }

View File

@ -0,0 +1,25 @@
#include <stdlib.h>
#include <stdio.h>
#include "../fichier.h/menu.h"
int MenuDebut(){
InitialiserGraphique();
EcrireTexte(1000,760,"Score", 2);
}
int menu(){
while(menu){
while(ToucheEnAttente()){
t = Touche();
switch(t3){
case XK_q :
FermerGraphique();
return EXIT_SUCCESS;
return;
case XK_Right :
menu = 0;
return
}
}
}
}

View File

@ -4,37 +4,20 @@
#include "../fichier.h/serpent.h" #include "../fichier.h/serpent.h"
#include "../fichier.h/pastille.h" #include "../fichier.h/pastille.h"
#define LARGEUR_FENETRE 1250 void Pomme(){
#define HAUTEUR_FENETRE 750 int pos_x[60];
#define TAILLE_CELLULE 25 int pos_y[60];
#define DELAI_MILLISECONDES 100 int segment = 10;
#define ESPACE_NOIR 100 int p, pp;
int pomme, pommex[5], pommey[5];
void genererPomme() { for(p=0; p<6; p++){
int pommex[5]; if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
int pommey[5]; segment+=2;
int i; pommex[p] = ((rand() % (60)+1)*20);
for (i = 0; i < 5; i++) { pommey[p] = ((rand() % (27)+1)*20);
pommex[i] = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; }
pommey[i] = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
} }
} for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
void dessinerPomme() {
int i;
int pommex[5];
int pommey[5];
ChoisirCouleurDessin(CouleurParComposante(255, 0, 0));
for (i = 0; i < 5; i++) {
RemplirRectangle(pommex[i], pommey[i], TAILLE_CELLULE, TAILLE_CELLULE);
}
}
int collisionAvecPomme() {
int i;
int pommex[5];
int pommey[5];
for (i = 0; i < 5; i++) {
return (serpent.corps[0].x == pommex[i] && serpent.corps[i].y == pommey[i]);
} }
} }

View File

@ -1,70 +1,135 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <graph.h> #include <graph.h>
#include "serpent.h" #include "../fichier.h/serpent.h"
#include "terrain.h" #include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
Serpent serpent; void Update_Serpent(int segment, int direction, int pos_x[60], int pos_y[60]){
int direction; int old_x[60];
int old_y[60];
void initialiserSerpent() { int x = 600;
serpent.longueur = 1; int y = 400;
serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur); int t, fond, i = 0, serpent;
serpent.corps[0].x = LARGEUR_FENETRE / 2; fond = ChargerSprite("../img/fond.png");
serpent.corps[0].y = HAUTEUR_FENETRE / 2; AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
} AfficherSprite(serpent, pos_x[0], pos_y[0]);
if (direction == 1 && direction != 2 ){
void dessinerSerpent() { pos_y[0]=old_y[0]-20;
int i; }
for (i = 0; i < serpent.longueur; i++) { if (direction == 2 && direction != 1) {
if (i % 2 == 0) { pos_y[0]=old_y[0]+20;
ChoisirCouleurDessin(CouleurParNom("yellow")); /*JAUNE*/ }
} if (direction == 3 & direction != 4) {
RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE); 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 Controle(int* direction , int* go_on) {
void deplacerSerpent() { int t;
int i; int last_direction = 0;
/* Déplacer le corps du serpent*/ while(ToucheEnAttente()) {
for (i = serpent.longueur - 1; i > 0; i--) { t = Touche();
serpent.corps[i] = serpent.corps[i - 1]; switch(t) {
} case XK_Left:
if (last_direction != 4) {
/* Déplacer la tête du serpent en fonction de la direction*/ *direction = 3;
switch (direction) { printf("gauche\n");
case 0: }
serpent.corps[0].x += TAILLE_CELLULE; return;
break; case XK_Right:
case 1: if (last_direction != 3) {
serpent.corps[0].y += TAILLE_CELLULE; *direction = 4;
break; }
case 2: return;
serpent.corps[0].x -= TAILLE_CELLULE; case XK_Up:
break; if (last_direction != 2) {
case 3: *direction = 1;
serpent.corps[0].y -= TAILLE_CELLULE; }
break; return;
} case XK_Down:
} if (last_direction != 1) {
*direction = 2;
int collisionAvecSerpent() { }
int i; return;
for (i = 1; i < serpent.longueur; i++) { case XK_Escape:
if (serpent.corps[0].x == serpent.corps[i].x && serpent.corps[0].y == serpent.corps[i].y) { *direction = 0;
return 1; *go_on = 0;
return;
case XK_p:
*direction = 0;
} }
} }
return 0;
} }
int Collision_Serpent(int* go_on){
int collisionAvecBordures() { int pos_x[60];
return (serpent.corps[0].x < 0 || serpent.corps[0].x >= LARGEUR_FENETRE || int pos_y[60];
serpent.corps[0].y < 0 || serpent.corps[0].y >= HAUTEUR_FENETRE); int murx[50];
} int mury[50];
int segment = 10;
void gestionCollision() { int i;
if (collisionAvecSerpent() || collisionAvecBordures()) { /*Serpent contre coté*/
FermerGraphique(); if (pos_x[0] >1160 || pos_x[0]<=20){
exit(EXIT_SUCCESS); 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);
} }

View File

@ -3,54 +3,57 @@
#include "../fichier.h/time.h" #include "../fichier.h/time.h"
#include "../fichier.h/terrain.h" #include "../fichier.h/terrain.h"
#include "../fichier.h/serpent.h" #include "../fichier.h/serpent.h"
#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];
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");
#define LARGEUR_FENETRE 1250 for (i = 0; i < segment; i++){
#define HAUTEUR_FENETRE 750 AfficherSprite(serpent, x-(i*20), y);
#define TAILLE_CELLULE 25 pos_x[i]=x-(i*20);
#define DELAI_MILLISECONDES 100 pos_y[i]=y;
#define ESPACE_NOIR 100 old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
void jeu(void) { }
InitialiserGraphique(); srand(time(NULL));
CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR); pomme=ChargerSprite("../img/pomme.png");
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0)); for (p = 0; p < 5; p++) {
EffacerEcran(CouleurParComposante(0, 0, 0)); pommex[p] = ((rand() % (55)+1)*20);
initialiserSerpent(); pommey[p] = ((rand() % (35)+1)*20);
genererPomme(); AfficherSprite(pomme, pommex[p],pommey[p]);
}
while (1) { for(i=0; i<30; i++){
dessinerDamier(); /* Dessiner le damier en fond*/ murx[i] = ((rand() % (55)+1)*20);
deplacerSerpent(); mury[i] = ((rand() % (35)+1)*20);
gestionCollision(); AfficherSprite(mur, murx[i], mury[i]);
dessinerSerpent();
dessinerPomme();
attente(DELAI_MILLISECONDES); /* Attente pour ralentir le serpent*/
if (SourisCliquee()) {
FermerGraphique();
exit(EXIT_SUCCESS);
}
if (ToucheEnAttente()) {
int touche = Touche();
switch (touche) {
case XK_Right:
direction = 0;
break;
case XK_Down:
direction = 1;
break;
case XK_Left:
direction = 2;
break;
case XK_Up:
direction = 3;
break;
case XK_Escape:
FermerGraphique();
}
}
} }
} }

View File

@ -1,39 +1,41 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <graph.h> #include <graph.h>
#include "../fichier.h/time.h"
#define CYCLE 10000L #define CYCLE 10000L
int seconde=0; /*time*/
int minute=0; void Update_Timer(void){
int seconde_actuel=0; int seconde =0;
int old_seconde=0; int minute=1;
char timer[6]; char timer[6];
snprintf(timer,6,"%02d:%02d", minute, seconde);
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
RemplirRectangle(10,700,12000,800);
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
EcrireTexte(50,760,"Temps: ",2);
EcrireTexte(160,760,timer,2);
}
void Timer(void){
int seconde = 0;
int minute = 0;
int seconde_actuel;
int old_seconde;
unsigned long int suivant; unsigned long int suivant;
int nombre; if(Microsecondes()> suivant){
suivant = Microsecondes()+CYCLE;
void Update_Timer(){ seconde_actuel = (suivant/1000000)%10;
snprintf(timer,6,"%02d:%02d", minute, seconde); if(seconde_actuel !=old_seconde){
ChoisirCouleurDessin(CouleurParComposante(0,0,0)); old_seconde = seconde_actuel;
RemplirRectangle(0,780,1200,800); if(seconde == 59){
ChoisirCouleurDessin(CouleurParComposante(255,255,255)); minute=minute+1;
EcrireTexte(10,800,timer,2); seconde=0;
} Update_Timer();
}else;{
void Timer(){ seconde = seconde+1;
if(Microsecondes()> suivant){ Update_Timer();
suivant = Microsecondes()+CYCLE; }
seconde_actuel = (suivant/1000000)%10; }
if(seconde_actuel !=old_seconde){ }
old_seconde = seconde_actuel;
if(seconde == 59){
minute=minute+1;
seconde=0;
Update_Timer();
}else{
seconde = seconde+1;
Update_Timer();
}
}
}
} }