fpctris.pp 24 KB

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