Bonjour,
J'ai suivi le cours de C sur "le site du zéro" et j'ai un problème lors du TP sur un Mario Sokoban.
Je pense avoir terminer mon code mais lorsque je l'exécute la fenêtre reste noire (il y a l'icon et le titre).
Je suppose que vous ne pourriez rien faire sans mon code alors le voici (bon courage pour vous plonger dedans ;) ):
constante.h
/*
constantes.h
------------
Par mateo21, pour Le Site du Zér0 (www.siteduzero.com)
Rôle : définit des constantes communes à tout le programme (taille de la fenêtre...)
*/
#ifndef DEF_CONSTANTES
#define DEF_CONSTANTES
#define TAILLE_BLOC 34 // Taille d'un bloc (carré) en pixels
#define NB_BLOCS_LARGEUR 12
#define NB_BLOCS_HAUTEUR 12
#define LARGEUR_FENETRE TAILLE_BLOC * NB_BLOCS_LARGEUR
#define HAUTEUR_FENETRE TAILLE_BLOC * NB_BLOCS_HAUTEUR
enum {HAUT, BAS, GAUCHE, DROITE};
enum {VIDE, MUR, CAISSE, OBJECTIF, MARIO, CAISSE_OK};
#endif
header.h
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL_image.h>
#include "constante.h"
int editeur(SDL_Surface *ecran);
int jouer(SDL_Surface *ecran);
int deplacerJoueur(int carte[][12] ,SDL_Rect *pos ,int direction ,SDL_Surface *ecran);
int test(int direction,SDL_Rect positionjoueur,int *carte);
int chargerNiveau(int carte[][12] ,SDL_Rect *pos);
int blitteur(int carte[][12] ,SDL_Surface *ecran,int direction);
main.c
#include "header.h"
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *ecran = NULL, *menu = NULL;
ecran = SDL_SetVideoMode(408,408,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetIcon(IMG_Load("caisse.jpg"), NULL);
SDL_WM_SetCaption("Mario Sokoban !", NULL);
SDL_Rect positionMenu;
positionMenu.x = 0;
positionMenu.y = 0;
menu = IMG_Load("menu.jpg");
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,0,0,255));
SDL_BlitSurface(menu,NULL,ecran,&positionMenu);
SDL_Event event;
int continuer = 1;
while(continuer == 1)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_1:
jouer(ecran);
break;
}
break;
}}
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,0,0,0));
SDL_BlitSurface(menu,NULL,ecran,&positionMenu);
SDL_Flip(ecran);
SDL_FreeSurface(menu);
SDL_Quit();
return EXIT_SUCCESS;
}
jouer.c
#include "Header.h"
int jouer (SDL_Surface *ecran)
{
SDL_Surface *mario[4]={0};
SDL_Surface *mur=NULL, *caisse=NULL, *caisseOk = NULL, *marioActuel = NULL;
SDL_Rect position, positionjoueur;
SDL_Event event;
int i =0, j = 0, continuer =1, objectifsRestants = 0;
int carte[12][12]={0};
mario[BAS] = IMG_Load("mario_bas.gif");
mario[HAUT] = IMG_Load("mario_haut.gif");
mario[DROITE] = IMG_Load("mario_droite.gif");
mario[GAUCHE] = IMG_Load("mario_gauche.gif");
marioActuel = mario[BAS];
chargerNiveau(carte,&positionjoueur);
SDL_EnableKeyRepeat(10,10);
while(continuer)
{
SDL_WaitEvent(&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_RIGHT:
marioActuel=mario[DROITE];
deplacerJoueur(carte,&positionjoueur,DROITE,ecran);
break;
case SDLK_LEFT:
marioActuel=mario[GAUCHE];
deplacerJoueur(carte,&positionjoueur,GAUCHE,ecran);
break;
case SDLK_DOWN:
marioActuel=mario[BAS];
deplacerJoueur(carte,&positionjoueur,BAS,ecran);
break;
case SDLK_UP:
marioActuel=mario[HAUT];
deplacerJoueur(carte,&positionjoueur,HAUT,ecran);
break;
}
break;
}
}
SDL_EnableKeyRepeat(0, 0);
}
deplacerJoueur.c
#include "Header.h"
int deplacerJoueur(int carte[][12] ,SDL_Rect *pos ,int direction,SDL_Surface *ecran)
{
int continuer = 0;
switch(direction)
{
case DROITE:
if((pos->x)+1!= MUR && ((pos->x)+2 != CAISSE && ( (pos->x)+1 == CAISSE || (pos->x)+1 == CAISSE_OK ) && (pos->x)+2 != CAISSE_OK))
{
carte[pos->y][pos->x]= VIDE;
if(carte[pos->y][(pos->x)+1] == CAISSE )
{
carte[pos->y][(pos->x)+2]== CAISSE;
}
if(carte[pos->y][(pos->x)+1] == OBJECTIF)
{
carte[pos->y][(pos->x)+2]== CAISSE_OK;
}
carte[pos->y][(pos->x)+1]= MARIO;
blitteur(carte,ecran,direction);
}
break;
case GAUCHE:
if((pos->x)-1!= MUR && ((pos->x)-2 != CAISSE && ( (pos->x)-1 == CAISSE || (pos->x)-1 == CAISSE_OK ) && (pos->x)-2 != CAISSE_OK))
{
carte[pos->y][pos->x]= VIDE;
carte[pos->y][(pos->x)-1]= MARIO;
if(carte[pos->y][(pos->x)-1] == CAISSE )
{
carte[pos->y][(pos->x)-2]== CAISSE;
}
if(carte[pos->y][(pos->x)-1] == OBJECTIF)
{
carte[pos->y][(pos->x)-2]== CAISSE_OK;
}
blitteur(carte,ecran,direction);
}
break;
case BAS:
if((pos->y)-1!= MUR && ((pos->y)-2 != CAISSE && ( (pos->y)-1 == CAISSE || (pos->y)-1 == CAISSE_OK ) && (pos->y)-2 != CAISSE_OK))
{
carte[pos->y][pos->x]= VIDE;
if(carte[(pos->y)+1][pos->x] == CAISSE )
{
carte[(pos->y)+2][pos->x]== CAISSE;
}
if(carte[(pos->y)+1][pos->x] == OBJECTIF)
{
carte[(pos->y)+2][pos->x]== CAISSE_OK;
}
carte[(pos->y)-1][pos->x] = MARIO;
blitteur(carte,ecran,direction);
}
break;
case HAUT:
if((pos->y)+1!= MUR && ((pos->y)+2 != CAISSE && ( (pos->y)+1 == CAISSE || (pos->y)+1 == CAISSE_OK ) && (pos->y)+2 != CAISSE_OK))
{
carte[pos->y][pos->x]= VIDE;
if(carte[(pos->y)-1][pos->x] == CAISSE )
{
carte[(pos->y)-2][pos->x]== CAISSE;
}
if(carte[(pos->y)-1][pos->x] == OBJECTIF)
{
carte[(pos->y)-2][pos->x]== CAISSE_OK;
}
carte[(pos->y)-1][pos->x]= MARIO;
blitteur(carte,ecran,direction);
}
break;
}
}
chargerNiveau.c
#include "Header.h"
int chargerNiveau(int carte[][12] , SDL_Rect *pos)
{
int i =0, j =0, lettre = 0;
FILE *fichier = NULL;
fichier = fopen("niveau.lvl","r");
if(fichier = 0)
{
exit(EXIT_FAILURE);
fprintf(stderr,"erreure d'ouverture du fichier niveau.lvl");
}
for(j = 0; j < 12; j++)
for(i = 0;lettre != '\0';i++)
{
lettre = fgetc(fichier);
switch(lettre)
{
case '1':
carte[j][i] = 1;
break;
case '2':
carte[j][i] = MUR;
break;
case '3':
carte[j][i] = CAISSE;
break;
case '4':
carte[j][i] = OBJECTIF;
break;
case '5':
carte[j][i] = MARIO;
pos->y = j;
pos->x = i;
break;
case '6':
carte[j][i] = CAISSE_OK;
break;
}
}
fclose(fichier);
}
blitteur.c
#include "Header.h"
int blitteur(int carte[][12],SDL_Surface *ecran,int direction)
{
int i = 0, j = 0;
SDL_Surface *mur=NULL, *caisse=NULL, *caisseOk = NULL, *marioActuel = NULL, *objectif = NULL;
SDL_Surface *mario[4]={0};
SDL_Rect blit;
mur = IMG_Load("mur.jpg");
caisse = IMG_Load("caisse.jpeg");
caisseOk = IMG_Load("caisse_ok.jpeg");
objectif = IMG_Load("objectif.jpeg");
mario[BAS] = IMG_Load("mario_bas.gif");
mario[HAUT] = IMG_Load("mario_haut.gif");
mario[DROITE] = IMG_Load("mario_droite.gif");
mario[GAUCHE] = IMG_Load("mario_gauche.gif");
switch(direction)
{
case DROITE:
marioActuel = mario[DROITE];
break;
case GAUCHE:
marioActuel = mario[GAUCHE];
break;
case HAUT:
marioActuel = mario[HAUT];
break;
case BAS:
marioActuel = mario[BAS];
break;
}
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,0,0,0));
for(i = 0; i < 12; i++)
{
for(j = 0; j < 12; j++)
{
blit.x = (j*34);
blit.y = (i*34);
switch(carte[i][j])
{
case MUR:
SDL_BlitSurface(mur,NULL,ecran,&blit);
break;
case CAISSE:
SDL_BlitSurface(caisse,NULL,ecran,&blit);
break;
case OBJECTIF:
SDL_BlitSurface(objectif,NULL,ecran,&blit);
break;
case MARIO:
SDL_BlitSurface(marioActuel,NULL,ecran,&blit);
break;
case CAISSE_OK:
SDL_BlitSurface(caisseOk,NULL,ecran,&blit);
break;
}
}
}
}
merci de votre aide.
Afficher la suite