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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
| program MemorySDL;
uses
sdl, sdl_image;
const
NbColonne = 6; NbLigne = 4; NbImage = 14;
TailleC = 60; Xmin = 0; Xmax = 14;
Ymin = 0; Ymax = 10; LargeurF = 540;
LongueurF = 780;
Type
TGrille = array [1.. NbColonne, 1.. NbLigne] of Char;
TImage = array [0..NbImage] of PSDL_Surface;
CVisible = array [1..NbColonne, 1..NbLigne] of Boolean;
{----------------------------------------------------------------------}
function CreerFenetre(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 ChargerImage(var Image: TImage);
begin
Image[0] := IMG_Load('Image2/Fond.png');
Image[1] := IMG_Load('Image2/Carte-2.png');
Image[2] := IMG_Load('Image2/Curseur.png');
Image[3] := IMG_Load('Image2/Image2-1.png');
Image[4] := IMG_Load('Image2/Image2-2.png');
Image[5] := IMG_Load('Image2/Image2-3.png');
Image[6] := IMG_Load('Image2/Image2-4.png');
Image[7] := IMG_Load('Image2/Image2-5.png');
Image[8] := IMG_Load('Image2/Image2-6.png');
Image[9] := IMG_Load('Image2/Image2-7.png');
Image[10] := IMG_Load('Image2/Image2-8.png');
Image[11] := IMG_Load('Image2/Image2-9.png');
Image[12] := IMG_Load('Image2/Image2-10.png');
Image[13] := IMG_Load('Image2/Image2-11.png');
Image[14] := IMG_Load('Image2/Image2-12.png');
end;
{----------------------------------------------------------------------}
procedure ChargerGrille(FGrille: string; var Grille: 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
Grille[y][x] := Iterator;
x := x + 1;
end;
y := y + 1;
end;
Close(LevelGrille);
end;
{----------------------------------------------------------------------}
procedure DessinerScene(Window: PSDL_Surface; var Image: TImage ; Grille: TGrille);
var
x, y: Integer;
Position: TSDL_Rect;
begin
for y := 1 to Nbligne do
begin
for x := 1 to NbColonne do
begin
Position.x := (x - 1) * TailleC;
Position.y := (y - 1) * TailleC;
case string(Grille[y][x]) of
'X' : SDL_BlitSurface(Image[1], nil, Window, @Position);
'A' : SDL_BlitSurface(Image[3], nil, Window, @Position);
'B' : SDL_BlitSurface(Image[4], nil, Window, @Position);
'C' : SDL_BlitSurface(Image[5], nil, Window, @Position);
'D' : SDL_BlitSurface(Image[6], nil, Window, @Position);
'E' : SDL_BlitSurface(Image[7], nil, Window, @Position);
'F' : SDL_BlitSurface(Image[8], nil, Window, @Position);
'G' : SDL_BlitSurface(Image[9], nil, Window, @Position);
'H' : SDL_BlitSurface(Image[10], nil, Window, @Position);
'I' : SDL_BlitSurface(Image[11], nil, Window, @Position);
'J' : SDL_BlitSurface(Image[12], nil, Window, @Position);
'K' : SDL_BlitSurface(Image[13], nil, Window, @Position);
'L' : SDL_BlitSurface(Image[14], nil, Window, @Position);
end;
end;
end;
end;
{----------------------------------------------------------------------}
procedure Visibilite(match : Boolean; x, y: Integer; var Visible: CVisible; Grille: TGrille);
begin
if (match=True) then
Visible[y][x]:=True;
if (match=False) then
begin
Visible[y][x]:=False;
Grille[y][x] := 'X';
end;
end;
{----------------------------------------------------------------------}
procedure DessinerCurseur(Window: PSDL_Surface; x, y: Integer; Curseur: PSDL_Surface);
var
Position: TSDL_Rect;
begin
Position.x := (x - 1) * TailleC;
Position.y := (y - 1) * TailleC;
SDL_BlitSurface(Curseur, nil, Window, @Position);
end;
{----------------------------------------------------------------------}
function CValide(Grille: TGrille; x, y: Integer): Boolean;
begin
if (x = Xmin) or (x = Xmax) or (y = Ymin) or (y = Ymax) then
Exit(False)
else
Exit(True);
end;
{----------------------------------------------------------------------}
procedure SelectionnerCarte(var x, y: Integer);
begin
x:=x;
y:=y;
end;
{----------------------------------------------------------------------}
procedure ComparerCarte(x1,y1,x2,y2 : Integer; Grille: TGrille ; var match : Boolean);
begin
if (Grille[y1][x1]) = (Grille[y2][x2]) then
match:=True
else
match:=False
end;
{----------------------------------------------------------------------}
procedure ActualiserGrille(var x1,y1,x2,y2 : Integer; match : Boolean; Grille: TGrille; var Visible : CVisible);
begin
if (match=False) then
begin
Visibilite(match,x1,y1, Visible, Grille);
Visibilite(match,x2,y2, Visible, Grille);
end;
if (match=True) then
begin
Visibilite(match,x1,y1, Visible, Grille);
Visibilite(match,x2,y2, Visible, Grille);
end;
end;
{----------------------------------------------------------------------}
procedure Deplacer(Window: PSDL_Surface; var Image: TImage; Grille: TGrille; S_x, S_y: Integer);
var
Event: TSDL_Event;
Fin: Boolean;
C_x, C_y: Integer;
begin
Fin := False;
C_x := 2;
C_y := 2;
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(Grille, C_x, C_y + 2) = True) then
C_y := C_y + 2;
end;
SDLK_UP:
begin
if (CVAlide(Grille, C_x, C_y - 2) = True) then
C_y := C_y - 2;
end;
SDLK_LEFT:
begin
if (CValide(Grille, C_x - 2, C_y) = True) then
C_x := C_x - 2;
end;
SDLK_RIGHT:
begin
if (CValide(Grille, C_x + 2, C_y) = True) then
C_x := C_x + 2;
end;
SDLK_RETURN:
begin
S_x := C_x;
S_y := C_y;
end;
end;
end;
end;
end;
end;
{----------------------------------------------------------------------}
procedure Tour(Window: PSDL_Surface; Image: TImage; x, y, x1, x2, y1, y2: Integer; Grille: TGrille; match: Boolean; Visible: CVisible);
var
i:Integer;
begin
match:=True;
i:=1;
repeat
begin
Deplacer(Window, Image, Grille, x, y);
SelectionnerCarte(x, y);
Visibilite(match, x, y, Visible, Grille);
i:=i+1;
end;
until i=2;
ComparerCarte(x1, y1, x2, y2, Grille, match);
ActualiserGrille(x1, y1, x2, y2, match, Grille, Visible);
end;
{----------------------------------------------------------------------}
procedure LibererImages(var Image: TImage);
var
i: Integer;
begin
for i := 0 to (NbImage + 1) do
begin
Dispose(Image[i]);
end;
end;
{----------------------------------------------------------------------}
procedure Gagner(Visible: CVisible; var boucle: Boolean);
var
i, j: Integer;
begin
for i:=1 to Nbligne do
for j:=1 to NbColonne do
if (Visible[i][j] = True) then
boucle:=True
else
boucle:=False;
end;
{----------------------------------------------------------------------}
var
boucle: Boolean;
Window: PSDL_Surface;
Image: TImage;
x, y, x1, x2, y1, y2, C_x, C_y, i, j: Integer;
Grille: TGrille;
match: Boolean;
Visible: CVisible;
begin
match := True;
boucle := False;
x:=0;
y:=0;
x1:=0;
x2:=0;
y1:=0;
y2:=0;
C_x:=0;
C_y:=0;
for i:=1 to NbColonne do
for j:=1 to NbLigne do
Visible[i][j] := False;
repeat
begin
ChargerImage(Image);
Window := CreerFenetre(LongueurF, LargeurF, 'Memory - Niveau : Intermédiaire');
SDL_BlitSurface(Image[0],nil,Window, nil);
ChargerGrille('Fichier2/GrilleLevel2.txt', Grille);
Tour(Window, Image, x, y, x1, y1, x2, y2, Grille, match, Visible);
DessinerScene(Window, Image, Grille);
DessinerCurseur(Window, C_x, C_y, Image[2]);
Gagner(Visible, boucle);
SDL_Flip(Window);
SDL_Delay(30);
end;
until boucle=True;
LibererImages(Image);
SDL_Quit;
end. |
Partager