2023-12-19 14:46:20 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <graph.h>
|
|
|
|
#include <time.h>
|
2023-12-20 13:20:49 +01:00
|
|
|
#define CYCLE 100000L
|
2023-12-19 17:27:02 +01:00
|
|
|
|
|
|
|
void DessinerScene(int murx[], int mury[], int minute, int seconde, char timer []){
|
2023-12-19 14:46:20 +01:00
|
|
|
int mur;
|
|
|
|
int i;
|
|
|
|
snprintf(timer,6,"%02d:%02d", minute ,seconde);
|
2023-12-20 21:26:34 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(218,209,77));
|
2023-12-19 14:46:20 +01:00
|
|
|
RemplirRectangle(20,20,1160,700);
|
|
|
|
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
|
|
|
|
srand(time(NULL));
|
|
|
|
for(i=0; i<30; i++){
|
|
|
|
murx[i] = ((rand() % (55)+1)*20);
|
|
|
|
mury[i] = ((rand() % (35)+1)*20);
|
2023-12-20 21:26:34 +01:00
|
|
|
ChargerImage("../img/cactus.png", murx[i], mury[i],0,0, 20,20);
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void Score(int segment){
|
|
|
|
int nombre;
|
|
|
|
char score[4];
|
2023-12-19 17:27:02 +01:00
|
|
|
nombre= (segment-10)*25;
|
2023-12-19 14:46:20 +01:00
|
|
|
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);
|
|
|
|
}
|
2023-12-19 17:27:02 +01:00
|
|
|
void Update_Timer(int minute, int seconde, char timer[]){
|
2023-12-19 14:46:20 +01:00
|
|
|
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,"time: ",2);
|
|
|
|
EcrireTexte(120,760,timer,2);
|
|
|
|
}
|
2023-12-19 17:27:02 +01:00
|
|
|
void Update_Serpent(int pos_x[], int pos_y[], int segment, int old_x[], int old_y[], int* direction){
|
2023-12-19 14:46:20 +01:00
|
|
|
int i = 0;
|
|
|
|
for (i=1 ; i<segment ; i++){
|
|
|
|
pos_x[i]=old_x[i-1];
|
|
|
|
pos_y[i]=old_y[i-1];
|
|
|
|
}
|
2023-12-19 17:27:02 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-12-20 14:27:23 +01:00
|
|
|
for (i=0 ; i<segment ; i++){
|
|
|
|
ChargerImage("../img/serpent2.png", pos_x[i], pos_y[i],0,0, 20,20);
|
2023-12-20 13:20:49 +01:00
|
|
|
}
|
2023-12-19 17:27:02 +01:00
|
|
|
for (i=0 ; i<segment ; i++){
|
2023-12-20 21:26:34 +01:00
|
|
|
ChoisirCouleurDessin(CouleurParComposante(218,209,77));
|
2023-12-20 14:27:23 +01:00
|
|
|
RemplirRectangle(pos_x[segment-1], pos_y[segment-1], 20,20);
|
|
|
|
|
2023-12-19 14:46:20 +01:00
|
|
|
old_x[i]=pos_x[i];
|
|
|
|
old_y[i]=pos_y[i];
|
2023-12-20 14:27:23 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 17:27:02 +01:00
|
|
|
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
void Collision_Serpent(int pos_x[], int pos_y[], int segment, int murx[], int mury[], int *go_on){
|
|
|
|
int i;
|
|
|
|
/*Serpent contre coté*/
|
2023-12-20 14:27:23 +01:00
|
|
|
if (pos_x[0] >1160 || pos_x[0]<=0){
|
2023-12-19 14:46:20 +01:00
|
|
|
*go_on=0;
|
|
|
|
}
|
|
|
|
/*Serpent contre coté*/
|
2023-12-20 14:27:23 +01:00
|
|
|
if (pos_y[0]<20 || pos_y[0] >=700){
|
2023-12-19 14:46:20 +01:00
|
|
|
*go_on=0;
|
|
|
|
}
|
|
|
|
/*Serpent contre Serpent*/
|
|
|
|
for (i = 1; i < segment; i++) {
|
2023-12-19 17:27:02 +01:00
|
|
|
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]){
|
2023-12-19 14:46:20 +01:00
|
|
|
*go_on = 0;
|
2023-12-19 17:27:02 +01:00
|
|
|
}
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
/*Serpent contre mur*/
|
|
|
|
for(i=0; i<30;i++){
|
|
|
|
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
|
|
|
|
*go_on=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 17:27:02 +01:00
|
|
|
void Timer(int *minute, int *seconde, unsigned long int *suivant, int *seconde_actuel, int *old_seconde, char timer[]){
|
|
|
|
if(Microsecondes()> *suivant){
|
|
|
|
*suivant = Microsecondes()+CYCLE;
|
|
|
|
*seconde_actuel = (*suivant/1000000)%10;
|
2023-12-19 14:46:20 +01:00
|
|
|
if(seconde_actuel !=old_seconde){
|
2023-12-19 17:27:02 +01:00
|
|
|
*old_seconde = *seconde_actuel;
|
|
|
|
if(*seconde == 59){
|
|
|
|
*minute=*minute+1;
|
|
|
|
*seconde=0;
|
|
|
|
Update_Timer(*minute, *seconde, timer);
|
|
|
|
}else{
|
|
|
|
*seconde = *seconde+1;
|
|
|
|
Update_Timer(*minute, *seconde, timer);
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Controle(int *direction, int last_direction, int *go_on) {
|
|
|
|
int t;
|
|
|
|
while(ToucheEnAttente()) {
|
|
|
|
t = Touche();
|
|
|
|
switch(t) {
|
|
|
|
case XK_Left:
|
|
|
|
if (last_direction != 4) {
|
|
|
|
*direction = 3;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Right:
|
|
|
|
if (last_direction != 3) {
|
|
|
|
*direction = 4;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Up:
|
|
|
|
if (last_direction != 2) {
|
|
|
|
*direction = 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Down:
|
|
|
|
if (last_direction != 1) {
|
|
|
|
*direction = 2;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case XK_Escape:
|
|
|
|
*direction = 0;
|
|
|
|
*go_on = 0;
|
|
|
|
return;
|
|
|
|
case XK_p:
|
|
|
|
*direction = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Serpent(int pos_x[], int pos_y[], int old_x[], int old_y[], int *segment, int murx[], int mury[], int *go_on, int *direction) {
|
|
|
|
int serpent;
|
2023-12-20 14:27:23 +01:00
|
|
|
int i = 0;
|
2023-12-19 17:27:02 +01:00
|
|
|
pos_x[0] = 600;
|
|
|
|
pos_y[0] = 400;
|
2023-12-19 14:46:20 +01:00
|
|
|
for (i = 0; i < *segment; i++){
|
2023-12-19 17:27:02 +01:00
|
|
|
pos_x[i]= pos_x[0];
|
|
|
|
pos_y[i]= pos_y[0];
|
2023-12-19 14:46:20 +01:00
|
|
|
old_x[i]=pos_x[i];
|
|
|
|
old_y[i]=pos_y[i];
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 21:26:34 +01:00
|
|
|
void InitialiserOeufs(int oeufx[], int oeufy[], int segment) {
|
2023-12-19 14:46:20 +01:00
|
|
|
int p;
|
2023-12-20 21:26:34 +01:00
|
|
|
int oeuf;
|
2023-12-19 14:46:20 +01:00
|
|
|
for (p = 0; p < 5; p++) {
|
2023-12-20 21:26:34 +01:00
|
|
|
oeufx[p] = ((rand() % (55) + 1) * 20);
|
|
|
|
oeuf[p] = ((rand() % (35) + 1) * 20);
|
|
|
|
ChargerImage("../img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20);
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 21:26:34 +01:00
|
|
|
void Oeuf(int pos_x[], int pos_y[], int oeufx[], int oeufy[], int *segment){
|
2023-12-19 14:46:20 +01:00
|
|
|
int p;
|
2023-12-20 21:26:34 +01:00
|
|
|
int oeuf;
|
2023-12-19 14:46:20 +01:00
|
|
|
for (p = 0; p < 5; p++) {
|
2023-12-20 21:26:34 +01:00
|
|
|
ChargerImage("../img/oeuf.png", oeufx[p], oeufy[p],0,0, 20,20);
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
for(p=0; p<5; p++){
|
2023-12-20 21:26:34 +01:00
|
|
|
if(oeufx[p]==pos_x[0] && oeufy[p]==pos_y[0]){
|
2023-12-19 14:46:20 +01:00
|
|
|
(*segment) +=2;
|
2023-12-20 21:26:34 +01:00
|
|
|
oeufx[p] = ((rand() % (55)+1)*20);
|
|
|
|
oeufy[p] = ((rand() % (35)+1)*20);
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-20 13:20:49 +01:00
|
|
|
/*void Menu_debut(){
|
|
|
|
affiche = ChargerSprite("Img/image.png");
|
|
|
|
AfficherSprite(affiche,0,0);
|
2023-12-20 08:56:34 +01:00
|
|
|
}
|
2023-12-20 13:20:49 +01:00
|
|
|
Menu_debut();*/
|
2023-12-20 08:56:34 +01:00
|
|
|
|
|
|
|
|
2023-12-19 14:46:20 +01:00
|
|
|
int main(void){
|
|
|
|
int segment = 10;
|
|
|
|
int go_on = 1;
|
|
|
|
int direction = 4;
|
2023-12-19 17:27:02 +01:00
|
|
|
int minute = 0;
|
|
|
|
int seconde = 0;
|
|
|
|
int seconde_actuel = 0;
|
|
|
|
int old_seconde = 0;
|
|
|
|
unsigned long int suivant;
|
2023-12-20 13:20:49 +01:00
|
|
|
int pos_x[2400];
|
|
|
|
int pos_y[2400];
|
|
|
|
int old_x[2400];
|
|
|
|
int old_y[2400];
|
2023-12-20 21:26:34 +01:00
|
|
|
int oeufx[5];
|
|
|
|
int oeufy[5];
|
2023-12-19 14:46:20 +01:00
|
|
|
int murx[30];
|
|
|
|
int mury[30];
|
2023-12-19 17:27:02 +01:00
|
|
|
char timer[6];
|
2023-12-19 14:46:20 +01:00
|
|
|
int *pointeur_segment = &segment;
|
|
|
|
int *pointeur_go_on = &go_on;
|
|
|
|
int *pointeur_direction = &direction;
|
2023-12-19 17:27:02 +01:00
|
|
|
int *pointeur_minute = &minute;
|
|
|
|
int *pointeur_seconde = &seconde;
|
|
|
|
unsigned long int *pointeur_suivant = &suivant;
|
|
|
|
int *pointeur_seconde_actuel = &seconde_actuel;
|
|
|
|
int *pointeur_old_seconde = &old_seconde;
|
2023-12-19 14:46:20 +01:00
|
|
|
InitialiserGraphique();
|
|
|
|
CreerFenetre(350,100,1200,900);
|
|
|
|
EffacerEcran(CouleurParComposante(0,0,0));
|
|
|
|
suivant = Microsecondes()+CYCLE;
|
|
|
|
old_seconde=(suivant/1000000)%10;
|
2023-12-19 17:27:02 +01:00
|
|
|
DessinerScene(murx, mury, minute, seconde ,timer);
|
2023-12-20 21:26:34 +01:00
|
|
|
InitialiserOeufs(oeufx, oeufy, segment);
|
2023-12-19 17:27:02 +01:00
|
|
|
Serpent(pos_x, pos_y, old_x, old_y, pointeur_segment, murx, mury, pointeur_go_on, pointeur_direction);
|
2023-12-19 14:46:20 +01:00
|
|
|
while(go_on){
|
2023-12-19 17:27:02 +01:00
|
|
|
Timer( pointeur_minute, pointeur_seconde, pointeur_suivant, pointeur_seconde_actuel, pointeur_old_seconde, timer);
|
|
|
|
Score(segment);
|
2023-12-19 14:46:20 +01:00
|
|
|
Controle(pointeur_direction, 0, pointeur_go_on);
|
2023-12-19 17:27:02 +01:00
|
|
|
Update_Serpent(pos_x, pos_y, segment, old_x, old_y, pointeur_direction);
|
|
|
|
Collision_Serpent(pos_x, pos_y, segment, murx, mury, pointeur_go_on);
|
2023-12-20 13:20:49 +01:00
|
|
|
usleep(100000);
|
2023-12-20 21:26:34 +01:00
|
|
|
Oeuf(pos_x, pos_y, oeufx, oeufy, pointeur_segment);
|
2023-12-19 14:46:20 +01:00
|
|
|
}
|
|
|
|
FermerGraphique();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|