Commit 10978c4e49a6656b1f966a5d9fed456931168366
1 parent
cab189ea
space invaders
Showing
4 changed files
with
320 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,167 @@ | @@ -0,0 +1,167 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include "Graphique/libgraph.h" | ||
3 | +#include <stdlib.h> | ||
4 | +#include "liste.h" | ||
5 | + | ||
6 | +#define TAILLE_X 700 | ||
7 | +#define TAILLE_DESC 40 | ||
8 | +#define LARGEUR_MONS 30 | ||
9 | +#define HAUTEUR_MONS 30 | ||
10 | +#define LARGEUR_VAIS 35 | ||
11 | +#define SPEED 1 | ||
12 | +#define MISS_SPEED 5 | ||
13 | +#define LARGEUR_MISS 5 | ||
14 | +#define HAUTEUR_MISS 10 | ||
15 | + | ||
16 | +extern int score; | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | +l_entite* create_l_entite(int x, int y, int speed, int image) { | ||
21 | + l_entite* monster_1 = (l_entite*)malloc(sizeof(l_entite)); | ||
22 | + if (!monster_1) { | ||
23 | + fprintf(stderr, "Memory allocation failed!\n"); | ||
24 | + exit(1); | ||
25 | + } | ||
26 | + monster_1->ent.image = image; | ||
27 | + monster_1->ent.x = x; | ||
28 | + monster_1->ent.y = y; | ||
29 | + monster_1->ent.speed = speed; | ||
30 | + monster_1->next = NULL; | ||
31 | + return monster_1; | ||
32 | +} | ||
33 | + | ||
34 | +entite create_entite(entite ent, int x, int y, int speed, int image) { | ||
35 | + ent.image = image; | ||
36 | + ent.x = x; | ||
37 | + ent.y = y; | ||
38 | + ent.speed = speed; | ||
39 | + return ent; | ||
40 | +} | ||
41 | + | ||
42 | +void add_entite(l_entite** head, int x, int y, int speed, int image) { | ||
43 | + l_entite* new_monster = create_l_entite(x, y, speed, image); | ||
44 | + if (*head == NULL) { | ||
45 | + *head = new_monster; | ||
46 | + } else { | ||
47 | + l_entite* current = *head; | ||
48 | + while (current->next != NULL) { | ||
49 | + current = current->next; | ||
50 | + } | ||
51 | + current->next = new_monster; | ||
52 | + } | ||
53 | +} | ||
54 | + | ||
55 | + | ||
56 | + | ||
57 | + | ||
58 | +void remove_entite(l_entite** head, l_entite* target) { | ||
59 | + if (head == NULL || *head == NULL || target == NULL) { | ||
60 | + return; | ||
61 | + } | ||
62 | + | ||
63 | + if (*head == target) { | ||
64 | + l_entite* temp = *head; | ||
65 | + *head = (*head)->next; | ||
66 | + free(temp); | ||
67 | + return; | ||
68 | + } | ||
69 | + | ||
70 | + l_entite* current = *head; | ||
71 | + while (current->next != NULL && current->next != target) { | ||
72 | + current = current->next; | ||
73 | + } | ||
74 | + | ||
75 | + if (current->next == target) { | ||
76 | + l_entite* temp = current->next; | ||
77 | + current->next = current->next->next; | ||
78 | + free(temp); | ||
79 | + } | ||
80 | +} | ||
81 | + | ||
82 | + | ||
83 | + | ||
84 | + | ||
85 | + | ||
86 | + | ||
87 | +void display_l_entite(l_entite* mons) { | ||
88 | + l_entite* ptr = mons; | ||
89 | + while (ptr != NULL) { | ||
90 | + afficherLutin(ptr->ent.image, ptr->ent.x, ptr->ent.y); | ||
91 | + ptr = ptr->next; | ||
92 | + } | ||
93 | +} | ||
94 | + | ||
95 | + | ||
96 | + | ||
97 | + | ||
98 | + | ||
99 | +void display_entite(entite ent) { | ||
100 | + afficherLutin(ent.image, ent.x, ent.y); | ||
101 | +} | ||
102 | + | ||
103 | + | ||
104 | + | ||
105 | + | ||
106 | +void moveMissile(l_entite* msl) { | ||
107 | + l_entite* ptr = msl; | ||
108 | + while (ptr != NULL) { | ||
109 | + ptr->ent.y -= MISS_SPEED; | ||
110 | + ptr = ptr->next; | ||
111 | + } | ||
112 | +} | ||
113 | + | ||
114 | + | ||
115 | + | ||
116 | + | ||
117 | + | ||
118 | +void addMissile(l_entite** head, entite vaisseau, int image) { | ||
119 | + add_entite(head, vaisseau.x + (LARGEUR_VAIS / 2), vaisseau.y - HAUTEUR_MISS, MISS_SPEED, image); | ||
120 | +} | ||
121 | + | ||
122 | + | ||
123 | + | ||
124 | +void updateScore() { | ||
125 | + score += 10; | ||
126 | + printf("Score: %d\n", score); | ||
127 | + | ||
128 | + char scoreText[20]; | ||
129 | + sprintf(scoreText, "Score: %d", score); | ||
130 | + lutinTexte(scoreText, COULEUR_BLANC); | ||
131 | +} | ||
132 | + | ||
133 | + | ||
134 | + | ||
135 | + | ||
136 | + | ||
137 | +void detecterCollisions(l_entite* l_missile, l_entite* l_monstre) { | ||
138 | + l_entite *msl_ptr = l_missile, *msl_prev = NULL; | ||
139 | + l_entite *mon_ptr = l_monstre, *mon_prev = NULL; | ||
140 | + | ||
141 | + while (msl_ptr != NULL) { | ||
142 | + mon_ptr = l_monstre; | ||
143 | + mon_prev = NULL; | ||
144 | + | ||
145 | + while (mon_ptr != NULL) { | ||
146 | + if (msl_ptr->ent.x >= mon_ptr->ent.x && | ||
147 | + msl_ptr->ent.x <= (mon_ptr->ent.x + LARGEUR_MONS) && | ||
148 | + msl_ptr->ent.y >= mon_ptr->ent.y && | ||
149 | + msl_ptr->ent.y <= (mon_ptr->ent.y + HAUTEUR_MONS)) { | ||
150 | + | ||
151 | + | ||
152 | + remove_entite(&l_missile, msl_ptr); | ||
153 | + remove_entite(&l_monstre, mon_ptr); | ||
154 | + | ||
155 | + | ||
156 | + updateScore(); | ||
157 | + | ||
158 | + | ||
159 | + break; | ||
160 | + } | ||
161 | + mon_prev = mon_ptr; | ||
162 | + mon_ptr = mon_ptr->next; | ||
163 | + } | ||
164 | + msl_prev = msl_ptr; | ||
165 | + msl_ptr = msl_ptr->next; | ||
166 | + } | ||
167 | +} |
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#define TAILLE_X 700 | ||
4 | + | ||
5 | + | ||
6 | +typedef struct entite | ||
7 | +{int image;int x;int y;int speed;}entite; | ||
8 | + | ||
9 | + | ||
10 | +typedef struct l_entite | ||
11 | +{entite ent;struct l_entite* next;}l_entite; | ||
12 | + | ||
13 | +l_entite* create_l_entite (int x,int y,int speed,int image); | ||
14 | +entite create_entite (entite ent,int x,int y, int speed, int image); | ||
15 | +void add_entite (l_entite** head,int x,int y,int speed,int image); | ||
16 | +void display_l_entite (l_entite* mons); | ||
17 | +void display_entite (entite ent); | ||
18 | +void moveMissile (l_entite* msl); | ||
19 | +void addMissile (l_entite** head, entite vaisseau,int image); | ||
20 | +void detecterCollisions(l_entite* l_missile, l_entite* l_monstre); | ||
21 | +void remove_entite(l_entite** head, l_entite* target); | ||
22 | +void updateScore(); |
@@ -0,0 +1,96 @@ | @@ -0,0 +1,96 @@ | ||
1 | +#include "Graphique/libgraph.h" | ||
2 | +#include <unistd.h> | ||
3 | +#include <stdbool.h> | ||
4 | +#include <SDL/SDL.h> | ||
5 | +#include "liste.h" | ||
6 | + | ||
7 | +#define TAILLE_X 700 | ||
8 | +#define TAILLE_Y 700 | ||
9 | +#define LARGEUR_MONS 30 | ||
10 | +#define HAUTEUR_MONS 30 | ||
11 | +#define LARGEUR_VAIS 35 | ||
12 | +#define M_SPEED 5 | ||
13 | +#define CADENCE_MISSILE 600 | ||
14 | +#define LARGEUR_MISS 5 | ||
15 | +#define HAUTEUR_MISS 10 | ||
16 | + | ||
17 | + | ||
18 | +int score = 0; | ||
19 | + | ||
20 | + | ||
21 | +int main() { | ||
22 | + l_entite* W_monster = NULL; | ||
23 | + l_entite* B_monster = NULL; | ||
24 | + l_entite* l_missile = NULL; | ||
25 | + entite vaisseau; | ||
26 | + entite missile_ref; | ||
27 | + | ||
28 | + creerSurface(TAILLE_X, TAILLE_Y, "Space Invaders"); | ||
29 | + | ||
30 | + int ZB = lutinTexte("Space invaders", COULEUR_BLEU); | ||
31 | + int M1 = chargerLutin("Graphique/invader_monstre1_2.bmp", COULEUR_BLEU); | ||
32 | + int M2 = chargerLutin("Graphique/invader_monstre2_1.bmp", COULEUR_BLEU); | ||
33 | + int V = chargerLutin("Graphique/invader_canon.bmp", COULEUR_BLEU); | ||
34 | + int MI = chargerLutin("Graphique/invader_missile.bmp", COULEUR_BLEU); | ||
35 | + | ||
36 | + W_monster = create_l_entite(0, 0, 1, M1); | ||
37 | + B_monster = create_l_entite(0, 100, 1, M2); | ||
38 | + vaisseau = create_entite(vaisseau, 0.5 * TAILLE_X, 0.95 * TAILLE_Y, 1, V); | ||
39 | + missile_ref = create_entite(missile_ref, vaisseau.x + (LARGEUR_VAIS / 2), 400, 1, MI); | ||
40 | + l_missile = create_l_entite(vaisseau.x + (LARGEUR_VAIS / 2), vaisseau.y, 1, MI); | ||
41 | + | ||
42 | + int i = 0; | ||
43 | + int a = 60; | ||
44 | + for (i = 0; i < 4; i++) { | ||
45 | + add_entite(&W_monster, (W_monster->ent).x + a, (W_monster->ent).y, 1, M1); | ||
46 | + add_entite(&B_monster, (B_monster->ent).x + a, (B_monster->ent).y, 1, M2); | ||
47 | + a += 60; | ||
48 | + } | ||
49 | + | ||
50 | + while (1) { | ||
51 | + evenement ev; | ||
52 | + char touche; | ||
53 | + lireEvenement(&ev, &touche, NULL); | ||
54 | + | ||
55 | + if (ev == quitter) { | ||
56 | + break; | ||
57 | + } | ||
58 | + | ||
59 | + rectanglePlein(0, 0, TAILLE_X, TAILLE_Y, COULEUR_NOIR); | ||
60 | + display_l_entite(W_monster); | ||
61 | + display_l_entite(B_monster); | ||
62 | + display_entite(vaisseau); | ||
63 | + moveMonster(W_monster); | ||
64 | + moveMonster(B_monster); | ||
65 | + | ||
66 | + if (ev == toucheBas && touche == 'm') { | ||
67 | + if (l_missile == NULL || l_missile->ent.y < CADENCE_MISSILE) { | ||
68 | + addMissile(&l_missile, vaisseau, MI); | ||
69 | + } | ||
70 | + } | ||
71 | + | ||
72 | + display_l_entite(l_missile); | ||
73 | + moveMissile(l_missile); | ||
74 | + detecterCollisions(l_missile, B_monster); | ||
75 | + | ||
76 | + if (ev == toucheBas && touche == 'd') { | ||
77 | + vaisseau.x += 10; | ||
78 | + if (vaisseau.x > TAILLE_X - LARGEUR_VAIS) { | ||
79 | + vaisseau.x -= 10; | ||
80 | + } | ||
81 | + } | ||
82 | + | ||
83 | + if (ev == toucheBas && touche == 'q') { | ||
84 | + vaisseau.x -= 10; | ||
85 | + if (vaisseau.x < 0) { | ||
86 | + vaisseau.x += 10; | ||
87 | + } | ||
88 | + } | ||
89 | + | ||
90 | + afficherLutin(ZB, 400, 400); | ||
91 | + majSurface(); | ||
92 | + SDL_Delay(10); | ||
93 | + } | ||
94 | + | ||
95 | + return 0; | ||
96 | +} |
@@ -0,0 +1,35 @@ | @@ -0,0 +1,35 @@ | ||
1 | + | ||
2 | +TARGET = project | ||
3 | + | ||
4 | + | ||
5 | +CC = gcc | ||
6 | +CFLAGS = -g -W -Wall -Wextra | ||
7 | + | ||
8 | + | ||
9 | +LIBDIR = src/Graphique | ||
10 | +LDFLAGS = -L$(LIBDIR) -lgraph -lSDL -lSDL_ttf | ||
11 | + | ||
12 | + | ||
13 | +OBJETS = main.o liste.o $(LIBDIR)/libgraph.a | ||
14 | + | ||
15 | +default: $(TARGET) | ||
16 | + | ||
17 | +liste.o: liste.c liste.h | ||
18 | + $(CC) $(CFLAGS) -c liste.c | ||
19 | + | ||
20 | +main.o: main.c liste.h $(LIBDIR)/libgraph.h | ||
21 | + $(CC) $(CFLAGS) -c main.c | ||
22 | + | ||
23 | + | ||
24 | +$(LIBDIR)/libgraph.a: | ||
25 | + $(MAKE) -C $(LIBDIR) | ||
26 | + | ||
27 | + | ||
28 | +$(TARGET): $(OBJETS) | ||
29 | + $(CC) $(OBJETS) -o $(TARGET) $(LDFLAGS) | ||
30 | + | ||
31 | + | ||
32 | +.PHONY: clean | ||
33 | +clean: | ||
34 | + rm -f *.o $(TARGET) | ||
35 | + |