home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gt_snake.prg < prev    next >
Text File  |  1993-10-14  |  4KB  |  166 lines

  1. /*
  2.  File......: GT_SNAKE.prg
  3.  Author....: Phillip Hamlyn
  4.  BBS.......: The Dark Knight Returns
  5.  Net/Node..: 050/069
  6.  User Name.: Phillip Hamlyn
  7.  Date......: 03/03/93
  8.  Revision..: 1.0
  9.  
  10.  This is an original work by Phillip Hamlyn and is placed in the
  11.  public domain.
  12.  
  13.  Modification history:
  14.  ---------------------
  15.  
  16.  Rev 1.0 03/03/93
  17.  Initial revision.
  18. */
  19.  
  20. /*  $DOC$
  21.  *  $FUNCNAME$
  22.  *      GT_SNAKE()
  23.  *  $CATEGORY$
  24.  *      Screen Saver
  25.  *  $ONELINER$
  26.  *      Novell-style snake screen saver
  27.  *  $SYNTAX$
  28.  *      GT_Snake(<cColour>) -> NIL
  29.  *  $ARGUMENTS$
  30.  *      <cColour> This is the snakes colour, defaults to setColour()
  31.  *  $RETURNS$
  32.  *      NIL
  33.  *  $DESCRIPTION$
  34.  *      Screen saver for any Clipper application. This screen saver draws a
  35.  *      snake on the screen and lets it fly !
  36.  *  $EXAMPLES$
  37.  *      GT_Snake("W+/BG") -> NIL
  38.  *  $SEEALSO$
  39.  *
  40.  *  $INCLUDE$
  41.  *
  42.  *  $END$
  43.  */
  44.  
  45. #include "gt_lib.ch"
  46.  
  47. * * * * * * * * *
  48. FUNCTION GT_Snake (cColour)
  49. * * * * * * * * *
  50. #define NORTH 1
  51. #define SOUTH 2
  52. #define EAST 3
  53. #define WEST 4
  54. LOCAL  aWorm, bInterrupt
  55. local nDirection := NORTH
  56. local cScreen := ""
  57. local nRand := 0, f := 0
  58. // possible directions...
  59. local nNewDirs := { {SOUTH,EAST,WEST} ,;
  60.                     {NORTH,WEST,EAST} ,;
  61.                     {WEST ,SOUTH,NORTH},;
  62.                     {NORTH,EAST,SOUTH} }
  63.  
  64. local nX,nY
  65.  
  66. local cWormCol := iif(cColour == NIL,setcolor(),cColour)
  67.  
  68. bInterrupt := GT_Interrupt()
  69.  
  70. aWorm :={{12,29,'█',cWormCol},;
  71.          {12,30,'█',cWormCol },;
  72.          {12,31,'█',cWormCol },;
  73.          {12,32,'█',cWormCol },;
  74.          {12,33,'▓',cWormCol },;
  75.          {12,34,'▓',cWormCol },;
  76.          {12,35,'▓',cWormCol },;
  77.          {12,36,'▓',cWormCol },;
  78.          {12,37,'▒',cWormCol },;
  79.          {12,38,'▒',cWormCol },;
  80.          {12,39,'▒',cWormCol},;
  81.          {12,40,'▒',cWormCol },;
  82.          {12,41,'░',cWormCol },;
  83.          {12,42,'░',cWormCol },;
  84.          {12,43,'░',cWormCol},;
  85.          {12,44,'░',cWormCol },;
  86.          {12,45,' ',cWormCol }}
  87.  
  88. // m_csroff() // Turn off your mouse if required.
  89.  
  90. cScreen := savescreen(0,0,maxrow(),maxcol())
  91.  
  92. do while TRUE // LOOP until interrupt is given.
  93.  
  94.    // Now check the keyboard etc.
  95.    if eval(bInterrupt)
  96.       exit
  97.    endif
  98.  
  99.    // Does the worm change direction ?
  100.    nRand := val(right(str(seconds(),8,2),1))
  101.    // Force this if the worm is going off the screen
  102.  
  103.    do while TRUE // till we get a valid direction ...
  104.  
  105.       do case
  106.          case nDirection == NORTH .and. aWorm[1][1] == 0
  107.             nRand := 10 // impossible to get normaly !
  108.          case nDirection == SOUTH .and. aWorm[1][1] == maxrow()
  109.             nRand := 10 // impossible to get normaly !
  110.          case nDirection == EAST .and. aWorm[1][2] == 0
  111.             nRand := 10 // impossible to get normaly !
  112.          case nDirection == WEST .and. aWorm[1][2] == maxCOL()
  113.             nRand := 10 // impossible to get normaly !
  114.       endcase
  115.  
  116.       if nRand > 7
  117.          nRand := val(right(str(seconds(),8,2),1))
  118.  
  119.          // we need to change direction.
  120.          // Make Rand itself /3 so we get a 1,2,3 number
  121.          nRand := round(nRand / 3,0)
  122.          nRand := max(nRand,1)
  123.          nRand := min(3,nRand)
  124.          // Ok should be itself / 3 and either 1,2,3
  125.          // probably not totaly random but who cares ?
  126.          nDirection := nNewDirs[nDirection][nRand]
  127.       else
  128.          exit // found a valid direction
  129.       endif
  130.  
  131.    enddo // till we get a valid direction
  132.  
  133.    nX := 0 // elements to advance
  134.    nY := 0
  135.    // Advance the worm !
  136.    do case
  137.       case nDirection == NORTH
  138.          nX := -.5
  139.       case nDirection == SOUTH
  140.          nX := .5
  141.       case nDirection == EAST
  142.          nY := -1
  143.       case nDirection == WEST
  144.          nY := 1
  145.    endcase
  146.  
  147.    // now apply this to the entire worms coordinates and paint it.
  148.  
  149.    for f := len(aWorm) to 1 step -1
  150.       @ aWorm[f][1], aWorm[f][2] say aWorm[f][3] colour aWorm[f][4]
  151.       if f == 1
  152.          aWorm[f][1] := aWorm[f][1] + nX
  153.          aWorm[f][2] := aWorm[f][2] + nY
  154.       else
  155.          aWorm[f][1] := aWorm[f-1][1]
  156.          aWorm[f][2] := aWorm[f-1][2]
  157.       endif
  158.    next f
  159.  
  160. enddo
  161.  
  162. restscreen(0,0,maxrow(),maxcol(),cScreen)
  163. // m_csron() // Turn on mouse cursor if required.
  164.  
  165. RETURN NIL // Nothing better to return.
  166.