Partage
  • Partager sur Facebook
  • Partager sur Twitter

[SDL] Quelques questions concernant mon code

2 events ou encore pk un carré de ne s'affiche pas

    18 mai 2006 à 20:13:36

    Hello !
    Je fais un snake, et j'ai 2 questions.
    La première, comment faire, pour qu'à l'intérieur d'un case (lui même à l'intéreur de switch(event.key.keysym.sym)) on puisse gérer les évêments ? Avec 2 events ? (Vous allez comprendre avec le code de quoi je veux parler)
    [edit] Je viens de faire avec 2 events, ca va vachement mieux; mais il reste un p'tit probleme. Quand on appuie sur "m", ca quitte le jeu, hors, je veux que ca reviennen au menu ! J'ai édité le code également ! [edit]
    Secondo, mon snake bug légèrement au début, il a un trou au milieu. Vous allez comprendre en éxecutant tout ça. V'la les sources (dont je me suis permis de supprimer certains choses) :

    CONSTANTES.H :

    int jeu (SDL_Surface* ecran);
    void perdu (SDL_Surface* ecran);

    #define TAILLE_BLOC 20 /* LARGEUR ET HAUTEUR */
    #define NB_BLOC_LARGEUR 20
    #define NB_BLOC_HAUTEUR 20
    #define LARGEUR_ECRAN (TAILLE_BLOC * NB_BLOC_LARGEUR)
    #define HAUTEUR_ECRAN (TAILLE_BLOC * NB_BLOC_HAUTEUR)

    enum {HAUT, BAS, GAUCHE, DROITE};

    #include <SDL/SDL.h>


    MAIN.C :

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

    #include "constantes.h"


    int main(int argc, char *argv[])
    {
        SDL_Surface* ecran = NULL, *fond = NULL,*instru = NULL;
        SDL_Rect Pfond;
        SDL_Event event, event2;
        int continuer = 1, gagner = 0, continuer2 = 1;
       
        if (SDL_Init (SDL_INIT_VIDEO) == -1)
        {
               exit (EXIT_FAILURE);
        }

       SDL_WM_SetCaption ("Snake", NULL);
       ecran = SDL_SetVideoMode (LARGEUR_ECRAN,HAUTEUR_ECRAN,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
       fond = SDL_LoadBMP("Menu.bmp");
       instru = SDL_LoadBMP ("Instructions.bmp");
       
       Pfond.x = 0;
       Pfond.y = 0;
       
     
       while (continuer)
       {
            SDL_WaitEvent (&event);
           
            switch(event.type)
            {
                case SDL_QUIT:
                    continuer = 0;
                    break;
                   
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_KP1 :
                            gagner = jeu(ecran);
                            continuer = 0;
                            break;
                           
                        case SDLK_KP2 :
                            break;
                           
                        case SDLK_KP3 : /* BUG ICI */
                           
                             while (continuer2)
                            {
                                 SDL_WaitEvent (&event2);
           
                              switch(event2.type)
                             {
                              case SDL_KEYDOWN :
                                    if (event2.key.keysym.sym == SDLK_m)
                                    {
                                      continuer2 = 0;
                                      break;
                                    }
                              case SDL_QUIT:
                               continuer = 0;
                               continuer2 = 0;
                               break;
                             
                             }
                            SDL_BlitSurface (instru, NULL, ecran, &Pfond);
                            SDL_Flip (ecran);
                           }
                   
                    }
                    break;
            }
           
         
           
       SDL_FillRect (ecran, NULL, SDL_MapRGB (ecran -> format,255,255,255))
       SDL_BlitSurface (fond, NULL, ecran, &Pfond);
       SDL_Flip(ecran);     
      } 
        SDL_FreeSurface(fond);
        SDL_FreeSurface(instru);
        SDL_Quit();
     
        return EXIT_SUCCESS;
    }


    JEU.C :

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

    #include "constantes.h"

    int jeu (SDL_Surface* ecran)
    {
        int GAGNER = 1, tailleSerpent = 5, continuer = 1, tailleTableau = 4, actuel = 0, precedant = 0, direction = DROITE, compteur;
       
        /* tailleSerpent vaut la taille du corps du serpent, mais il faut ajouter la tête,
           tailleTableau vaut tailleSerpent-- */

           
        SDL_Surface* serpent, *mur, *tete;
        SDL_Rect Pmur, Pserpent[400], Ptete;
        SDL_Event event;
       
        serpent = SDL_LoadBMP ("Serpent.bmp");
        tete = SDL_LoadBMP ("TeteSerpent.bmp");
     
        SDL_FillRect (ecran, NULL, SDL_MapRGB (ecran -> format, 255,255,255));
       
        /* POSTION INITITALE */
     
        Pserpent[0].x = TAILLE_BLOC;
        Pserpent[0].y = TAILLE_BLOC;
       
         for (int i = 1; i <= tailleTableau; i++)
        {
         Pserpent[i].x = Pserpent[i - 1].x + TAILLE_BLOC ;  // Pserpent[i] vaut 1 bloc de + que celui d'avant
         Pserpent[i].y = 20;
        }
       
        Ptete.x = Pserpent[tailleTableau].x + TAILLE_BLOC;
        Ptete.y = 20;
       
        /* JEU */
       
         while (continuer)
       {
            SDL_PollEvent (&event);
           
            switch(event.type)
            {
                case SDL_QUIT:
                    continuer = 0;
                    break;
                case SDL_KEYDOWN :
                    switch (event.key.keysym.sym)
                    {
                        case SDLK_DOWN :
                           if (direction != HAUT )
                              direction = BAS;
                            break;
                           
                        case SDLK_UP :
                            if (direction != BAS )
                              direction = HAUT;
                            break;
                           
                        case SDLK_RIGHT :
                            if (direction != GAUCHE )
                                direction = DROITE;
                            break;
                           
                        case SDLK_LEFT :
                            if (direction != DROITE )
                              direction = GAUCHE;
                            break
                    }   
            }
         actuel = SDL_GetTicks();
         
         if (actuel - precedant > 600)  /* le serpent avant tout seul */
         {   
           precedant = actuel;
             if (direction == BAS)
             {
               /* SUPPRIMER sinon trop grand */
             } 
             
             else if (direction == HAUT)
             {
                /* SUPPRIMER sinon trop grand */
             }
             
             else if (direction == DROITE)
             {
    /* Je vous laisse celui-ci, juste assez pour l'éxecution puisque la direction de départ est droite ! Vous aurez le temps de voir le bug */

                Pserpent[tailleTableau].x =  Ptete.x;   
                Pserpent[tailleTableau].y =  Ptete.y;   
                Ptete.x += TAILLE_BLOC ;
               
                   for (int i = 0; i < tailleTableau; i++) // Le bloc du serpent prend la place du suivant
                  {
                        Pserpent[i].x = Pserpent[i + 1].x;
                        Pserpent[i].y = Pserpent[i + 1].y;
                        if (Ptete.y == Pserpent[i].y && Ptete.x == Pserpent[i].x)
                          GAGNER = 0;
                  }
            }
             
             else if (direction == GAUCHE)
             {
                /* SUPPRIMER sinon trop grand */
             } 
             
             else
               exit (EXIT_FAILURE);
               
         } /* if (actuel - precedant > 600) */
         
         else
         {
                SDL_Delay(precedant - actuel + 600);
         }
         
         if (GAGNER == 0)
          perdu (ecran);
         
            SDL_FillRect (ecran, NULL, SDL_MapRGB (ecran -> format,255,255,255));
            for (int i = 0; i <= tailleTableau; i++)
            {
               SDL_BlitSurface (serpent, NULL, ecran, &Pserpent[i]);
            }
            SDL_BlitSurface (tete, NULL, ecran, &Ptete);
            SDL_Flip (ecran);
           
      } /*  while (continuer)*/
     
      /* FREE SURFACE */
     
      SDL_FreeSurface (mur);
      SDL_FreeSurface (serpent);
      SDL_FreeSurface (tete);

        return GAGNER;
    }


    Merci de m'aider !

    • Partager sur Facebook
    • Partager sur Twitter

    [SDL] Quelques questions concernant mon code

    × 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