97 lines
2.4 KiB
C
97 lines
2.4 KiB
C
|
#include<stdlib.h>
|
||
|
#include<stdio.h>
|
||
|
#include<graph.h>
|
||
|
#include<time.h>
|
||
|
|
||
|
#define HAUTEUR 40
|
||
|
#define LARGEUR 60
|
||
|
|
||
|
#define CYCLE 10000L
|
||
|
|
||
|
#define SEGMENT 25
|
||
|
|
||
|
enum Direction { UP=2, DOWN=3, LEFT=0, RIGHT=1 };
|
||
|
|
||
|
void graphique() {
|
||
|
InitialiserGraphique();
|
||
|
CreerFenetre(1700, 950, 1700, 950);
|
||
|
couleur c, b;
|
||
|
b = CouleurParComposante(0, 0, 0);
|
||
|
ChoisirCouleurDessin(b);
|
||
|
RemplirRectangle(0, 0, 1750, 950);
|
||
|
c = CouleurParComposante(111, 255, 94);
|
||
|
ChoisirCouleurDessin(c);
|
||
|
RemplirRectangle(100, 100, LARGEUR * SEGMENT, HAUTEUR * SEGMENT);
|
||
|
FermerGraphique();
|
||
|
}
|
||
|
|
||
|
void serpent(int tab[][LARGEUR], int taille) {
|
||
|
int posx_s, posy_s, i;
|
||
|
for (i = 0; i < taille; i++) {
|
||
|
posx_s = tab[i][0] * SEGMENT;
|
||
|
posy_s = tab[i][1] * SEGMENT;
|
||
|
couleur s = CouleurParComposante(255, 255, 0);
|
||
|
ChoisirCouleurDessin(s);
|
||
|
RemplirRectangle(posx_s, posy_s, SEGMENT, SEGMENT);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void mouvement(int tab[][LARGEUR], int *taille, int *dir) {
|
||
|
for (int i = *taille - 1; i > 0; i--) {
|
||
|
tab[i][0] = tab[i - 1][0];
|
||
|
tab[i][1] = tab[i - 1][1];
|
||
|
}
|
||
|
if (ToucheEnAttente()) {
|
||
|
if (Touche() == XK_Left && *dir != RIGHT) {
|
||
|
*dir = LEFT;
|
||
|
} else if (Touche() == XK_Right && *dir != LEFT) {
|
||
|
*dir = RIGHT;
|
||
|
} else if (Touche() == XK_Up && *dir != DOWN) {
|
||
|
*dir = UP;
|
||
|
} else if (Touche() == XK_Down && *dir != UP) {
|
||
|
*dir = DOWN;
|
||
|
}
|
||
|
}
|
||
|
if (*dir == LEFT) {
|
||
|
tab[0][0] -= 1;
|
||
|
if (tab[0][0] < 0) tab[0][0] = LARGEUR - 1;
|
||
|
} else if (*dir == RIGHT) {
|
||
|
tab[0][0] += 1;
|
||
|
if (tab[0][0] >= LARGEUR) tab[0][0] = 0;
|
||
|
} else if (*dir == UP) {
|
||
|
tab[0][1] -= 1;
|
||
|
if (tab[0][1] < 0) tab[0][1] = HAUTEUR - 1;
|
||
|
} else if (*dir == DOWN) {
|
||
|
tab[0][1] += 1;
|
||
|
if (tab[0][1] >= HAUTEUR) tab[0][1] = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
int dir = RIGHT;
|
||
|
int tab[HAUTEUR * LARGEUR][2];
|
||
|
unsigned long suivant;
|
||
|
int g = 0;
|
||
|
suivant = Microsecondes() + CYCLE;
|
||
|
int taille = 3;
|
||
|
|
||
|
for (int i = 0; i < taille; i++) {
|
||
|
tab[i][0] = LARGEUR / 2;
|
||
|
tab[i][1] = HAUTEUR / 2 + 1;
|
||
|
}
|
||
|
|
||
|
graphique();
|
||
|
|
||
|
while (1) {
|
||
|
if (Microsecondes() > suivant) {
|
||
|
g++;
|
||
|
graphique();
|
||
|
suivant = Microsecondes() + CYCLE;
|
||
|
}
|
||
|
mouvement(tab, &taille, &dir);
|
||
|
serpent(tab, taille);
|
||
|
}
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|