SAE11_2023/SAE_semestre1/src/serpent.c

111 lines
2.9 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
#include "../fichier.h/serpent.h"
#include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
void InitialiserSerpent(Serpent *serpent, int x, int y) {
serpent->longueur = 10;
serpent->directionX = 1;
serpent->directionY = 0;
serpent->corps[0].x = x;
serpent->corps[0].y = y;
}
void DeplacerSerpent(Serpent *serpent) {
int i;
for ( i = serpent->longueur - 1; i > 0; i--) {
serpent->corps[i] = serpent->corps[i - 1];
}
serpent->corps[0].x += serpent->directionX * 20;
serpent->corps[0].y += serpent->directionY * 20;
2023-12-04 17:27:48 +01:00
}
void Controle(int* direction , int* go_on) {
int t;
int last_direction = 0;
while(ToucheEnAttente()) {
t = Touche();
switch(t) {
case XK_Left:
if (last_direction != 4) {
*direction = 3;
printf("gauche\n");
}
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;
}
2023-12-04 17:27:48 +01:00
}
}
int VerifierCollision(const Serpent *serpent) {
int teteX = serpent->corps[0].x;
int teteY = serpent->corps[0].y;
2023-12-07 14:32:42 +01:00
int i;
if (teteX < 0 || teteX >= 1200 || teteY < 0 || teteY >= 900-80) {
return 1;
2023-12-04 17:27:48 +01:00
}
for (i = 1; i < serpent->longueur; i++) {
if (teteX == serpent->corps[i].x && teteY == serpent->corps[i].y) {
return 1;
}
}
return 0;
2023-12-07 14:32:42 +01:00
}
void Score(int segment){
int nombre;
char score[4];
nombre = (segment-10)*10;
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-07 14:32:42 +01:00
}
void DessinerSerpent(const Serpent *serpent) {
int i;
couleur couleurSerpent = CouleurParComposante(34, 139, 34);
2023-12-07 14:32:42 +01:00
ChoisirEcran(1);
EffacerEcran(CouleurParNom("grey"));
ChoisirCouleurDessin(couleurSerpent);
/* Afficher la tête du serpent */
RemplirRectangle(serpent->corps[0].x, serpent->corps[0].y, 20, 20);
for (i = 0; i < serpent->longueur; i++) {
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, 20, 20);
}
ChoisirEcran(0);
AfficherFenetre();
CopierZone(1, 0, 0, 0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0, 0);
2023-12-04 17:27:48 +01:00
}