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
| program Memory;
uses
sdl, sdl_image;
type
TGrille = array[1..9, 1..13] 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('Image2/Fond.png');
TImage[2] := IMG_Load('Image2/Carte-2.png');
TImage[3] := IMG_Load('Image2/Curseur.png');
TImage[4] := IMG_Load('Image2/Image2-1.png');
TImage[5] := IMG_Load('Image2/Image2-2.png');
TImage[6] := IMG_Load('Image2/Image2-3.png');
TImage[7] := IMG_Load('Image2/Image2-4.png');
TImage[8] := IMG_Load('Image2/Image2-5.png');
TImage[9] := IMG_Load('Image2/Image2-6.png');
TImage[10] := IMG_Load('Image2/Image2-7.png');
TImage[11] := IMG_Load('Image2/Image2-8.png');
TImage[12] := IMG_Load('Image2/Image2-9.png');
TImage[13] := IMG_Load('Image2/Image2-10.png');
TImage[14] := IMG_Load('Image2/Image2-11.png');
TImage[15] := IMG_Load('Image2/Image2-12.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 13 do
begin
Position.x := (x - 1) * 60;
Position.y := (y - 1) * 60;
if string(AGrille[y][x]) = 'Z' then
SDL_BlitSurface(TImage[1], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'X' then
SDL_BlitSurface(TImage[2], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'A' then
SDL_BlitSurface(TImage[4], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'B' then
SDL_BlitSurface(TImage[5], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'C' then
SDL_BlitSurface(TImage[6], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'D' then
SDL_BlitSurface(TImage[7], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'E' then
SDL_BlitSurface(TImage[8], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'F' then
SDL_BlitSurface(TImage[9], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'G' then
SDL_BlitSurface(TImage[10], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'H' then
SDL_BlitSurface(TImage[11], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'I' then
SDL_BlitSurface(TImage[12], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'J' then
SDL_BlitSurface(TImage[13], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'K' then
SDL_BlitSurface(TImage[14], nil, AWindow, @Position);
if string(AGrille[y][x]) = 'L' then
SDL_BlitSurface(TImage[15], 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) * 60;
Position.y := (y - 1) * 60;
SDL_BlitSurface(ASurface, nil, AWindow, @Position);
end;
function CValide(AGrille: TGrille; x, y: Integer): Boolean;
begin
if (x = 0) or (x = 14) or (y = 0) or (y = 10) then
Exit(False)
else
Exit(True);
end;
function ComparerCartes(RGrille : TGrille ; C1_x,C1_y,C2_x,C2_y : Integer):Boolean;
begin
If RGrille[C1_y][C1_x] <> RGrille[C2_y][C2_x] then
Exit(True)
else
Exit(False);
end;
procedure ActualiserGrille(C1_x, C2_x, C1_y, C2_y:Integer; RGrille: TGrille ; var AGRille : TGrille; var i: Integer);
begin
if (ComparerCartes(RGrille, C1_x, C2_x, C1_y, C2_y) = True) then
begin
i:=0;
C1_x:=0;
C2_x:=0;
C1_y:=0;
C2_y:=0;
end
else
begin
AGrille[C1_y][C1_x] := 'X';
AGrille[C2_y][C2_x] := 'X';
i:=0;
C1_x:=0;
C2_x:=0;
C1_y:=0;
C2_y:=0;
end;
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, C_y, S_y,i, C1_x, C2_x, C1_y, C2_y: Integer;
RGrille : TGrille;
begin
Fin := False;
C_x := 2;
C_y := 2;
S_x := 0;
S_y := 0;
C1_x := 0;
C1_y := 0;
C2_x := 0;
C2_y := 0;
i:= 0;
LoadGrille('Fichier2/FichierGrille2.txt', RGrille);
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) and (i<>2) then
begin
case Event.key.keysym.sym of
SDLK_DOWN:
begin
if (CValide(AGrille, C_x, C_y + 2) = True) then
begin
C_y += 2;
end;
end;
SDLK_UP:
begin
if (CVAlide(AGrille, C_x, C_y - 2) = True) 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) = True) then
begin
C_x += 2;
end;
end;
SDLK_RETURN:
begin
S_x := C_x;
S_y := C_y;
i:=i+1;
end;
end;
if (C1_x = 0) then
begin
C1_x:=S_x;
C1_y:= S_y;
end
else
begin
C2_x:=S_x;
C2_y:= S_y;
end;
end;
if S_x <> 0 then
AGrille[S_y, S_x] := RGrille[S_y, S_x];
DrawScene(AWindow, TImage, AGrille);
DrawSurface(AWindow, C_x, C_y, TImage[3]);
ActualiserGrille(C1_x, C2_x, C1_y, C2_y, RGrille, AGrille,i);
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 15 do
begin
Dispose(Image[i]);
end;
end;
var
Grille: TGrille;
Image: array[1..16] of PSDL_Surface;
Window: PSDL_Surface;
begin
LoadGrille('Fichier2/GrilleLevel2.txt', Grille);
LoadImage(Image);
Window := NewWindow(13* 60, 9 * 60, 'Memory - Niveau : Intermédiaire');
PBoucle(Window, Image, Grille);
FreeImages(Image);
SDL_Quit;
end. |
Partager