Bonjour,
je suis un programmateur plutôt débutant, et j'ai achevé la conception d'un jeu.
Ce jeu marchait il y a quelque temps, je sais donc que mon code n'est pas mauvais. Malheureusement, le jeu se lance , je vois donc mon menu principal mais le pc plante quand je lui demande de commencer le jeu...
Mon debugger m'indique qu'il y a une "segmentation fault" , a la ligne 273 de mon "jeu.c" (le code est
)
Code : Sélectionner tout - Visualiser dans une fenêtre à part SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
Voici mon code complet:
LES FICHIERS .C:
main:
jeu:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 #include <stdlib.h> #include <stdio.h> #include <SDL/SDL.h> #include <SDL/SDL_ttf.h> #include <FMOD.h> #include "constantes.h" #include "ecriture.h" #include "chargeurdeniveau.h" #include "jeu.h" int main(int argc, char *argv[]) { long i = 255, j = 0, k = 0; bool optionencours = false, continuer = true; long directionverticale = 2, directionhorizontale = 1; long tempsactuel, tempsprecedent = 0; long nivdifficulte = 2, nombredejoueur = 1; char ecriture[20] = "", ecriturejoueur[20] = ""; SDL_Event(event); SDL_Surface *ecran = NULL, *menu = NULL, *imageregles = NULL, *balledumenu = NULL, *imageoption = NULL, *curseuroption = NULL, *difficulte = NULL, *textediff = NULL, *nbjoueur = NULL, *textejoueur = NULL; SDL_Rect positionmenu, positionballedumenu, positioncurseur, positiondiff, positiontextediff, positionnbjoueur, positiontextejoueur; FSOUND_STREAM *musique = NULL; TTF_Font *police = NULL; SDL_Init(SDL_INIT_VIDEO); FSOUND_Init(44100, 32, 0); TTF_Init(); SDL_WM_SetIcon(SDL_LoadBMP("icone.bmp"), NULL); ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); SDL_WM_SetCaption("trampo-world", NULL); SDL_ShowCursor(SDL_DISABLE); SDL_EnableKeyRepeat(1000, 1000); menu = SDL_LoadBMP("menu.bmp"); balledumenu = SDL_LoadBMP("balle menu.bmp"); difficulte = SDL_LoadBMP("difficulte.bmp"); nbjoueur = SDL_LoadBMP("nombre de joueur.bmp"); imageoption = SDL_LoadBMP("options.bmp"); curseuroption = SDL_LoadBMP("curseur selection.bmp"); imageregles = SDL_LoadBMP("regles.bmp"); musique = FSOUND_Stream_Open("theme.mid", 0, 0, 0); positionmenu.x = 0; positionmenu.y = 0; positionballedumenu.x = 100; positionballedumenu.y = 100; positioncurseur.x = 0; positioncurseur.y = 90; positiondiff.x = 150; positiondiff.y = 150; positiontextediff.x = 130; positiontextediff.y = 130; positionnbjoueur.x = 0; positionnbjoueur.y = 220; positiontextejoueur.y = 220; FSOUND_SetVolume(FSOUND_ALL, 255); while(continuer) { FSOUND_Stream_Play(FSOUND_FREE, musique); SDL_PollEvent(&event); switch(event.type) { case SDL_QUIT: if(optionencours == false) continuer = false; break; case SDL_MOUSEBUTTONDOWN: if(event.motion.x <= 25 & event.motion.y <= 25) optionencours = false; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: if(optionencours == false) continuer = false; else { optionencours = false; SDL_WaitEvent(&event); } break; case SDLK_j: if(optionencours == false) SDL_WaitEvent(&event); FSOUND_Stream_Stop(musique); jeu(ecran, nivdifficulte, nombredejoueur);//on joue positionballedumenu.x = 100; positionballedumenu.y = 100; break; case SDLK_o: if(optionencours == false) optionencours = true; break; case SDLK_r: if(optionencours == false) { reglesencours(ecran, i, j, k, positionballedumenu, balledumenu, directionhorizontale, directionverticale);// on affiche les regles SDL_WaitEvent(&event); } break; case SDLK_1: if( optionencours == true & positioncurseur.y == 90) nivdifficulte = 1; else if( optionencours == true & positioncurseur.y == 180) nombredejoueur = 1; break; case SDLK_2: if( optionencours == true & positioncurseur.y == 90) nivdifficulte = 2; else if( optionencours == true & positioncurseur.y == 180) nombredejoueur = 2; break; case SDLK_3: if( optionencours == true & positioncurseur.y == 90) nivdifficulte = 3; break; case SDLK_4: if( optionencours == true & positioncurseur.y == 90) nivdifficulte = 4; break; default: break; } break; } tempsactuel = SDL_GetTicks(); if( tempsactuel - tempsprecedent > 10)//prise du temps { tempsprecedent = tempsactuel; /*pour que la balle des menus bouge*/ positionballedumenu.x = positionballedumenu.x + directionhorizontale; positionballedumenu.y = positionballedumenu.y + directionverticale; if( positionballedumenu.x >= LARGEUR_FENETRE - 20 | positionballedumenu.x == 0) directionhorizontale = -directionhorizontale; if( positionballedumenu.y >= HAUTEUR_FENETRE - 20 | positionballedumenu.y == 0) directionverticale = -directionverticale; /*pour que la couleur de fond de l'ecran change tout le temps*/ if( optionencours == false) { if(i < 255 & j == 0 & k == 255) i++; else if(i == 255 & j < 255 & k == 0) j++; else if(i == 0 & j == 255 & k < 255) k++; else if(k == 0 & j == 255) i--; else if(i == 0 & k == 255) j--; else if(i == 255 & j == 0) k--; } /*blits et tout*/ SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, i, j, k)); SDL_SetColorKey(menu, SDL_SRCCOLORKEY, SDL_MapRGB(menu->format, 0, 0, 0)); SDL_SetColorKey(balledumenu, SDL_SRCCOLORKEY, SDL_MapRGB(balledumenu->format, 255, 0, 0)); if(optionencours == false) { SDL_BlitSurface(menu, NULL, ecran, &positionmenu); SDL_BlitSurface(balledumenu, NULL, ecran, &positionballedumenu); SDL_ShowCursor(SDL_DISABLE); } else if(optionencours == true) { SDL_ShowCursor(SDL_ENABLE);// affiche le curseur SDL_BlitSurface(imageoption, NULL, ecran, &positionmenu);//affiche le menu des options /***********************************************************************************************/ /*le texte*/ police = TTF_OpenFont("arialbd.ttf", 30); SDL_Color couleurGrise = {192, 192, 192}, couleurBlanche = {255, 255, 255}; switch(nivdifficulte) { case 1: sprintf(ecriture, "debutant"); break; case 2: sprintf(ecriture, "moyen"); break; case 3: sprintf(ecriture, "confirme"); break; case 4: sprintf(ecriture, "expert"); break; default: break; } SDL_FreeSurface(textediff); textediff = TTF_RenderText_Shaded(police, ecriture, couleurBlanche, couleurGrise ); SDL_BlitSurface(textediff, NULL, ecran, &positiontextediff); switch(nombredejoueur) { case 1: sprintf(ecriturejoueur, "1 joueur"); positiontextejoueur.x = 25; break; case 2: sprintf(ecriturejoueur, "2 joueurs"); positiontextejoueur.x = 20; break; default: break; } SDL_FreeSurface(textejoueur); textejoueur = TTF_RenderText_Shaded(police, ecriturejoueur, couleurBlanche, couleurGrise ); SDL_BlitSurface(textejoueur, NULL, ecran, &positiontextejoueur); /*fin du texte*/ /***********************************************************************************************/ SDL_SetColorKey(curseuroption, SDL_SRCCOLORKEY, SDL_MapRGB(curseuroption->format, 0, 0, 0)); SDL_BlitSurface(curseuroption, NULL, ecran, &positioncurseur); SDL_BlitSurface(balledumenu, NULL, ecran, &positionballedumenu); if( event.motion.y > 95 & event.motion.y < 180) { positioncurseur.y = 90; SDL_SetAlpha( difficulte, SDL_SRCALPHA, 200); SDL_BlitSurface(difficulte, NULL, ecran, &positiondiff); } else if( event.motion.y > 185 & event.motion.y < 270) { positioncurseur.y = 180; SDL_SetAlpha( nbjoueur, SDL_SRCALPHA, 200); SDL_BlitSurface(nbjoueur, NULL, ecran, &positionnbjoueur); } if( positioncurseur.y == 0) positioncurseur.y = 90; }// fin de option en cours SDL_Flip(ecran); SDL_Delay(5); }//fin du if principal else SDL_Delay(10 - ( tempsactuel - tempsprecedent)); }//fin du while(continuer) SDL_FreeSurface(balledumenu); SDL_FreeSurface(menu); SDL_FreeSurface(imageoption); SDL_FreeSurface(imageregles); SDL_FreeSurface(curseuroption); SDL_FreeSurface(nbjoueur); SDL_FreeSurface(difficulte); SDL_FreeSurface(textediff); SDL_FreeSurface(textejoueur); FSOUND_Close(); SDL_Quit(); TTF_Quit(); return EXIT_SUCCESS; } int reglesencours(SDL_Surface *ecran, long i, long j, long k, SDL_Rect positionballedumenu, SDL_Surface *balledumenu, long directionhorizontale, long directionverticale) { SDL_Surface *imageregles = NULL; SDL_Rect position; position.x = 0; position.y = 0; bool continuerregle = true; imageregles = SDL_LoadBMP("regles.bmp"); SDL_Event(event); while(continuerregle) { SDL_PollEvent(&event); switch(event.type) { case SDL_MOUSEBUTTONDOWN: if(event.motion.x <= 25 & event.motion.y <= 25) continuerregle = false; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: continuerregle = false; break; default: break; } break; } /*pour que la balle des menus bouge*/ positionballedumenu.x = positionballedumenu.x + directionhorizontale; positionballedumenu.y = positionballedumenu.y + directionverticale; if( positionballedumenu.x >= LARGEUR_FENETRE - 20 | positionballedumenu.x == 0) directionhorizontale = -directionhorizontale; if( positionballedumenu.y >= HAUTEUR_FENETRE - 20 | positionballedumenu.y == 0) directionverticale = -directionverticale; /*pour que la couleur de fond de l'ecran change tout le temps*/ if(i < 255 & j == 0 & k == 255) i++; if(i == 255 & j < 255 & k == 0) j++; if(i == 0 & j == 255 & k < 255) k++; if(k == 0 & j == 255) i--; if(i == 0 & k == 255) j--; if(i == 255 & j == 0) k--; SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, i, j, k)); SDL_ShowCursor(SDL_ENABLE); SDL_SetColorKey(imageregles, SDL_SRCCOLORKEY, SDL_MapRGB(imageregles->format, 255, 255, 255)); SDL_BlitSurface(imageregles, NULL, ecran, &position); SDL_BlitSurface(balledumenu, NULL, ecran, &positionballedumenu); SDL_Flip(ecran); SDL_Delay(5); } }
chargeurdeniveau:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 /*jeu.cpp gere le jeu en general*/ #include <stdlib.h> #include <stdio.h> #include <SDL/SDL.h> #include <SDL/SDL_ttf.h> #include <FMOD.h> #include "constantes.h" #include "ecriture.h" #include "chargeurdeniveau.h" #include "jeu.h" long jeu( SDL_Surface *ecran, long nivdifficulte, long nombredejoueur) { bool continuerlejeu = true, gagne = false, j1gagne = false, j2gagne = false; long compteurderebond = 0, compteurdeux = 0, k = 0; long tempsactuel, tempsprecedent = 0, tempslancementdujeu; int niveau[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0}; long direction[4], directionactuelle; directionactuelle = direction[droite]; long directiondeux; directiondeux = direction[droite]; SDL_Event(event); SDL_ShowCursor(SDL_DISABLE); /*chargement de sons, surfaces , sprites, et demarrage de la musique de fond*/ FSOUND_SAMPLE *rebond = NULL, *rebondmur = NULL, *pasrebond = NULL, *finniveau = NULL; FSOUND_STREAM *musique = NULL; SDL_Surface *balle[5] = {NULL}, *trampog = NULL, *trampod = NULL, *arrivee = NULL, *ecrannoir = NULL, *cadre = NULL; SDL_Rect positionballe, positionballedeux, positiontrampog, positiontrampod, positionarrivee, positionzero; trampog = SDL_LoadBMP("trampog.bmp");/* c'est sa : \ */ trampod = SDL_LoadBMP("trampod.bmp");/* c'est sa : / */ balle[normal] = SDL_LoadBMP("balle.bmp"); balle[saut] = SDL_LoadBMP("balle bleue.bmp"); balle[verte] = SDL_LoadBMP("balle j2.bmp"); arrivee = SDL_LoadBMP("arrivée.bmp"); ecrannoir = SDL_LoadBMP("noir.bmp"); cadre = SDL_LoadBMP("cadredejeu.bmp"); rebondmur = FSOUND_Sample_Load(FSOUND_FREE, "BOING.WAV", 0, 0, 0); rebond = FSOUND_Sample_Load(FSOUND_FREE, "TWINGY.WAV", 0, 0, 0); pasrebond = FSOUND_Sample_Load(FSOUND_FREE, "teleport.wav", 0, 0, 0); finniveau = FSOUND_Sample_Load(FSOUND_FREE, "niveaufini.wav", 0, 0, 0); musique = FSOUND_Stream_Open("trampomix.mid", 0, 0, 0); FSOUND_SetVolume(FSOUND_ALL, 255); positionballe.x = 0; positionballe.y = TAILLE_BLOC; positionzero.x = 0; positionzero.y = 0; balle[actuelle] = balle[normal]; balle[deux] = balle[verte]; /* fin de chargement de sons, surfaces , sprites, et demarrage de la musique de fond*/ /*chargement du niveau*/ chargerNiveau(niveau, nivdifficulte); if( nivdifficulte == 4) { positionballe.x = TAILLE_BLOC; positionballe.y = 14 * TAILLE_BLOC; } positionballedeux.x = positionballe.x; positionballedeux.y = positionballe.y; tempslancementdujeu = SDL_GetTicks() / 1000; while(continuerlejeu) { FSOUND_Stream_Play(FSOUND_FREE, musique); SDL_PollEvent(&event); switch(event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_SPACE: balle[actuelle] = balle[saut]; break; case SDLK_UP: balle[deux] = balle[saut]; break; case SDLK_ESCAPE: continuerlejeu = 0; break; default: break; } } tempsactuel = SDL_GetTicks(); if( tempsactuel - tempsprecedent > 20)//prise du temps { tempsprecedent = tempsactuel; /****************************************************************************************************/ /****************************************************************************************************/ /*le mouvement de la balle*/ if(gagne == false & nombredejoueur == 1) { if(directionactuelle == direction[gauche]) positionballe.x --; else if(directionactuelle == direction[droite]) positionballe.x ++; else if(directionactuelle == direction[haut]) positionballe.y --; else if(directionactuelle == direction[bas]) positionballe.y ++; if( positionballe.x == LARGEUR_FENETRE - 20) { directionactuelle = direction[gauche]; compteurderebond++; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } else if( positionballe.x == 0) { directionactuelle = direction[droite]; compteurderebond++; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } else if( positionballe.y == HAUTEUR_FENETRE - 20) { directionactuelle = direction[haut]; compteurderebond++; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } else if( positionballe.y == 0 & niveau[positionballe.x / TAILLE_BLOC][positionballe.y / TAILLE_BLOC] != 3) { directionactuelle = direction[bas]; compteurderebond++; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } if( niveau[positionballe.x / TAILLE_BLOC][positionballe.y / TAILLE_BLOC] == 2 & positionballe.x % TAILLE_BLOC == 0 & positionballe.y % TAILLE_BLOC == 0 & balle[actuelle] == balle[normal])//rencontre balle et trampo droite (/) { compteurderebond++; FSOUND_PlaySound(FSOUND_FREE, rebond); if( directionactuelle == direction[droite]) directionactuelle = direction[haut]; else if( directionactuelle == direction[gauche]) directionactuelle = direction[bas]; else if( directionactuelle == direction[haut]) directionactuelle = direction[droite]; else if( directionactuelle == direction[bas]) directionactuelle = direction[gauche]; } else if( niveau[positionballe.x / TAILLE_BLOC][positionballe.y / TAILLE_BLOC] == 1 & positionballe.x % TAILLE_BLOC == 0 & positionballe.y % TAILLE_BLOC == 0 & balle[actuelle] == balle[normal])//rencontre balle et trampo gauche (\) { compteurderebond++; FSOUND_PlaySound(FSOUND_FREE, rebond); if( directionactuelle == direction[droite]) directionactuelle = direction[bas]; else if( directionactuelle == direction[gauche]) directionactuelle = direction[haut]; else if( directionactuelle == direction[haut]) directionactuelle = direction[gauche]; else if( directionactuelle == direction[bas]) directionactuelle = direction[droite]; } else if( niveau[positionballe.x / TAILLE_BLOC][positionballe.y / TAILLE_BLOC] == 1 & positionballe.x % TAILLE_BLOC == 0 & positionballe.y % TAILLE_BLOC == 0 & balle[actuelle] == balle[saut] ) FSOUND_PlaySound(FSOUND_FREE, pasrebond); else if( niveau[positionballe.x / TAILLE_BLOC][positionballe.y / TAILLE_BLOC] == 2 & positionballe.x % TAILLE_BLOC == 0 & positionballe.y % TAILLE_BLOC == 0 & balle[actuelle] == balle[saut] ) FSOUND_PlaySound(FSOUND_FREE, pasrebond); if(niveau[positionballe.x / TAILLE_BLOC][positionballe.y / TAILLE_BLOC] == 3 & positionballe.x % TAILLE_BLOC == 0 & positionballe.y % TAILLE_BLOC == 0) { FSOUND_PlaySound(FSOUND_FREE, finniveau); if( nombredejoueur == 1) gagne = true; else j1gagne = true; } } /*fin du mouvement de la balle*/ /*le mouvement de la balle deux*/ if(gagne == false & nombredejoueur == 2) { if(directiondeux == direction[gauche]) positionballedeux.x --; else if(directiondeux == direction[droite]) positionballedeux.x ++; else if(directiondeux == direction[haut]) positionballedeux.y --; else if(directiondeux == direction[bas]) positionballedeux.y ++; if( positionballedeux.x == LARGEUR_FENETRE - 20) { compteurdeux++; directiondeux = direction[gauche]; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } else if( positionballedeux.x == 0) { compteurdeux++; directiondeux = direction[droite]; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } else if( positionballedeux.y == HAUTEUR_FENETRE - 20) { compteurdeux++; directiondeux = direction[haut]; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } else if( positionballedeux.y == 0 & niveau[positionballedeux.x / TAILLE_BLOC][positionballedeux.y / TAILLE_BLOC] != 3) { compteurdeux++; directiondeux = direction[bas]; FSOUND_PlaySound(FSOUND_FREE, rebondmur); } if( niveau[positionballedeux.x / TAILLE_BLOC][positionballedeux.y / TAILLE_BLOC] == 2 & positionballedeux.x % TAILLE_BLOC == 0 & positionballedeux.y % TAILLE_BLOC == 0 & balle[deux] == balle[verte])//rencontre balle et trampo droite (/) { compteurdeux++; FSOUND_PlaySound(FSOUND_FREE, rebond); if( directiondeux == direction[droite]) directiondeux = direction[haut]; else if( directiondeux == direction[gauche]) directiondeux = direction[bas]; else if( directiondeux == direction[haut]) directiondeux = direction[droite]; else if( directiondeux == direction[bas]) directiondeux = direction[gauche]; } else if( niveau[positionballedeux.x / TAILLE_BLOC][positionballedeux.y / TAILLE_BLOC] == 1 & positionballedeux.x % TAILLE_BLOC == 0 & positionballedeux.y % TAILLE_BLOC == 0 & balle[deux] == balle[verte])//rencontre balle et trampo gauche (\) { compteurdeux++; FSOUND_PlaySound(FSOUND_FREE, rebond); if( directiondeux == direction[droite]) directiondeux = direction[bas]; else if( directiondeux == direction[gauche]) directiondeux = direction[haut]; else if( directiondeux == direction[haut]) directiondeux = direction[gauche]; else if( directiondeux == direction[bas]) directiondeux = direction[droite]; } else if( niveau[positionballedeux.x / TAILLE_BLOC][positionballedeux.y / TAILLE_BLOC] == 1 & positionballedeux.x % TAILLE_BLOC == 0 & positionballedeux.y % TAILLE_BLOC == 0 & balle[deux] == balle[saut] ) FSOUND_PlaySound(FSOUND_FREE, pasrebond); else if( niveau[positionballedeux.x / TAILLE_BLOC][positionballedeux.y / TAILLE_BLOC] == 2 & positionballedeux.x % TAILLE_BLOC == 0 & positionballedeux.y % TAILLE_BLOC == 0 & balle[deux] == balle[saut] ) FSOUND_PlaySound(FSOUND_FREE, pasrebond); if(niveau[positionballedeux.x / TAILLE_BLOC][positionballedeux.y / TAILLE_BLOC] == 3 & positionballedeux.x % TAILLE_BLOC == 0 & positionballedeux.y % TAILLE_BLOC == 0) { FSOUND_PlaySound(FSOUND_FREE, finniveau); j2gagne = true; } } /*fin du mouvement de la balle deux*/ /****************************************************************************************************/ /****************************************************************************************************/ if( j1gagne == true & j2gagne == true) gagne = true; /*blit*/ SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0)); SDL_SetColorKey(balle[normal], SDL_SRCCOLORKEY, SDL_MapRGB(balle[normal]->format, 255, 0, 0)); SDL_SetColorKey(balle[saut], SDL_SRCCOLORKEY, SDL_MapRGB(balle[saut]->format, 255, 0, 0)); SDL_SetColorKey(balle[verte], SDL_SRCCOLORKEY, SDL_MapRGB(balle[verte]->format, 255, 0, 0)); SDL_SetColorKey(trampod, SDL_SRCCOLORKEY, SDL_MapRGB(trampod->format, 255, 255, 255)); SDL_SetColorKey(trampog, SDL_SRCCOLORKEY, SDL_MapRGB(trampog->format, 255, 255, 255)); SDL_SetColorKey(arrivee, SDL_SRCCOLORKEY, SDL_MapRGB(arrivee->format, 255, 255, 255)); SDL_SetColorKey(cadre, SDL_SRCCOLORKEY, SDL_MapRGB(cadre->format, 255, 255, 255)); /*le terrain*/ for (long i = 0 ; i < NB_BLOCS_LARGEUR ; i++) { for (long j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) { positiontrampog.x = i * TAILLE_BLOC; positiontrampog.y = j * TAILLE_BLOC; positiontrampod.x = i * TAILLE_BLOC; positiontrampod.y = j * TAILLE_BLOC; positionarrivee.x = i * TAILLE_BLOC; positionarrivee.y = j * TAILLE_BLOC + 3; switch(niveau[i][j]) { case 0: break; case 1: SDL_BlitSurface(trampog, NULL, ecran, &positiontrampog); break; case 2: SDL_BlitSurface(trampod, NULL, ecran, &positiontrampod); break; case 3: SDL_BlitSurface(arrivee, NULL, ecran, &positionarrivee); break; case 4: if( gagne == false) { SDL_BlitSurface(balle[actuelle], NULL, ecran, &positionballe); if( nombredejoueur == 2) SDL_BlitSurface(balle[deux], NULL, ecran, &positionballedeux); } break; } } }//fin de la mise en place du terrain balle[actuelle] = balle[normal]; balle[deux] = balle[verte]; if(gagne == false) SDL_BlitSurface( cadre, NULL, ecran, &positionzero); else if(gagne == true) { k += 3; SDL_SetAlpha( ecrannoir, SDL_SRCALPHA, k); SDL_BlitSurface( ecrannoir, NULL, ecran, &positionzero); } if( k == 255) continuerlejeu = false; /***********************************************************/ ecriture(ecran, compteurderebond, compteurdeux, tempslancementdujeu, gagne, nivdifficulte, k); SDL_Flip(ecran); }//fin du if principal else SDL_Delay( 20 - (tempsactuel - tempsprecedent)); }//fin du while SDL_FreeSurface(ecrannoir); SDL_FreeSurface(balle[normal]); SDL_FreeSurface(balle[saut]); SDL_FreeSurface(balle[verte]); SDL_FreeSurface(trampog); SDL_FreeSurface(trampod); SDL_FreeSurface(cadre); SDL_FreeSurface(arrivee); FSOUND_Stream_Stop(musique); }//fin de jeu.cpp
ecriture:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* chargeur de niveau recupere les données dans les fichiers "terrain.txt" pour charger la map */ #include <stdlib.h> #include <stdio.h> #include <SDL/SDL.h> #include "chargeurdeniveau.h" int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR], long nivdifficulte) { FILE* fichier = NULL; char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1]; int i = 0, j = 0; switch(nivdifficulte) { case 1: fichier = fopen("terrain debutant.txt", "r"); break; case 2: fichier = fopen("terrain moyen.txt", "r"); break; case 3: fichier = fopen("terrain confirme.txt", "r"); break; case 4: fichier = fopen("terrain expert.txt", "r"); break; } fgets(ligneFichier, NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1, fichier); for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++) { for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++) { switch (ligneFichier[(i * NB_BLOCS_LARGEUR) + j]) { case '1': niveau[j][i] = 1; break; case '2': niveau[j][i] = 2; break; case '3': niveau[j][i] = 3; break; case '4': niveau[j][i] = 4; break; } } } fclose(fichier); return 1; }
LES FICHIERS .H:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /*ecriture.cpp sert a gerer toutes les ecritures du jeu, et les records */ #include <stdlib.h> #include <stdio.h> #include <SDL/SDL.h> #include <SDL/SDL_ttf.h> #include <FMOD.h> #include "ecriture.h" char ecriture( SDL_Surface *ecran, long compteurderebond, long compteurdeux, long tempslancementdujeu, long gagne, long nivdifficulte, long k) { long compteurdetemps = (SDL_GetTicks() / 1000) - tempslancementdujeu; long chiffresurlefichier; bool recordbattu = false; char ecriture[20] = ""; char ecriturevictoire[20] = ""; char ecriturerecord[20] = ""; FILE* fichierrecordrebond = NULL; switch(nivdifficulte) { case 1: fichierrecordrebond = fopen("terrain1.recordrebond", "r"); break; case 2: fichierrecordrebond = fopen("terrain2.recordrebond", "r"); break; case 3: fichierrecordrebond = fopen("terrain3.recordrebond", "r"); break; case 4: fichierrecordrebond = fopen("terrain4.recordrebond", "r"); break; } fscanf(fichierrecordrebond, "%ld", &chiffresurlefichier); fclose(fichierrecordrebond); SDL_Surface *texte = NULL, *textevictoire = NULL, *texterecord = NULL; SDL_Rect positiontexte, positiontextevictoire, positiontexterecord; TTF_Font *police = NULL; FSOUND_SAMPLE *record = NULL; record = FSOUND_Sample_Load(FSOUND_FREE, "nouveau record.wav", 0, 0, 0); /*ecriture du temps et du nombre de rebonds*/ police = TTF_OpenFont("arialbd.ttf", 12); SDL_Color couleurNoire = {0, 0, 0}, couleurBlanche = {255, 255, 255}, couleurVerte = { 0, 255, 0}, couleurRouge = { 255, 0, 0}; positiontexte.x = 0; positiontexte.y = 364; if( compteurdeux == 0) sprintf(ecriture, "Temps : %ld secondes Rebonds : %ld(Record = %ld)",compteurdetemps ,compteurderebond, chiffresurlefichier); else sprintf(ecriture, "Temps : %ld'' Rebonds(j1) : %ld, Rebonds(j2) : %ld",compteurdetemps ,compteurderebond, compteurdeux); SDL_FreeSurface(texte); texte = TTF_RenderText_Shaded(police, ecriture, couleurBlanche, couleurVerte ); if(gagne == 1)//quand on a gagne alors qu'il n'y a qu'un joueur { /*enregistrement du nombre de rebonds dans le fichier seulement si ce nombre est le record*/ if(chiffresurlefichier > compteurderebond & compteurdeux == 0) { positiontexterecord.x = 0; positiontexterecord.y = 150; police = TTF_OpenFont("arialbd.ttf", 35); sprintf(ecriturerecord, "Nouveau Record !"); SDL_FreeSurface(texterecord); texterecord = TTF_RenderText_Shaded(police, ecriturerecord, couleurRouge, couleurNoire ); recordbattu = true; if(k == 255) { switch(nivdifficulte) { case 1: fichierrecordrebond = fopen("terrain1.recordrebond", "w"); break; case 2: fichierrecordrebond = fopen("terrain2.recordrebond", "w"); break; case 3: fichierrecordrebond = fopen("terrain3.recordrebond", "w"); break; case 4: fichierrecordrebond = fopen("terrain4.recordrebond", "w"); break; } fprintf(fichierrecordrebond, "%ld", compteurderebond); fclose(fichierrecordrebond); FSOUND_PlaySound(FSOUND_FREE, record); } } if(recordbattu == false & compteurdeux == 0) { /*ecriture du mot "gagne"*/ police = TTF_OpenFont("arialbd.ttf", 62); positiontextevictoire.x = 0; positiontextevictoire.y = 150; sprintf(ecriturevictoire, "GAGNE !!!"); SDL_FreeSurface(textevictoire); textevictoire = TTF_RenderText_Shaded(police, ecriturevictoire, couleurRouge, couleurNoire ); } else if( compteurderebond < compteurdeux) { police = TTF_OpenFont("arialbd.ttf", 45); positiontextevictoire.x = 0; positiontextevictoire.y = 150; sprintf(ecriturevictoire, "La balle jaune a gagne"); SDL_FreeSurface(textevictoire); textevictoire = TTF_RenderText_Shaded(police, ecriturevictoire, couleurRouge, couleurNoire ); } else if( compteurderebond > compteurdeux) { police = TTF_OpenFont("arialbd.ttf", 45); positiontextevictoire.x = 0; positiontextevictoire.y = 150; sprintf(ecriturevictoire, "La balle verte a gagne"); SDL_FreeSurface(textevictoire); textevictoire = TTF_RenderText_Shaded(police, ecriturevictoire, couleurRouge, couleurNoire ); } else if( compteurderebond == compteurdeux) { police = TTF_OpenFont("arialbd.ttf", 70); positiontextevictoire.x = 0; positiontextevictoire.y = 150; sprintf(ecriturevictoire, "EGALITE"); SDL_FreeSurface(textevictoire); textevictoire = TTF_RenderText_Shaded(police, ecriturevictoire, couleurRouge, couleurNoire ); } }// fin du if(gagne) else SDL_BlitSurface(texte, NULL, ecran, &positiontexte); SDL_BlitSurface(textevictoire, NULL, ecran, &positiontextevictoire); SDL_BlitSurface(texterecord, NULL, ecran, &positiontexterecord); }
constantes:
jeu:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #ifndef CONSTANTES_H_INCLUDED #define CONSTANTES_H_INCLUDED #define TAILLE_BLOC 20 // Taille d'un bloc (carré) en pixels #define NB_BLOCS_LARGEUR 15 #define NB_BLOCS_HAUTEUR 19 #define LARGEUR_FENETRE TAILLE_BLOC * NB_BLOCS_LARGEUR #define HAUTEUR_FENETRE TAILLE_BLOC * NB_BLOCS_HAUTEUR int reglesencours(SDL_Surface *ecran, long i, long j, long k, SDL_Rect positionballedumenu, SDL_Surface *balledumenu, long directionhorizontale, long directionverticale); enum {saut, normal, actuelle, deux, verte}; enum {haut, bas, gauche, droite}; #endif
écriture
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #ifndef AVANTLEJEU_H_INCLUDED #define AVANTLEJEU_H_INCLUDED long jeu( SDL_Surface *ecran, long nivdifficulte, long nombredejoueur); #endif // AVANTLEJEU_H_INCLUDED
chargeurdeniveau
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 #ifndef ECRITURE_H_INCLUDED #define ECRITURE_H_INCLUDED char ecriture( SDL_Surface *ecran, long compteurderebond, long compteurdeux, long tempslancementdujeu, long gagne, long nivdifficulte, long k); #endif // ECRITURE_H_INCLUDED
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 #ifndef CHARGEURDENIVEAU_H_INCLUDED #define CHARGEURDENIVEAU_H_INCLUDED #define NB_BLOCS_LARGEUR 15 #define NB_BLOCS_HAUTEUR 19 #define TAILLE_BLOC 20 int chargerNiveau(int niveau[][NB_BLOCS_HAUTEUR], long nivdifficulte); void editeur(SDL_Surface* ecran); enum{ VIDE, TRAMPOG, TRAMPOD, ARRIVEE, BALLE}; #endif // CHARGEURDENIVEAU_H_INCLUDED
Merci beaucoup a celui ou celle qui trouvera la solution a mon probleme
Partager