194 lines
5.7 KiB
C
194 lines
5.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <graph.h>
|
|
#include <time.h>
|
|
|
|
|
|
#define LARGEUR_FENETRE 1050
|
|
#define HAUTEUR_FENETRE 750
|
|
#define TAILLE_CASE 15
|
|
#define NBR_COLONNE 60
|
|
#define NBR_LIGNE 40
|
|
|
|
void ConvertirMicrosecondes(unsigned long microsecondes, int *minutes, int *secondes) {
|
|
*secondes = (microsecondes % 60000000) / 1000000;
|
|
*minutes = *secondes % 60;
|
|
*secondes = *secondes - *minutes * 60;
|
|
}
|
|
|
|
void AttendreMillisecondes(unsigned long millisecondes) {
|
|
unsigned long debut = Microsecondes();
|
|
unsigned long attente = millisecondes * 1000;
|
|
while(Microsecondes() - debut < attente){
|
|
}
|
|
}
|
|
|
|
void timer(){
|
|
const unsigned long CYCLE = 1000000L;
|
|
|
|
unsigned long suivant = Microsecondes() + CYCLE;
|
|
|
|
while (1) {
|
|
if (Microsecondes() > suivant) {
|
|
|
|
int minutes, secondes;
|
|
ConvertirMicrosecondes(Microsecondes(), &minutes, &secondes);
|
|
|
|
printf("Temps écoulé : %02d:%02d\n", minutes, secondes);
|
|
EcrireTexte(40*TAILLE_CASE,2*TAILLE_CASE,minutes,2);
|
|
EcrireTexte(40*TAILLE_CASE,5*TAILLE_CASE,secondes,2);
|
|
suivant = Microsecondes() + CYCLE;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
typedef struct {
|
|
int x;
|
|
int y;
|
|
} Segment;
|
|
|
|
|
|
/*Fonction permettant d'afficher le serpent*/
|
|
void afficherSerpent(Segment serpent[], int taille) {
|
|
int i;
|
|
couleur couleurSerpent = CouleurParNom("yellow");
|
|
ChoisirCouleurDessin(couleurSerpent);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
serpent[i].x = LARGEUR_FENETRE / 2 + i * TAILLE_CASE;
|
|
serpent[i].y = HAUTEUR_FENETRE / 2;
|
|
RemplirRectangle(serpent[i].x , serpent[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/*Fonction recursive permettant d'afficher au moment de l'initialisation un nombre donnée en parametre de pastille Rouge (oui j'en suis fiere meme si c'est pas incroyable)*/
|
|
void afficherPastilleAleatoire(int z) {
|
|
int x,y,i;
|
|
if(z>0){
|
|
couleur rouge = CouleurParNom("red");
|
|
x = rand() % 60 * TAILLE_CASE + 5*15;
|
|
y = rand() % 40 * TAILLE_CASE + 5*15;
|
|
ChoisirCouleurDessin(rouge);
|
|
RemplirRectangle(x, y, TAILLE_CASE, TAILLE_CASE);
|
|
afficherPastilleAleatoire(z-1);
|
|
}
|
|
}
|
|
|
|
|
|
/*Fonction initialisant la fenetre de jeux en vert*/
|
|
void EcranJeu(){
|
|
int case_colone,case_ligne;
|
|
couleur v;
|
|
v = CouleurParNom("light green");
|
|
for (case_ligne = 2; case_ligne < NBR_LIGNE+5; case_ligne++) {
|
|
for (case_colone = 5; case_colone < NBR_COLONNE+5; case_colone++) {
|
|
|
|
ChoisirCouleurDessin(v);
|
|
RemplirRectangle(case_colone * TAILLE_CASE, case_ligne * TAILLE_CASE, TAILLE_CASE, TAILLE_CASE);
|
|
}
|
|
}
|
|
}
|
|
|
|
void deplacerSerpent(int longueurSerpent, Segment serpent[], int direction){
|
|
int i;
|
|
couleur c;
|
|
couleur v;
|
|
couleur s;
|
|
c = CouleurParNom("black");
|
|
s = CouleurParNom("yellow");
|
|
v = CouleurParNom("light green");
|
|
|
|
for(i=0;i<longueurSerpent;i++){
|
|
|
|
ChoisirCouleurDessin(v);
|
|
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
}
|
|
|
|
for(i=longueurSerpent-1;i>0;i--){
|
|
serpent[i] = serpent[i-1];
|
|
}
|
|
|
|
switch(direction){
|
|
case 1:
|
|
serpent[0].x += TAILLE_CASE;
|
|
break;
|
|
case 2:
|
|
serpent[0].x -= TAILLE_CASE;
|
|
break;
|
|
case 3:
|
|
serpent[0].y -= TAILLE_CASE;
|
|
break;
|
|
case 4:
|
|
serpent[0].y += TAILLE_CASE;
|
|
break;
|
|
|
|
}
|
|
|
|
for(i=0;i<longueurSerpent;i++){
|
|
|
|
ChoisirCouleurDessin(s);
|
|
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
|
|
}
|
|
AttendreMillisecondes(100);
|
|
}
|
|
|
|
|
|
int main() {
|
|
int touche,i, j,x=0,direction=1,longueurSerpent=10;
|
|
Segment* serpent = (Segment*) malloc (longueurSerpent*sizeof(Segment));
|
|
couleur c;
|
|
couleur v;
|
|
couleur s;
|
|
|
|
InitialiserGraphique();
|
|
CreerFenetre(400, 250, LARGEUR_FENETRE, HAUTEUR_FENETRE);
|
|
srand(time(NULL));
|
|
c = CouleurParNom("black");
|
|
s = CouleurParNom("yellow");
|
|
v = CouleurParNom("green");
|
|
EffacerEcran(c);
|
|
EcranJeu();
|
|
afficherPastilleAleatoire(5);
|
|
afficherSerpent(serpent, longueurSerpent);
|
|
|
|
|
|
while(1){
|
|
int touche;
|
|
|
|
|
|
|
|
if(ToucheEnAttente()){
|
|
touche = Touche();
|
|
switch(touche){
|
|
case XK_Right:
|
|
direction = 1;
|
|
break;
|
|
case XK_Left:
|
|
direction = 2;
|
|
break;
|
|
case XK_Up:
|
|
direction = 3;
|
|
break;
|
|
case XK_Down:
|
|
direction = 4;
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
}
|
|
deplacerSerpent(longueurSerpent, serpent, direction);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Touche();
|
|
FermerGraphique();
|
|
return EXIT_SUCCESS;
|
|
}
|