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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
{
This program is both available in XTDFPC as in the FPC demoes.
Copyright (C) 1999 by Marco van de Voort
SameGame is a standard game in GNOME and KDE. I liked it, and I
automatically brainstormed how I would implement it.
It turned out to be really easy, and is basically only 100 lines or so,
the rest is scorekeeping, helptext, menu etc.
The game demonstrates some features of the MSMOUSE unit, and some of
the Crt and Graph units. (depending whether it is compiled with
UseGraphics or not)
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
PROGRAM SameGame;
{$ifdef mswindows}
{$define UseGraphics} { as in gameunit }
{$endif mswindows}
{$ifdef UseGraphics}
{$ifdef mswindows}
{$define Win32Graph}
{$apptype GUI}
{$endif}
{$endif}
Uses
{$ifdef mswindows}
Windows,
{$endif}
{$ifdef Win32Graph}
WinCrt,
{$else}
Crt,
{$endif}
Dos,
{$IFDEF UseGraphics}
Graph,
{$INFO GRAPH}
{$ENDIF}
GameUnit;
CONST
{$IFDEF UseGraphics}
GrFieldX = 10; {X topleft of playfield}
GrFieldY = 70; {Y topleft of playfield}
ScalerX = 22; {ScalerX x Scaler y dots
must be approx a square}
ScalerY = 20;
{$ENDIF}
FieldX = 10; {Top left playfield
coordinates in squares(textmode)}
FieldY = 3; {Top left playfield coordinates}
PlayFieldXDimension = 20; {Dimensions of playfield}
PlayFieldYDimension = 15;
{$IFDEF UseGraphics}
RowDispl = 15;
MenuX = 480;
MenuY = 120;
grNewGameLine = 'NEW GAME';
grHelpLine = 'HELP';
grEndGame = 'END GAME';
{$ENDIF}
{Used colors. Colors[0..2] are the colors used on the playfield, Colors[3]
is the background and Colors[4] is the color used to mark the pieces}
Colors : ARRAY [0..4] OF LONGINT = (White,Blue,Red,Black,LightMagenta);
TYPE PlayFieldType=ARRAY[0..PlayFieldXDimension-1,0..PlayFieldYDimension-1] OF BYTE;
{$IFDEF UseGraphics}
PROCEDURE DisplayPlayField(CONST PlayField:PlayFieldType);
{Screen routine, simply puts the array Playfield on screen.
Both used for displaying the normal grid as the grid with a certain area marked}
VAR X,Y : LONGINT;
LastOne,
NumbLast : LONGINT;
BEGIN
HideMouse;
FOR Y:=0 TO PlayFieldYDimension-1 DO
BEGIN
X:=0;
REPEAT
LastOne:=PlayField[X,Y];
NumbLast:=X;
WHILE (PlayField[X,Y]=LastOne) AND (X<(PlayFieldXDimension-1))DO
INC(X);
SetFillStyle(SolidFill,Colors[LastOne]);
Bar(GrFieldX+NumbLast*ScalerX,GrFieldY+Y*ScalerY,GrFieldX+X*ScalerX-1,GrFieldY+(Y+1)*ScalerY-1);
UNTIL X>=(PlayFieldXDimension-1);
END;
ShowMouse;
END;
{$ELSE}
PROCEDURE DisplayPlayField(CONST PlayField:PlayFieldType);
{Screen routine, simply puts the array Playfield on screen.
Both used for displaying the normal grid as the grid with a certain area marked}
VAR X,Y : LONGINT;
BEGIN
FOR Y:=0 TO PlayFieldYDimension-1 DO
BEGIN
GotoXY(FieldX,Y+FieldY);
FOR X:=0 TO PlayFieldXDimension-1 DO
BEGIN
TextColor(Colors[PlayField[X,Y]]);
Write(#219#219);
END;
END;
END;
{$ENDIF}
PROCEDURE ShowHelp;
{Shows some explanation of the game and waits for a key}
{$ifndef UseGraphics}
VAR I : LONGINT;
{$endif}
BEGIN
{$IFDEF UseGraphics}
HideMouse;
SetbkColor(black);
SetViewPort(0,0,getmaxx,getmaxy,clipoff);
ClearViewPort;
SetTextStyle(0,Horizdir,2);
OutTextXY(220,10,'SAMEGAME');
SetTextStyle(0,Horizdir,1);
OutTextXY(5,40+1*LineDistY,' is a small game, with a principle copied from some KDE game');
OutTextXY(5,40+3*LineDistY,'I tried to implement it under Go32V2 to demonstrate the MsMouse unit');
OutTextXY(5,40+4*LineDistY,'When it worked, I tried to get it running under Linux. I succeeded,');
OutTextXY(5,40+5*LineDistY,'but the mouse unit of the API doesn'#39't work with GPM 1.17');
OutTextXY(5,40+7*LineDistY,'If you move over the playfield, aggregates of one color will be marked');
OutTextXY(5,40+8*LineDistY,'in purple. If you then press the left mouse button, that aggregate will');
OutTextXY(5,40+9*LineDistY,'disappear, and the playfield will collapse to the bottom-left. Please');
OutTextXY(5,40+10*LineDistY,'keep in mind that only an aggregate of two blocks or more will disappear.');
OutTextXY(5,40+12*LineDistY,'For every aggregate you let disappear you get points, but the score is');
OutTextXY(5,40+13*LineDistY,'quadratic proportional to the number of blocks killed. So 4 times killing');
OutTextXY(5,40+14*LineDistY,' a 4 block aggregate scores 4 points, and one time 16 blocks will score 64');
OutTextXY(5,40+15*LineDistY,'blocks. The purpose of the game is obtaining the highscore');
OutTextXY(5,40+17*LineDistY,'If you manage to empty the entire playfield, you'#39'll get a bonus');
OutTextXY(5,40+19*LineDistY,'Press any key to get back to the game');
ShowMouse;
{$ELSE}
FOR I:=2 TO 24 DO
BEGIN
GotoXY(1,I);
ClrEol;
END;
GotoXY(1,3); TextColor(White);
Write('SAMEGAME');
SetDefaultColor;
WriteLn(' is a small game, with a principle copied from some KDE game');
WriteLn;
WriteLn('I tried to implement it under Go32V2 to demonstrate the MsMouse unit');
Writeln('When it worked, I tried to get it running under Linux. I succeeded,');
Writeln('but the mouse unit of the API doesn'#39't work with GPM 1.17');
Writeln;
WriteLn('If you move over the playfield, aggregates of one color will be marked');
Writeln('in purple. If you then press the left mouse button, that aggregate will');
Writeln('disappear, and the playfield will collapse to the bottom-left. Please');
Writeln('keep in mind that only an aggregate of two blocks or more will disappear.');
Writeln;
Writeln('For every aggregate you let disappear you get points, but the score is');
Writeln('quadratic proportional to the number of blocks killed. So 4 times killing');
Writeln(' a 4 block aggregate scores 4 points, and one time 16 blocks will score 64');
Writeln('blocks. The purpose of the game is obtaining the highscore');
Writeln;
Writeln('If you manage to empty the entire playfield, you'#39'll get a bonus');
Writeln;
WriteLn('Press any key to get back to the game');
{$ENDIF}
GetKey;
END;
VAR MarkField,PlayField : PlayFieldType; {The playfield, and the marked form}
CubesMarked : LONGINT; {Cubes currently marked}
Score : LONGINT; {The current score}
LastScore : LONGINT;
PROCEDURE ShowButtons;
{Shows the clickable buttons}
BEGIN
{$IFNDEF UseGraphics}
TextColor(Yellow); TextBackGround(Blue);
GotoXY(60,5); Write('NEW game');
GotoXY(60,6); Write('HELP');
GotoXY(60,7); Write('END game');
{$IFDEF Unix}
GotoXY(60,8); Write('Force IBM charset');
{$ENDIF}
SetDefaultColor;
{$ELSE}
SetTextStyle(0,Horizdir,1);
OutTextXY(MenuX,MenuY,grNewGameLine);
OutTextXY(MenuX,MenuY+RowDispl,grHelpLine);
OutTextXY(MenuX,MenuY+2*RowDispl,grEndGame);
{$ENDIF}
END;
FUNCTION PlayFieldPiecesLeft:LONGINT;
{Counts pieces/cubes/blocks left on the playfield}
VAR I,J,K : LONGINT;
BEGIN
K:=0;
FOR I:=0 TO PlayFieldXDimension-1 DO
FOR J:=0 TO PlayFieldYDimension-1 DO
IF PlayField[I,J]<>3 THEN
INC(K);
PlayFieldPiecesLeft:=K;
END;
PROCEDURE ShowScore;
{Simply procedure to update the score}
{$IFDEF UseGraphics}
VAR S : String;
{$ENDIF}
BEGIN
{$IFDEF UseGraphics}
Str(Score:5,S);
SetFillStyle(SolidFill,0);
Bar(300,440,450,458);
OutTextXY(300,440,'Score :'+S);
{$ELSE}
TextColor(White);
GotoXY(20,23); Write(' ':20);
GotoXY(20,23); Write('Score : ',Score);
SetDefaultColor;
{$ENDIF}
END;
FUNCTION CubesToScore : LONGINT;
{Function to calculate score from the number of cubes. Should have a higher
order than linear, or the purpose of the game disappears}
BEGIN
CubesToScore:=(CubesMarked*CubesMarked) DIV 4;
END;
PROCEDURE MarkAfield(X,Y:LONGINT);
{Recursively marks the area adjacent to (X,Y);}
VAR TargetColor : LONGINT;
PROCEDURE MarkRecur(X1,Y1:LONGINT);
{Marks X1,Y1, checks if neighbours (horizontally or vertically) are the
same color}
BEGIN
IF (PlayField[X1,Y1]=TargetColor) AND (MarkField[X1,Y1]<>4) THEN
BEGIN
MarkField[X1,Y1]:=4;
INC(CubesMarked);
IF X1>0 THEN
MarkRecur(X1-1,Y1);
IF Y1>0 THEN
MarkRecur(X1,Y1-1);
IF X1<(PlayFieldXDimension-1) THEN
MarkRecur(X1+1,Y1);
IF Y1<(PlayFieldYDimension-1) THEN
MarkRecur(X1,Y1+1);
END;
END;
BEGIN
CubesMarked:=0;
TargetColor:=PlayField[X,Y];
IF TargetColor<>3 THEN {Can't mark black space}
MarkRecur(X,Y);
END;
PROCEDURE FillPlayfield;
{Initial version, probably not nice to play with.
Some Life'ish algoritm would be better I think. (so that more aggregates exist)}
VAR X,Y,Last,Now : LONGINT;
BEGIN
Last:=0;
FOR X:=0 TO PlayFieldXDimension-1 DO
FOR Y:=0 TO PlayFieldYDimension-1 DO
BEGIN
Now:=RANDOM(4);
IF Now=3 THEN
Now:=Last;
PlayField[X,Y]:=Now;
Last:=Now;
END;
MarkField:=PlayField;
END;
PROCEDURE Colapse;
{Processes the playfield if the mouse button is used.
First the procedure deletes the marked area, and let gravity do its work
Second the procedure uses as if some gravity existed on the left of the
playfield }
VAR X, Y,J :LONGINT;
BEGIN
{Vertical colapse: All marked pieces are deleted, and let gravity do it's work}
IF CubesMarked>1 THEN
BEGIN
FOR X:=0 TO PlayFieldXDimension-1 DO
BEGIN
Y:=PlayFieldYDimension-1; J:=Y;
while y>=0 do
begin
IF MarkField[X,Y]<>4 THEN
BEGIN
PlayField[X,J]:=PlayField[X,Y];
DEC(J);
END;
DEC(Y);
end;
FOR Y:=0 TO J DO
PlayField[X,Y]:=3;
END;
J:=0;
FOR X:=PlayFieldXDimension-2 DOWNTO 0 DO
BEGIN
IF PlayfIeld[X,PlayFieldYDimension-1]=3 THEN
BEGIN
Move(PlayfIeld[X+1,0],PlayField[X,0],PlayFieldYDimension*(PlayFieldXDimension-X-1));
INC(J);
END;
END;
IF J<>0 THEN
FillChar(PlayField[PlayFieldXDimension-J,0],J*PlayFieldYDimension,#3);
INC(Score,CubesToScore);
ShowScore;
END;
END;
PROCEDURE BuildScreen;
{Some procedures that build the screen}
Var S:String; // do not remove. Depends on usegraphics.
BEGIN
{$IFDEF UseGraphics}
setbkcolor(black);
setviewport(0,0,getmaxx,getmaxy,clipoff);
clearviewport;
{$ELSE}
ClrScr;
{$ENDIF}
Score:=0;
ShowScore;
ShowButtons;
ShowHighScore;
ShowMouse;
{$IFDEF UseGraphics}
SetTextStyle(0,Horizdir,2);
OuttextXY(10,10,'SameGame v0.03, (C) by Marco v/d Voort. ');
SetTextStyle(0,Horizdir,1);
OuttextXY(50,40,'A demo for the FPC RTL and API units Crt,(MS)Mouse and Graph');
{$ELSE}
GotoXY(1,1);
TextColor(Yellow);
Write('SameGame v0.02');
TextColor(White);
Write(' A demo for the ');
TextColor(Yellow); Write('FPC');
TextColor(White); Write(' API or MsMouse unit. By Marco van de Voort');
SetDefaultColor;
{$ENDIF}
IF LastScore<>0 THEN
BEGIN
{$Ifdef UseGraphics}
SetTextStyle(0,Horizdir,1);
Str(LastScore,S);
OuttextXY(50,40,'The Score in the last game was :'+S);
{$else}
GotoXY(10,20);
Write('The score in the last game was :',LastScore);
{$endif}
END;
DisplayPlayField(PlayField);
MarkField:=PlayField;
END;
PROCEDURE DoMainLoopMouse;
{The main game loop. The entire game runs in this procedure, the rest is
initialisation/finalisation (like loading and saving highscores etc etc)}
VAR X,Y,
MX,MY,MState,Dummy : LONGINT;
EndOfGame : LONGINT;
S : String;
BEGIN
RANDOMIZE;
REPEAT
FillPlayField;
BuildScreen;
EndOfGame:=0;
REPEAT
GetMouseState(MX,MY,MState);
{$IFDEF UseGraphics}
X:=2*((MX-GrFieldX) DIV ScalerX) +FieldX;
Y:=((MY-GrFieldY) DIV ScalerY) +FieldY-1;
{$ELSE}
X:=MX SHR 3;
Y:=MY SHR 3;
{$ENDIF}
IF PlayFieldPiecesLeft=0 THEN
BEGIN
INC(Score,1000);
EndOfGame:=1;
END
ELSE
BEGIN
{$IFDEF UseGraphics}
IF (MX>=MenuX) AND (MX<(MenuX+16*Length(GrNewGameLine))) THEN
BEGIN {X in clickable area}
IF (MY>=MenuY) AND (MY<(MenuY+RowDispl*3+2)) THEN
BEGIN
X:=65; {X doesn't matter as long as it is 60..69}
Y:=((MY-MenuY) DIV RowDispl)+4;
END;
END;
{$ENDIF}
IF (X>=60) AND (X<=69) THEN
BEGIN
IF (MState AND LButton) <>0 THEN {If leftbutton pressed,}
BEGIN
IF Y=4 THEN
EndOfGame:=1;
IF Y=6 THEN
EndOfGame:=2;
IF (EndOfGame>0) AND (PlayFieldPiecesLeft=0) THEN
INC(Score,1000);
IF Y=5 THEN
BEGIN
ShowHelp;
BuildScreen;
END;
{$IFDEF Unix}
IF Y=7 THEN
BEGIN
write(#27+'(K');
BuildScreen;
END;
{$ENDIF}
END;
END;
IF (X>=(FieldX-2)) AND (Y>=(FieldY-2)) THEN
BEGIN
DEC(X,FieldX-1);
DEC(Y,FieldY-1);
X:=X SHR 1;
IF (x>=0) and (y>=0) and (X<PlayFieldXDimension) AND (Y<PlayFieldYDimension) THEN
BEGIN
IF MarkField[X,Y]<>4 THEN
BEGIN
MarkField:=PlayField;
MarkAfield(X,Y);
DisplayPlayField(MarkField);
{$ifdef UseGraphics}
SetFillStyle(SolidFill,black);
Bar(420,440,540,460);
SetTextStyle(0,Horizdir,1);
Str(CubesToScore,S);
OuttextXY(420,440,'Marked : '+S);
{$else}
TextColor(White);
GotoXY(20,22);
Write(' ':20);
GotoXY(20,22);
Write('Marked :',CubesToScore);
{$endif}
END;
IF (MarkField[X,Y]=4) AND ((MState AND LButton) <>0) THEN
{If leftbutton pressed,}
BEGIN
REPEAT {wait untill it's released.
The moment of pressing counts}
GetMouseState(X,Y,Dummy);
UNTIL (Dummy AND LButton)=0;
Colapse;
MarkField:=PlayField;
DisplayPlayField(MarkField);
END
END
END;
IF KeyPressed THEN
BEGIN
X:=GetKey;
IF (CHR(X) IN ['X','x','Q','q']) OR (X=27) THEN
EndOfGame:=2;
END;
END;
UNTIL EndOfGame>0;
ShowScore;
X:=SlipInScore(Score);
IF X<>0 THEN
BEGIN
HideMouse;
ShowHighScore;
{$IFDEF UseGraphics}
Str(Score:5,S);
OutTextXY(HighX+150,HighY+LineDistY*(10-X),S);
GrInputStr(S,HighX,HighY+LineDistY*(10-X),16,12,10,FALSE,AlfaBeta);
{$ELSE}
InputStr(S,HighX,HighY+12-X,10,FALSE,AlfaBeta);
{$ENDIF}
HighScore[X-1].Name:=S;
ShowMouse;
END;
LastScore:=Score;
UNTIL EndOFGame=2;
END;
CONST FileName='samegame.scr';
VAR I : LONGINT;
{$IFDEF UseGraphics}
gd,gm : INTEGER;
Pal : PaletteType;
{$ENDIF}
BEGIN
{$IFDEF UseGraphics}
{$ifdef mswindows}
ShowWindow(GetActiveWindow,0);
{$endif}
gm:=vgahi;
gd:=vga;
InitGraph(gd,gm,'');
if GraphResult <> grOk then
begin
Writeln('Graph driver ',gd,' graph mode ',gm,' not supported');
Halt(1);
end;
SetFillStyle(SolidFill,1);
GetDefaultPalette(Pal);
SetAllPalette(Pal);
{$ifdef mswindows}
Windows.SetWindowText(GraphWindow,'Samegame, a demonstration of Free Pascal');
{$endif}
{$ENDIF}
IF NOT MousePresent THEN
BEGIN
Writeln('No mouse found. A mouse is required!');
HALT;
END;
FOR I:=0 TO 9 DO
HighScore[I].Score:=I*1500;
LoadHighScore(FileName);
InitMouse;
{$ifndef Win32Graph}
CursorOff;
{$endif}
{$IFDEF UseGraphics}
HighX:=450; HighY:=220; {the position of the highscore table}
{$else}
HighX:=52; HighY:=10; {the position of the highscore table}
{$endif}
DoMainLoopMouse;
HideMouse;
DoneMouse;
{$ifndef Win32Graph}
CursorOn;
{$endif}
SaveHighScore;
{$IFDEF UseGraphics}
CloseGraph;
{$ENDIF}
{$ifndef Win32Graph}
ClrScr;
Writeln;
Writeln('Last games'#39' score was : ',Score);
{$endif}
END.
|