maj timer + ajout 5 pommes

This commit is contained in:
Jude Christ AISSI 2023-12-04 17:27:48 +01:00
parent 82b8b9a879
commit be324de28e
7 changed files with 89 additions and 77 deletions

View File

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

View File

@ -1,5 +1,10 @@
#ifndef SERPENT_H #ifndef SERPENT_H
#define SERPENT_H #define SERPENT_H
typedef struct{
Position *corps;
int longueur;
} Serpent;
Position pomme;
Serpent serpent;
#endif #endif

View File

View File

@ -1,5 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
afficher

View File

@ -1,23 +1,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <graph.h> #include <graph.h>
#include <time.h> #include <time.h>
#include "time.c"
#define LARGEUR_FENETRE 1250 #define LARGEUR_FENETRE 1250
#define HAUTEUR_FENETRE 750 #define HAUTEUR_FENETRE 750
#define TAILLE_CELLULE 25 #define TAILLE_CELLULE 25
#define DELAI_MILLISECONDES 100 #define DELAI_MILLISECONDES 100
#define ESPACE_NOIR 100 #define ESPACE_NOIR 100
#define CYCLE 100000L #define CYCLE 1000L
int j,i; int j,i;
int secondes = 0; int pommex[5];
int minutes = 0; int pommey[5];
int seconde_actuel = 0;
int old_seconde = 0;
char timer[6];
unsigned long int suivant;
typedef struct { typedef struct {
int x, y; int x, y;
} Position; } Position;
@ -30,8 +25,10 @@ Position pomme;
Serpent serpent; Serpent serpent;
int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/ int direction = 0; /* 0: droite, 1: bas, 2: gauche, 3: haut*/
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;
@ -40,7 +37,7 @@ void initialiserSerpent() {
void dessinerSerpent() { void dessinerSerpent() {
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);
} }
@ -70,16 +67,21 @@ void deplacerSerpent() {
} }
int i; int i;
void genererPomme() { void genererPomme() {
pomme.x = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; for(i=0; i<5; i++){
pomme.y = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE; pommex[i] = rand() % (LARGEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
pommey[i] = rand() % (HAUTEUR_FENETRE / TAILLE_CELLULE) * TAILLE_CELLULE;
}
} }
void dessinerPomme() { void dessinerPomme() {
ChoisirCouleurDessin(CouleurParComposante(255, 0, 0)); ChoisirCouleurDessin(CouleurParComposante(255, 0, 0));
RemplirRectangle(pomme.x, pomme.y, TAILLE_CELLULE, TAILLE_CELLULE); for(i=0; i<5; i++){
RemplirRectangle(pommex[i], pommey[i], TAILLE_CELLULE, TAILLE_CELLULE);
}
} }
int collisionAvecPomme() { int collisionAvecPomme() {
return (serpent.corps[0].x == pomme.x && serpent.corps[0].y == pomme.y); for(i=0;i<5;i++){
return (serpent.corps[0].x == pommex[i] && serpent.corps[i].y == pommey[i]);
}
} }
int collisionAvecSerpent() { int collisionAvecSerpent() {
@ -110,31 +112,6 @@ void dessinerDamier() {
} }
} }
} }
void Update_Timer(){
snprintf(timer,6, "%02d:%02d", minutes,secondes);
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(0, 1200, 1250, 800);
ChoisirCouleurDessin(CouleurParNom("white"));
EcrireTexte(10,800,timer,2);
}
void Timer(){
if(Microsecondes()> suivant){
suivant = Microsecondes()+CYCLE;
seconde_actuel = ((suivant/1000)%10);
if (seconde_actuel != old_seconde){
old_seconde = seconde_actuel;
if(secondes == 59){
minutes = minutes++;
secondes = 0;
Update_Timer();
}else{
secondes = secondes++;
Update_Timer();
}
}
}
}
void gestionCollision() { void gestionCollision() {
if (collisionAvecPomme()) { if (collisionAvecPomme()) {
serpent.longueur++; serpent.longueur++;
@ -154,13 +131,11 @@ void attente(int milliseconds) {
/* Attente*/ /* Attente*/
} }
} }
void jeu() { void jeu() {
InitialiserGraphique(); InitialiserGraphique();
CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR); CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE + ESPACE_NOIR);
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0)); ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
EffacerEcran(CouleurParComposante(0, 0, 0)); EffacerEcran(CouleurParComposante(0, 0, 0));
initialiserSerpent(); initialiserSerpent();
genererPomme(); genererPomme();
@ -168,12 +143,9 @@ void jeu() {
dessinerDamier(); /* Dessiner le damier en fond*/ dessinerDamier(); /* Dessiner le damier en fond*/
Timer(); Timer();
deplacerSerpent(); deplacerSerpent();
gestionCollision(); gestionCollision();
dessinerSerpent(); dessinerSerpent();
dessinerPomme(); dessinerPomme();
attente(DELAI_MILLISECONDES); /* Attente pour ralentir le serpent*/ attente(DELAI_MILLISECONDES); /* Attente pour ralentir le serpent*/
if (SourisCliquee()) { if (SourisCliquee()) {
@ -206,4 +178,4 @@ void jeu() {
int main() { int main() {
jeu(); jeu();
return 0; return 0;
} }

View File

@ -1,27 +1,64 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <graph.h> #include <graph.h>
#include "../pastille.h"
int direction = 4;
int t;
void Controle(void){
printf("JSHUIHUS");
while(ToucheEnAttente()){
t = Touche();
switch(t){
case XK_Left:
printf("Gauche\n");
break;
case XK_Right :
printf("Droite\n");
break;
case XK_Up:
printf("Haut");
break;
case XK_Down:
printf("bas");
break;
}
}
}
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 = 10;
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(CouleurParComposante(0,0,0)); /*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;
}
}
void gestionCollision() {
if (collisionAvecPomme()) {
serpent.longueur++;
serpent.corps = realloc(serpent.corps, sizeof(Position) * serpent.longueur);
genererPomme();
}
if (collisionAvecSerpent() || collisionAvecBordures()) {
FermerGraphique();
exit(EXIT_SUCCESS);
}
}

View File

@ -9,13 +9,14 @@ int seconde_actuel=0;
int old_seconde=0; int old_seconde=0;
char timer[6]; char timer[6];
unsigned long int suivant; unsigned long int suivant;
int nombre;
void Update_Timer(){ void Update_Timer(){
snprintf(timer,6,"%02d:%02d", minute, seconde); snprintf(timer,6,"%02d:%02d", minute, seconde);
ChoisirCouleurDessin(CouleurParComposante(0,0,0)); ChoisirCouleurDessin(CouleurParComposante(0,0,0));
RemplirRectangle(0,700,1200,800); RemplirRectangle(0,780,1200,800);
ChoisirCouleurDessin(CouleurParComposante(255,255,255)); ChoisirCouleurDessin(CouleurParComposante(255,255,255));
EcrireTexte(10,760,timer,2); EcrireTexte(10,800,timer,2);
} }
void Timer(){ void Timer(){
@ -35,3 +36,4 @@ void Timer(){
} }
} }
} }