home *** CD-ROM | disk | FTP | other *** search
/ Más de 2,500 Juegos / CD1.iso / ZIPDAT / 0828 / 0828.ZIP / MAIN.PAS < prev   
Pascal/Delphi Source File  |  1998-03-08  |  6KB  |  234 lines

  1. {Maggot - Remco de Korte - Soft Machine - 1998
  2.  
  3. Made with Delphi 1,  so it'll run in Win3.xx.
  4. Look at the code, study it, ask questions about it, change it, but please:
  5. don't steal.
  6. You can also have a look at this code to see how to make a program into a
  7. screensaver. Look at the projectsource and at the Saverout and Show/Hide
  8. procedures in the mainscreen-code. That's all.
  9.  
  10. Have fun!
  11.  
  12. Mail me at remcodek@xs4all.nl}
  13.  
  14. unit Main;
  15.  
  16. interface
  17.  
  18. uses
  19.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  20.   Forms, Dialogs, ExtCtrls, StdCtrls;
  21.  
  22. type
  23.   Tmainscreen = class(TForm)
  24.     Timer1: TTimer;
  25.     Label1: TLabel;
  26.     pic1: TImage;
  27.     pic: TImage;
  28.     Label2: TLabel;
  29.     procedure FormCreate(Sender: TObject);
  30.     procedure Timer1Timer(Sender: TObject);
  31.     procedure FormClick(Sender: TObject);
  32.     procedure FormShow(Sender: TObject);
  33.     procedure FormHide(Sender: TObject);
  34.     procedure FormPaint(Sender: TObject);
  35.   private
  36.     { Private declarations }
  37.     procedure saverout(var Msg:TMsg; var Handled:boolean);
  38.     procedure capturedesktop;
  39.   public
  40.     { Public declarations }
  41.   end;
  42.  
  43. var
  44.   mainscreen: Tmainscreen;
  45.  
  46. implementation
  47.  
  48. {$R *.DFM}
  49.  
  50. const
  51.   maxlength=100;
  52.   maxnumber=256;
  53.  
  54. type
  55.   mag_rec=record
  56.     headx,heady:integer;
  57.     tailx,taily:integer;
  58.     bodyx,bodyy:array[1..maxlength] of integer;
  59.     leng:integer; {length}
  60.     dir:byte; {direction of movement:  1  2  3
  61.                                        8  x  4
  62.                                        7  6  5}
  63.   end;
  64.   mag_rec_ptr=^mag_rec;
  65.  
  66. var
  67.   xc,yc:integer;
  68.   xmax,ymax:integer;
  69.   mag:array[1..maxnumber] of mag_rec_ptr;
  70.   i,j,k,x,y:integer;
  71.   crs:Tpoint;
  72.   dc:hdc;
  73.   tmp:tcanvas;
  74.   desk:tbitmap;
  75.   numb:integer;
  76.   pw,ph:integer;
  77.  
  78. procedure Tmainscreen.saverout(var Msg : TMsg; var Handled : boolean);
  79. var
  80.   done : boolean;
  81. begin
  82.   if Msg.message = WM_MOUSEMOVE then
  83.     done := (Abs(LOWORD(Msg.lParam) - crs.x) > 5) or
  84.             (Abs(HIWORD(Msg.lParam) - crs.y) > 5)
  85.   else
  86.     done := (Msg.message = WM_KEYDOWN) or (Msg.message = WM_ACTIVATE) or
  87.             (Msg.message = WM_ACTIVATEAPP) or (Msg.message = WM_NCACTIVATE);
  88.   if done then
  89.   begin
  90.     releasedc(0,dc);
  91.     Close;
  92.   end;
  93. end;
  94.  
  95. procedure Tmainscreen.capturedesktop;
  96.  
  97. begin
  98.   mainscreen.hide;
  99.   dc:=GetWindowDC(getdesktopwindow);
  100.   tmp:=tcanvas.create;
  101.   tmp.handle:=dc;
  102.   canvas.copyrect(rect(100,100,200,200),tmp,rect(300,300,400,400));
  103.   mainscreen.show;
  104. end;
  105.  
  106. procedure Tmainscreen.FormCreate(Sender: TObject);
  107.  
  108. var
  109.   i,j:integer;
  110.  
  111. begin
  112.   randomize;
  113.   xmax:=screen.width;
  114.   ymax:=screen.height;
  115.   width:=xmax;
  116.   height:=ymax;
  117.   left:=0;
  118.   top:=0;
  119.   xc:=xmax div 2;
  120.   yc:=ymax div 2;
  121.  
  122.   for i:=1 to maxnumber do {initialize data}
  123.   begin
  124.     new(mag[i]);
  125.     with mag[i]^ do
  126.     begin
  127.       headx:=xc;
  128.       heady:=yc;
  129.       leng:=5;
  130.       for j:=1 to leng do
  131.       begin
  132.         bodyx[j]:=xc;
  133.         bodyy[j]:=yc;
  134.       end;
  135.       tailx:=xc;
  136.       taily:=yc;
  137.       dir:=random(8)+1;
  138.     end;
  139.   end;
  140.   dc:=getwindowdc(getdesktopwindow);
  141.   tmp:=tcanvas.create;
  142.   tmp.handle:=dc;
  143.   desk:=tbitmap.create;
  144.   desk.width:=xmax;
  145.   desk.height:=ymax;
  146.   desk.canvas.copyrect(rect(0,0,xmax,ymax),tmp,rect(0,0,xmax,ymax));
  147.   numb:=64;
  148.   pw:=pic.width;
  149.   ph:=pic.height;
  150. end;
  151.  
  152. procedure Tmainscreen.Timer1Timer(Sender: TObject);
  153.  
  154. begin
  155.   for i:=1 to numb do
  156.   with mag[i]^ do
  157.   begin
  158.     canvas.pixels[tailx,taily]:=pic.canvas.pixels[tailx mod pw,taily mod ph]; {wipes tail}
  159.     canvas.pixels[tailx,taily+1]:=pic.canvas.pixels[tailx mod pw,(taily+1) mod ph]; {wipes tail}
  160.     canvas.pixels[headx,heady+1]:=clGray;
  161.     canvas.pixels[headx,heady]:=clSilver; {wipes head, makes it part of the body}
  162.     {calculate new place of head}
  163.     x:=headx;
  164.     y:=heady;
  165.     if dir in [1..3] then y:=y-1;
  166.     if dir in [3..5] then x:=x+1;
  167.     if dir in [5..7] then y:=y+1;
  168.     if dir in [7,8,1] then x:=x-1;
  169.   {check if it's valid}
  170.     if x<0 then x:=0;
  171.     if x>xmax then x:=xmax;
  172.     if y<0 then y:=0;
  173.     if y>ymax then y:=ymax;
  174.     if (x<>headx) or (y<>heady) then {if not the maggot won't move}
  175.     begin
  176.       tailx:=bodyx[leng];
  177.       taily:=bodyy[leng];
  178.       for j:=leng downto 2 do
  179.       begin
  180.         bodyx[j]:=bodyx[j-1];
  181.         bodyy[j]:=bodyy[j-1];
  182.       end;
  183.       bodyx[1]:=headx;
  184.       bodyy[1]:=heady;
  185.       headx:=x;
  186.       heady:=y;
  187.       if leng<100 then if random(500)=0 then
  188.       begin
  189.         inc(leng); {it grows}
  190.         bodyx[leng]:=tailx;
  191.         bodyy[leng]:=taily;
  192.       end;
  193.     end;
  194.     canvas.pixels[headx,heady]:=clWhite;
  195.     canvas.pixels[tailx,taily]:=clGray; {draw head and tail}
  196.     {change direction}
  197.     dir:=dir-1+random(3);
  198.     if dir=0 then dir:=8;
  199.     if dir=9 then dir:=1;
  200.   end;
  201.   if random(64)=0 then if numb<maxnumber then
  202.   begin
  203.     inc(numb);
  204.     if numb=128 then {if random(1000)=0 then} pic.canvas.copyrect(rect(0,0,pw,ph),pic1.canvas,rect(0,0,pw,ph));
  205.     label2.caption:=inttostr(numb);
  206.   end;
  207. end;
  208.  
  209. procedure Tmainscreen.FormClick(Sender: TObject);
  210. begin
  211.   application.terminate;
  212. end;
  213.  
  214. procedure Tmainscreen.FormShow(Sender: TObject);
  215. begin
  216.   GetCursorPos(crs);
  217.   Application.OnMessage:=SAverout;
  218.   showcursor(false);
  219. end;
  220.  
  221. procedure Tmainscreen.FormHide(Sender: TObject);
  222. begin
  223.   application.OnMessage:=nil;
  224.   timer1.enabled:=false;
  225.   showcursor(true);
  226. end;
  227.  
  228. procedure Tmainscreen.FormPaint(Sender: TObject);
  229. begin
  230.   canvas.draw(0,0,desk);
  231. end;
  232.  
  233. end.
  234.