Ajout paramètre dans le fichier game.c

This commit is contained in:
2023-12-18 14:59:07 +01:00
parent e5def4ff96
commit 12062a0d0d
9 changed files with 397 additions and 159 deletions

20
SAE_semestre1/out/fruit.c Normal file
View File

@@ -0,0 +1,20 @@
#include <stdlib.h>
#include <graph.h>
#include <time.h>
#include "fruit.h"
void pomme(){
int pos_x[60];
int pos_y[60];
int p, pp;
int pomme, pommex[5], pommey[5];
for(p=0; p<6; p++){
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
pommex[p] = ((rand() % (60)+1)*20);
pommey[p] = ((rand() % (27)+1)*20);
}
}
for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
}

21
SAE_semestre1/out/fruit.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef FRUIT_H
#define FRUIT_H
#include"serpent.h"
typedef struct {
int x;
int y;
int estMangee;
int sprite; /* Ajout d'un identifiant pour le sprite de la pomme */
} Pomme;
void InitialiserPommes(Pomme pommes[], int nombrePommes);
void GenererPommes(Pomme pommes[], int nombrePommes);
void AfficherPommes(Pomme pommes[], int nombrePommes);
void MangerPomme(Pomme pommes[], int nombrePommes, int x, int y);
void DessinerPomme(int x, int y, int sprite);
int ChargerSprite(char *file);
void AfficherSprite(int n, int x, int y);
void LibererSprite(int n);
#endif

87
SAE_semestre1/out/main.c Normal file
View File

@@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include "serpent.h"
#include "fruit.h"
#define LARGEUR_FENETRE 1600 /* Largeur de la fenêtre (60 colonnes de jeu + murs de 2 cases de chaque côté) */
#define HAUTEUR_FENETRE 1000 /* Hauteur de la fenêtre (40 lignes de jeu + murs de 2 cases en haut et en bas) */
#define TAILLE_CASE 20 /* Taille d'une case pour le jeu de Snake */
#define NB_COLONNES 60 /* Nombre de colonnes du jeu */
#define NB_LIGNES 40 /* Nombre de lignes du jeu */
#define CYCLE 100000
#define VITESSE_SERPENT 100000
#define NB_POMMES 5
int main() {
couleur couleurFond = CouleurParComposante(200, 200, 200);
int touchePressee, i;
Serpent serpent;
unsigned long tempsPrecedent = Microsecondes();
unsigned long tempsActuel;
couleur couleurMurs = CouleurParComposante(0, 0, 0);
Pomme pommes[NB_POMMES];
srand(time(NULL));
InitialiserGraphique();
CreerFenetre(10, 10, LARGEUR_FENETRE, HAUTEUR_FENETRE);
EffacerEcran(couleurFond);
AfficherFenetre();
pomme();
InitialiserSerpent(&serpent, LARGEUR_FENETRE / 2, HAUTEUR_FENETRE / 2);
while (1) {
if (ToucheEnAttente()) {
int touche = Touche();
if (touche == XK_Left && serpent.directionX != 1) {
serpent.directionX = -1;
serpent.directionY = 0;
} else if (touche == XK_Right && serpent.directionX != -1) {
serpent.directionX = 1;
serpent.directionY = 0;
} else if (touche == XK_Up && serpent.directionY != 1) {
serpent.directionX = 0;
serpent.directionY = -1;
} else if (touche == XK_Down && serpent.directionY != -1) {
serpent.directionX = 0;
serpent.directionY = 1;
} else if (touche == XK_Escape) {
break;
}
}
tempsActuel = Microsecondes();
if (tempsActuel - tempsPrecedent >= VITESSE_SERPENT) {
DeplacerSerpent(&serpent);
if (VerifierCollision(&serpent)) {
FermerGraphique();
return EXIT_SUCCESS;
}
EffacerEcran(couleurFond);
DessinerSerpent(&serpent);
AfficherFenetre();
tempsPrecedent = tempsActuel;
ChoisirCouleurDessin(couleurMurs);
RemplirRectangle(0, HAUTEUR_FENETRE - 80, LARGEUR_FENETRE, 80);
}
}
for (i = 0; i < NB_POMMES; i++) {
LibererSprite(pommes[i].sprite);
}
FermerGraphique();
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,30 @@
#ifndef SERPENT_H
#define SERPENT_H
#define LARGEUR_FENETRE 1200
#define HAUTEUR_FENETRE 900
#define TAILLE_CASE 20
#define NB_COLONNES 60
#define NB_LIGNES 40
#define CYCLE 100000
typedef struct {
int x , y;
} Corps;
typedef struct {
int longueur;
int directionX;
int directionY;
Corps corps[NB_COLONNES * NB_LIGNES];
} Serpent;
void InitialiserSerpent(Serpent *serpent, int x, int y);
void DeplacerSerpent(Serpent *serpent);
int VerifierCollision(const Serpent *serpent);
void DessinerSerpent(const Serpent *serpent);
#endif

View File

@@ -1,20 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include"serpent.h"
int test(int nombre)
{
return nombre;
#include <unistd.h>
#define LARGEUR_FENETRE 1200
#define HAUTEUR_FENETRE 900
#define TAILLE_CASE 20
#define NB_COLONNES 60
#define NB_LIGNES 40
#define CYCLE 100000
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;
}
int main(void)
{
void DeplacerSerpent(Serpent *serpent) {
int i;
int toto = 5;
for ( i = serpent->longueur - 1; i > 0; i--) {
serpent->corps[i] = serpent->corps[i - 1];
}
serpent->corps[0].x += serpent->directionX * TAILLE_CASE;
serpent->corps[0].y += serpent->directionY * TAILLE_CASE;
}
int VerifierCollision(const Serpent *serpent) {
int teteX = serpent->corps[0].x;
int teteY = serpent->corps[0].y;
int i;
printf("%d",test(toto));
if (teteX < 0 || teteX >= LARGEUR_FENETRE || teteY < 0 || teteY >= HAUTEUR_FENETRE-80) {
return 1;
}
for (i = 1; i < serpent->longueur; i++) {
if (teteX == serpent->corps[i].x && teteY == serpent->corps[i].y) {
return 1;
}
}
return 0;
}
void DessinerSerpent(const Serpent *serpent) {
int i;
couleur couleurSerpent = CouleurParComposante(34, 139, 34);
ChoisirEcran(1);
EffacerEcran(CouleurParNom("grey"));
ChoisirCouleurDessin(couleurSerpent);
/* Afficher la tête du serpent */
RemplirRectangle(serpent->corps[0].x, serpent->corps[0].y, TAILLE_CASE, TAILLE_CASE);
return EXIT_SUCCESS;
}
for (i = 0; i < serpent->longueur; i++) {
RemplirRectangle(serpent->corps[i].x, serpent->corps[i].y, TAILLE_CASE, TAILLE_CASE);
}
ChoisirEcran(0);
AfficherFenetre();
CopierZone(1, 0, 0, 0, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0, 0);
}