2023-11-28 23:04:54 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define CYCLE 10000L
|
|
|
|
|
|
|
|
/* Variables */
|
|
|
|
|
|
|
|
int seconde = 0;
|
|
|
|
int minute = 0;
|
|
|
|
int seconde_actuel = 0;
|
|
|
|
int old_seconde = 0;
|
|
|
|
char timer[6];
|
|
|
|
unsigned long int suivant;
|
|
|
|
|
|
|
|
int go_on = 1;
|
|
|
|
|
|
|
|
int serpent;
|
|
|
|
int x = 600;
|
|
|
|
int y = 400;
|
|
|
|
int direction = 4;
|
|
|
|
int t;
|
|
|
|
|
|
|
|
int segment = 10;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
int pos_x[60];
|
|
|
|
int pos_y[60];
|
|
|
|
int old_x[60];
|
|
|
|
int old_y[60];
|
|
|
|
|
|
|
|
int pomme, p, pp;
|
|
|
|
int pomx[5];
|
|
|
|
int pomy[5];
|
|
|
|
|
|
|
|
|
|
|
|
void GenererPomme(int indice) {
|
|
|
|
do {
|
|
|
|
pomx[indice] = ((rand() % 58) + 1) * 20;
|
|
|
|
pomy[indice] = ((rand() % 35) + 1) * 20;
|
|
|
|
} while (pomx[indice] < 20 || pomx[indice] > 1160 || pomy[indice] < 20 || pomy[indice] > 700);
|
|
|
|
|
|
|
|
printf("Nouvelle pomme à la position : (%d, %d)\n", pomx[indice], pomy[indice]);
|
|
|
|
|
|
|
|
AfficherSprite(pomme, pomx[indice], pomy[indice]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DessinerScene() {
|
|
|
|
snprintf(timer, 6, "%02d:%02d", minute, seconde);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
ChargerImageFond("terrain.jpg");
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
EcrireTexte(10, 760, timer, 2);
|
|
|
|
serpent = ChargerSprite("serpent.png");
|
|
|
|
pomme = ChargerSprite("pomme.png");
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
for (p = 0; p < 5; p++) {
|
|
|
|
GenererPomme(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Update_Timer() {
|
|
|
|
snprintf(timer, 6, "%02d:%02d", minute, seconde);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(0, 0, 0));
|
|
|
|
RemplirRectangle(0, 700, 1200, 800);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 255, 255));
|
|
|
|
EcrireTexte(10, 760, timer, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Update_Serpent() {
|
2023-11-28 23:06:14 +01:00
|
|
|
|
2023-11-28 23:04:54 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
if (pos_x[0] > 1180 || pos_x[0] < 20 || pos_y[0] < 20 || pos_y[0] > 700) {
|
|
|
|
go_on = 0;
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255, 0, 0));
|
|
|
|
EcrireTexte(500, 400, "Game Over", 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer() {
|
|
|
|
if (Microsecondes() > suivant) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
case XK_p:
|
|
|
|
direction = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Serpent() {
|
|
|
|
if (direction == 1) {
|
|
|
|
pos_y[0] = old_y[0] - 20;
|
|
|
|
}
|
|
|
|
if (direction == 2) {
|
|
|
|
pos_y[0] = old_y[0] + 20;
|
|
|
|
}
|
|
|
|
if (direction == 3) {
|
|
|
|
pos_x[0] = old_x[0] - 20;
|
|
|
|
}
|
|
|
|
if (direction == 4) {
|
|
|
|
pos_x[0] = old_x[0] + 20;
|
|
|
|
}
|
|
|
|
Update_Serpent();
|
|
|
|
Terrain();
|
|
|
|
usleep(100000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(350, 100, 1200, 800);
|
|
|
|
EffacerEcran(CouleurParComposante(0, 0, 0));
|
|
|
|
suivant = Microsecondes() + CYCLE;
|
|
|
|
old_seconde = (suivant / 1000000) % 10;
|
|
|
|
DessinerScene();
|
|
|
|
|
|
|
|
while (go_on) {
|
|
|
|
Timer();
|
|
|
|
Controle();
|
|
|
|
Serpent();
|
|
|
|
|
|
|
|
for (p = 0; p < 5; p++) {
|
|
|
|
AfficherSprite(pomme, pomx[p], pomy[p]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(3000000);
|
|
|
|
FermerGraphique();
|
|
|
|
return 0;
|
|
|
|
}
|