Files
SAE11_2023/serpent.c

206 lines
11 KiB
C

#include <stdlib.h>
#include <graph.h>
#include <stdio.h>
#include "serpent.h"
#include "menu_terrain.h"
#include "temps_score.h"
/*Fonction permettant de créer un serpent de base avec une taille donnée*/
void Creation_Serpent(Segment serpent[], int taille){
int i;
for (i = 0; i < taille; i++)
{
serpent[i].x = (COLONNES / 2) * TAILLE_CASE;
serpent[i].y = (LIGNES / 2) * TAILLE_CASE;
}
}
/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le haut de l'écran*/
void Afficher_Serpent_Haut(Segment serpent[], int taille, int rotation){
int i;
if (rotation == 2){
ChargerImage("Image/Serpent_Tete_Haut.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
for (i = 1; i < taille-1; i++){
if (serpent[i-1].y == serpent[i].y){
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else{
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
ChoisirCouleurDessin(CouleurParComposante(0, 217, 87));
RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE);
if (serpent[taille-2].x == serpent[taille-1].x){
if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Haut.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}else if(serpent[taille-2].y == serpent[taille-1].y){
if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
}
}
/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le bas de l'écran*/
void Afficher_Serpent_Bas(Segment serpent[], int taille, int rotation){
int i;
if(rotation == -2){
ChargerImage("Image/Serpent_Tete_Bas.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
for (i = 1; i < taille-1; i++){
if (serpent[i-1].y == serpent[i].y){
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else{
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
ChoisirCouleurDessin(CouleurParComposante(0, 217, 87));
RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE);
if (serpent[taille-2].x == serpent[taille-1].x){
if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Haut2.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}else if(serpent[taille-2].y == serpent[taille-1].y){
if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
}
}
/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le droite de l'écran*/
void Afficher_Serpent_Droite(Segment serpent[], int taille, int rotation){
int i;
if (rotation == 1){
ChargerImage("Image/Serpent_Tete_Droite.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
for (i = 1; i < taille-1; i++){
if (serpent[i-1].x == serpent[i].x){
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else{
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
ChoisirCouleurDessin(CouleurParComposante(0, 217, 87));
RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE);
if (serpent[taille-2].x == serpent[taille-1].x){
if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Haut.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}else if(serpent[taille-2].y == serpent[taille-1].y){
if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
}
}
/*Fonction permettant d'afficher le serpent avec une taille donnée et si la tête vise le gauche de l'écran*/
void Afficher_Serpent_Gauche(Segment serpent[], int taille, int rotation){
int i;
if (rotation == -1){
ChargerImage("Image/Serpent_Tete_Gauche.png", serpent[0].x, serpent[0].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
for (i = 1; i < taille-1; i++){
if (serpent[i-1].x == serpent[i].x){
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Vertical.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else{
RemplirRectangle(serpent[i].x, serpent[i].y, TAILLE_CASE, TAILLE_CASE);
ChargerImage("Image/Serpent_Corps_Horizontal.png", serpent[i].x, serpent[i].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
ChoisirCouleurDessin(CouleurParComposante(0, 217, 87));
RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE);
if (serpent[taille-2].x == serpent[taille-1].x){
if (serpent[taille-2].y - (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Bas.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].y + (1 * TAILLE_CASE) == serpent[taille-1].y){
ChargerImage("Image/Serpent_Queu_Haut.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}else if(serpent[taille-2].y == serpent[taille-1].y){
if (serpent[taille-2].x - (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Droite.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}else if(serpent[taille-2].x + (1 * TAILLE_CASE) == serpent[taille-1].x){
ChargerImage("Image/Serpent_Queu_Gauche.png", serpent[taille-1].x, serpent[taille-1].y, 0, 0, TAILLE_CASE, TAILLE_CASE);
}
}
}
}
/*Fonction permettant d'afficher le serpent avec une taille donnée*/
void Afficher_Serpent(Segment serpent[], int taille, int rotation)
{
ChoisirCouleurDessin(CouleurParComposante(0, 217, 87));
RemplirRectangle(serpent[0].x, serpent[0].y, TAILLE_CASE, TAILLE_CASE);
RemplirRectangle(serpent[taille-1].x, serpent[taille-1].y, TAILLE_CASE, TAILLE_CASE);
Afficher_Serpent_Haut( serpent, taille, rotation);
Afficher_Serpent_Bas( serpent, taille, rotation);
Afficher_Serpent_Droite( serpent, taille, rotation);
Afficher_Serpent_Gauche( serpent, taille, rotation);
}
/*Fonction permettant au serpent de se déplacer*/
void Deplacer_Serpent(int *longueurSerpent, Segment serpent[], int direction, unsigned int vitesse_serpent){
int i;
ChoisirCouleurDessin(CouleurParComposante(0, 217, 87));
RemplirRectangle(serpent[*longueurSerpent-1].x, serpent[*longueurSerpent-1].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 -1:
serpent[0].x -= TAILLE_CASE;
break;
case 2:
serpent[0].y -= TAILLE_CASE;
break;
case -2:
serpent[0].y += TAILLE_CASE;
break;
}
Afficher_Serpent(serpent, *longueurSerpent, direction);
Attendre(vitesse_serpent);
}
/*Fonction l'emplacement de la tête du serpent pour la mort*/
void Mort_Serpent(int longueurSerpent, Segment serpent[], int score, int minutes, int secondes, char texte[], int* rejouer, int* gameover){
int i;
if (serpent[0].x < 5 * TAILLE_CASE || serpent[0].x >= (COLONNES * TAILLE_CASE) + 5 * TAILLE_CASE || serpent[0].y < CONTOURE_H * TAILLE_CASE || serpent[0].y >= (LIGNES * TAILLE_CASE) + 2 * TAILLE_CASE){
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(serpent[0].x, serpent[0].y, TAILLE_CASE, TAILLE_CASE);
Attendre(1000);
*gameover = 0;
/*Execution de la fontion perdu*/
Perdu(texte, score, minutes, secondes, rejouer);
}
for (i = 1; i < longueurSerpent; i++){
if (serpent[0].x == serpent[i].x && serpent[0].y == serpent[i].y){
Attendre(1000);
*gameover = 0;
Perdu(texte, score, minutes, secondes, rejouer);
}
}
}