home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / SpriteEngine / SE Random / SpriteHandlers.p < prev    next >
Encoding:
Text File  |  1995-04-06  |  4.3 KB  |  138 lines  |  [TEXT/MWPS]

  1. unit SpriteHandlers;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  8.         OSUtils, ToolUtils, OSEvents, 
  9. {$ENDC}
  10.         QDOffScreen, SpriteStructure, SpriteTools;
  11.  
  12.     procedure MoveSprite (theSprite: SpritePtr);
  13.     procedure HitSprite (theSprite: SpritePtr; anotherSprite: SpritePtr);
  14.     procedure InitSprites;
  15.  
  16. implementation
  17.  
  18. { Custom handlers - application dependent ***}
  19. {}
  20. {Edit this as necessary. It should always include the following three routines:}
  21. {}
  22. {MoveSprite: move the sprite}
  23. {}
  24. {HitSprite: handle collisions between two sprites}
  25. {}
  26. {InitSprites: Load all faces and create initial sprites }
  27.  
  28.  
  29.  
  30. var
  31. randomPositionFace, randomSpeedFace, changeSometimesFace, randomImpulseFace:GrafPtr;
  32.  
  33.  
  34. Procedure MoveSprite(theSprite:SpritePtr);
  35.     function Sgn(i:Integer):Integer;
  36.     begin
  37.         if i > 0 then Sgn := 1
  38.         else
  39.         if i < 0 then Sgn := -1
  40.         else
  41.         Sgn := 0;
  42.     end; {Sgn}
  43. begin
  44.     Case theSprite^.kind of
  45.         randomPositionSprite:
  46.             begin
  47.             theSprite^.position.h := theSprite^.position.h + Rand(7) - 3;
  48.             theSprite^.position.v := theSprite^.position.v + Rand(7) - 3;
  49.             if KeepOnScreen(theSprite) then ;
  50.             end;
  51.         randomSpeedSprite:
  52.             begin
  53.             theSprite^.fixedPointPosition.h := theSprite^.fixedPointPosition.h + theSprite^.speed.h;
  54.             theSprite^.fixedPointPosition.v := theSprite^.fixedPointPosition.v + theSprite^.speed.v;
  55.             theSprite^.position.h := BSR(theSprite^.fixedPointPosition.h, 4);
  56.             theSprite^.position.v := BSR(theSprite^.fixedPointPosition.v, 4);
  57.             If  KeepOnScreenFixed(theSprite) Then    
  58.              begin 
  59.                 theSprite^.speed.h := theSprite^.speed.h - Sgn(theSprite^.speed.h);
  60.                 theSprite^.speed.v := theSprite^.speed.v - Sgn(theSprite^.speed.v);
  61.              End;
  62.             theSprite^.speed.h := theSprite^.speed.h + Rand(15) - 7;
  63.             theSprite^.speed.v := theSprite^.speed.v + Rand(15) - 7;
  64.             end;
  65.         changeSometimesSprite:
  66.         begin
  67.             theSprite^.fixedPointPosition.h := theSprite^.fixedPointPosition.h + theSprite^.speed.h;
  68.             theSprite^.fixedPointPosition.v := theSprite^.fixedPointPosition.v + theSprite^.speed.v;
  69.             theSprite^.position.h := BSR(theSprite^.fixedPointPosition.h , 4);
  70.             theSprite^.position.v := BSR(theSprite^.fixedPointPosition.v , 4);
  71.             if KeepOnScreenFixed(theSprite) then ;
  72.             If  (Rand(500) < 10) Then
  73.              begin 
  74.                 theSprite^.speed.h := Rand(63) - 31;
  75.                 theSprite^.speed.v := Rand(63) - 31;
  76.              End;
  77.             end;
  78.         randomImpulseSprite:
  79.         begin
  80.             theSprite^.fixedPointPosition.h := theSprite^.fixedPointPosition.h + theSprite^.speed.h;
  81.             theSprite^.fixedPointPosition.v := theSprite^.fixedPointPosition.v + theSprite^.speed.v;
  82.             theSprite^.position.h := BSR(theSprite^.fixedPointPosition.h , 4);
  83.             theSprite^.position.v := BSR(theSprite^.fixedPointPosition.v , 4);
  84.             if KeepOnScreenFixed(theSprite) then ;
  85.             theSprite^.speed.h := theSprite^.speed.h * 19 div 20;
  86.             theSprite^.speed.v := theSprite^.speed.v * 19 div 20;
  87.             If  (theSprite^.speed.h = 0) AND (theSprite^.speed.v = 0) Then            
  88.              begin
  89.                 theSprite^.speed.h := Rand(511) - 255;
  90.                 theSprite^.speed.v := Rand(511) - 255;
  91.              End;
  92.             end;
  93.      End;
  94.  
  95.  End; (*MoveSprite*)
  96.  
  97.  
  98. Procedure HitSprite(theSprite:SpritePtr; anotherSprite:SpritePtr);
  99.  Begin  End; (*HitSprite*)
  100.  
  101.  
  102.  
  103. Procedure InitSprites;
  104.     var theSprite:SpritePtr;
  105. (*Load all pictures*)
  106.      begin 
  107.     randomPositionFace := LoadFaceFromCicn(128);        (*cicn resource #128.*)
  108.     randomSpeedFace := LoadFaceFromCicn(129);        (*cicn resource #129.*)
  109.     changeSometimesFace := LoadFaceFromCicn(130);    (*cicn resource #130.*)
  110.     randomImpulseFace := LoadFaceFromCicn(131);        (*cicn resource #131.*)
  111.  
  112. (*Create sprites*)
  113.     theSprite := NewSprite;
  114.     theSprite^.kind := randomPositionSprite;
  115.     theSprite^.face := randomPositionFace;
  116.     SetPt(theSprite^.position, 50, 50);
  117.  
  118.     theSprite := NewSprite;
  119.     theSprite^.kind := randomSpeedSprite;
  120.     theSprite^.face := randomSpeedFace;
  121.     SetPt(theSprite^.fixedPointPosition, BSL(100,4), BSL(100,4));
  122.     SetPt(theSprite^.speed, 0, 0);
  123.  
  124.     theSprite := NewSprite;
  125.     theSprite^.kind := changeSometimesSprite;
  126.     theSprite^.face := changeSometimesFace;
  127.     SetPt(theSprite^.fixedPointPosition, BSL(150,4), BSL(150,4));
  128.     SetPt(theSprite^.speed, 0, 0);
  129.  
  130.     theSprite := NewSprite;
  131.     theSprite^.kind := randomImpulseSprite;
  132.     theSprite^.face := randomImpulseFace;
  133.     SetPt(theSprite^.fixedPointPosition, BSL(200,4), BSL(200,4));
  134.     SetPt(theSprite^.speed, 0, 0);
  135.  End; (*InitSprites*)
  136. end.
  137.  
  138.