fpctris.pp 24 KB

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