reglage bug pomme

This commit is contained in:
2023-11-30 09:59:11 +01:00
parent 9f286a6f80
commit 1b79b72792
2 changed files with 158 additions and 167 deletions

BIN
JEUX_SERPENT/fond.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

@@ -5,201 +5,192 @@
#include <unistd.h> #include <unistd.h>
#define CYCLE 10000L #define CYCLE 10000L
/* Variables */ /*variables*/
int seconde = 0; /*timer*/
int minute = 0; int seconde=0;
int seconde_actuel = 0; int minute=0;
int old_seconde = 0; int seconde_actuel=0;
int old_seconde=0;
char timer[6]; char timer[6];
unsigned long int suivant; unsigned long int suivant;
int go_on = 1; /*fin de jeu*/
int go_on=1;
/*serpent*/
int serpent; int serpent;
int x = 600; int x = 600;
int y = 400; int y = 400;
int direction = 4; int direction = 4; /*1 : vers le haut , 2 : vers le bas; 3 : vers la gauche, 4 : vers la droite*/
int t; int t;
int segment = 10; int segment=10;
int i=0;
int i = 0;
int pos_x[60]; int pos_x[60];
int pos_y[60]; int pos_y[60];
int old_x[60]; int old_x[60];
int old_y[60]; int old_y[60];
int pomme, p, pp; /*variable pomme*/
int pomx[5]; int p=0;
int pomy[5]; int pp=0;
int pomme, pommex[5], pommey[5];
int fond;
/*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");
void GenererPomme(int indice) { for (i = 0; i < segment; i++){
do { AfficherSprite(serpent, x-(i*20), y);
pomx[indice] = ((rand() % 58) + 1) * 20; pos_x[i]=x-(i*20);
pomy[indice] = ((rand() % 35) + 1) * 20; pos_y[i]=y;
} while (pomx[indice] < 20 || pomx[indice] > 1160 || pomy[indice] < 20 || pomy[indice] > 700); old_y[i]=pos_y[i];
old_x[i]=pos_x[i];
printf("Nouvelle pomme à la position : (%d, %d)\n", pomx[indice], pomy[indice]); }
AfficherSprite(pomme, pomx[indice], pomy[indice]);
}
void DessinerScene() {
ChoisirCouleurDessin(CouleurParComposante(0, 100, 0));
RemplirRectangle(20, 20, 1160, 700);
snprintf(timer, 6, "%02d:%02d", minute, seconde);
ChoisirCouleurDessin(CouleurParComposante(0, 255, 0));
pomme = ChargerSprite("pomme.png");
serpent = ChargerSprite("serpent.png");
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
EcrireTexte(10, 760, timer, 2);
srand(time(NULL)); srand(time(NULL));
pomme=ChargerSprite("pomme.png");
for (p = 0; p < 5; p++) { for (p = 0; p < 5; p++) {
GenererPomme(p); pommex[p] = ((rand() % (58)+1)*20);
} pommey[p] = ((rand() % (35)+1)*20);
AfficherSprite(pomme, pommex[p], pommey[p]);
for (i = 0; i < segment; i++) {
AfficherSprite(serpent, x - (i * 20), y);
pos_x[i] = x - (i * 20);
pos_y[i] = y;
old_y[i] = pos_y[i];
old_x[i] = pos_x[i];
} }
} }
/*Fonction pour mettre à jour unuquement le timer*/
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,700,1200,800);
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255)); ChoisirCouleurDessin(CouleurParComposante(255,255,255));
EcrireTexte(10, 760, timer, 2); EcrireTexte(10,760,timer,2);
} }
/*fonction pour mettre à jour la position du serpent*/
void Update_Serpent() { void Update_Serpent(){
ChoisirCouleurDessin(CouleurParComposante(0, 100, 0)); AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
RemplirRectangle(20, 20, 1160, 700);
AfficherSprite(serpent, pos_x[0], pos_y[0]); AfficherSprite(serpent, pos_x[0], pos_y[0]);
for (i=1 ; i<segment ; i++){
for (i = 1; i < segment; i++) { pos_x[i]=old_x[i-1];
pos_x[i] = old_x[i - 1]; pos_y[i]=old_y[i-1];
pos_y[i] = old_y[i - 1];
AfficherSprite(serpent, pos_x[i], pos_y[i]); AfficherSprite(serpent, pos_x[i], pos_y[i]);
} }
old_x[0]=pos_x[0];
if (i > segment) { old_y[0]=pos_y[0];
pos_x[i] = 0; for (i=1 ; i<segment ; i++){
pos_y[i] = 0; old_x[i]=pos_x[i];
segment--; old_y[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 Terrain() { void Terrain(){
if (pos_x[0] > 1180 || pos_x[0] < 20 || pos_y[0] < 20 || pos_y[0] > 700) { if (pos_x[0] >1180 || pos_x[0]<20)
go_on = 0; go_on=0;
ChoisirCouleurDessin(CouleurParComposante(255, 0, 0)); if (pos_y[0]<0 || pos_y[0] >700)
EcrireTexte(500, 400, "Game Over", 2); go_on=0;
}
} }
/*Fonction pour calculer le temps*/
void Timer() { void Timer(){
if (Microsecondes() > suivant) { if(Microsecondes()> suivant){
suivant = Microsecondes() + CYCLE; suivant = Microsecondes()+CYCLE;
seconde_actuel = (suivant / 1000000) % 10; seconde_actuel = (suivant/1000000)%10;
if (seconde_actuel != old_seconde) { if(seconde_actuel !=old_seconde){
old_seconde = seconde_actuel; old_seconde = seconde_actuel;
if (seconde == 59) { if(seconde == 59){
minute = minute + 1; minute=minute+1;
seconde = 0; seconde=0;
Update_Timer(); Update_Timer();
} else { }else{
seconde = seconde + 1; seconde = seconde+1;
Update_Timer(); Update_Timer();
} }
} }
} }
} }
/*Input Serpent*/
void Controle() { void Controle(){
while (ToucheEnAttente()) { while(ToucheEnAttente()){
t = Touche(); t = Touche();
switch (t) { switch(t){
case XK_Left: case XK_Left :
direction = 3; direction=3;
break; break;
case XK_Right: case XK_Right:
direction = 4; direction=4;
break; break;
case XK_Up: case XK_Up:
direction = 1; direction=1;
break; break;
case XK_Down: case XK_Down:
direction = 2; direction=2;
break; break;
case XK_Escape: case XK_Escape:
go_on = 0; go_on=0;
break; break;
case XK_p:
direction = 0;
break; break;
} }
} }
} }
void Serpent() { /*Avancement automatique du serpent en fonction de la direction*/
if (direction == 1) { void Serpent(){
pos_y[0] = old_y[0] - 20; if (direction == 1){
pos_y[0]=old_y[0]-20;
} }
if (direction == 2) { if (direction == 2){
pos_y[0] = old_y[0] + 20; pos_y[0]=old_y[0]+20;
} }
if (direction == 3) { if (direction == 3){
pos_x[0] = old_x[0] - 20; pos_x[0]=old_x[0]-20;
} }
if (direction == 4) { if (direction == 4){
pos_x[0] = old_x[0] + 20; pos_x[0]=old_x[0]+20;
} }
Update_Serpent(); Update_Serpent();
Terrain(); Terrain();
usleep(100000); usleep(100000);
} }
/*Apparition aléatoire des pommes*/
void Pomme(){
for (pp = 0; pp < 5; ++pp)
{
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
}
int main() { /*Fonction Principale*/
int main(){
/* paramétrage de la fenêtre + chargement première scène */
InitialiserGraphique(); InitialiserGraphique();
CreerFenetre(350, 100, 1200, 800); CreerFenetre(350,100,1200,800);
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();
while (go_on) {
/*Boucle Principale du Programme*/
while(go_on){
Timer(); Timer();
Controle(); Controle();
Serpent(); Serpent();
Pomme();
} }
for (p = 0; p < 5; p++) {
AfficherSprite(pomme, pomx[p], pomy[p]);
}
usleep(3000000);
FermerGraphique();
return 0;
/* fermeture de la fenêtre si ECHAP pressé*/
FermerGraphique();
return EXIT_SUCCESS;
} }