Partage
  • Partager sur Facebook
  • Partager sur Twitter

Jeu qui rame

Apres changement du mode d'affichage des sprites

Anonyme
    28 juillet 2006 à 2:53:34

    Salut,

    J'ai voulu faire une enième amélioration pour mon sokoban, mais la j'ai un problème, depuis la derniere modif, mon jeu rame. ( le perso se déplace 1 sec apres que j'ai appuyé sur le bouton )

    J'ai modifié le systeme d'affichage des sprites, au lieu de devoir charger plein de sprite au départ, je les aient tous rassemblé en une image, et je blit la partie qui m'intéresse.

    Ma fonction jouer:

    void jouer(SDL_Surface* ecran, long niveau, int *quitter, int reprendre_partie, int new_level)

    {

        // initiallisation des variables



        long nbr_mouvement = 0, nbr_caisse_pousse = 0;

        long nbr_niveau = nombreNiveau(new_level);

        long retour_menu = 0; // retourne au menu principal



        int continuer = 1, objectif_restants = 0;

        int i = 0, c = 0;

        int carte[NBR_BLOC_LARGEUR][NBR_BLOC_HAUTEUR] = {0};

        int pousse_une_caisse[4] = {0}; // determine vers ou est pousser la caisse

        int autosave = 0;

        int debut_du_niveau = 1;



        char score_dep[25] = {0}, score_pous[25] = {0};

        char record_dep[25] = {0}, record_pous[25] = {0};



        SDL_Surface *sprites = {NULL};

        SDL_Surface *affichage_score_dep = NULL, *affichage_score_pous = NULL;

        SDL_Surface *affichage_record_dep = NULL, *affichage_record_pous = NULL;



        SDL_Rect position, position_joueur, perso_actuel, position_image;

        SDL_Event event;

        SDL_Color blanc = {255,255,255};

        SDL_Color noir = {0,0,0};



        TTF_Font *police;




            sprites = IMG_Load("zelda.png");

        police = TTF_OpenFont("comic.ttf",15);



        TTF_SetFontStyle(police, TTF_STYLE_BOLD | TTF_STYLE_ITALIC);





        // Chargement des sprites



        /*if ( !chargement_option_jeu(&autosave, perso, &mur, &caisse, &caisse_ok, &objectif, &vide) )

             exit(EXIT_FAILURE);*/




        do // jeu fini ou arret du jeu

        {

            if ( !continuer )

                continuer = 1;



            if ( debut_du_niveau )

            {

                nbr_mouvement = 0;

                nbr_caisse_pousse = 0;

                debut_du_niveau = 0;

            }



            perso_actuel.x =0;
            perso_actuel.y =0;
            perso_actuel.w =TAILLE_BLOC;
            perso_actuel.h =TAILLE_BLOC;



             //on charge le niveau et ses records

            if ( !reprendre_partie )

            {

                if ( !chargerNiveau(carte, niveau, new_level, 0) )

                    exit(EXIT_FAILURE);

            }

            else

            {

                if ( !chargerPartie(carte, &niveau, &nbr_mouvement, &nbr_caisse_pousse) )

                    exit(EXIT_FAILURE);



                reprendre_partie = 0;

            }



            // on charge le record du niveau

            if ( !chargerRecord(niveau, record_dep, record_pous, new_level) )

                exit(EXIT_FAILURE);



            // on recherche la position initial du perso



            for ( i = 0; i < NBR_BLOC_HAUTEUR; i++)

            {

                for ( c = 0; c < NBR_BLOC_LARGEUR; c++)

                {

                    if ( carte[i][c] == PERSO )

                    {

                        position_joueur.x = i;

                        position_joueur.y = c;



                        carte[i][c] = VIDE;

                    }

                }

            }



            SDL_EnableKeyRepeat(100, 100);



            // sauvegarde automatique



            if ( autosave )

            {

                if ( !sauvgardeProgression(carte, niveau, position_joueur, nbr_mouvement, nbr_caisse_pousse) )

                    exit(EXIT_FAILURE);

            }



            while ( continuer )

            {

                SDL_WaitEvent(&event);

                switch(event.type)

                {

                    case SDL_QUIT:

                        continuer = 0;

                        retour_menu = 1;

                        *quitter = 1;

                        break;



                    case SDL_KEYDOWN:

                        switch(event.key.keysym.sym)

                        {

                            case SDLK_ESCAPE: // retour en ariere

                                retour_menu = 1;

                                continuer = 0;

                                break;



                            case SDLK_r: //recomencer le niveau

                                continuer = 0;

                                break;



                            case SDLK_s: // sauvgarde la progression

                                if ( !new_level )

                                {

                                    if ( !sauvgardeProgression(carte, niveau, position_joueur, nbr_mouvement, nbr_caisse_pousse) )

                                        exit(EXIT_FAILURE);

                                }

                                break;



                            case SDLK_n: // donne le niveau en cour

                                afficheNiveau(ecran, niveau, &(*quitter));

                                break;



                            case SDLK_PAGEUP:

                                if ( new_level )

                                {

                                    if ( niveau < nbr_niveau - 1 )

                                    {

                                        niveau++;

                                        continuer = 0;

                                    }

                                }

                                break;



                            case SDLK_PAGEDOWN:

                                if ( new_level )

                                {

                                    if ( niveau > 0 )

                                    {

                                        niveau--;

                                        continuer = 0;

                                    }

                                    break;

                                }



                            case SDLK_PRINT:

                                if ( !printScreen(ecran) )

                                    exit(EXIT_FAILURE);

                                break;



                            case SDLK_UP: // se déplace vers le haut

                                deplacerJoueur(carte, &position_joueur, HAUT, pousse_une_caisse,

                                               &nbr_mouvement, &nbr_caisse_pousse);



                                if ( pousse_une_caisse[HAUT] )

                                {

                                    if ( perso_actuel.x == TAILLE_BLOC*5 + 5 && perso_actuel.y == 0)
                                    {

                                        perso_actuel.x = TAILLE_BLOC*7 + 7;
                                        perso_actuel.y = 0;
                                    }

                                    else
                                    {

                                        perso_actuel.x == TAILLE_BLOC*5 + 5;
                                        perso_actuel.y = 0;
                                    }



                                        pousse_une_caisse[HAUT] = 0;

                                }

                                else

                                {

                                    if ( perso_actuel.x == TAILLE_BLOC*4 + 4 && perso_actuel.y == 0)
                                    {

                                        perso_actuel.x = TAILLE_BLOC*6 + 6 ;
                                        perso_actuel.y = 0;
                                    }

                                    else
                                    {

                                        perso_actuel.x = TAILLE_BLOC*4 + 4;
                                        perso_actuel.y = 0;
                                    }

                                }

                                break;



                            case SDLK_DOWN: // se déplace vers le bas

                                deplacerJoueur(carte, &position_joueur, BAS, pousse_une_caisse,

                                               &nbr_mouvement, &nbr_caisse_pousse);



                                if ( pousse_une_caisse[BAS] == 1 )

                                {

                                    if ( perso_actuel.x == TAILLE_BLOC + 1 && perso_actuel.y == 0)
                                    {

                                        perso_actuel.x = TAILLE_BLOC*3 + 3;
                                        perso_actuel.y = 0;
                                    }

                                    else
                                    {

                                        perso_actuel.x = TAILLE_BLOC + 1;
                                        perso_actuel.y = 0;
                                    }



                                    pousse_une_caisse[BAS] = 0;

                                }

                                else

                                {

                                    if ( perso_actuel.x == 0 && perso_actuel.y == 0)
                                    {

                                        perso_actuel.x = TAILLE_BLOC*2 + 2;
                                        perso_actuel.y = 0;
                                    }

                                    else
                                    {

                                        perso_actuel.x = 0;
                                        perso_actuel.y = 0;
                                    }

                                }



                                break;



                            case SDLK_RIGHT: // se déplace vers la droite

                                deplacerJoueur(carte, &position_joueur, DROITE, pousse_une_caisse,

                                               &nbr_mouvement, &nbr_caisse_pousse);



                                if ( pousse_une_caisse[DROITE] == 1 )

                                {

                                    if ( perso_actuel.x == TAILLE_BLOC + 1 && perso_actuel.y == TAILLE_BLOC + 1)
                                    {

                                        perso_actuel.x = TAILLE_BLOC * 3 + 3;
                                        perso_actuel.y = TAILLE_BLOC + 1;
                                    }

                                    else
                                    {

                                        perso_actuel.x = TAILLE_BLOC + 1;
                                        perso_actuel.y = TAILLE_BLOC + 1;
                                    }


                                    pousse_une_caisse[DROITE] = 0;

                                }

                                else

                                {

                                    if ( perso_actuel.x == 0 && perso_actuel.y == TAILLE_BLOC + 1)
                                    {

                                        perso_actuel.x = TAILLE_BLOC * 2 + 2;
                                        perso_actuel.y = TAILLE_BLOC + 1;
                                    }

                                    else
                                    {

                                        perso_actuel.x = 0;
                                        perso_actuel.y = TAILLE_BLOC + 1;
                                    }

                                }



                                break;



                            case SDLK_LEFT: // se déplace vers la gauche

                                deplacerJoueur(carte, &position_joueur, GAUCHE, pousse_une_caisse,

                                               &nbr_mouvement, &nbr_caisse_pousse);



                                if ( pousse_une_caisse[GAUCHE] == 1 )

                                {

                                    if ( perso_actuel.x == TAILLE_BLOC*5 + 5 && perso_actuel.y == TAILLE_BLOC + 1)
                                    {

                                        perso_actuel.x = TAILLE_BLOC*7 + 7;
                                        perso_actuel.y = TAILLE_BLOC + 1;
                                    }

                                    else
                                    {

                                        perso_actuel.x = TAILLE_BLOC*5 + 5;
                                        perso_actuel.y = TAILLE_BLOC + 1;
                                    }



                                        pousse_une_caisse[GAUCHE] = 0;

                                }

                                else

                                {

                                    if ( perso_actuel.x == TAILLE_BLOC*4 + 4 && perso_actuel.y == TAILLE_BLOC + 1)
                                    {

                                        perso_actuel.x = TAILLE_BLOC*6 + 6 ;
                                        perso_actuel.y == TAILLE_BLOC + 1;
                                    }

                                    else
                                    {

                                        perso_actuel.x = TAILLE_BLOC*4 + 4 ;
                                        perso_actuel.y == TAILLE_BLOC + 1;
                                    }

                                }

                                break;

                        }

                        break;

                }



                SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));



                // placement des objets à l'écran

                objectif_restants = 0;



                for ( i = 0; i < NBR_BLOC_LARGEUR ; i++ )

                {

                    for ( c = 0; c < NBR_BLOC_HAUTEUR; c++ )

                    {

                        position.x = i * TAILLE_BLOC;

                        position.y = c * TAILLE_BLOC;



                        // on blit la surface vide pour que les images transparentes

                        // aient l'image "vide" en arrière plan

                        position_image.x= 0;

                        position_image.y= TAILLE_BLOC*2 + 2;

                        SDL_BlitSurface(sprites, &position_image, ecran, &position);



                        switch(carte[i][c])

                        {

                            case MUR:
                                    position_image.x= TAILLE_BLOC + 1;
                                    position_image.y= TAILLE_BLOC*2 + 2;

                                SDL_BlitSurface(sprites, &position_image, ecran, &position);

                                break;



                            case CAISSE:
                             position_image.x= TAILLE_BLOC*3 + 3;
                                    position_image.y= TAILLE_BLOC*2 + 2;

                                SDL_BlitSurface(sprites, &position_image, ecran, &position);

                                break;



                            case CAISSE_OK:
                                    position_image.x= TAILLE_BLOC*4 + 4;
                                    position_image.y= TAILLE_BLOC*2 + 2;

                                SDL_BlitSurface(sprites, &position_image, ecran, &position);

                                break;



                            case OBJECTIF:
                                    position_image.x= TAILLE_BLOC*2 + 2;
                                    position_image.y= TAILLE_BLOC*2 + 2;

                                SDL_BlitSurface(sprites, &position_image, ecran, &position);

                                objectif_restants = 1;

                                break;



                             case VIDE:
                                    position_image.x= 0;
                                    position_image.y= TAILLE_BLOC*2 + 2;

                                SDL_BlitSurface(sprites, &position_image, ecran, &position);

                                break;

                        }

                    }

                }



                // on regarde si il reste des objectifs

                if ( !objectif_restants )

                {

                    if ( !sauvgardeRecord(niveau, nbr_mouvement, nbr_caisse_pousse, new_level) )

                        exit(EXIT_FAILURE);



                    niveau++;

                    niveauReussi(ecran, &(*quitter), niveau, new_level);



                    if ( *quitter )

                        retour_menu = 1;



                    debut_du_niveau = 1;

                    continuer = 0;

                }



                // on blit le joueur

                position.x = position_joueur.x * TAILLE_BLOC;

                position.y = position_joueur.y * TAILLE_BLOC;



                SDL_BlitSurface(sprites, &perso_actuel, ecran, &position);



                // on blit le score et les records



                    //score

                    sprintf(score_dep, "Déplacements: %ld", nbr_mouvement);

                    sprintf(score_pous, "Caisses poussées: %ld", nbr_caisse_pousse);



                    affichage_score_dep = TTF_RenderText_Solid(police, score_dep, noir);

                    affichage_score_pous = TTF_RenderText_Solid(police, score_pous, noir);



                    position.x = 10;

                    position.y = 10;

                    SDL_BlitSurface(affichage_score_dep, NULL, ecran, &position);



                    position.x = 10;

                    position.y = 25;

                    SDL_BlitSurface(affichage_score_pous, NULL, ecran, &position);



                    //record



                    affichage_record_dep = TTF_RenderText_Solid(police, record_dep, noir);

                    affichage_record_pous = TTF_RenderText_Solid(police, record_pous, noir);



                    position.x = LARGEUR_FENETRE - affichage_record_pous->w;

                    position.y = 10;

                    SDL_BlitSurface(affichage_record_dep, NULL, ecran, &position);



                    position.x = LARGEUR_FENETRE - affichage_record_pous->w;

                    position.y = 25;

                    SDL_BlitSurface(affichage_record_pous, NULL, ecran, &position);



                SDL_Flip(ecran);

            }



        } while ( niveau < nbr_niveau && !retour_menu );



        SDL_EnableKeyRepeat(0, 0);



        SDL_FreeSurface(sprites);

        SDL_FreeSurface(affichage_score_dep);

        SDL_FreeSurface(affichage_score_pous);

        SDL_FreeSurface(affichage_record_dep);

        SDL_FreeSurface(affichage_record_pous);





        TTF_CloseFont(police);

    }




    Le problème vient-il de la méthode utilisée qui est pas super, ou c'est ma modification qui est super mal codé ?

    Merci d'avance pour l'aide :)

    @+ TheDead Master
    • Partager sur Facebook
    • Partager sur Twitter

    Jeu qui rame

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown