IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Free Pascal Discussion :

[SDL] Problème SDL - Jeu du Memory [Free Pascal]


Sujet :

Free Pascal

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Février 2014
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2014
    Messages : 14
    Par défaut [SDL] Problème SDL - Jeu du Memory
    Salut à tous !

    Je viens à vous car j'ai un gros problème avec mon code. En effet, je dois réaliser, dans le cadre d'un projet, une SDL pour le jeu du Memory. Néanmoins, je n'arrive pas à retourner deux cartes différentes. En effet, lorsque je clique sur "entrée" la première carte se dévoile mais à chaque nouveau clic celle-ci se déplace à l'endroit du clic.

    Voici mon code, pour vous donner une idée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    program Memory2SDL;
     
    uses sdl, sdl_image, sysutils;
     
    type TGrille = array[1..9, 1..11] of Char;
     
    {---------------------------------------------------------------------}
     
    function NewWindow(x, y : Integer ; Caption : PChar) : PSDL_Surface;
     
    var Window : PSDL_Surface;
     
    begin
        SDL_Init(SDL_INIT_VIDEO);
        Window := SDL_SetVideoMode(x, y, 32, SDL_DOUBLEBUF);
        SDL_WM_SetCaption(Caption, Nil);
        Exit(Window);
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    procedure LoadImage(var TImage : Array of PSDL_Surface);
    begin
        TImage[1] := IMG_Load('Fond.png');
        TImage[2] := IMG_Load('Carte.png');
        TImage[3] := IMG_Load('Curseur.png');
        TImage[4] := IMG_Load('Carte2.png');
     
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    procedure LoadGrille(FGrille : String ; var AGrille : TGrille);
    var LevelGrille : Text;
        Buffer : String;
        x, y : Integer;
        Iterator : Char;
     
    begin
        y := 1;
        Assign(LevelGrille, FGrille);
        Reset(LevelGrille);
        while not Eof(LevelGrille) do
        begin
            Readln(LevelGrille, Buffer);
            x := 1;
            for Iterator in Buffer do
            begin
                AGrille[y][x] := Iterator;
                x := x + 1;
            end;
            y := y + 1;
        end;
        Close(LevelGrille);
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    procedure DrawScene(AWindow : PSDL_Surface ; var TImage : Array of PSDL_Surface ;
        var AGrille : TGrille);
     
    var x, y : Integer;
        Position : TSDL_Rect;
     
    begin
        for y := 1 to 9 do
        begin
            for x := 1 to 11 do
            begin
                Position.x := (x - 1) * 80;
                Position.y := (y - 1) * 80;
                if String(AGrille[y][x]) = 'f' then
                    SDL_BlitSurface(TImage[1], nil, AWindow, @Position);
                if String(AGrille[y][x]) = 'c' then
                    SDL_BlitSurface(TImage[2] , nil, AWindow, @Position);
            end;
        end;
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    procedure DrawCursor(AWindow : PSDL_Surface;  x, y : Integer ; ACursor : PSDL_Surface);
     
    var Position : TSDL_Rect;
     
    begin
        Position.x := (x) * 80;
        Position.y := (y) * 80;
        SDL_BlitSurface(ACursor, nil, AWindow, @Position);
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    procedure DrawCarte1(AWindow : PSDL_Surface;  x, y : Integer ; ACarte : PSDL_Surface);
     
    var Position : TSDL_Rect;
     
    begin
        Position.x := x * 80;
        Position.y := y * 80;
        SDL_BlitSurface(ACarte, nil, AWindow, @Position);
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    function CValide(AGrille : TGrille; x, y : Integer) : Boolean;
     
    begin
        if (x = -1) or (x = 11) or (y = -1) or (y = 9) then 
    		Exit(False)
    	else	
            Exit(True);    
    end;
     
     
    {---------------------------------------------------------------------}
     
     
     
    procedure PBoucle(AWindow : PSDL_Surface ; var TImage : Array of PSDL_Surface ;
        var AGrille : TGrille);
     
    var Event : TSDL_Event;
        Fin : Boolean;
        C_x, S_x : Integer;
        C_y, S_y : Integer;
        Cursor : PSDL_Surface;
     
    begin
        Fin := False;
        C_x := 1;
        C_y := 1;
        Cursor := TImage[3];
        while not Fin do
        begin
            while SDL_PollEvent(@Event) <> 0 do
            begin
                if Event.Type_ = SDL_QUITEV then
                    Fin := True;
                if Event.Type_ = SDL_KEYDOWN then
                begin
                    case Event.key.keysym.sym of 
     
    		SDLK_DOWN :	begin
    						if CValide(AGrille, C_x, C_y + 2) then
    						begin
    							C_y += 2;
    						end;
    					end;
     
    		SDLK_UP : begin
    						if CVAlide(AGrille, C_x, C_y - 2) then
    						begin
    							C_y -= 2;
    						end;
    					end;
     
    		SDLK_LEFT : begin
    						if CValide(AGrille, C_x - 2, C_y)=True then
    						begin
    							C_x -= 2;
    						end;
    					end;
     
    		SDLK_RIGHT : begin
    						if CValide(AGrille, C_x + 2, C_y) then
    						begin
    							C_x += 2;
    						end;
    					end;
     
    		SDLK_RETURN : begin
    						S_x:=C_x;
    						S_y:=C_y;
    					end;
                end;
            end;
            DrawScene(AWindow, TImage, AGrille);
            DrawCursor(AWindow, C_x, C_y, Cursor); 
            DrawCarte1(Awindow, S_x, S_y, TImage[4]);
     
            SDL_Flip(AWindow);
            SDL_Delay(30);
        end;
    end;
    end;
     
    {---------------------------------------------------------------------}
     
     
    procedure FreeImages(var Image : Array of PSDL_Surface);
     
    var i : Integer;
     
    begin
        for i := 1 to 4 do
        begin
            Dispose(Image[i]);
        end;
    end;
     
     
    {---------------------------------------------------------------------}
     
     
    var Grille : TGrille;
        Image : Array [1..5] of PSDL_Surface;
        Window : PSDL_Surface;
     
    begin
        LoadGrille('level2.grille', Grille);
        LoadImage(Image);
        Window := NewWindow(880, 700, 'Memory - Niveau : Intermédiaire');
        PBoucle(Window, Image, Grille);
        FreeImages(Image);
        SDL_Quit;
    end.
    Merci d'avance

  2. #2
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 167
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 167
    Billets dans le blog
    9
    Par défaut
    Bonjour !

    J'ai regardé ton code mais je n'ai pas pu l'essayer, faute d'avoir les différents fichiers que le programme utilise. Si ça ne t'embête pas, ce serait bien de les poster. Autrement, il sera difficile (pour moi du moins) de t'aider.

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Février 2014
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2014
    Messages : 14
    Par défaut
    Merci pour ta réponse

    Voici les images et fichier associés au code (cf fichiers attachés) :
    • Carte.png corresponds à la première image
    • Carte2.png corresponds à la deuxième image
    • Curseur.png corresponds à la troisième image
    • Fond.png corresponds à la quatrième image
    • le fichier correspond à level2.grille
    Images attachées Images attachées     
    Fichiers attachés Fichiers attachés

  4. #4
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 167
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 167
    Billets dans le blog
    9
    Par défaut
    Ce qui se passe, c'est que ta procédure DrawScene redessine tout sans tenir compte des cartes qui ont été retournées (elle redessine toutes les cartes à l'envers). Si tu mets en commentaire l'appel de cette procédure, tu verras que tes deux cartes sont bien retournées.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
        while SDL_PollEvent(@Event) <> 0 do
        begin
          ...
          //DrawScene(AWindow, TImage, AGrille);
    Bref, dans la procédure DrawScene, il faudrait prévoir le cas de la carte déjà retournée, ce qui suppose qu'il y ait une valeur correspondant à ce cas dans ta variable Grille (et non pas seulement les deux valeurs "fond" et "carte").

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Février 2014
    Messages
    14
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2014
    Messages : 14
    Par défaut
    Ah merci tu m'ôte une épine du pied !

    Par contre cela signifie que je dois modifier mon fichier à un endroit précis et je ne connais pas d'instructions permettant de faire cela (car un Rewrite effacerait tout mon fichier)

  6. #6
    Rédacteur/Modérateur

    Avatar de Roland Chastain
    Homme Profil pro
    Enseignant
    Inscrit en
    Décembre 2011
    Messages
    4 167
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Décembre 2011
    Messages : 4 167
    Billets dans le blog
    9
    Par défaut
    Citation Envoyé par Taazer Voir le message
    Ah merci tu m'ôtes une épine du pied !
    De rien.

    Citation Envoyé par Taazer Voir le message
    Par contre cela signifie que je dois modifier mon fichier à un endroit précis et je ne connais pas d'instructions permettant de faire cela (car un Rewrite effacerait tout mon fichier)
    Non, ce n'est pas le fichier qu'il faut modifier ; c'est la variable Grille. Voici une modification rapide de ton code pour te montrer le principe.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
     
    program Memory;
     
    uses
      sdl, sdl_image;
     
    type
      TGrille = array[1..9, 1..11] of Char;
     
    function NewWindow(x, y: Integer; Caption: PChar): PSDL_Surface;
    var
      Window: PSDL_Surface;
    begin
      SDL_Init(SDL_INIT_VIDEO);
      Window := SDL_SetVideoMode(x, y, 32, SDL_DOUBLEBUF);
      SDL_WM_SetCaption(Caption, nil);
      Exit(Window);
    end;
     
    procedure LoadImage(var TImage: array of PSDL_Surface);
    begin
      TImage[1] := IMG_Load('Fond.png');
      TImage[2] := IMG_Load('Carte.png');
      TImage[3] := IMG_Load('Curseur.png');
      TImage[4] := IMG_Load('Carte2.png');
    end;
     
    procedure LoadGrille(FGrille: string; var AGrille: TGrille);
    var
      LevelGrille: Text;
      Buffer: string;
      x, y: Integer;
      Iterator: Char;
    begin
      y := 1;
      Assign(LevelGrille, FGrille);
      Reset(LevelGrille);
      while not Eof(LevelGrille) do
      begin
        ReadLn(LevelGrille, Buffer);
        x := 1;
        for Iterator in Buffer do
        begin
          AGrille[y][x] := Iterator;
          x := x + 1;
        end;
        y := y + 1;
      end;
      Close(LevelGrille);
    end;
     
    procedure DrawScene(AWindow: PSDL_Surface; var TImage: array of PSDL_Surface; var AGrille: TGrille);
    var
      x, y: Integer;
      Position: TSDL_Rect;
    begin
      for y := 1 to 9 do
      begin
        for x := 1 to 11 do
        begin
          Position.x := (x - 1) * 80;
          Position.y := (y - 1) * 80;
          if string(AGrille[y][x]) = 'f' then
            SDL_BlitSurface(TImage[1], nil, AWindow, @Position);
          if string(AGrille[y][x]) = 'c' then
            SDL_BlitSurface(TImage[2], nil, AWindow, @Position);
          if string(AGrille[y][x]) = 'd' then
            SDL_BlitSurface(TImage[4], nil, AWindow, @Position);
        end;
      end;
    end;
     
    procedure DrawSurface(AWindow: PSDL_Surface; x, y: Integer; ASurface: PSDL_Surface);
    var
      Position: TSDL_Rect;
    begin
      Position.x := (x - 1) * 80;
      Position.y := (y - 1) * 80;
      SDL_BlitSurface(ASurface, nil, AWindow, @Position);
    end;
     
    function CValide(AGrille: TGrille; x, y: Integer): Boolean;
    begin
      if (x = -1) or (x = 11) or (y = -1) or (y = 9) then
        Exit(False)
      else
        Exit(True);
    end;
     
    procedure PBoucle(AWindow: PSDL_Surface; var TImage: array of PSDL_Surface; var AGrille: TGrille);
    var
      Event: TSDL_Event;
      Fin: Boolean;
      C_x, S_x: Integer;
      C_y, S_y: Integer;
      //Cursor: PSDL_Surface;
    begin
      Fin := False;
      C_x := 2;
      C_y := 2;
      S_x := 0;
      S_y := 0;
      //Cursor := TImage[3];
      while not Fin do
      begin
        while SDL_PollEvent(@Event) <> 0 do
        begin
          if Event.Type_ = SDL_QUITEV then
            Fin := True;
     
          if Event.Type_ = SDL_KEYDOWN then
          begin
            case Event.key.keysym.sym of
              SDLK_DOWN:
                begin
                  if CValide(AGrille, C_x, C_y + 2) then
                  begin
                    C_y += 2;
                  end;
                end;
              SDLK_UP:
                begin
                  if CVAlide(AGrille, C_x, C_y - 2) then
                  begin
                    C_y -= 2;
                  end;
                end;
              SDLK_LEFT:
                begin
                  if CValide(AGrille, C_x - 2, C_y) = True then
                  begin
                    C_x -= 2;
                  end;
                end;
              SDLK_RIGHT:
                begin
                  if CValide(AGrille, C_x + 2, C_y) then
                  begin
                    C_x += 2;
                  end;
                end;
              SDLK_RETURN:
                begin
                  S_x := C_x;
                  S_y := C_y;
                end;
            end;
          end;
          if S_x <> 0 then
            AGrille[S_y, S_x] := 'd'; // d comme dévoilé
          DrawScene(AWindow, TImage, AGrille);
          DrawSurface(AWindow, C_x, C_y, {Cursor}TImage[3]);
          //DrawSurface(Awindow, S_x, S_y, TImage[4]);
          SDL_Flip(AWindow);
          SDL_Delay(30);
        end;
      end;
    end;
     
    procedure FreeImages(var Image: array of PSDL_Surface);
    var
      i: Integer;
    begin
      for i := 1 to 4 do
      begin
        Dispose(Image[i]);
      end;
    end;
     
    var
      Grille: TGrille;
      Image: array[1..5] of PSDL_Surface;
      Window: PSDL_Surface;
     
    begin
      LoadGrille('level2.txt', Grille);
      LoadImage(Image);
      Window := NewWindow(11* 80, 9 * 80, 'Memory');
      PBoucle(Window, Image, Grille);
      FreeImages(Image);
      SDL_Quit;
    end.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [SDL] Problème jeu de labyrinthe
    Par Gottfried dans le forum SDL
    Réponses: 4
    Dernier message: 25/07/2007, 16h18
  2. [SDL] Problème de vitesse
    Par Hybrix dans le forum Développement 2D, 3D et Jeux
    Réponses: 26
    Dernier message: 06/01/2007, 22h31
  3. [SDL] problème de débogage sdl_ttf
    Par mouteb dans le forum SDL
    Réponses: 4
    Dernier message: 08/09/2006, 17h06
  4. [SDL] Problème avec SDL_Flip()
    Par Drannor dans le forum C
    Réponses: 6
    Dernier message: 24/11/2005, 22h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo