Probleme en c

Signaler
Messages postés
2
Date d'inscription
samedi 18 octobre 2008
Statut
Membre
Dernière intervention
21 octobre 2008
-
Messages postés
2
Date d'inscription
samedi 18 octobre 2008
Statut
Membre
Dernière intervention
21 octobre 2008
-
bonjour, j'ai un probleme dans un jeu en C : lorsque mon avion touche un mur, il n'explose pas. voici le code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include <sdl.h>
#include <sdl_image.h>
#include

int main(int argc, char *argv[])
{
SDL_Surface *ecran NULL, *texte[10] {NULL}, *avion = NULL, *explosion = NULL, *barre[100] = {NULL};
SDL_Rect positionAvion, positionExplosion, positionTexte[10] = {0}, positionBarre[100] = {0};
SDL_Event event;
TTF_Font *police = NULL;
SDL_Color couleurJaune = {230, 230, 0};

int continuer 1, m 0, i = 0, v= 0, time_1 = 0, time_2 = 0 ,time_3 = 0, time_4 = 0, time_5 = 0, time_6 = 0, jeu = 1, points = 0;
char chaine[20] = "", score[20] = "";

SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
police = TTF_OpenFont("/Users/guillaumeuettwiller/Desktop/copter/visitor.ttf", 30);

ecran = SDL_SetVideoMode(700, 400, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); /* Double Buffering */
SDL_WM_SetCaption("iPlane", NULL);
SDL_WM_SetIcon(IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/icone.png"), NULL);


avion = IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/avion.png");
SDL_SetColorKey(avion, SDL_SRCCOLORKEY, SDL_MapRGB(avion->format, 0, 0, 0));
positionAvion.x = 100;
positionAvion.y = 200;

explosion = IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/explosion.png");
SDL_SetColorKey(explosion, SDL_SRCCOLORKEY, SDL_MapRGB(explosion->format, 0, 0, 0));
positionExplosion.x = 100;
positionExplosion.y = 300;

i = 0;
while (i < 100)
{
barre[i]=IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/barre.jpeg");
SDL_SetColorKey(barre[i], SDL_SRCCOLORKEY, SDL_MapRGB(barre[i]->format, 0, 0, 0));
i++;
}

SDL_EnableKeyRepeat(20, 20);
SDL_ShowCursor(SDL_DISABLE);

while (continuer)
{
sprintf(score,"Score %ld",points);
texte[1] = TTF_RenderText_Solid(police, score, couleurJaune);
positionTexte[1].x = 700 - (texte[1]->w + 5);
positionTexte[1].y = 1;


time_1 = SDL_GetTicks();
if (time_1 - time_2 > 20)
{
if(positionAvion.y < 340 && positionAvion.y > 2 && jeu == 1)
positionAvion.y += 6;

time_2 = time_1;

}

time_3 = SDL_GetTicks();

if(time_3 - time_4 > 750)
{
time_4 = time_3;

points++;

}

time_5 = SDL_GetTicks();
if (time_5 - time_6 > 1000)
{
time_6 = time_5;
positionBarre[v].x = 600;
positionBarre[v].y = (rand()%(300-10+1))+10;
v++;
}


i = 0;


while (i < v)
{
positionBarre[i].x -=10;
if (positionBarre[i].x< 5)
SDL_SetAlpha(barre[i], SDL_SRCALPHA, 0);
SDL_BlitSurface(barre[i],NULL,ecran,&positionBarre[i]);
i++;

}


if ( positionAvion.y > 339 || positionAvion.y < 2 || ((positionAvion.y + avion->h) >= positionBarre[i].y && positionAvion.y <= (positionBarre[i].y + barre[i]->h) && positionAvion.x ==positionBarre[i].x)) //je pense avoir un probleme par ici
{
positionExplosion.y = positionAvion.y;

SDL_SetAlpha(avion, SDL_SRCALPHA, 0);
SDL_BlitSurface(explosion, NULL, ecran, &positionExplosion);

texte[0] = TTF_RenderText_Blended(police,"GAME OVER",couleurJaune);
positionTexte[0].x = 265;
positionTexte[0].y = 200;
SDL_BlitSurface(texte[0], NULL, ecran, &positionTexte[0]);

jeu = 0;
SDL_WaitEvent(&event);
if(event.type SDL_KEYDOWN && event.key.keysym.sym SDLK_ESCAPE)
continuer = 0;
}


SDL_Flip(ecran);


SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
break;
case SDLK_SPACE:
if (jeu == 0)
break;
positionAvion.y -= 8;
break;
}
break;
}

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
SDL_BlitSurface(avion, NULL, ecran, &positionAvion);
SDL_BlitSurface(texte[1], NULL, ecran, &positionTexte[1]);

}

SDL_FreeSurface(avion);
SDL_Quit();
TTF_CloseFont(police);
TTF_Quit();


