fpctris.pp 24 KB

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