fpctris.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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. {$ENDIF}
  507. {Here should be some terminal-detection for Linux}
  508. nonupdatemode:=FALSE;
  509. {$IFNDEF UseGraphics}
  510. HelpMode :=TRUE;
  511. {$ENDIF}
  512. {$IFDEF Unix}
  513. UseColor:=FALSE;
  514. {$ELSE}
  515. UseColor:=TRUE;
  516. {$ENDIF}
  517. {$ifndef Win32Graph}
  518. ClrScr;
  519. CursorOff;
  520. {$endif}
  521. RANDOMIZE;
  522. HighX:=BaseX;
  523. HighY:=BaseY;
  524. CreateFiguresArray; { Load and precalculate a lot of stuff}
  525. {$IFNDEF UseGraphics}
  526. IF UseColor THEN
  527. Style:= ColorString
  528. ELSE
  529. Style:=DumbTermStr;
  530. {$ENDIF}
  531. NrFigures:=7; {Default standard tetris mode, only use
  532. the first 7 standard figures}
  533. CalculateTotalChance; {Calculated the total of all weightfactors}
  534. EndGame:=FALSE; {When TRUE, end of game has been detected}
  535. FixHickup:=0; {Used to avoid unnecessary pauses with the "down key"}
  536. CreateFrame; {Draws all background garbadge}
  537. TheFigure:=-1;
  538. NextFigure:=GetNextFigure; {Two figures have to be inited. The first
  539. figure starts dropping, and that is this
  540. one}
  541. InitAFigure(TheFigure); {The second figure is the figure to be
  542. displayed as NEXT. That's this char :-)}
  543. DisplMainField; {Display/update the grid}
  544. Counter:=0; {counts up to IterationPerDelay}
  545. DelayTime:=200; {Time of delay}
  546. IterationPerDelay:=4; {= # Delays per shift down of figure}
  547. Lines:=0; {Lines that have disappeared}
  548. Score:=0;
  549. ShowLines;
  550. REPEAT
  551. IF KeyPressed THEN {The function name says it all}
  552. BEGIN
  553. Key:=ORD(READKEY);
  554. IF Key=0 THEN {Function key?}
  555. Key:=ORD(READKEY) SHL 8;
  556. CASE Key OF {Check for all keys}
  557. ArrU : BEGIN
  558. Temp:=(FigureNr+3) AND 3;
  559. IF ((TopX+LeftSizeArray[TheFigure][FigureNr])<0) THEN
  560. BEGIN
  561. IF (LeftSizeArray[TheFigure][FigureNr]<=LeftSizeArray[TheFigure][Temp]) THEN
  562. TurnFigure;
  563. END
  564. ELSE
  565. IF (TopX+7-RightSizeArray[TheFigure][FigureNr])>TheWidth THEN
  566. BEGIN
  567. IF (RightSizeArray[TheFigure][FigureNr]<=RightSizeArray[TheFigure][Temp]) THEN
  568. TurnFigure;
  569. END
  570. ELSE
  571. TurnFigure;
  572. END;
  573. ArrL : BEGIN
  574. IF (TopX+LeftSizeArray[TheFigure][FigureNr])>=0 THEN
  575. BEGIN
  576. Temp:=TopX+1-LeftSizeArray[TheFigure][FigureNr];
  577. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  578. IF MatchPosition(Figures[TheFigure][FigureNr],TopX-1,TopY) THEN
  579. DEC(TopX);
  580. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  581. END;
  582. END;
  583. ArrR : BEGIN
  584. IF (TopX+7-RightSizeArray[TheFigure][FigureNr])<=TheWidth THEN
  585. BEGIN
  586. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  587. IF MatchPosition(Figures[TheFigure][FigureNr],TopX+1,TopY) THEN
  588. INC(TopX);
  589. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  590. END;
  591. END;
  592. ArrD : BEGIN
  593. IF FixHickup=0 THEN
  594. BEGIN
  595. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  596. Temp:=TopY;
  597. WHILE MatchPosition(Figures[TheFigure][FigureNr],TopX,TopY+1) DO
  598. INC(TopY);
  599. Temp:=TopY-Temp;
  600. INC(Score,Temp DIV 2);
  601. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  602. FixHickUp:=4;
  603. END;
  604. END;
  605. ORD('q'),
  606. ESC : BEGIN
  607. SetDefaultColor;
  608. {$ifndef Win32Graph}
  609. GotoXY(1,25);
  610. {$endif}
  611. EndGame:=TRUE;
  612. END;
  613. {$IFNDEF UseGraphics}
  614. ORD('C'),
  615. ORD('c') : BEGIN
  616. UseColor:=NOT UseColor;
  617. IF UseColor THEN
  618. Style:= ColorString
  619. ELSE
  620. BEGIN
  621. SetDefaultColor;
  622. Style:=DumbTermStr;
  623. END;
  624. CreateFrame;
  625. RedrawScreen;
  626. DisplMainField;
  627. END;
  628. ORD('S'),
  629. ORD('s') : BEGIN
  630. IF NOT nonupdatemode THEN
  631. BEGIN
  632. NonUpdateMode:=TRUE;
  633. helpmode:=NOT helpmode;
  634. END
  635. ELSE
  636. HelpMode:=NOT helpmode;
  637. CreateFrame;
  638. ShowLines;
  639. ShowNextFigure(NextFigure);
  640. END;
  641. {$ENDIF}
  642. ORD('H'),
  643. ORD('h') : BEGIN
  644. nonupdatemode:=NOT nonupdatemode;
  645. CreateFrame;
  646. ShowLines;
  647. ShowNextFigure(NextFigure);
  648. END;
  649. ORD('E'),
  650. ORD('e'): BEGIN {Extra figures on/off}
  651. IF NrFigures<>NrFiguresLoaded THEN
  652. NrFigures:=NrFiguresLoaded {Extra figures}
  653. ELSE
  654. NrFigures:=7; {Standard Tetris figures}
  655. CalculateTotalChance; {Recalculate weight-totals}
  656. IF UseColor THEN
  657. SetDefaultColor;
  658. ShowGameMode;
  659. END;
  660. ORD('p') : BEGIN {"p" : Pause}
  661. Key:=ORD(ReadKey);
  662. IF Key=0 THEN
  663. Key:=ORD(ReadKey);
  664. END;
  665. {$IFNDEF UseGraphics}
  666. {$IFDEF Unix}
  667. ORD('i') : write(#27+'(K');
  668. {$ENDIF}
  669. {$ENDIF}
  670. END; {END OF Key CASE}
  671. END { OF If KeyPressed}
  672. ELSE
  673. BEGIN
  674. {$IFDEF Unix}
  675. GotoXY(50,10); {Get cursor out of the way, CursorOn/Off
  676. doesn't work on telnet-terminals}
  677. {$ENDIF}
  678. Delay(DelayTime);
  679. END;
  680. INC(Counter);
  681. IF (Counter=IterationPerDelay) OR (FixHickup=1) THEN
  682. BEGIN
  683. IF FixHickup=1 THEN
  684. Counter:=IterationPerDelay-1
  685. ELSE
  686. Counter:=0;
  687. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,TRUE);
  688. FixHickup:=0;
  689. IF MatchPosition(Figures[TheFigure][FigureNr],TopX,TopY+1) THEN
  690. BEGIN
  691. INC(TopY);
  692. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  693. END
  694. ELSE
  695. BEGIN
  696. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  697. FixColField(TheFigure);
  698. IF InitAFigure(TheFigure) THEN
  699. BEGIN
  700. FixMainFieldLines;
  701. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  702. DisplMainField;
  703. Delay(DelayTime*IterationPerDelay);
  704. END
  705. ELSE
  706. BEGIN
  707. FixFigureInField(Figures[TheFigure][FigureNr],TopX,TopY,FALSE);
  708. EndGame:=TRUE;
  709. END;
  710. END;
  711. END
  712. ELSE
  713. IF FixHickup>1 THEN
  714. DEC(FixHickup);
  715. DisplMainField;
  716. UNTIL EndGame;
  717. FixHighScores;
  718. {$ifndef Win32Graph}
  719. CursorOn;
  720. SetDefaultColor;
  721. GotoXY(1,25);
  722. {$endif}
  723. {$IFDEF UseGraphics}
  724. {$ifndef Win32}
  725. TextMode(CO80);
  726. {$endif}
  727. {$ENDIF}
  728. END;
  729. CONST FileName='fpctris.scr';
  730. VAR I : LONGINT;
  731. BEGIN
  732. FOR I:=0 TO 9 DO
  733. HighScore[I].Score:=(I+1)*750;
  734. LoadHighScore(FileName);
  735. DoFpcTris;
  736. SaveHighScore;
  737. END.
  738. {
  739. $Log$
  740. Revision 1.5 2002-09-07 15:06:34 peter
  741. * old logs removed and tabs fixed
  742. Revision 1.4 2002/06/02 09:49:17 marco
  743. * Renamefest
  744. }