Ceci est un petit programme que j'ai fait dans le but d'apprendre à faire des jeux en 2D.
Pour plus d'informmations ou bien pour en discuter sur le sujet voilà mon msn "migon.31@hotmail.fr".
Source / Exemple :
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//Les attributs de l'écran
const int SCREEN_WIDTH = 238;
const int SCREEN_HEIGHT = 224;
const int SCREEN_BPP = 32;
//Les attributs de la feuille des sprites
const int SHEET_WIDTH = 602;
const int SHEET_HEIGHT = 1257;
//Les attributs du personnage
const int PERSONE_WIDTH = 30;
const int PERSONE_HEIGHT = 57;
//les chrono
int i1,i2=0;
//le numero de stage
int stage=1;
//etat du joue
bool gagner=false;
//Les surfaces
SDL_Surface *personnage_screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *bouclier = NULL;
SDL_Surface *porte = NULL;
SDL_Surface *Ganger = NULL;
SDL_Surface *Cle = NULL;
SDL_Surface *screen = NULL;
//La structure d'événements
SDL_Event event;
//la banque des images
SDL_Rect imageG[ 4 ];
SDL_Rect imageD[ 4 ];
SDL_Rect R;
SDL_Rect B[2];
SDL_Rect table;
SDL_Rect Porte;
//animation du bouclier
int anim_bouclier=0;
//effet transparence
int alpha = SDL_ALPHA_TRANSPARENT;
int alpha2 = SDL_ALPHA_OPAQUE;
//La classe Timer
class personnage
{
private:
//possition du personnage
int posx;
int posy;
//direction du joeur 1pour droite, 0pour gauche.
int direct;
//true pour actif , false pour non actif
bool statut;
//le numero d'image à afficher
int anim;
public:
//Le constructeur permettant l'initialisation des variables
personnage();
//traiter les evenements clavier
void evenement();
//traiter les collisions
bool collision (int A, SDL_Rect B);
//taux de rafraichissment
bool chrono();
//montrer le personnage
void affichage();
};
//charger une image sans le mauve
SDL_Surface *load_image( std::string filename )
{
//L'image qui est chargée
SDL_Surface* loadedImage = NULL;
//L'image optimisée qu'on va utiliser
SDL_Surface* optimizedImage = NULL;
//Chargement de l'image
loadedImage = IMG_Load( filename.c_str() );
//Si l'image a bien chargée
if( loadedImage != NULL )
{
//Création de l'image optimisée
optimizedImage = SDL_DisplayFormat( loadedImage );
//Libération de l'ancienne image
SDL_FreeSurface( loadedImage );
//Si la surface a bien été optimisée
if( optimizedImage != NULL )
{ //ne pas charger la couleur mauve
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 80, 80, 224 ) );
}
}
//On retourne l'image optimisée
return optimizedImage;
}
//charger une image sans le marron
SDL_Surface *load_image2( std::string filename )
{
//L'image qui est chargée
SDL_Surface* loadedImage = NULL;
//L'image optimisée qu'on va utiliser
SDL_Surface* optimizedImage = NULL;
//Chargement de l'image
loadedImage = IMG_Load( filename.c_str() );
//Si l'image a bien chargée
if( loadedImage != NULL )
{
//Création de l'image optimisée
optimizedImage = SDL_DisplayFormat( loadedImage );
//Libération de l'ancienne image
SDL_FreeSurface( loadedImage );
//Si la surface a bien été optimisée
if( optimizedImage != NULL )
{ //ne pas charger la couleur marron
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 111, 79, 51 ) );
}
}
//On retourne l'image optimisée
return optimizedImage;
}
//charger une image sans le bleu nuit
SDL_Surface *load_image3( std::string filename )
{
//L'image qui est chargée
SDL_Surface* loadedImage = NULL;
//L'image optimisée qu'on va utiliser
SDL_Surface* optimizedImage = NULL;
//Chargement de l'image
loadedImage = IMG_Load( filename.c_str() );
//Si l'image a bien chargée
if( loadedImage != NULL )
{
//Création de l'image optimisée
optimizedImage = SDL_DisplayFormat( loadedImage );
//Libération de l'ancienne image
SDL_FreeSurface( loadedImage );
//Si la surface a bien été optimisée
if( optimizedImage != NULL )
{ //ne pas charger la couleur bleu nuit
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 8, 33, 82));
}
}
//On retourne l'image optimisée
return optimizedImage;
}
//preparer l'ecran
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
//On blitte la surface
SDL_BlitSurface( source, clip, destination, &offset );
}
//préparer les differentes positions du personnage
void decoupage()
{
//arriere plan
R.x = 10;
R.y = 10;
R.w = 238;
R.h = 224;
//bouclier
B[0].x = 0;
B[0].y = 0;
B[0].w = 29.5;
B[0].h = 32;
B[1].x = 29.5;
B[1].y = 0;
B[1].w = 29.5;
B[1].h = 32;
//table
table.x = 102;
table.y = 105;
table.w = 60;
table.h = 50;
//personnage
imageG[ 0 ].x = 20;
imageG[ 0 ].y = 60*3;
imageG[ 0 ].w = PERSONE_WIDTH;
imageG[ 0 ].h = PERSONE_HEIGHT;
imageG[ 1 ].x = 20+PERSONE_WIDTH+2;
imageG[ 1 ].y = 60*3;
imageG[ 1 ].w = PERSONE_WIDTH;
imageG[ 1 ].h = PERSONE_HEIGHT;
imageG[ 2 ].x = 20*2+PERSONE_WIDTH+15;
imageG[ 2 ].y = 60*3;
imageG[ 2 ].w = PERSONE_WIDTH;
imageG[ 2 ].h = PERSONE_HEIGHT-3;
imageG[ 3 ].x = 20*3+PERSONE_WIDTH-5;
imageG[ 3 ].y = 60*3;
imageG[ 3 ].w = PERSONE_WIDTH;
imageG[ 3 ].h = PERSONE_HEIGHT-3;
imageD[ 0 ].x = 20*23-5;
imageD[ 0 ].y = 60*3;
imageD[ 0 ].w = PERSONE_WIDTH;
imageD[ 0 ].h = PERSONE_HEIGHT;
imageD[ 1 ].x = 20*23-3+PERSONE_WIDTH;
imageD[ 1 ].y = 60*3;
imageD[ 1 ].w = PERSONE_WIDTH;
imageD[ 1 ].h = PERSONE_HEIGHT;
imageD[ 2 ].x = 20*23+PERSONE_WIDTH+30;
imageD[ 2 ].y = 60*3;
imageD[ 2 ].w = PERSONE_WIDTH;
imageD[ 2 ].h = PERSONE_HEIGHT-3;
imageD[ 3 ].x = 20*23+PERSONE_WIDTH+60;
imageD[ 3 ].y = 60*3;
imageD[ 3 ].w = PERSONE_WIDTH;
imageD[ 3 ].h = PERSONE_HEIGHT-3;
//la porte
Porte.x = 0;
Porte.y = 0;
Porte.w = 34;
Porte.h = 25;
}
bool personnage::collision (int a, SDL_Rect B)
{
//collision avec la table
//traitement du coté droit de la table
if (((personnage::posx > B.x-30)&&(personnage::posx < B.x-30+table.w))&&((personnage::posy>43)&&(personnage::posy<43+table.h)))
return true;
//traitement du coté gauche de la table
if (((personnage::posx < B.x-30+table.w+10)&&(personnage::posx > B.x-30))&&((personnage::posy>43)&&(personnage::posy<43+table.h)))
return true;
//traitement du coté haut de la table
if (((personnage::posy > 43)&&(personnage::posy < 43+table.h))&&((personnage::posx>B.x-30)&&(personnage::posx<B.x-30+table.w)))
return true;
//traitement du coté bat de la table
if (((personnage::posy < 43+table.h)&&(personnage::posy >43))&&((personnage::posx>B.x-30)&&(personnage::posx<B.x-30+table.w)))
return true;
//collision avc les murs
if ((personnage::posy < 20)&&(personnage::posx <109))
return true;
if ((personnage::posy < 10)&&((personnage::posx >109)&&(personnage::posx <148)))
return true;
if ((personnage::posx > 165)&&(personnage::posy <75))
return true;
if (personnage::posx > 205)
return true;
if ((personnage::posy > 112)&&((personnage::posx <93))||((personnage::posy > 112)&&(personnage::posx >115)))
return true;
if (personnage::posy > 170)
return true;
//collision avec le bouclier
if((personnage::posx < 15)&&((personnage::posy >79)&&(personnage::posy <107)))
{
anim_bouclier=1;
return true;
}
//collision avec la porte
if ((anim_bouclier==0)&&((personnage::posy > 112)&&((personnage::posx >90)&&(personnage::posx <115))))
return true;
}
//initialisation
void init()
{
//Initialisation de tous les sous-système de SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Mise en place de l'écran
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE);
//Mise en place de la barre caption
SDL_WM_SetCaption( "FILLE", NULL );
}
//chargement des fichiers
void load_files()
{
//Chargement
personnage_screen = load_image( "fille.png" );
background = load_image( "background.png" );
bouclier = load_image2( "bouclier.png" );
porte = load_image( "porte.png" );
Ganger = load_image ( "ganger.png" );
Cle = load_image3 ( "cle.png" );
}
//quitter
void clean_up()
{
//Libération des surfaces
SDL_FreeSurface( personnage_screen );
//On quitte SDL
SDL_Quit();
}
//initialiser le personnage
personnage::personnage()
{
posx = 30;
posy = 30;
anim = 0;
direct = 1;
statut = true;
}
bool personnage::chrono()
{
i1 = SDL_GetTicks();
if ( i1-i2 > 70)
{
i2=i1;
return true;
}
else return false;
}
//traiter les evenements
void personnage::evenement()
{
//Obtenir les KeyStates
Uint8 *keystates = SDL_GetKeyState( NULL );
bool move= false;
//tantque l'utilisateur na pas gagner en traite les evenements
if ( gagner== false)
{
if( keystates[ SDLK_UP ] )
{
posy-=3;
if ((posy <= 0) || (collision(posx,table)==true))
posy+=3;
move= true;
}
//Si Bas est pressé (la fleche)
if( keystates[ SDLK_DOWN ] )
{
posy+=3;
if (( posy > 535 )||(collision(posx,table)==true))
posy-=3;
move= true;
}
//Si Gauche est pressé (la fleche)
if( keystates[ SDLK_LEFT ] )
{
direct=0;
posx-=3;
if ((posx <= 0) || (collision(posx,table)==true))
posx+=3;
move= true;
}
//Si Droite est pressé (la fleche)
if( keystates[ SDLK_RIGHT ] )
{
direct=1;
posx+=3;
if ((posx >= SCREEN_WIDTH-30)||(collision(posx,table)==true))
posx-=3;
move= true;
}
}
if ( move == true ) anim++;
else anim = 0;
if ( anim == 4) anim = 0;
}
//préperer le personnage pour l'affichage
void personnage::affichage()
{
//s'il est temps pour rafraichire l'ecran et qu'on a pas encor finis le stage
if (/*(personnage::chrono())&&*/(gagner== false))
{
//affichage de l'arriere plan
apply_surface( 0, 0, background, screen, &R);
//afficahge du bouclier
apply_surface( 0, 125, bouclier, screen, &B[anim_bouclier]);
//affichage de la porte tant que le joueur na pa activé le bouclier
if ( anim_bouclier == 0 )
apply_surface( 102, 168, porte, screen, &Porte );
//affichage du personnage selon la direction
if ( direct == 1)
apply_surface( posx, posy, personnage_screen, screen, &imageD[ anim ]);
else
apply_surface( posx, posy, personnage_screen, screen, &imageG[ anim ]);
//affichage de la clé
apply_surface( 100, 195, Cle, screen);
}
//si le joueur gange
if(personnage::posy >160)
{
gagner = true;
alpha += 1;
SDL_SetAlpha( Ganger, SDL_SRCALPHA | SDL_RLEACCEL, alpha );
apply_surface( 0, 0, Ganger, screen);
if (alpha > 250) alpha=250;
//passer au stage suivant
stage=2;
}
}
//programme principal
int main( int argc, char* args[] )
{
//Ce qui va nous permettre de quitter
bool quitG=false, quit = false;
//Initialisation
init();
//Chargement des fichiers
load_files();
//decoupage
decoupage();
//declarer notre personnage
personnage fille;
//Tant que l'utilisateur n'a pas quitter
while( quit == false )
{
if( fille.chrono())
{
//traiter les evenement clavier
fille.evenement();
//afficher le personnage
fille.affichage();
SDL_Flip( screen);
}
//Tant qu'il y a un événement
while( SDL_PollEvent(&event))
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE: /* Appui sur la touche Echap, on arrête le programme */
quit = true;
break;
}
}//end of while du traitement d'evenements clavier
}//end of while du premier stage
//On libère les images et on quitte SDL
clean_up();
return 0;
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.