Menu

[r8]: / trunk / samples / FlashMine.pas  Maximize  Restore  History

Download this file

398 lines (364 with data), 9.0 kB

  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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
program FlashMine;
{ Flash Pascal Mines game (c) 2008 by Paul TOTH <tothpaul@free.fr> }
{$FRAME_WIDTH 240}
{$FRAME_HEIGHT 270}
type
TMethod=procedure of object;
MovieClip=external class
constructor Create(Parent:MovieClip; Name:string; Depth:integer) as Parent.createEmptyMovieClip;
procedure removeMovieClip();
procedure getURL(url,window:string);
procedure clear;
procedure lineStyle(width,color:integer);
procedure beginGradientFill(fillType:string; colors,alphas,ratios:array of integer; matrix:TObject);
procedure beginFill(color:integer);
procedure moveTo(x,y:double);
procedure lineTo(x,y:double);
procedure curveTo(x1,y1,x2,y2:double);
procedure endFill();
property _x:integer;
property _y:integer;
property _width:integer;
property _height:integer;
property _xscale:double;
property _yscale:double;
property onRelease:TMethod;
end;
TextFormat=external class
constructor Create(const FontName:string; Size:integer);
property align:string;
property color:integer;
property size:integer;
property bold:boolean;
end;
TextField=external class
constructor Create(Parent:MovieClip; Name:string; depth,left,top,width,height:integer) as Parent.createTextField;
procedure setTextFormat(Format:TextFormat);
procedure removeTextField;
property text:string;
property background:boolean;
property backgroundColor:integer;
end;
TCell=class(MovieClip)
State:integer;
Text :TextField;
Count:integer;
cx,cy:integer;
constructor Create(Name:string; x,y:integer);
procedure doRelease;
procedure Circle(radius:integer);
procedure Reset;
procedure DoFlag;
procedure IsUp(NewState:integer);
procedure IsDown(color:integer);
procedure IsBomb(color:integer);
procedure IsFlag(NewState:integer);
procedure IsWrong;
end;
TLink=class(MovieClip)
constructor Create;
procedure doRelease;
end;
TMatrix=class
matrixType:string;
x,y,w,h:double;
r:double;
constructor Create;
end;
function random:double external Math.random;
function floor(a:double):integer external Math.floor;
function sin(a:double):double external Math.sin;
function cos(a:double):double external Math.cos;
function KeyDown(key:integer):boolean external Key.isDown;
const
SHIFT=16;
var
Cells :array[0..15,0..15] of TCell;
Mines :integer;
Flags :integer;
x,y,i :integer;
_root :MovieClip;
link :TLink;
format :TextFormat;
Matrix :TMatrix;
Restart:boolean;
Bombs :TextField;
procedure CheckEmpty(x,y:integer);
begin
if (x>=0)and(y>=0)and(x<16)and(y<16) then begin
if (Cells[x,y].State=0) then
Cells[x,y].doRelease;
end;
end;
function GetState(x,y:integer):integer;
begin
if (x<0)or(y<0)or(x>15)or(y>15) then Result:=0 else Result:=Cells[x,y].State;
end;
procedure NewGame;
var
x,y,i:integer;
begin
for x:=0 to 15 do
for y:=0 to 15 do
Cells[x,y].Reset;
i:=40;
Bombs.text:=IntToStr(i);
format.color:=$00ff00;
Bombs.setTextFormat(format);
Mines:=16*16;
Flags:=0;
while i>0 do begin
x:=floor(random()*16);
y:=floor(random()*16);
if Cells[x,y].State=0 then begin
Cells[x,y].State:=1;
i:=i-1;
end;
end;
for x:=0 to 15 do
for y:=0 to 15 do begin
Cells[x,y].Count:=GetState(x-1,y-1)
+GetState(x-1,y )
+GetState(x-1,y+1)
+GetState(x ,y-1)
//+GetState(x ,y )
+GetState(x ,y+1)
+GetState(x+1,y-1)
+GetState(x+1,y )
+GetState(x+1,y+1);
end;
Restart:=False;
end;
constructor TLink.Create;
var
t:TextField;
begin
inherited Create(nil,'title',-1);
beginFill($000080);
moveTo(0,0);
lineTo(240,0);
lineTo(240,15);
lineTo(0,15);
endFill;
t:=TextField.Create(Self,'title_caption',0,0,0,240,15);
t.text:='MineSweeper (c)2008 by Paul TOTH';
format.color:=$ffffff;
t.setTextFormat(format);
onRelease:=doRelease;
end;
procedure TLink.doRelease;
begin
getURL('http://sourceforge.net/projects/flashpascal','_blank');
end;
constructor TCell.Create(Name:string; x,y:integer);
begin
inherited Create(nil,Name,x+16*y);
cx:=x;
cy:=y;
_x:=x*15+7;
_y:=y*15+7+30;
onRelease:=doRelease;
end;
procedure TCell.doRelease;
var
x,y:integer;
begin
if Restart then begin
NewGame;
end else begin
if KeyDown(SHIFT) then begin
DoFlag;
end else begin
if State=0 then begin
Mines:=Mines-1;
IsDown($a0a0a0);
if Count>0 then begin
Text:=TextField.Create(Self,IntToStr(cx)+'_'+IntToStr(cy),0,-7,-7,15,15);
Text.text:=IntToStr(Count);
case Count of
1 : format.color:=$0000ff;
2 : format.color:=$008000;
3 : format.color:=$ff00ff;
4 : format.color:=$000080;
5 : format.color:=$800000;
6 : format.color:=$008080;
7 : format.color:=$ff00ff;
8 : format.color:=$808000;
end;
Text.setTextFormat(format);
end else begin
for x:=-1 to +1 do
for y:=-1 to +1 do
CheckEmpty(x+cx,y+cy);
end;
end;
if State=1 then begin
Restart:=True;
IsBomb($ff0000);
for x:=0 to 15 do
for y:=0 to 15 do
case Cells[x,y].State of
1: Cells[x,y].IsBomb($a0a0a0);
2: Cells[x,y].IsWrong;
end;
end;
end;
end;
if (Flags=40)and(Mines=40) then begin
Bombs.text:='You WIN !';
format.color:=$00ff00;
Bombs.setTextFormat(format);
Restart:=True;
end;
end;
procedure TCell.Circle(Radius:integer);
var
a:double;
c1,s1:double;
c2,s2:double;
r2:double;
begin
a:=3.14/4;
c1:=cos(a);
s1:=sin(a);
a:=3.14/8;
c2:=cos(a);
s2:=sin(a);
r2:=11*Radius/10;
moveTo(0,Radius);
curveTo( r2*s2, r2*c2, Radius*s1, Radius*c1);
curveTo( r2*c2, r2*s2, Radius,0);
curveTo( r2*c2,-r2*s2, Radius*s1,-Radius*c1);
curveTo( r2*s2,-r2*c2, 0,-Radius);
curveTo(-r2*s2,-r2*c2,-Radius*c1,-Radius*s1);
curveTo(-r2*c2,-r2*s2, -Radius,0);
curveTo(-r2*c2, r2*s2,-Radius*c1, Radius*s1);
curveTo(-r2*s2, r2*c2, 0,Radius);
end;
procedure TCell.Reset;
begin
if (State<0)and(Count>0) then Text.removeTextField();
IsUp(0);
end;
procedure TCell.DoFlag;
begin
if (State>=0)and(Flags<40) then begin
case State of
0: IsFlag(2);
1: IsFlag(3);
2: IsUp(0);
3: IsUp(1);
end;
Flags:=Flags-1;
Bombs.text:=IntToStr(40-Flags);
format.color:=$00ff00;
Bombs.setTextFormat(format);
end;
end;
procedure TCell.IsUp(NewState:integer);
begin
State:=NewState;
clear;
beginFill($c0c0c0);
lineStyle(1,$ffffff);
moveTo(-7, 7);
lineTo(-7,-7);
lineTo( 7,-7);
lineStyle(1,$808080);
lineTo( 7, 7);
lineTo(-7, 7);
endFill;
end;
procedure TCell.IsDown(color:integer);
begin
State:=-1;
clear;
beginFill(color);
lineStyle(1,$808080);
moveTo(-7, 7);
lineTo(-7,-7);
lineTo( 7,-7);
lineStyle(1,$c0c0c0);
lineTo( 7, 7);
lineTo(-7, 7);
endFill;
end;
procedure TCell.IsBomb(color:integer);
begin
IsDown(color);
lineStyle(0,0);
beginGradientFill('radial',[$ffffff,0],[100,100],[0,255],Matrix);
circle(5);
endFill;
lineStyle(2,0);
moveTo(0, 5); lineTo(0, 6);
moveTo(0,-5); lineTo(0,-6);
moveTo( 5,0); lineTo( 6,0);
moveTo(-5,0); lineTo(-6,0);
lineStyle(1,0);
moveTo( 3.5, 3.5); lineTo( 4.5, 4.5);
moveTo( 3.5,-3.5); lineTo( 4.5,-4.5);
moveTo(-3.5,-3.5); lineTo(-4.5,-4.5);
moveTo(-3.5, 3.5); lineTo(-4.5, 4.5);
end;
procedure TCell.IsFlag(NewState:integer);
begin
if NewState=2 then Mines:=Mines-1;
Flags:=Flags+2;
State:=NewState;
lineStyle(1,0);
beginFill(0);
moveTo(0,3);
lineTo(-5,+5);
lineTo(+5,+5);
lineTo(0,3);
lineTo(0,-2);
endFill;
lineStyle(1,$ff0000);
beginFill($ff0000);
moveTo(0,-1);
lineTo(0,-5);
lineTo(-5,-3);
lineTo(0,-1);
endFill;
end;
procedure TCell.IsWrong;
begin
IsDown($a0a0a0);
lineStyle(2,$ff0000);
moveTo(-5,-5);
lineTo(+5,+5);
moveTo(-5,+5);
lineTo(+5,-5);
end;
constructor TMatrix.Create;
begin
matrixType:='box';
x:=-4.5;
y:=-4.5;
w:=6;
h:=6;
r:=0;
end;
function AddCaption(name,text:string; depth,left,top,width,height,color,background:integer):TextField;
begin
Result:=TextField.Create(nil,name,depth,left,top,width,height);
Result.backgroundcolor:=background;
Result.background:=true;
Result.Text:=text;
format.color:=color;
Result.setTextFormat(format);
end;
begin
format:=TextFormat.Create('Tahoma',9);
format.bold:=true;
format.align:='center';
link:=TLink.Create;
Bombs:=AddCaption('bombs','40',-2,0,16,120,15,$00ff00,0);
AddCaption('help','Shift+Click for Flags',-3,120,16,120,15,0,$e0e0f0);
Matrix:=TMatrix.Create;
for x:=0 to 15 do
for y:=0 to 15 do
Cells[x,y]:=TCell.Create('cell_'+IntToStr(x)+'_'+IntToStr(y),x,y);
NewGame;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.