Séparation game.c

This commit is contained in:
Jude Christ AISSI 2023-12-07 14:32:42 +01:00
parent be324de28e
commit 711ab48c32
15 changed files with 152 additions and 344 deletions

View File

@ -1,25 +1,11 @@
CC = gcc CC = gcc
CFLAGS = -ansi -pedantic -lgraph -g CFLAGS = -Wall -Wextra -std=c99
SRCDIR = ./src LIBS = -lgraph -lm
HDIR = ./fichier.h
ODIR = ./out
OFILES = $(subst src/,out/,$(subst .c,.o,$(shell find $(SRCDIR)/ -type f)))
EXE = snake
but : $(EXE) all: main
$(ODIR)/%.o : $(SRCDIR)/%.c main: main.c jeu.c serpent.c pomme.c
@mkdir -p $(@D) $(CC) $(CFLAGS) -o main main.c jeu.c serpent.c pomme.c $(LIBS)
$(CC) -c $< -o $@
$(EXE) : $(OFILES) clean:
$(CC) $(CFLAGS) -o $(EXE) $(OFILES) rm main
run : $(EXE)
./$(EXE)
clean : -rm -f $(OFILES) snake
.PHONY : but clean

View File

@ -1,6 +0,0 @@
#ifndef CONTROLE_H
#define CONTROLE_H
void Controle();
#endif

View File

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

View File

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

View File

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

View File

@ -1,7 +1,8 @@
#ifndef TIME_H #ifndef TIME_H
#define TIME_H #define TIME_H
#define CYCLE 10000L #define CYCLE 10000L
void Timer(unsigned long int suivant); void Timer(unsigned long int suivant);
#endif TIME_H #endif

BIN
SAE_semestre1/img/fond.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 B

BIN
SAE_semestre1/img/pomme.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 492 B

After

Width:  |  Height:  |  Size: 493 B

View File

@ -1,29 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
int t ;
int direction;
int go_on =0;
void Controle(){
while(ToucheEnAttente()){
t = Touche();
switch(t){
case XK_Left :
direction=3;
break;
case XK_Right:
direction=4;
break;
case XK_Up:
direction=1;
break;
case XK_Down:
direction=2;
break;
case XK_Escape:
go_on=0;
break;
break;
}
}
}

View File

@ -1,181 +0,0 @@
#include <stdlib.h>
#include <graph.h>
#include <time.h>
#include "time.c"
#define LARGEUR_FENETRE 1250
#define HAUTEUR_FENETRE 750
#define TAILLE_CELLULE 25
#define DELAI_MILLISECONDES 100
#define ESPACE_NOIR 100
#define CYCLE 1000L
int j,i;
int pommex[5];
int pommey[5];
typedef struct {
int x, y;
} Position;
typedef struct {
Position *corps;
int longueur;
} Serpent;
Position pomme;
Serpent serpent;
int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/
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() {
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 deplacerSerpent() {
/* 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 i;
void genererPomme() {
for(i=0; i<5; i++){
pommex[i] = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
pommey[i] = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
}
}
void dessinerPomme() {
ChoisirCouleurDessin(CouleurParComposante(255, 0, 0));
for(i=0; i<5; i++){
RemplirRectangle(pommex[i], pommey[i], TAILLE_CELLULE, TAILLE_CELLULE);
}
}
int collisionAvecPomme() {
for(i=0;i<5;i++){
return (serpent.corps[0].x == pommex[i] && serpent.corps[i].y == pommey[i]);
}
}
int collisionAvecSerpent() {
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;
}
}
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 dessinerDamier() {
int cell_largeur = LARGEUR_FENETRE / TAILLE_CELLULE;
int cell_hauteur = HAUTEUR_FENETRE / TAILLE_CELLULE;
for (i = 0; i < cell_hauteur; i++) {
for (j = 0; j < cell_largeur; j++) {
if ((i + j) % 2 == 0) {
ChoisirCouleurDessin(CouleurParComposante(144, 238, 144));
} else {
ChoisirCouleurDessin(CouleurParComposante(143, 218, 143));
}
RemplirRectangle(j * TAILLE_CELLULE, i * TAILLE_CELLULE, TAILLE_CELLULE, TAILLE_CELLULE);
}
}
}
void gestionCollision() {
if (collisionAvecPomme()) {
serpent.longueur++;
serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur);
genererPomme();
}
if (collisionAvecSerpent() || collisionAvecBordures()) {
FermerGraphique();
exit(EXIT_SUCCESS);
}
}
void attente(int milliseconds) {
clock_t start_time = clock();
while ((clock() - start_time) * 1000 / CLOCKS_PER_SEC < milliseconds) {
/* Attente*/
}
}
void jeu() {
InitialiserGraphique();
CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR);
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
EffacerEcran(CouleurParComposante(0, 0, 0));
initialiserSerpent();
genererPomme();
while (1) {
dessinerDamier(); /* Dessiner le damier en fond*/
Timer();
deplacerSerpent();
gestionCollision();
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();
}
}
}
}
int main() {
jeu();
return 0;
}

