progprinc.c
7 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdio.h>
#include <stdlib.h>
#include <libFile.h>
#include <libgraph.h>
#include <unistd.h>
#include <time.h>
#include <SDL/SDL.h>
#define MAX_ELEMENTS 50
typedef struct{
char pseudo[MAX_ELEMENTS];
int scorefait;
}scorejoueur;
typedef struct {
int x;
int y;
}position;
typedef struct {
position contenu[MAX_ELEMENTS];
int nombre;
} liste_poly;
void creationgrille(){
int i;
for(i = 0 ; i < 20; i++)
{
rectanglePlein(i*50,0,1,1000,5); //pose de la grille
rectanglePlein(0,i*50,1000,1,5);
} }
void affichage(element_t e)
{
position *p = e;
printf("%d %d \n", p->x, p->y);
}
void creerchampignons(int n,liste_poly *muschrooms,int champi){
position poschampi;
int i,w;
int test=0;
for(w=0;w<n;w++)
{
poschampi.x = (rand()%20)*50; //fixe coordonnées des champignons
poschampi.y = (rand()%20)*50;
for(i=0; i<w; i++){
if(muschrooms->contenu[i].x == poschampi.x && muschrooms->contenu[i].y == poschampi.y ) test=1;
}
if((poschampi.y > 570 || poschampi.y < 430) && test==0)
{
muschrooms->contenu[muschrooms->nombre++]=poschampi;
afficherLutin(champi,(muschrooms->contenu[w].x)+1,(muschrooms->contenu[w].y)+6);
}
else w--;
test = 0;
}
}
void creationcenti(liste_t *l, int Teted, int Corpsd){
int i;
position *pos;
position posmillepatte;
for(i=0; i<4; i++)
{
posmillepatte.x = (300 + 50*i);
posmillepatte.y = 500;
enfiler(l,&posmillepatte,sizeof(position));
}
//affichage des corpsd du centipede
for(i=0; i<4; i++)
{
pos = enieme(i,l);
afficherLutin(Corpsd,pos->x,pos->y);
}
posmillepatte.x = 500;
posmillepatte.y = 500;
enfiler(l,&posmillepatte,sizeof(position)); //affichage de la tete du Centipède
pos = enieme(4,l);
afficherLutin(Teted,pos->x,pos->y);
}
int main() {
SDL_keysym* detail;
srand(time(NULL));
char touche;
unsigned char bas;
int largeur = 1000;
int hauteur = 1000;
char *name;
char *titre = "Le Centipede";
scorejoueur resultat;
liste_poly testscore;
position *pos;
position posmillepatte;
int champi = chargerLutin("Lutins/millepatte_champi.bmp",0);
int Teted = chargerLutin("Lutins/millepatte_tete_droite.bmp",0);
int Teteg = chargerLutin("Lutins/millepatte_tete_gauche.bmp",0);
int Teteh = chargerLutin("Lutins/millepatte_tete_haut.bmp",0);
int Teteb = chargerLutin("Lutins/millepatte_tete_bas.bmp",0);
int Corpsg = chargerLutin("Lutins/millepatte_corps_gauche.bmp",0);
int Corpsd = chargerLutin("Lutins/millepatte_corps_droite.bmp",0);
int Corpsh = chargerLutin("Lutins/millepatte_corps_haut.bmp",0);
int Corpsb = chargerLutin("Lutins/millepatte_corps_bas.bmp",0);
int gameover = chargerLutin("Lutins/game_over.bmp",0);
int level1 = chargerLutin("Lutins/level1.bmp",0);
int level2 = chargerLutin("Lutins/level2.bmp",0);
int level3 = chargerLutin("Lutins/level3.bmp",0);
int levelclr = chargerLutin("Lutins/level_cleared.bmp",0);
int youwin = chargerLutin("Lutins/youwin.bmp",0);
int Tete;
int Corps;
int i;
int abs, ord, obstacle, couleur,level=1,speed=5000,total=0,nombrechampis=6, s;
if(champi<0 || Teted<0 || Teteg<0 || Teteh <0 || Teteb<0 || Corpsg<0 || Corpsd<0 || Corpsh<0 || Corpsb<0 || gameover<0 || level1<0 || level2<0 || level3<0 || levelclr<0 || youwin<0){ printf("Image pas là\n"); exit(-1); }
printf("joueur !! quel est votre pseudo ? :");
scanf("%s",&name);
resultat.nom = name;
creerSurface(largeur,hauteur,titre);
majSurface();
int gomme = creerLutin(0,0,50,50,-1);
while(level<=3) {
queue = 0;
liste_poly muschrooms={.nombre=0};
liste_t listecenti={NULL,0,0};
int w,t,compteur=0,allonge;
rectanglePlein(0,0,1000,1000,1);
if(level==1) { afficherLutin(level1,250,250); majSurface(); sleep(1); }
if(level==2) { afficherLutin(level2,250,250); majSurface(); sleep(1); }
if(level==3) { afficherLutin(level3,250,250); majSurface(); sleep(1); }
rectanglePlein(0,0,1000,1000,-1);
creationcenti(&listecenti,Teted,Corpsd);
creerchampignons(nombrechampis,&muschrooms,champi);
majSurface();
attendreEvenement();
s = 0;
t = 0;
while(1){
for(i=0;i<speed;i++)
{
if(lireTouche(&bas,&touche,(void **)&detail) && bas==1)
{
if(detail->sym==SDLK_RIGHT) t=0;
if(detail->sym==SDLK_UP) t=1;
if(detail->sym==SDLK_LEFT) t=2;
if(detail->sym==SDLK_DOWN) t=3;
}
usleep(1);
}
if((t==0 && s!=2) || s==0) { abs=50; ord=0; Tete = Teted; Corps = Corpsd; s=0; }
if((t==1 && s!=3) || s==1) { abs=0; ord=(-50); Tete = Teteh; Corps = Corpsh; s=1; }
if((t==2 && s!=0) || s==2) { abs=-50; ord=0; Tete = Teteg; Corps = Corpsg; s=2; }
if((t==3 && s!=1) || s==3) { abs=0; ord=50; Tete = Teteb; Corps = Corpsb; s=3; }
pos = dernier(&listecenti);
posmillepatte.x = pos->x+abs;
posmillepatte.y = pos->y+ord;
if(posmillepatte.x==1000 || posmillepatte.y==1000 || posmillepatte.x==-50 || posmillepatte.y==-50)
{
afficherLutin(gameover,250,433);
majSurface();
sleep(5);
break;
}
majSurface();
couleur = couleurPixel(posmillepatte.x+25,posmillepatte.y+25);
if(couleur!=1) obstacle = 1;
else obstacle = 0;
allonge = 0;
if(obstacle==1){
for(w=0;w<nombrechampis;w++)
{
if((muschrooms.contenu[w].x) == posmillepatte.x && (muschrooms.contenu[w].y) == posmillepatte.y) { allonge = 1; } }
if(allonge==1){
afficherLutin(gomme,posmillepatte.x,posmillepatte.y);
afficherLutin(gomme,pos->x,pos->y);
afficherLutin(Corps,(pos->x),pos->y);
enfiler(&listecenti,&posmillepatte,sizeof(posmillepatte));
afficherLutin(Tete,posmillepatte.x,posmillepatte.y);
compteur++;
}
else {
afficherLutin(gameover,250,433);
majSurface();
sleep(5);
break;
}
}
if(obstacle==0){
afficherLutin(gomme,pos->x,pos->y);
afficherLutin(Corps,(pos->x),pos->y);
enfiler(&listecenti,&posmillepatte,sizeof(posmillepatte));
afficherLutin(Tete,posmillepatte.x,posmillepatte.y);
pos = premier(&listecenti);
afficherLutin(gomme,pos->x,pos->y);
defiler(&listecenti);
}
majSurface();
if(compteur==nombrechampis) break;
if(touche=='a')
{
fermerSurface(); //condition d'arret
return 0;
}
}
total = total+compteur;
if(compteur != nombrechampis) break;
if(level==3) { afficherLutin(youwin,204,400); majSurface(); sleep(3); }
else { afficherLutin(levelclr,304,454); majSurface(); sleep(3); }
compteur = 0;
nombrechampis = nombrechampis+3;
speed = speed/2.2;
level++;
}
printf("votre score est de %d\n",total);
resultat.scorefait = total;
FILE *fichier = NULL;
fichier = fopen("scores.txt",r+);
if(fichier==NULL) { printf("\n le fichier n'a pas plus etre ouvert"); fermerSurface(); }
else{
testscore->contenu[testscore->nombre++]=fread;
}
fermerSurface();
fclose(fichier);
return 0;
}