home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / snakedemo.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  4.9 KB  |  189 lines

  1.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2.     ' SnakeDemo
  3.  
  4.     ' Set screen mode first.
  5.     textmode (TEXT_BUFFERED)
  6.     resizetext (40, 25)
  7.  
  8.     ' Variables
  9.  
  10.     ' Snake tail
  11.     const maxTail = 4000            ' Maximum tail length in characters
  12.     dim tx (maxTail), ty (maxTail)
  13.     dim head, tail                  ' Head will walk the array writing positions as head is drawn
  14.                                     ' Tail will walk the array reading positions as tail is erased
  15.     dim stretch                     ' Used to stretch the snake
  16.  
  17.     ' Snake head position and direction
  18.     dim x, y, xd, yd
  19.  
  20.     ' Bug position
  21.     dim bx, by
  22.  
  23.     ' Timing
  24.     dim delay
  25.  
  26.     ' Working variables
  27.     dim t, a$, key, score
  28.     
  29.     goto start
  30.  
  31.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  32.     ' Subroutines
  33.  
  34. showMessage:
  35.     locate (textcols () - len (a$)) / 2, y
  36.     print a$    
  37.     y = y + 1
  38.     return
  39.  
  40. waitKey:
  41.     DrawText ()            ' Show screen
  42.     while inkey$ () <> "": wend
  43.     while inkey$ () = "" and not Joy_Button (0): wend
  44.     y = y + 1
  45.     return
  46.  
  47. setupScreen:
  48.     ' Setup screen
  49.     ' Draw borders
  50.     cls
  51.     locate 0, 0
  52.     color (230, 230, 230)
  53.     for t = 1 to textCols (): print "#": next       ' Top border
  54.     locate 0, textRows () - 1
  55.     for t = 1 to textCols () - 1: print "#": next   ' Bottom border
  56.     color (200, 200, 200)
  57.     for t = 1 to textRows () - 2
  58.         locate 0, t: print"#"                       ' Left border
  59.         locate textCols () - 1, t: print "#"        ' Right border
  60.     next
  61.     return
  62.  
  63. makeBug:
  64.     ' Place bug in a random position
  65.     bx = rnd () % (textCols () - 2) + 1
  66.     by = rnd () % (textRows () - 2) + 1
  67.     return
  68.  
  69.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  70.     ' Level start
  71.  
  72. start:
  73. nextLevel:
  74.  
  75.     ' Setup screen
  76.     gosub setupScreen
  77.  
  78.     ' Show instructions
  79.     color (180, 200, 230)
  80.     y = textRows () / 2 - 3
  81.     a$ = "Snake demo": gosub showMessage
  82.     y = y + 1
  83.     a$ = "ARROW KEYS turn snake": gosub showMessage
  84.     y = y + 1
  85.     a$ = "Press any key to start": gosub showMessage
  86.     gosub waitKey
  87.  
  88.     ' Redraw screen (to get rid of message)
  89.     gosub setupScreen
  90.  
  91.     ' Setup snake
  92.     head = 0: tail = 0
  93.     stretch = 5
  94.     x = textCols () / 2
  95.     y = textRows () - 2
  96.     xd = 0
  97.     yd = -1
  98.  
  99.     ' Setup timing
  100.     delay = 150        ' 150 msec 
  101.  
  102.     ' Setup bug
  103.     gosub makebug
  104.  
  105.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  106.     ' Main loop
  107.  
  108.     while true
  109.  
  110.         ' Draw bug
  111.         color (255, 255, 255)
  112.         locate bx, by: print "o"
  113.         
  114.         ' Read input
  115.         Joy_Keys ()
  116.         key = inscankey()
  117.         if key = VK_UP    and yd <>  1 then yd = -1: xd = 0 endif
  118.         if key = VK_DOWN  and yd <> -1 then yd =  1: xd = 0 endif
  119.         if key = VK_LEFT  and xd <>  1 then xd = -1: yd = 0 endif
  120.         if key = VK_RIGHT and xd <> -1 then xd =  1: yd = 0 endif
  121.  
  122.         ' Move snake's head
  123.         x = x + xd: y = y + yd
  124.  
  125.         ' Did we get the bug?
  126.         if x = bx and y = by then 
  127.             stretch = stretch + 10            ' Stretch the tail
  128.             gosub MakeBug                    ' Move bug to new position
  129.             delay   = delay - 8
  130.             if delay < 10 then delay = 10 endif
  131.         else     
  132.             ' Otherwise, did we crash into something?
  133.             if charat$ (x, y) <> " " then goto dead endif
  134.         endif
  135.  
  136.         ' Draw snake's head
  137.         color (20, 255, 20)
  138.         locate x, y: print "X"
  139.  
  140.         ' Add head position to tail
  141.         tx (head) = x
  142.         ty (head) = y
  143.         head = (head + 1) % maxTail
  144.  
  145.         ' Move or stretch tail
  146.         if stretch > 0 then
  147.             stretch = stretch - 1                    ' Don't advance the tail while stretching
  148.         else
  149.             locate tx (tail), ty (tail): print " "    ' Erase tail
  150.             tail = (tail + 1) % maxTail            ' Advance tail
  151.         endif
  152.  
  153.         ' Display updated screen
  154.         DrawText ()
  155.         WaitTimer (delay)
  156.     wend
  157.  
  158. dead:
  159.  
  160.     ' Draw crashed head
  161.     color (255, 255, 155)
  162.     locate x, y: print "*"
  163.  
  164.     ' Calculate snake length.
  165.     ' (This is the player's score)
  166.     score = head - tail
  167.     if score < 0 then score = score + maxTail endif
  168.  
  169.     ' Draw crashed message
  170.     color (180, 200, 230)
  171.     y = textRows () / 2 - 2
  172.     a$ = "Game Over!": gosub showMessage
  173.     y = y + 1
  174.     a$ = "You scored: " + (head - tail) % maxTail: gosub showMessage
  175.     DrawText ()                            ' Let player see updated screen
  176.     
  177.     ' Pause for 1 second
  178.     ' This gives the player time to stop bashing keys :-)
  179.     sleep (1000)
  180.  
  181.     y = y + 1
  182.     a$ = "Press any key": gosub showMessage
  183.                     
  184.     ' Wait for keypress
  185.     gosub waitkey    
  186.     
  187.     ' Restart the game
  188.     run
  189.