From 10978c4e49a6656b1f966a5d9fed456931168366 Mon Sep 17 00:00:00 2001 From: mrabia Date: Thu, 29 Aug 2024 14:45:34 +0200 Subject: [PATCH] space invaders --- liste.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ liste.h | 22 ++++++++++++++++++++++ main.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ makefile.txt | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 320 insertions(+), 0 deletions(-) create mode 100644 liste.c create mode 100644 liste.h create mode 100644 main.c create mode 100644 makefile.txt diff --git a/liste.c b/liste.c new file mode 100644 index 0000000..ac0ec34 --- /dev/null +++ b/liste.c @@ -0,0 +1,167 @@ +#include +#include "Graphique/libgraph.h" +#include +#include "liste.h" + +#define TAILLE_X 700 +#define TAILLE_DESC 40 +#define LARGEUR_MONS 30 +#define HAUTEUR_MONS 30 +#define LARGEUR_VAIS 35 +#define SPEED 1 +#define MISS_SPEED 5 +#define LARGEUR_MISS 5 +#define HAUTEUR_MISS 10 + +extern int score; + + + +l_entite* create_l_entite(int x, int y, int speed, int image) { + l_entite* monster_1 = (l_entite*)malloc(sizeof(l_entite)); + if (!monster_1) { + fprintf(stderr, "Memory allocation failed!\n"); + exit(1); + } + monster_1->ent.image = image; + monster_1->ent.x = x; + monster_1->ent.y = y; + monster_1->ent.speed = speed; + monster_1->next = NULL; + return monster_1; +} + +entite create_entite(entite ent, int x, int y, int speed, int image) { + ent.image = image; + ent.x = x; + ent.y = y; + ent.speed = speed; + return ent; +} + +void add_entite(l_entite** head, int x, int y, int speed, int image) { + l_entite* new_monster = create_l_entite(x, y, speed, image); + if (*head == NULL) { + *head = new_monster; + } else { + l_entite* current = *head; + while (current->next != NULL) { + current = current->next; + } + current->next = new_monster; + } +} + + + + +void remove_entite(l_entite** head, l_entite* target) { + if (head == NULL || *head == NULL || target == NULL) { + return; + } + + if (*head == target) { + l_entite* temp = *head; + *head = (*head)->next; + free(temp); + return; + } + + l_entite* current = *head; + while (current->next != NULL && current->next != target) { + current = current->next; + } + + if (current->next == target) { + l_entite* temp = current->next; + current->next = current->next->next; + free(temp); + } +} + + + + + + +void display_l_entite(l_entite* mons) { + l_entite* ptr = mons; + while (ptr != NULL) { + afficherLutin(ptr->ent.image, ptr->ent.x, ptr->ent.y); + ptr = ptr->next; + } +} + + + + + +void display_entite(entite ent) { + afficherLutin(ent.image, ent.x, ent.y); +} + + + + +void moveMissile(l_entite* msl) { + l_entite* ptr = msl; + while (ptr != NULL) { + ptr->ent.y -= MISS_SPEED; + ptr = ptr->next; + } +} + + + + + +void addMissile(l_entite** head, entite vaisseau, int image) { + add_entite(head, vaisseau.x + (LARGEUR_VAIS / 2), vaisseau.y - HAUTEUR_MISS, MISS_SPEED, image); +} + + + +void updateScore() { + score += 10; + printf("Score: %d\n", score); + + char scoreText[20]; + sprintf(scoreText, "Score: %d", score); + lutinTexte(scoreText, COULEUR_BLANC); +} + + + + + +void detecterCollisions(l_entite* l_missile, l_entite* l_monstre) { + l_entite *msl_ptr = l_missile, *msl_prev = NULL; + l_entite *mon_ptr = l_monstre, *mon_prev = NULL; + + while (msl_ptr != NULL) { + mon_ptr = l_monstre; + mon_prev = NULL; + + while (mon_ptr != NULL) { + if (msl_ptr->ent.x >= mon_ptr->ent.x && + msl_ptr->ent.x <= (mon_ptr->ent.x + LARGEUR_MONS) && + msl_ptr->ent.y >= mon_ptr->ent.y && + msl_ptr->ent.y <= (mon_ptr->ent.y + HAUTEUR_MONS)) { + + + remove_entite(&l_missile, msl_ptr); + remove_entite(&l_monstre, mon_ptr); + + + updateScore(); + + + break; + } + mon_prev = mon_ptr; + mon_ptr = mon_ptr->next; + } + msl_prev = msl_ptr; + msl_ptr = msl_ptr->next; + } +} diff --git a/liste.h b/liste.h new file mode 100644 index 0000000..5c31e00 --- /dev/null +++ b/liste.h @@ -0,0 +1,22 @@ +#include +#include +#define TAILLE_X 700 + + +typedef struct entite +{int image;int x;int y;int speed;}entite; + + +typedef struct l_entite +{entite ent;struct l_entite* next;}l_entite; + +l_entite* create_l_entite (int x,int y,int speed,int image); +entite create_entite (entite ent,int x,int y, int speed, int image); +void add_entite (l_entite** head,int x,int y,int speed,int image); +void display_l_entite (l_entite* mons); +void display_entite (entite ent); +void moveMissile (l_entite* msl); +void addMissile (l_entite** head, entite vaisseau,int image); +void detecterCollisions(l_entite* l_missile, l_entite* l_monstre); +void remove_entite(l_entite** head, l_entite* target); +void updateScore(); diff --git a/main.c b/main.c new file mode 100644 index 0000000..7244a82 --- /dev/null +++ b/main.c @@ -0,0 +1,96 @@ +#include "Graphique/libgraph.h" +#include +#include +#include +#include "liste.h" + +#define TAILLE_X 700 +#define TAILLE_Y 700 +#define LARGEUR_MONS 30 +#define HAUTEUR_MONS 30 +#define LARGEUR_VAIS 35 +#define M_SPEED 5 +#define CADENCE_MISSILE 600 +#define LARGEUR_MISS 5 +#define HAUTEUR_MISS 10 + + +int score = 0; + + +int main() { + l_entite* W_monster = NULL; + l_entite* B_monster = NULL; + l_entite* l_missile = NULL; + entite vaisseau; + entite missile_ref; + + creerSurface(TAILLE_X, TAILLE_Y, "Space Invaders"); + + int ZB = lutinTexte("Space invaders", COULEUR_BLEU); + int M1 = chargerLutin("Graphique/invader_monstre1_2.bmp", COULEUR_BLEU); + int M2 = chargerLutin("Graphique/invader_monstre2_1.bmp", COULEUR_BLEU); + int V = chargerLutin("Graphique/invader_canon.bmp", COULEUR_BLEU); + int MI = chargerLutin("Graphique/invader_missile.bmp", COULEUR_BLEU); + + W_monster = create_l_entite(0, 0, 1, M1); + B_monster = create_l_entite(0, 100, 1, M2); + vaisseau = create_entite(vaisseau, 0.5 * TAILLE_X, 0.95 * TAILLE_Y, 1, V); + missile_ref = create_entite(missile_ref, vaisseau.x + (LARGEUR_VAIS / 2), 400, 1, MI); + l_missile = create_l_entite(vaisseau.x + (LARGEUR_VAIS / 2), vaisseau.y, 1, MI); + + int i = 0; + int a = 60; + for (i = 0; i < 4; i++) { + add_entite(&W_monster, (W_monster->ent).x + a, (W_monster->ent).y, 1, M1); + add_entite(&B_monster, (B_monster->ent).x + a, (B_monster->ent).y, 1, M2); + a += 60; + } + + while (1) { + evenement ev; + char touche; + lireEvenement(&ev, &touche, NULL); + + if (ev == quitter) { + break; + } + + rectanglePlein(0, 0, TAILLE_X, TAILLE_Y, COULEUR_NOIR); + display_l_entite(W_monster); + display_l_entite(B_monster); + display_entite(vaisseau); + moveMonster(W_monster); + moveMonster(B_monster); + + if (ev == toucheBas && touche == 'm') { + if (l_missile == NULL || l_missile->ent.y < CADENCE_MISSILE) { + addMissile(&l_missile, vaisseau, MI); + } + } + + display_l_entite(l_missile); + moveMissile(l_missile); + detecterCollisions(l_missile, B_monster); + + if (ev == toucheBas && touche == 'd') { + vaisseau.x += 10; + if (vaisseau.x > TAILLE_X - LARGEUR_VAIS) { + vaisseau.x -= 10; + } + } + + if (ev == toucheBas && touche == 'q') { + vaisseau.x -= 10; + if (vaisseau.x < 0) { + vaisseau.x += 10; + } + } + + afficherLutin(ZB, 400, 400); + majSurface(); + SDL_Delay(10); + } + + return 0; +} diff --git a/makefile.txt b/makefile.txt new file mode 100644 index 0000000..e3350fa --- /dev/null +++ b/makefile.txt @@ -0,0 +1,35 @@ + +TARGET = project + + +CC = gcc +CFLAGS = -g -W -Wall -Wextra + + +LIBDIR = src/Graphique +LDFLAGS = -L$(LIBDIR) -lgraph -lSDL -lSDL_ttf + + +OBJETS = main.o liste.o $(LIBDIR)/libgraph.a + +default: $(TARGET) + +liste.o: liste.c liste.h + $(CC) $(CFLAGS) -c liste.c + +main.o: main.c liste.h $(LIBDIR)/libgraph.h + $(CC) $(CFLAGS) -c main.c + + +$(LIBDIR)/libgraph.a: + $(MAKE) -C $(LIBDIR) + + +$(TARGET): $(OBJETS) + $(CC) $(OBJETS) -o $(TARGET) $(LDFLAGS) + + +.PHONY: clean +clean: + rm -f *.o $(TARGET) + -- libgit2 0.21.2