fpctris.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. {
  2. $Id$
  3. This program is both available in XTDFPC as in the FPC demoes.
  4. Copyright (C) 1999 by Marco van de Voort
  5. FPCTris implements a simple Crt driven Tetrisish game to demonstrate the
  6. Crt unit. (KeyPressed, ReadKey, GotoXY, Delay,TextColor,TextBackground)
  7. Quality games cost money, so that's why this one is free.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. PROGRAM FPCTris;
  15. { Trying to make a tetris from zero as a demo for FPC.
  16. Problems: - Colorsupport is a hack which handicaps creating a better
  17. update mechanism. (is done now)
  18. - Graph version input command has no cursor.
  19. - Graph or text isn't decided runtime, but compilertime.
  20. - Linux status graph version unknown at this moment.
  21. - Graphic and textmode speed of the game is not the same.
  22. The delay is fixed, and the time required to update is
  23. not constant due to optimisations.
  24. Coordinate system:
  25. 0 -> TheWidth-1 A figure is coded in a LONGINT like this:
  26. ---------
  27. 0 | * | ..*. 00100000 MSB
  28. | | ** | ..*. 00100000
  29. V | * | .**. 01100000
  30. | | .... 00000000 LSB
  31. |+ ++ ++|
  32. |++ ++++++| so 00100000001000000110000000000000b
  33. |+++++++++|
  34. ---------
  35. TheHeight-1
  36. }
  37. {$ifdef UseGraphics}
  38. {$ifdef Win32}
  39. {$define Win32Graph}
  40. {$APPTYPE GUI}
  41. {$endif}
  42. {$endif}
  43. Uses
  44. {$ifdef Win32Graph}
  45. WinCrt, Windows,
  46. {$else}
  47. Crt,
  48. {$endif}
  49. Dos,
  50. {$IFDEF UseGraphics}
  51. Graph,
  52. {$ENDIF}
  53. GameUnit;
  54. {$DEFINE DoubleCache}
  55. CONST TheWidth = 11; {Watch out, also correct RowMask!}
  56. TheHeight = 20;
  57. {$IFNDEF UseGraphics}
  58. PosXField = 10; { Upper X,Y coordinates of playfield}
  59. PosYField = 3;
  60. {$ENDIF}
  61. MaxFigures= 16; {Maximum # figures place is reserved for.}
  62. NrLevels = 12; {Number of levels currenty defined}
  63. { FieldSpace= 177;}
  64. {$IFDEF UseGraphics}
  65. DisplGrX=110;
  66. DisplGrY=90;
  67. DisplGrScale=16;
  68. HelpY=130;
  69. {$ENDIF}
  70. {$IFDEF UseGraphics}
  71. BaseX =300; {Coordinates of highscores}
  72. BaseY =HelpY+20+8*LineDistY; {y coordinate relative to other options}
  73. {$ELSE}
  74. BaseX =40;
  75. BaseY =9;
  76. {$ENDIF}
  77. TYPE TetrisFieldType = ARRAY [0..25] OF LONGINT;
  78. LevelInfoType = ARRAY [0..NrLevels-1] OF LONGINT;
  79. FigureType = LONGINT; { actually array[0..4][0..4] of bit rounded up to a longint}
  80. { CHARSET = SET OF CHAR;}
  81. {The figures, are converted to binary bitmaps on startup.}
  82. CONST GraphFigures : ARRAY[0..4] OF String[80] =(
  83. '.*... .*... .*... ..*.. .*... .*... **... **... ..**. .**.. ..*.. *....',
  84. '.*... .*... .**.. .**.. .*... .**.. **... .*... ..*.. .**.. ..*.. **...',
  85. '**... .**.. ..*.. .*... .*... .*... ..... .*... ..*.. .**.. **.** .**..',
  86. '..... ..... ..... ..... .*... ..... ..... .***. ***.. .**.. ..*.. ..**.',
  87. '..... ..... ..... ..... ..... ..... ..... ..... ..... .**.. ..*.. .....');
  88. {Their relative occurance : }
  89. FigureChance : ARRAY[0..MaxFigures-1] OF LONGINT =(
  90. 8, 8, 8, 8, 8, 8, 10, 1, 1, 1, 1, 1,0,0,0,0 );
  91. {Scores per figure. Not necessarily used. Just for future use}
  92. FigureScore : ARRAY[0..MaxFigures-1] OF LONGINT =(
  93. 2, 2, 4, 4, 1, 2, 2, 10, 10, 10, 20, 10,0,0,0,0 );
  94. {Diverse AND/OR masks to manipulate graphics}
  95. {general table to mask out a bit 31=msb 0=lsb}
  96. AndTable : ARRAY[0..31] OF LONGINT=($80000000,$40000000,$20000000,$10000000,
  97. $8000000,$4000000,$2000000,$1000000,$800000,$400000,$200000,$100000,
  98. $80000,$40000,$20000,$10000,$8000,$4000,$2000,$1000,$800,$400,$200,$100,
  99. $80,$40,$20,$10,8,4,2,1);
  100. {Mask to isolate a row of a (FigureType)}
  101. MagicMasks : ARRAY[0..4] OF LONGINT = ($F8000000,$07C00000,$003E0000,$0001F000,$00000F80);
  102. {Mask to check if a line is full; a bit for every column aligned to left.}
  103. RowMask = $FFE00000;
  104. {Masks to calculate if the left or rightside is partially empty, write them
  105. in binary, and put 5 bits on a row. }
  106. LeftMask : ARRAY[0..4] OF LONGINT = ($84210800,$C6318C00,$E739CE00,$F7BDEF00,$FFFFFFE0);
  107. RightMask: ARRAY[0..4] OF LONGINT = ($08421080,$18C63180,$39CE7380,$7BDEF780,$FFFFFF80);
  108. {Allowed characters entering highscores}
  109. {This constant/parameter is used to detect a certain bug. The bug was fixed, but
  110. I use the constant to remind where the bug was, and what is related to eachother.}
  111. Tune=-1;
  112. {First array is a table to find the level for a given number of dissappeared lines
  113. the second and third are the delaytime and iterationlevel per level. }
  114. LevelBorders : LevelInfoType = ( 10, 20, 30, 45, 60, 80,100,130,160,200,240,280);
  115. DelayLevel : LevelInfoType = (100, 90, 80, 70, 60, 60, 50, 40, 40, 20, 20,10);
  116. IterationLevel: LevelInfoType = ( 5, 5, 5, 5, 5, 4, 4, 4, 3, 3, 2, 2);
  117. {Some frequently used chars in high-ascii and low-ascii. UseColor selects between
  118. them}
  119. ColorString = #196#179#192#217#219;
  120. DumbTermStr = '-|..*';
  121. { A multiplication factor to reward killing more then one line with one figure}
  122. ProgressiveFactor : ARRAY[1..5] OF LONGINT = (10,12,16,22,30);
  123. VAR
  124. TopX,TopY : LONGINT; {Coordinates figure relative
  125. to left top of playfield}
  126. FigureNr : LONGINT; {Nr in Figure cache, second
  127. index in Figures}
  128. {$IFDEF DoubleCache}
  129. BackField, {Copy of the screen for faster matching}
  130. {$ENDIF}
  131. MainField : TetrisFieldType; {The screen grid}
  132. ColorField : ARRAY[0..TheHeight-1,0..TheWidth-1] OF LONGINT; {The color info}
  133. DelayTime : LONGINT; {Delay time, can be used for
  134. implementing levels}
  135. IterationPerDelay : LONGINT; {Iterations of mainloop (incl delay)
  136. before the piece falls down a row}
  137. TotalChance : LONGINT; {Sum of FigureChange array}
  138. Lines : LONGINT; {Completed lines}
  139. NrFigures : LONGINT; {# Figures currently used}
  140. RightSizeArray, {Nunber of empty columns to the left }
  141. LeftSizeArray, {or right of the figure/piece}
  142. Figures : ARRAY[0..MaxFigures-1,0..3] OF LONGINT; {All bitmap info of figures}
  143. NrFiguresLoaded : LONGINT; {Total figures available in GraphFigures}
  144. CurrentCol : LONGINT; {Color of current falling piece}
  145. UseColor : BOOLEAN; {Color/Mono mode}
  146. Level : LONGINT; {The current level number}
  147. {$IFNDEF UseGraphics}
  148. Style : String; {Contains all chars to create the field}
  149. {$ENDIF}
  150. nonupdatemode : BOOLEAN; {Helpmode/highscore screen or game mode}
  151. {$IFNDEF UseGraphics}
  152. HelpMode : BOOLEAN;
  153. {$ENDIF}
  154. NextFigure : LONGINT; {Next figure to fall}
  155. Score : LONGINT; {The score}
  156. FUNCTION RRotate(Figure:FigureType;ColumnsToDo:LONGINT):FigureType;
  157. {Rotate a figure to the right (=clockwise).
  158. This new (v0.06) routine performs a ColumnsTodo x ColumnsToDo rotation,
  159. instead of always a 4x4 (v0.04) or 5x5 (v0.05) rotation.
  160. This avoids weird, jumpy behaviour when rotating small pieces.}
  161. VAR I,J, NewFig:LONGINT;
  162. BEGIN
  163. NewFig:=0;
  164. FOR I:=0 TO ColumnsToDo-1 DO
  165. FOR J:=0 TO ColumnsToDo-1 DO
  166. IF Figure AND AndTable[I*5+J]<>0 THEN
  167. NewFig:=NewFig OR AndTable[(ColumnsToDo-1-I)+5*(J)]; {}
  168. RRotate:=NewFig;
  169. END;
  170. { LeftSize and RightSize count the number of empty lines to the left and
  171. right of the character. On the below character LeftSize will return 2 and
  172. RightSize will return 1.
  173. ..*.
  174. ..*.
  175. ..*.
  176. ..*.
  177. }
  178. FUNCTION RightSize(Fig:FigureType):LONGINT;
  179. VAR I : LONGINT;
  180. BEGIN
  181. I:=0;
  182. WHILE ((Fig AND RightMask[I])=0) AND (I<5) DO
  183. INC(I);
  184. IF I>4 THEN
  185. HALT;
  186. Rightsize:=I;
  187. END;
  188. FUNCTION Leftsize(Fig:FigureType):LONGINT;
  189. VAR I : LONGINT;
  190. BEGIN
  191. I:=0;
  192. WHILE ((Fig AND LeftMask[I])=0) AND (I<5) DO
  193. INC(I);
  194. IF I>4 THEN
  195. HALT;
  196. Leftsize:=I;
  197. END;
  198. FUNCTION FigSym(Figure:LONGINT;RightSizeFig:LONGINT):LONGINT;
  199. {Try to find the "symmetry" of a figure, the smallest square (1x1,2x2,3x3 etc)
  200. in which the figure fits. This requires all figures designed to be aligned to
  201. topleft.}
  202. VAR ColumnsToDo : LONGINT;
  203. BEGIN
  204. {Determine which bottom rows aren't used}
  205. ColumnsToDo:=5;
  206. WHILE ((Figure AND MagicMasks[ColumnsToDo-1])=0) AND (ColumnsToDo>1) DO
  207. DEC(ColumnsToDo);
  208. {Compare with columns used, already calculated, and take the biggest}
  209. IF ColumnsToDo<(5-RightSizeFig) THEN
  210. ColumnsToDo:=5-RightSizeFig;
  211. FigSym:=ColumnsToDo;
  212. END;
  213. PROCEDURE CreateFiguresArray;
  214. {Reads figures from ASCII representation into binary form, and creates the
  215. rotated representations, and the number of empty columns to the right and
  216. left per figure. }
  217. VAR I,J,K,L,Symmetry : LONGINT;
  218. BEGIN
  219. NrFigures:=0; K:=1;
  220. WHILE K<Length(GraphFigures[0]) DO
  221. BEGIN
  222. IF GraphFigures[0][K]=' ' THEN
  223. INC(K);
  224. L:=0;
  225. FOR I:=0 TO 4 DO {Rows}
  226. FOR J:=0 TO 4 DO {Columns}
  227. IF GraphFigures[I][K+J]='*' THEN
  228. L:=L OR AndTable[I*5+J];
  229. Figures[NrFigures][0]:=L;
  230. INC(NrFigures);
  231. INC(K,5);
  232. END;
  233. NrFiguresLoaded:=NrFigures;
  234. FOR I:= 0 TO NrFigures-1 DO
  235. BEGIN
  236. RightSizeArray[I][0]:=RightSize(Figures[I][0]);
  237. LeftSizeArray[I][0]:=LeftSize(Figures[I][0]);
  238. Symmetry:=FigSym(Figures[I][0],RightSizeArray[I][0]);
  239. FOR J:=0 TO 2 DO {Create the other 3 by rotating}
  240. BEGIN
  241. Figures[I][J+1]:=RRotate(Figures[I][J],Symmetry);
  242. RightSizeArray[I][J+1]:=RightSize(Figures[I][J+1]);
  243. LeftSizeArray[I][J+1]:=LeftSize(Figures[I][J+1]);
  244. END;
  245. END;
  246. {Clear main grid}
  247. FillChar(MainField,SIZEOF(TetrisFieldType),0);
  248. END;
  249. PROCEDURE CalculateTotalChance;
  250. {Called after a change in the the number of figures, normally 7 (standard)
  251. or NrFiguresLoaded (10 right now) to recalculate the total of the chance table}
  252. VAR Temp:LONGINT;
  253. BEGIN
  254. TotalChance:=0;
  255. FOR Temp:=0 TO NrFigures-1 DO INC(TotalChance,FigureChance[Temp]);
  256. END;
  257. FUNCTION MatchPosition(Fig:FigureType;X,Y:LONGINT): BOOLEAN;
  258. {Most important routine. Tries to position the figure on the position
  259. IF it returns FALSE then the piece overlaps something on the background,
  260. or the lower limit of the playfield
  261. }
  262. VAR I,J,K : LONGINT;
  263. Match: BOOLEAN;
  264. BEGIN
  265. Match:=TRUE;
  266. FOR I:=0 TO 4 DO
  267. BEGIN
  268. K:=Fig;
  269. K:=K AND MagicMasks[I];
  270. IF K<>0 THEN
  271. BEGIN
  272. J:=5*(I)-X+Tune;
  273. IF J>0 THEN
  274. K:=K SHL J
  275. ELSE
  276. IF J<0 THEN
  277. K:=K SHR -J;
  278. IF (MainField[Y+I] AND K)<>0 THEN
  279. Match:=FALSE;
  280. END;
  281. END;
  282. I:=4;
  283. IF (Fig AND MagicMasks[4])=0 THEN
  284. DEC(I);
  285. IF (Fig AND MagicMasks[3])=0 THEN
  286. DEC(I);
  287. IF (Fig AND MagicMasks[2])=0 THEN
  288. DEC(I);
  289. IF (Y+I)>=TheHeight THEN
  290. Match:=FALSE;
  291. MatchPosition:=Match;
  292. END;
  293. PROCEDURE FixFigureInField(Fig:FigureType;X,Y:LONGINT;Clear:BOOLEAN);
  294. {Blends the figure into the background, or erases the figure from the
  295. background}
  296. VAR I,J,K : LONGINT;
  297. BEGIN
  298. FOR I:=0 TO 4 DO
  299. BEGIN
  300. K:=Fig;
  301. K:=K AND MagicMasks[I];
  302. IF K<>0 THEN
  303. BEGIN
  304. J:=5*I-X+Tune;
  305. IF J>0 THEN
  306. K:=K SHL J
  307. ELSE
  308. IF J<0 THEN
  309. K:=K SHR (-J);
  310. IF Clear THEN
  311. BEGIN
  312. K:=K XOR -1;
  313. MainField[Y+I]:= MainField[Y+I] AND K;
  314. END
  315. ELSE
  316. MainField[Y+I]:= MainField[Y+I] OR K;
  317. END;
  318. END;
  319. END;
  320. PROCEDURE FixColField(ThisFig:LONGINT);
  321. {Puts color info of a figure into the colorgrid, simplified
  322. FixFigureInField on byte instead of bit manipulation basis.}
  323. VAR I,J,K : LONGINT;
  324. BEGIN
  325. FOR I:=0 TO 4 DO
  326. BEGIN
  327. K:=Figures[ThisFig][FigureNr];
  328. IF (I+TopY)<=TheHeight THEN
  329. FOR J:=0 TO 4 DO
  330. BEGIN
  331. IF (K AND AndTable[J+5*I])<>0 THEN
  332. ColorField[TopY+I,TopX-Tune+J]:=CurrentCol
  333. END;
  334. END;
  335. END;
  336. PROCEDURE RedrawScreen;
  337. {Frustrates the caching system so that the entire screen is redrawn}
  338. VAR I : LONGINT;
  339. BEGIN
  340. FOR I:=0 TO TheHeight-1 DO
  341. BackField[I]:=MainField[I] XOR -1; {backup copy is opposite of MainField}
  342. END;
  343. FUNCTION GetNextFigure:LONGINT;
  344. VAR IndTotal,Temp,TheFigure : LONGINT;
  345. BEGIN
  346. Temp:=RANDOM(TotalChance);
  347. IndTotal:=0;
  348. TheFigure:=0;
  349. WHILE Temp>=IndTotal DO
  350. BEGIN
  351. INC(IndTotal,FigureChance[TheFigure]);
  352. INC(TheFigure);
  353. END;
  354. dec(thefigure);
  355. GetNextFigure:=TheFigure;
  356. END;
  357. {$IFDEF UseGraphics}
  358. {$I ftrisgr.inc}
  359. {$ELSE}
  360. {$I ftristxt.inc}
  361. {$ENDIF}
  362. FUNCTION InitAFigure(VAR TheFigure:LONGINT) : BOOLEAN;
  363. {A new figure appears in the top of the screen. If return value=FALSE then
  364. the piece couldn't be created (when it is overlapping with the background.
  365. That's the game-over condition)}
  366. VAR Temp : LONGINT;
  367. BEGIN
  368. TopX:=(TheWidth-4) DIV 2; { Middle of Screen}
  369. TopY:=0;
  370. FigureNr:=1;
  371. IF TheFigure<>-1 THEN
  372. INC(Score,FigureScore[TheFigure]);
  373. IF NOT NonUpdateMode THEN
  374. FixScores;
  375. Temp:=GetNextFigure; {Determine next char (after the one this
  376. initafigure created has got down)}
  377. TheFigure:=NextFigure; {Previous NextFigure becomes active now.}
  378. NextFigure:=Temp;
  379. InitAFigure:=MatchPosition(Figures[TheFigure][0],TopX,TopY);
  380. ShowNextFigure(NextFigure);
  381. CurrentCol:=RANDOM(14)+1;
  382. END;
  383. PROCEDURE FixLevel(Lines:LONGINT);
  384. BEGIN
  385. Level:=0;
  386. WHILE (Lines>LevelBorders[Level]) AND (Level<HIGH(LevelBorders)) DO
  387. INC(Level);
  388. DelayTime:=DelayLevel[Level];
  389. IterationPerDelay:=IterationLevel[Level];
  390. END;
  391. PROCEDURE FixMainFieldLines;
  392. {Deletes full horizontal lines from the playfield will also get some
  393. score-keeping code in the future.}
  394. VAR I,LocalLines : LONGINT;
  395. BEGIN
  396. I:=TheHeight-1;
  397. LocalLines:=0;
  398. WHILE I>=0 DO
  399. BEGIN
  400. IF (MainField[I] XOR RowMask)=0 THEN
  401. BEGIN
  402. Move(MainField[0],MainField[1],I*4);
  403. Move(ColorField[0,0],ColorField[1,0],4*I*TheWidth);
  404. MainField[0]:=0;
  405. FillChar(ColorField[0,0],0,TheWidth);
  406. INC(LocalLines);
  407. END
  408. ELSE
  409. DEC(I);
  410. END;
  411. INC(Lines,LocalLines);
  412. I:=Level;
  413. FixLevel(Lines);
  414. IF LocalLines<>0 THEN
  415. BEGIN
  416. INC(Score,ProgressiveFactor[LocalLines]*LocalLines);
  417. ShowLines;
  418. END;
  419. {$IFDEF DoubleCache}
  420. IF UseColor THEN
  421. RedrawScreen;
  422. {$ENDIF}
  423. END;
  424. PROCEDURE DoFPCTris;
  425. {The main routine. Initialisation, keyboard loop}
  426. VAR EndGame : BOOLEAN;
  427. FixHickup : LONGINT;
  428. Counter : LONGINT;
  429. Temp,Key : LONGINT;
  430. TheFigure : LONGINT; {Current first index in Figures}
  431. PROCEDURE TurnFigure;
  432. {Erases a figure from the grid, turns it if possible, and puts it back on
  433. again}
  434. BEGIN
  435. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  436. IF MatchPosition(Figures[TheFigure][Temp],TopX,TopY) THEN
  437. FigureNr:=Temp;
  438. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  439. END;
  440. PROCEDURE FixHighScores;
  441. VAR I : LONGINT;
  442. {$IFNDEF UseGraphics}
  443. J : LONGINT;
  444. {$ENDIF}
  445. S : String;
  446. BEGIN
  447. {$IFDEF UseGraphics}
  448. Str(Score:5,S);
  449. SetFillStyle(SolidFill,0); {Clear part of playfield}
  450. Bar(DisplGrX+DisplGrScale,DisplGrY + ((TheHeight DIV 2)-2)*DisplGrScale,
  451. DisplGrX+(TheWidth-1)*(DisplGrScale), DisplGrY + DisplGrScale*((TheHeight DIV 2)+5));
  452. SetTextStyle(0,Horizdir,2);
  453. OuttextXY(DisplGrX+DisplGrScale,DisplGrY+ DisplGrScale*((TheHeight DIV 2)-1),'GAME OVER');
  454. SetTextStyle(0,Horizdir,1);
  455. OutTextXY(DisplGrX+DisplGrScale,DisplGrY+ DisplGrScale*((TheHeight DIV 2)+3),'Score= '+S);
  456. {$ELSE}
  457. FOR J:=9 TO 22 DO
  458. BEGIN
  459. GotoXY(40,J);
  460. Write(' ':38);
  461. END;
  462. IF UseColor THEN
  463. TextColor(White);
  464. GotoXY(40,23);
  465. Writeln('Game Over, score = ',Score);
  466. {$ENDIF}
  467. I:=SlipInScore(Score);
  468. IF I<>0 THEN
  469. BEGIN
  470. NonUpdateMode:=TRUE;
  471. {$IFNDEF UseGraphics}
  472. HelpMode:=FALSE;
  473. {$ENDIF}
  474. ShowHighScore;
  475. {$IFDEF UseGraphics}
  476. OutTextXY(450,HelpY+20+(17-I+1)*LineDistY,S);
  477. GrInputStr(S,300,HelpY+20+(17-I+1)*LineDistY,16,12,10,FALSE,AlfaBeta);
  478. {$ELSE}
  479. InputStr(S,40,21-I,10,FALSE,AlfaBeta);
  480. {$ENDIF}
  481. HighScore[I-1].Name:=S;
  482. END;
  483. ShowHighScore;
  484. END;
  485. {$IFDEF UseGraphics}
  486. VAR
  487. gd,gm : INTEGER;
  488. Pal : PaletteType;
  489. {$ENDIF}
  490. BEGIN
  491. {$IFDEF UseGraphics}
  492. {$ifdef Win32}
  493. ShowWindow(GetActiveWindow,0);
  494. {$endif}
  495. gm:=vgahi;
  496. gd:=vga;
  497. InitGraph(gd,gm,'');
  498. if GraphResult <> grOk then
  499. begin
  500. Writeln('Graph driver ',gd,' graph mode ',gm,' not supported');
  501. Halt(1);
  502. end;
  503. SetFillStyle(SolidFill,1);
  504. GetDefaultPalette(Pal);
  505. SetAllPalette(Pal);
  506. {$ifdef win32}
  507. Windows.SetWindowText(GraphWindow,'FPCTris, a demonstration of Free Pascal');
  508. {$endif}
  509. {$ENDIF}
  510. {Here should be some terminal-detection for Linux}
  511. nonupdatemode:=FALSE;
  512. {$IFNDEF UseGraphics}
  513. HelpMode :=TRUE;
  514. {$ENDIF}
  515. {$IFDEF Unix}
  516. UseColor:=FALSE;
  517. {$ELSE}
  518. UseColor:=TRUE;
  519. {$ENDIF}
  520. {$ifndef Win32Graph}
  521. ClrScr;
  522. CursorOff;
  523. {$endif}
  524. RANDOMIZE;
  525. HighX:=BaseX;
  526. HighY:=BaseY;
  527. CreateFiguresArray; { Load and precalculate a lot of stuff}
  528. {$IFNDEF UseGraphics}
  529. IF UseColor THEN
  530. Style:= ColorString
  531. ELSE
  532. Style:=DumbTermStr;
  533. {$ENDIF}
  534. NrFigures:=7; {Default standard tetris mode, only use
  535. the first 7 standard figures}
  536. CalculateTotalChance; {Calculated the total of all weightfactors}
  537. EndGame:=FALSE; {When TRUE, end of game has been detected}
  538. FixHickup:=0; {Used to avoid unnecessary pauses with the "down key"}
  539. CreateFrame; {Draws all background garbadge}
  540. TheFigure:=-1;
  541. NextFigure:=GetNextFigure; {Two figures have to be inited. The first
  542. figure starts dropping, and that is this
  543. one}
  544. InitAFigure(TheFigure); {The second figure is the figure to be
  545. displayed as NEXT. That's this char :-)}
  546. DisplMainField; {Display/update the grid}
  547. Counter:=0; {counts up to IterationPerDelay}
  548. DelayTime:=200; {Time of delay}
  549. IterationPerDelay:=4; {= # Delays per shift down of figure}
  550. Lines:=0; {Lines that have disappeared}
  551. Score:=0;
  552. ShowLines;
  553. REPEAT
  554. IF KeyPressed THEN {The function name says it all}
  555. BEGIN
  556. Key:=ORD(READKEY);
  557. IF Key=0 THEN {Function key?}
  558. Key:=ORD(READKEY) SHL 8;
  559. CASE Key OF {Check for all keys}
  560. ArrU : BEGIN
  561. Temp:=(FigureNr+3) AND 3;
  562. IF ((TopX+LeftSizeArray[TheFigure][FigureNr])<0) THEN
  563. BEGIN
  564. IF (LeftSizeArray[TheFigure][FigureNr]<=LeftSizeArray[TheFigure][Temp]) THEN
  565. TurnFigure;
  566. END
  567. ELSE
  568. IF (TopX+7-RightSizeArray[TheFigure][FigureNr])>TheWidth THEN
  569. BEGIN
  570. IF (RightSizeArray[TheFigure][FigureNr]<=RightSizeArray[TheFigure][Temp]) THEN
  571. TurnFigure;
  572. END
  573. ELSE
  574. TurnFigure;
  575. END;
  576. ArrL : BEGIN
  577. IF (TopX+LeftSizeArray[TheFigure][FigureNr])>=0 THEN
  578. BEGIN
  579. Temp:=TopX+1-LeftSizeArray[TheFigure][FigureNr];
  580. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  581. IF MatchPosition(Figures[TheFigure][FigureNr],TopX-1,TopY) THEN
  582. DEC(TopX);
  583. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  584. END;
  585. END;
  586. ArrR : BEGIN
  587. IF (TopX+7-RightSizeArray[TheFigure][FigureNr])<=TheWidth THEN
  588. BEGIN
  589. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  590. IF MatchPosition(Figures[TheFigure][FigureNr],TopX+1,TopY) THEN
  591. INC(TopX);
  592. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  593. END;
  594. END;
  595. ArrD : BEGIN
  596. IF FixHickup=0 THEN
  597. BEGIN
  598. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  599. Temp:=TopY;
  600. WHILE MatchPosition(Figures[TheFigure][FigureNr],TopX,TopY+1) DO
  601. INC(TopY);
  602. Temp:=TopY-Temp;
  603. INC(Score,Temp DIV 2);
  604. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  605. FixHickUp:=4;
  606. END;
  607. END;
  608. ORD('q'),
  609. ESC : BEGIN
  610. SetDefaultColor;
  611. {$ifndef Win32Graph}
  612. GotoXY(1,25);
  613. {$endif}
  614. EndGame:=TRUE;
  615. END;
  616. {$IFNDEF UseGraphics}
  617. ORD('C'),
  618. ORD('c') : BEGIN
  619. UseColor:=NOT UseColor;
  620. IF UseColor THEN
  621. Style:= ColorString
  622. ELSE
  623. BEGIN
  624. SetDefaultColor;
  625. Style:=DumbTermStr;
  626. END;
  627. CreateFrame;
  628. RedrawScreen;
  629. DisplMainField;
  630. END;
  631. ORD('S'),
  632. ORD('s') : BEGIN
  633. IF NOT nonupdatemode THEN
  634. BEGIN
  635. NonUpdateMode:=TRUE;
  636. helpmode:=NOT helpmode;
  637. END
  638. ELSE
  639. HelpMode:=NOT helpmode;
  640. CreateFrame;
  641. ShowLines;
  642. ShowNextFigure(NextFigure);
  643. END;
  644. {$ENDIF}
  645. ORD('H'),
  646. ORD('h') : BEGIN
  647. nonupdatemode:=NOT nonupdatemode;
  648. CreateFrame;
  649. ShowLines;
  650. ShowNextFigure(NextFigure);
  651. END;
  652. ORD('E'),
  653. ORD('e'): BEGIN {Extra figures on/off}
  654. IF NrFigures<>NrFiguresLoaded THEN
  655. NrFigures:=NrFiguresLoaded {Extra figures}
  656. ELSE
  657. NrFigures:=7; {Standard Tetris figures}
  658. CalculateTotalChance; {Recalculate weight-totals}
  659. IF UseColor THEN
  660. SetDefaultColor;
  661. ShowGameMode;
  662. END;
  663. ORD('p') : BEGIN {"p" : Pause}
  664. Key:=ORD(ReadKey);
  665. IF Key=0 THEN
  666. Key:=ORD(ReadKey);
  667. END;
  668. {$IFNDEF UseGraphics}
  669. {$IFDEF Unix}
  670. ORD('i') : write(#27+'(K');
  671. {$ENDIF}
  672. {$ENDIF}
  673. END; {END OF Key CASE}
  674. END { OF If KeyPressed}
  675. ELSE
  676. BEGIN
  677. {$IFDEF Unix}
  678. GotoXY(50,10); {Get cursor out of the way, CursorOn/Off
  679. doesn't work on telnet-terminals}
  680. {$ENDIF}
  681. Delay(DelayTime);
  682. END;
  683. INC(Counter);
  684. IF (Counter=IterationPerDelay) OR (FixHickup=1) THEN
  685. BEGIN
  686. IF FixHickup=1 THEN
  687. Counter:=IterationPerDelay-1
  688. ELSE
  689. Counter:=0;
  690. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  691. FixHickup:=0;
  692. IF MatchPosition(Figures[TheFigure][FigureNr],TopX,TopY+1) THEN
  693. BEGIN
  694. INC(TopY);
  695. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  696. END
  697. ELSE
  698. BEGIN
  699. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  700. FixColField(TheFigure);
  701. IF InitAFigure(TheFigure) THEN
  702. BEGIN
  703. FixMainFieldLines;
  704. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  705. DisplMainField;
  706. Delay(DelayTime*IterationPerDelay);
  707. END
  708. ELSE
  709. BEGIN
  710. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  711. EndGame:=TRUE;
  712. END;
  713. END;
  714. END
  715. ELSE
  716. IF FixHickup>1 THEN
  717. DEC(FixHickup);
  718. DisplMainField;
  719. UNTIL EndGame;
  720. FixHighScores;
  721. {$ifndef Win32Graph}
  722. CursorOn;
  723. SetDefaultColor;
  724. GotoXY(1,25);
  725. {$endif}
  726. {$IFDEF UseGraphics}
  727. {$ifndef Win32}
  728. TextMode(CO80);
  729. {$endif}
  730. {$ENDIF}
  731. END;
  732. CONST FileName='fpctris.scr';
  733. VAR I : LONGINT;
  734. BEGIN
  735. FOR I:=0 TO 9 DO
  736. HighScore[I].Score:=(I+1)*750;
  737. LoadHighScore(FileName);
  738. DoFpcTris;
  739. SaveHighScore;
  740. END.
  741. {
  742. $Log$
  743. Revision 1.6 2004-02-18 16:43:04 marco
  744. * added an API call to avoid the "Graph Window" window title
  745. Revision 1.5 2002/09/07 15:06:34 peter
  746. * old logs removed and tabs fixed
  747. Revision 1.4 2002/06/02 09:49:17 marco
  748. * Renamefest
  749. }