fpctris.pp 24 KB

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