10978c4e
mahmoudrabia
space invaders
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include <stdio.h>
#include "Graphique/libgraph.h"
#include <stdlib.h>
#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;
}
}
|