Ajout paramètre dans le fichier game.c

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

View File

@ -1,12 +1,30 @@
#ifndef SERPENT_H
#define SERPENT_H
#include "../fichier.h/terrain.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 Update_Serpent(int, int,int*, int*);
void Controle(int*, int*);
void Serpent(int*);
int Collision_Serpent(int*);
void Score(int*);
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

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;
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);
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);
printf("%d",test(toto));
return EXIT_SUCCESS;
}

View File

@ -16,56 +16,23 @@ unsigned long int suivant;
int go_on=1;
/*serpent*/
int serpent;
int x = 600;
int y = 400;
int direction = 4;
int t;
int segment=10;
int i=0;
int pos_x[60];
int pos_y[60];
int old_x[60];
int old_y[60];
int last_direction = 0;
int p=0;
int pp=0;
int pomme, pommex[5], pommey[5];
int fond;
int nombre;
char score[4];
int murx[50];
int mury[50];
int mur;
void DessinerScene(){
void DessinerScene(int murx[30], int mury[30]){
int mur;
int i;
int fond;
snprintf(timer,6,"%02d:%02d", minute ,seconde);
ChoisirCouleurDessin(CouleurParComposante(91,222,122));
RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
serpent=ChargerSprite("../img/serpent.png");
fond = ChargerSprite("../img/fond.png");
mur = ChargerSprite("../img/mur.png");
for (i = 0; i < segment; i++){
AfficherSprite(serpent, x-(i*20), y);
pos_x[i]=x-(i*20);
pos_y[i]=y;
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
srand(time(NULL));
pomme=ChargerSprite("../img/pomme.png");
for (p = 0; p < 5; p++) {
pommex[p] = ((rand() % (55)+1)*20);
pommey[p] = ((rand() % (35)+1)*20);
AfficherSprite(pomme, pommex[p],pommey[p]);
}
for(i=0; i<30; i++){
murx[i] = ((rand() % (55)+1)*20);
mury[i] = ((rand() % (35)+1)*20);
@ -89,9 +56,20 @@ void Update_Timer(){
EcrireTexte(50,760,"time: ",2);
EcrireTexte(120,760,timer,2);
}
/*serpent*/
int serpent;
int x = 600;
int y = 400;
int direction = 4;
int t;
void Update_Serpent(){
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
int segment=10;
int old_x[60];
int old_y[60];
void Update_Serpent(int *pos_x[60], int *pos_y[60], int* segment, int *old_x[60], int *old_y[60]){
int i = 0;
int serpent;
AfficherSprite(serpent, pos_x[0], pos_y[0]);
for (i=1 ; i<segment ; i++){
pos_x[i]=old_x[i-1];
@ -104,22 +82,22 @@ void Update_Serpent(){
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
}
void Collision_Serpent(){
int bombe;
bombe = ChargerSprite("../img/bombe.png");
void Collision_Serpent(int pos_x[60], int pos_y[60], int* segment, int murx[30], int mury[30]){
int mur;
int i;
int *pointeur_murx;
pointeur_murx = murx;
int *pointeur_mury;
pointeur_mury = mury;
mur = ChargerSprite("../img/mur.png");
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
ChoisirCouleurDessin(CouleurParNom("red"));
RemplirRectangle(pos_x[0],pos_x[0], 20,20);
go_on=0;
}
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
ChoisirCouleurDessin(CouleurParNom("red"));
RemplirRectangle(pos_y[0],pos_y[0], 20,20);
go_on=0;
}
/*Serpent contre Serpent*/
@ -130,7 +108,6 @@ void Collision_Serpent(){
/*Serpent contre mur*/
for(i=0; i<30;i++){
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
AfficherSprite(bombe, murx[i], mury[i]);
go_on=0;
}
}
@ -155,7 +132,8 @@ void Timer(){
}
}
void Controle() {
void Controle(int direction, int last_direction) {
int t;
while(ToucheEnAttente()) {
t = Touche();
switch(t) {
@ -189,7 +167,36 @@ void Controle() {
}
}
void Serpent() {
void Serpent(int pos_x[60], int pos_y[60], int old_x[60], int old_y[60], int* segment, int murx[30], int mury[30]) {
int x = 600;
int y = 400;
int i = 0;
/*Pointeur Position serpent*/
int *pointeur_pos_x;
pointeur_pos_x = pos_x;
int *pointeur_pos_y ;
pointeur_pos_y = pos_y;
int *pointeur_segment = &segment;
/*Pointeur ancienne position*/
int *pointeur_old_x;
pointeur_old_x = old_x;
int *pointeur_old_y;
pointeur_old_y = old_y;
/*Pointeur mur*/
int *pointeur_murx;
pointeur_murx = murx;
int *pointeur_mury;
pointeur_mury = mury;
serpent=ChargerSprite("../img/serpent.png");
for (i = 0; i < segment; i++){
AfficherSprite(serpent, x-(i*20), y);
pos_x[i]=x-(i*20);
pos_y[i]=y;
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
if (direction == 1 && direction != 2 ){
pos_y[0]=old_y[0]-20;
}
@ -203,6 +210,26 @@ void Serpent() {
pos_x[0]=old_x[0]+20;
}
Update_Serpent(*pointeur_pos_x, *pointeur_pos_y, *pointeur_segment, *pointeur_old_x, *pointeur_old_y);
Collision_Serpent(*pointeur_pos_x, *pointeur_pos_y, *pointeur_segment, *pointeur_murx, *pointeur_mury);
usleep(100000);
}
void Pomme(int pos_x[60], int pos_y[60], int *pommex[5], int *pommey[5]){
int p = 0;
int pp = 0;
int pomme;
pomme=ChargerSprite("../img/pomme.png");
for (p = 0; p < 5; p++) {
pommex[p] = ((rand() % (55)+1)*20);
pommey[p] = ((rand() % (35)+1)*20);
AfficherSprite(pomme, pommex[p],pommey[p]);
}
for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
for(p=0; p<6; p++){
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
segment+=2;
@ -210,37 +237,40 @@ void Serpent() {
pommey[p] = ((rand() % (27)+1)*20);
}
}
Update_Serpent();
Collision_Serpent();
usleep(100000);
}
void Pomme(){
for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
}
int main(){
/*Pointeur Position serpent*/
int *pointeur_pos_x;
pointeur_pos_x = pos_x;
int *pointeur_pos_y ;
pointeur_pos_y = pos_y;
int *pointeur_segment = &segment;
/*Pointeur ancienne position*/
int *pointeur_old_x;
pointeur_old_x = old_x;
int *pointeur_old_y;
pointeur_old_y = old_y;
/*Pointeur pomme*/
int *pointeur_pommex;
pointeur_pommex = pommex;
int *pointeur_pommey;
pointeur_pommey = pommey;
InitialiserGraphique();
CreerFenetre(350,100,1200,900);
EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10;
DessinerScene();
DessinerScene(30,30);
while(go_on){
Timer();
Score();
Controle();
Serpent();
Pomme();
Controle(4, 0);
Serpent(10);
Pomme(*pointeur_pos_x, *pointeur_pos_y, *pointeur_pommex, *pointeur_pommey);
}
FermerGraphique();
return EXIT_SUCCESS;

View File

@ -23,8 +23,8 @@ int main(void){
usleep(1000);
while(go_on){
Timer();
Score(pointeur_segment);
Serpent(&go_on);
Score();
Serpent();
Pomme();
}
FermerGraphique();

View File

@ -5,39 +5,24 @@
#include "../fichier.h/terrain.h"
#include "../fichier.h/pastille.h"
void Update_Serpent(int segment, int direction, int pos_x[60], int pos_y[60]){
int old_x[60];
int old_y[60];
int x = 600;
int y = 400;
int t, fond, i = 0, serpent;
fond = ChargerSprite("../img/fond.png");
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
AfficherSprite(serpent, pos_x[0], pos_y[0]);
if (direction == 1 && direction != 2 ){
pos_y[0]=old_y[0]-20;
}
if (direction == 2 && direction != 1) {
pos_y[0]=old_y[0]+20;
}
if (direction == 3 & direction != 4) {
pos_x[0]=old_x[0]-20;
}
if (direction == 4 && direction != 3) {
pos_x[0]=old_x[0]+20;
}
for (i=1 ; i<segment ; i++){
pos_x[i]=old_x[i-1];
pos_y[i]=old_y[i-1];
AfficherSprite(serpent, pos_x[i], pos_y[i]);
}
old_x[0]=pos_x[0];
old_y[0]=pos_y[0];
for (i=1 ; i<segment ; i++){
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
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;
}
void Controle(int* direction , int* go_on) {
int t;
int last_direction = 0;
@ -74,41 +59,22 @@ void Controle(int* direction , int* go_on) {
}
}
}
int Collision_Serpent(int* go_on){
int pos_x[60];
int pos_y[60];
int murx[50];
int mury[50];
int segment = 10;
int VerifierCollision(const Serpent *serpent) {
int teteX = serpent->corps[0].x;
int teteY = serpent->corps[0].y;
int i;
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
return *go_on= 0;
} else{
if (teteX < 0 || teteX >= 1200 || teteY < 0 || teteY >= 900-80) {
return 1;
}
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
return *go_on= 0;
} else{
return 1;
}
/*Serpent contre Serpent*/
for (i = 1; i < segment; i++) {
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i]){
return *go_on = 0;
} else {
return 1;
}
}
/*Serpent contre mur*/
for(i=0; i<30;i++){
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
return *go_on= 0;
}else{
for (i = 1; i < serpent->longueur; i++) {
if (teteX == serpent->corps[i].x && teteY == serpent->corps[i].y) {
return 1;
}
}
return 0;
}
void Score(int segment){
int nombre;
@ -121,15 +87,25 @@ void Score(int segment){
EcrireTexte(1000,760,"Score: ",2);
EcrireTexte(1100,760,score,2);
}
void Serpent(int* go_on) {
int direction=0;
int pos_x[60];
int pos_y[60];
int* pointeur_pos_x= pos_x;
int* pointeur_pos_y= pos_y;
int* pointeur_direction=&direction;
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, 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);
Controle(pointeur_direction, go_on);
Update_Serpent(segment, direction, pointeur_pos_x, pointeur_pos_y );
Collision_Serpent(go_on);
}