serpent bouge et terrain fini
This commit is contained in:
161
main.c
161
main.c
@@ -1,22 +1,167 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <graph.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LARGEUR_FENETRE 1050
|
||||
#define HAUTEUR_FENETRE 750
|
||||
#define TAILLE_CASE 15
|
||||
#define NBR_LIGNE 40
|
||||
#define NBR_COLONNE 60
|
||||
#define NBR_LIGNE 40
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
for (i = 0; i < taille; i++) {
|
||||
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);
|
||||
}
|
||||
Attendre(100);
|
||||
}
|
||||
|
||||
void timer(){
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
int case_ligne,case_colonne;
|
||||
int touche,i, j,x=0,direction=1,longueurSerpent=10;
|
||||
Segment serpent[10];
|
||||
couleur c;
|
||||
c=CouleurParComposante(100,250,100);
|
||||
couleur v;
|
||||
couleur s;
|
||||
|
||||
InitialiserGraphique();
|
||||
CreerFenetre(400,225,900,600);
|
||||
CreerFenetre(400, 250, LARGEUR_FENETRE, HAUTEUR_FENETRE);
|
||||
srand(time(NULL));
|
||||
c = CouleurParNom("black");
|
||||
s = CouleurParNom("yellow");
|
||||
v = CouleurParNom("green");
|
||||
EffacerEcran(c);
|
||||
for (case_ligne=0;case_ligne<NBR_LIGNE;case_ligne++){
|
||||
for (case_colonne=0;case_colonne<NBR_COLONNE;case_colonne++)
|
||||
DessinerRectangle(case_ligne*TAILLE_CASE,case_colonne*TAILLE_CASE,TAILLE_CASE,TAILLE_CASE);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user