fpctris.pp 24 KB

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