Jeu : attrape moi !

Description

Un jeu sympathique (SDL), une map est créée, à certains endroits sont disposés des arbustes. Il y a un petit fantôme rouge qui démarre dans un coin de la MAP, et dans l'autre coin, un petit chevalier avec une épée et un bouclier.

Chaque joueur incarne un personnage et peu se déplacer sur la map selon un système de cases. Le but pour le chevalier, c'est d'attraper le fantôme, et le but du fantôme c'est de lui échapper... Pour celà, les deux joueurs disposes chacun de petits pouvoir, le fantôme passe "sous" les arbres, tandis que le chevalier passe "SUR" les arbres... Le fantôme peut se dédoubler, le chevalier lui peu poser des mines, se transforer en arbre lui même ou bien encore inverser la map...
(3 semaines de programmation en C et 2 jour d'apprentissage à la SDL)

Source / Exemple :


#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <FMOD/fmod.h>

#define TAILLE_BLOC         48
# define NB_BLOCS_HAUTEUR  14
# define NB_BLOCS_LARGEUR  18
#define LARGEUR_FENETRE     TAILLE_BLOC * NB_BLOCS_LARGEUR
#define HAUTEUR_FENETRE     TAILLE_BLOC * NB_BLOCS_HAUTEUR
enum {VIDE, ARBRE, DYNAMITE};

int main(int argc, char *argv[])
{
    long i;
    long j = 0;
    int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {VIDE, VIDE, ARBRE, ARBRE, VIDE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, VIDE, ARBRE, VIDE, VIDE, VIDE,VIDE, ARBRE, ARBRE, ARBRE, ARBRE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, VIDE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, VIDE, ARBRE, VIDE, VIDE, VIDE, ARBRE, ARBRE, ARBRE, VIDE, VIDE, ARBRE, ARBRE, VIDE, VIDE, VIDE, VIDE, VIDE, ARBRE, VIDE, VIDE};
    int fin = 0;
    int choix = 0;
    int transphormation = 7;
    int bouger = 20;
    int inverser = 2;
    int coupFantome = 0;
    int dedoublement = 2;
    int bombeEnPlace = 0;

    SDL_Rect position;
    SDL_Rect positionPersonnage;
    SDL_Rect positionFantome;
    SDL_Rect positionLeurre;
    SDL_Rect positionDynamite;

    positionFantome.x = 0;
    positionFantome.y = 0;

            positionDynamite.x = 1555;
            positionDynamite.y = 1555;

    SDL_Rect positionFin;
    positionFin.x = (LARGEUR_FENETRE/2) - (529/2);
    positionFin.y = (HAUTEUR_FENETRE/2) - (73/2);

    int continuer = 0;
    SDL_Event event;

    SDL_Surface *ecran = NULL;
    ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE);
    SDL_WM_SetIcon(SDL_LoadBMP("maison.bmp"), NULL);
    SDL_WM_SetCaption("Attrape moi si tu peux !", NULL);

    SDL_Surface *arbre = NULL;
    SDL_Surface *personnage = NULL;
    SDL_Surface *fantome = NULL;
    SDL_Surface *fantomeHAUT = NULL;
    SDL_Surface *fantomeBAS = NULL;
    SDL_Surface *fantomeGAUCHE = NULL;
    SDL_Surface *fantomeDROITE = NULL;
    SDL_Surface *Fin = NULL;
    SDL_Surface *dynamite = NULL;
    SDL_Surface *commandes = NULL;

    arbre = SDL_LoadBMP("arbre.bmp");
    personnage = SDL_LoadBMP("personnage.bmp");
    fantomeHAUT = SDL_LoadBMP("fantomeHAUT.bmp");
    fantomeBAS = SDL_LoadBMP("fantomeBAS.bmp");
    fantomeGAUCHE = SDL_LoadBMP("fantomeGAUCHE.bmp");
    fantomeDROITE = SDL_LoadBMP("fantomeDROITE.bmp");
    Fin = SDL_LoadBMP("fin.bmp");
    fantome = fantomeDROITE;
    dynamite = SDL_LoadBMP("dynamite.bmp");
    commandes = SDL_LoadBMP("commandes.bmp");

    SDL_Init(SDL_INIT_VIDEO | SDL_DOUBLEBUF | SDL_INIT_AUDIO);
    FSOUND_Init(44100, 32, 0);
    FSOUND_STREAM *musique = NULL;
    musique = FSOUND_Stream_Open("musique.mp3", 0, 0, 0);
    FSOUND_Stream_Play(FSOUND_FREE, musique);
    FSOUND_SetVolume(FSOUND_ALL, 120);

    positionPersonnage.x = NB_BLOCS_LARGEUR*TAILLE_BLOC - TAILLE_BLOC;
    positionPersonnage.y = NB_BLOCS_HAUTEUR*TAILLE_BLOC - TAILLE_BLOC;

    while (!continuer)
    {

    SDL_WaitEvent (&event);

        switch (event.type)
        {
            case SDL_QUIT:
            continuer = 1;
            break;
        case SDL_KEYDOWN:
        continuer = 1;
        break;
        }
        SDL_BlitSurface(commandes, NULL, ecran, &positionFantome);
        SDL_Flip(ecran);
    }

    while(continuer)
    {
        coupFantome--;
    SDL_WaitEvent (&event);

        switch (event.type)
        {
            case SDL_QUIT:
            continuer = 0;
            break;
        case SDL_KEYDOWN:
        switch(event.key.keysym.sym)
        {

            //COMMANDES DU JEU

            case SDLK_SPACE:
            ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE | SDL_FULLSCREEN);
            break;

            case SDLK_ESCAPE:
            ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, 32, SDL_HWSURFACE );
            break;

            case SDLK_DELETE:
            if (dedoublement)
            {
            coupFantome = 60;
            dedoublement--;
            }
            break;

            case SDLK_UP:
            fantome = fantomeHAUT;
            positionFantome.y = positionFantome.y-TAILLE_BLOC;
            break;

            case SDLK_DOWN:
            fantome = fantomeBAS;
            if(positionFantome.y < HAUTEUR_FENETRE - TAILLE_BLOC)
            positionFantome.y = positionFantome.y+TAILLE_BLOC;
            break;

            case SDLK_RIGHT:
            fantome = fantomeDROITE;
            if(positionFantome.x < LARGEUR_FENETRE - TAILLE_BLOC)
            positionFantome.x = positionFantome.x+TAILLE_BLOC;
            break;

            case SDLK_LEFT:
            fantome = fantomeGAUCHE;
            positionFantome.x = positionFantome.x-TAILLE_BLOC;
            break;

            case SDLK_q:
            transphormation = transphormation - 1;
            if (transphormation > 0)
            {
                if (personnage ==arbre)
                personnage = SDL_LoadBMP("personnage.bmp");

            else
            {
            personnage = arbre;
            }
            }

            break;

            case SDLK_TAB:
            if(inverser)
            {

        for (j = 0; j < NB_BLOCS_HAUTEUR; j++)
    {
        for (i = 0; i < NB_BLOCS_LARGEUR; i++)
        {

                position.x = i*TAILLE_BLOC;
                position.y = j*TAILLE_BLOC;

            if( carte[i][j] == ARBRE )
            {
            carte[i][j] = VIDE;
            }
            else if( carte[i][j] == VIDE )
            {
            carte[i][j] = ARBRE;
            }

        }
    }
    inverser--;
            }
            break;

            case SDLK_e:
            bombeEnPlace++;
            positionDynamite.x = positionPersonnage.x;
            positionDynamite.y = positionPersonnage.y;
            break;

            case SDLK_w:
                            if (personnage ==arbre)
                            bouger--;
                            if(!bouger)
                           {personnage = SDL_LoadBMP("personnage.bmp");
                            bouger = 15;
                           }

            positionPersonnage.y = positionPersonnage.y-TAILLE_BLOC;
            break;

            case SDLK_s:
                                        if (personnage ==arbre)
                            bouger--;
                            if(!bouger)
                           {personnage = SDL_LoadBMP("personnage.bmp");
                            bouger = 15;
                           }

            if(positionPersonnage.y < HAUTEUR_FENETRE - TAILLE_BLOC)
            positionPersonnage.y = positionPersonnage.y+TAILLE_BLOC;
            break;

            case SDLK_d:
                                                    if (personnage ==arbre)
                            bouger--;
                            if(!bouger)
                           {personnage = SDL_LoadBMP("personnage.bmp");
                            bouger = 15;
                           }

            if(positionPersonnage.x < LARGEUR_FENETRE - TAILLE_BLOC)
            positionPersonnage.x = positionPersonnage.x+TAILLE_BLOC;
            break;

            case SDLK_a:
                                                    if (personnage ==arbre)
                            bouger--;
                            if(!bouger)
                           {personnage = SDL_LoadBMP("personnage.bmp");
                            bouger = 15;
                           }

            positionPersonnage.x = positionPersonnage.x-TAILLE_BLOC;
            break;
            }
        break;
        }

           positionLeurre.x  = positionFantome.x + TAILLE_BLOC + TAILLE_BLOC;
        positionLeurre.y  = positionFantome.y + TAILLE_BLOC;

SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
SDL_BlitSurface(personnage, NULL, ecran, &positionPersonnage);
SDL_BlitSurface(dynamite, NULL, ecran, &positionDynamite);
SDL_BlitSurface(fantome, NULL, ecran, &positionFantome);
    if (coupFantome >0)
SDL_BlitSurface(fantome, NULL, ecran, &positionLeurre);

    for (j = 0; j < NB_BLOCS_HAUTEUR; j++)
    {
        for (i = 0; i < NB_BLOCS_LARGEUR; i++)
        {
                position.x = i*TAILLE_BLOC;
                position.y = j*TAILLE_BLOC;

        if (carte[j][i] == ARBRE)
        {
            SDL_BlitSurface(arbre, NULL, ecran, &position);
        }

        }
    }

SDL_BlitSurface(dynamite, NULL, ecran, &positionDynamite);
SDL_BlitSurface(personnage, NULL, ecran, &positionPersonnage);

SDL_Flip(ecran);

        if((positionLeurre.y == positionPersonnage.y && positionLeurre.x == positionPersonnage.x) || (positionLeurre.y == positionDynamite.y && positionLeurre.x == positionDynamite.x))
    {
               coupFantome = 0;
            positionDynamite.x = 2000;
    }

        if ((positionFantome.y == positionPersonnage.y && positionFantome.x == positionPersonnage.x) || (positionDynamite.y == positionFantome.y && positionDynamite.x == positionFantome.x))
        {
            for (i=0; i < 500; i++)
            {
                SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
        SDL_BlitSurface(Fin, NULL, ecran, &positionFin);
        SDL_Flip(ecran);
    positionPersonnage.x = NB_BLOCS_LARGEUR*TAILLE_BLOC - TAILLE_BLOC;
    positionPersonnage.y = NB_BLOCS_HAUTEUR*TAILLE_BLOC - TAILLE_BLOC;
    positionFantome.x = 0;
    positionFantome.y = 0;

            }
         transphormation = 7;
         bouger = 15;
         inverser = 4;
         coupFantome = 0;
         dedoublement = 1;
         positionDynamite.x = 1500;
        }
    }

//liberation de la memoire a faire ou pas (je viens de le voir et j'ai fait ça il y a longtemps) 

SDL_Quit();
FSOUND_Close();
return EXIT_SUCCESS;
    }

Conclusion :


Un jeu simple mais amusant (mes amis au Lycée sont pour ainsi dire "FAN" et on délire avec ça au CDI par exemple ^^)... Plein d'amélioration possible comme un compteur, enregistrer les scores (mais je n'avais pas appris à écrire dans des fichiers et la gestion du temps à l'époque... de plus c'est la misère pour afficher du texte il faut télécharger d'autres librairies...)

remarque : (Ajoutez musique.mp3 dans le répértoire ou se trouve le .exe pour la jouer)

Codes Sources

A voir également

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.

Du même auteur (cs_Passio)