View File

@ -1,59 +1,6 @@
#include <stdlib.h> #include "../fichier.h/terrain.h"
#include <stdio.h>
#include <graph.h>
#include <time.h>
#include <unistd.h>
#include "../fichier.h/time.h"
#include "../fichier.h/controle.h"
#define CYCLE 10000L
int go_on=0;
int go_on=1;
/*Fonction Pour créer la première scene du jeu*/
void DessinerScene(){
snprintf(timer,6,"%02d:%02d", minute, seconde);
ChoisirCouleurDessin(CouleurParComposante(141,199,63));
RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
EcrireTexte(10,760,timer,2);
serpent=ChargerSprite("serpent.png");
fond = ChargerSprite("fond.png");
int main() {
for (i = 0; i < segment; i++){ jeu();
AfficherSprite(serpent, x-(i*20), y); return 0;
pos_x[i]=x-(i*20);
pos_y[i]=y;
old_y[i]=pos_y[i];
old_x[i]=pos_x[i];
}
srand(time(NULL));
pomme=ChargerSprite("pomme.png");
for (p = 0; p < 5; p++) {
pommex[p] = ((rand() % (58)+1)*20);
pommey[p] = ((rand() % (35)+1)*20);
AfficherSprite(pomme, pommex[p], pommey[p]);
}
}
int main(){
/* paramétrage de la fenêtre + chargement première scène */
InitialiserGraphique();
CreerFenetre(350,100,1200,800);
EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10;
DessinerScene();
/*Boucle Principale du Programme*/
while(go_on){
Timer();
Controle();
Serpent();
Pomme();
}
/* fermeture de la fenêtre si ECHAP pressé*/
FermerGraphique();
return EXIT_SUCCESS;
} }

View File