return EXIT_SUCCESS;
}


</sdl_image.h></sdl.h></time.h></math.h></string.h></stdlib.h></stdio.h>

1 réponse

Messages postés
2
Date d'inscription
samedi 18 octobre 2008
Statut
Membre
Dernière intervention
21 octobre 2008

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf/SDL_ttf.h>

int main(int argc, char *argv[])
{
SDL_Surface *ecran NULL, *texte[10] {NULL}, *avion = NULL, *explosion = NULL, *barre[100] = {NULL};
SDL_Rect positionAvion, positionExplosion, positionTexte[10] = {0}, positionBarre[100] = {0};
SDL_Event event;
TTF_Font *police = NULL;
SDL_Color couleurJaune = {230, 230, 0};

int continuer 1, m 0, i = 0, v= 0, time_1 = 0, time_2 = 0 ,time_3 = 0, time_4 = 0, time_5 = 0, time_6 = 0, jeu = 1, points = 0;
char chaine[20] = "", score[20] = "";

SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
police = TTF_OpenFont("/Users/guillaumeuettwiller/Desktop/copter/visitor.ttf", 30);

ecran = SDL_SetVideoMode(700, 400, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); /* Double Buffering */
SDL_WM_SetCaption("iPlane", NULL);
SDL_WM_SetIcon(IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/icone.png"), NULL); // L'icÙne doit Ítre chargÈe avant SDL_SetVideoMode


avion = IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/avion.png");
SDL_SetColorKey(avion, SDL_SRCCOLORKEY, SDL_MapRGB(avion->format, 0, 0, 0));
positionAvion.x = 100;
positionAvion.y = 200;

explosion = IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/explosion.png");
SDL_SetColorKey(explosion, SDL_SRCCOLORKEY, SDL_MapRGB(explosion->format, 0, 0, 0));
positionExplosion.x = 100;
positionExplosion.y = 300;

i = 0;
while (i < 100)
{
barre[i]=IMG_Load("/Users/guillaumeuettwiller/Desktop/copter/barre.jpeg");
SDL_SetColorKey(barre[i], SDL_SRCCOLORKEY, SDL_MapRGB(barre[i]->format, 0, 0, 0));
i++;
}

SDL_EnableKeyRepeat(20, 20);
SDL_ShowCursor(SDL_DISABLE);

while (continuer)
{
sprintf(score,"Score %ld",points);
texte[1] = TTF_RenderText_Solid(police, score, couleurJaune);
positionTexte[1].x = 700 - (texte[1]->w + 5);
positionTexte[1].y = 1;


time_1 = SDL_GetTicks();
if (time_1 - time_2 > 20)
{
if(positionAvion.y < 340 && positionAvion.y > 2 && jeu == 1)
positionAvion.y += 6;

time_2 = time_1;

}

time_3 = SDL_GetTicks();

if(time_3 - time_4 > 750)
{
time_4 = time_3;

points++;

}

time_5 = SDL_GetTicks();
if (time_5 - time_6 > 1000)
{
time_6 = time_5;
positionBarre[v].x = 600;
positionBarre[v].y = (rand()%(300-10+1))+10;
v++;
}


i = 0;


while (i < v)
{
positionBarre[i].x -=10;
if (positionBarre[i].x< 5)
SDL_SetAlpha(barre[i], SDL_SRCALPHA, 0);
SDL_BlitSurface(barre[i],NULL,ecran,&positionBarre[i]);
i++;

}


if ( positionAvion.y > 339 || positionAvion.y < 2 || ((positionAvion.y + avion->h) >= positionBarre[i].y && positionAvion.y <= (positionBarre[i].y + barre[i]->h) && positionAvion.x == positionBarre[i].x))
{
positionExplosion.y = positionAvion.y;

SDL_SetAlpha(avion, SDL_SRCALPHA, 0);
SDL_BlitSurface(explosion, NULL, ecran, &positionExplosion);

texte[0] = TTF_RenderText_Blended(police,"GAME OVER",couleurJaune);
positionTexte[0].x = 265;
positionTexte[0].y = 200;
SDL_BlitSurface(texte[0], NULL, ecran, &positionTexte[0]);

jeu = 0;
SDL_WaitEvent(&event);
if(event.type SDL_KEYDOWN && event.key.keysym.sym SDLK_ESCAPE)
continuer = 0;
}


SDL_Flip(ecran);


SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
break;
case SDLK_SPACE:
if (jeu == 0)
break;
positionAvion.y -= 8;
break;
}
break;
}

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
SDL_BlitSurface(avion, NULL, ecran, &positionAvion);
SDL_BlitSurface(texte[1], NULL, ecran, &positionTexte[1]);

}

SDL_FreeSurface(avion);
SDL_Quit();
TTF_CloseFont(police);
TTF_Quit();


return EXIT_SUCCESS;
}