@ -0,0 +1,40 @@
#include <stdlib.h>
#include <graph.h>
#include "../fichier.h/pastille.h"
#include "../fichier.h/serpent.h"
#include "../fichier.h/pastille.h"
#define LARGEUR_FENETRE 1250
#define HAUTEUR_FENETRE 750
#define TAILLE_CELLULE 25
#define DELAI_MILLISECONDES 100
#define ESPACE_NOIR 100
void genererPomme() {
int pommex[5];
int pommey[5];
int i;
for (i = 0; i < 5; i++) {
pommex[i] = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
pommey[i] = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
}
}
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,34 +1,30 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <graph.h> #include <graph.h>
#include "../pastille.h" #include "serpent.h"
#include "terrain.h"
typedef struct {
Position *corps;
int longueur;
} Serpent;
Position pomme;
Serpent serpent; Serpent serpent;
int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/ int direction;
void initialiserSerpent() { void initialiserSerpent() {
serpent.longueur = 10; serpent.longueur = 1;
serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur); serpent.corps = (Position *)malloc(sizeof(Position) * serpent.longueur);
serpent.corps[0].x = LARGEUR_FENETRE / 2; serpent.corps[0].x = LARGEUR_FENETRE / 2;
serpent.corps[0].y = HAUTEUR_FENETRE / 2; serpent.corps[0].y = HAUTEUR_FENETRE / 2;
} }
void dessinerSerpent() { void dessinerSerpent() {
int i;
for (i = 0; i < serpent.longueur; i++) { for (i = 0; i < serpent.longueur; i++) {
if (i % 2 == 0) { if (i % 2 == 0) {
ChoisirCouleurDessin(CouleurParComposante(0,0,0)); /*JAUNE*/ ChoisirCouleurDessin(CouleurParNom("yellow")); /*JAUNE*/
} }
RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE); RemplirRectangle(serpent.corps[i].x, serpent.corps[i].y, TAILLE_CELLULE, TAILLE_CELLULE);
} }
} }
void deplacerSerpent() { void deplacerSerpent() {
int i;
/* Déplacer le corps du serpent*/ /* Déplacer le corps du serpent*/
for (i = serpent.longueur - 1; i > 0; i--) { for (i = serpent.longueur - 1; i > 0; i--) {
serpent.corps[i] = serpent.corps[i - 1]; serpent.corps[i] = serpent.corps[i - 1];
@ -50,13 +46,23 @@ void deplacerSerpent() {
break; break;
} }
} }
void gestionCollision() {
if (collisionAvecPomme()) {
serpent.longueur++;
serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur);
genererPomme();
}
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;
}
}
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()) { if (collisionAvecSerpent() || collisionAvecBordures()) {
FermerGraphique(); FermerGraphique();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -1,38 +1,56 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <graph.h> #include <graph.h>
/*#include "time.h"*/ #include "../fichier.h/time.h"
#include "../fichier.h/terrain.h"
#include "../fichier.h/serpent.h"
#define HAUTEUR 20
#define LARGEUR 30
#define TAILLE_CASE 20
#define ESPACE_NOIR 100
int direction = 4; #define LARGEUR_FENETRE 1250
int t; #define HAUTEUR_FENETRE 750
#define TAILLE_CELLULE 25
#define DELAI_MILLISECONDES 100
#define ESPACE_NOIR 100
void AfficherQuadrillage(){ void jeu(void) {
int j;
int i;
InitialiserGraphique(); InitialiserGraphique();
CreerFenetre(10, 10, 30 * TAILLE_CASE, 20 * TAILLE_CASE + ESPACE_NOIR); CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR);
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
for (i = 0; i < LARGEUR; i++) { EffacerEcran(CouleurParComposante(0, 0, 0));
for (j = 0; j < HAUTEUR; j++) { initialiserSerpent();
if ((i + j) % 2 == 0) { genererPomme();
ChoisirCouleurDessin(CouleurParComposante(144, 238, 144));
} else { while (1) {
ChoisirCouleurDessin(CouleurParComposante(0, 128, 0)); dessinerDamier(); /* Dessiner le damier en fond*/
deplacerSerpent();
gestionCollision();
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();
} }
RemplirRectangle(i * TAILLE_CASE, j * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
} }
} }
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
RemplirRectangle(0, HAUTEUR * TAILLE_CASE, LARGEUR * TAILLE_CASE, ESPACE_NOIR);
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
RemplirRectangle(0, 0, LARGEUR * TAILLE_CASE, TAILLE_CASE);
RemplirRectangle(0, 0, TAILLE_CASE, HAUTEUR * TAILLE_CASE);
RemplirRectangle(0, (HAUTEUR - 1) * TAILLE_CASE, LARGEUR * TAILLE_CASE, TAILLE_CASE);
RemplirRectangle((LARGEUR - 1) * TAILLE_CASE, 0, TAILLE_CASE, HAUTEUR * TAILLE_CASE);
} }