home *** CD-ROM | disk | FTP | other *** search
/ Game Programming for Teens / GameProgrammingForTeens.iso / Source / chapter12 / demo12-02.bb < prev    next >
Encoding:
Text File  |  2003-04-06  |  2.1 KB  |  111 lines

  1. ;demo12-02.bb - Demonstrates random variables
  2. Graphics 800,600
  3.  
  4. ;Set up automidhandle and backbuffer
  5. AutoMidHandle True
  6. SetBuffer BackBuffer()
  7.  
  8. ;Make sure we seed the random generator
  9. SeedRnd MilliSecs()
  10.  
  11.  
  12. ;CONSTANT
  13. ;this constant regulates how long it takes before the fly changes directions
  14. Const CHANGEDIRECTIONS = 1500    ;the fly changes every 1.5 seconds
  15.  
  16. ;The fly type
  17. Type fly
  18.     Field x,y  ;the coordinate position
  19.     Field xv,yv   ;the fly's velocity
  20.     Field image  ;The fly's image
  21. End Type
  22.  
  23.  
  24. ;let's create the fly
  25. fly.fly = New fly
  26.  
  27. ;Start the fly in the center of the screen
  28. fly\x = 400
  29. fly\y = 300
  30.  
  31. ;Give the fly a random velocity
  32. fly\xv = Rand(-15,15)
  33. fly\yv = Rand(-15,15)
  34.  
  35. ;Now we load the fly image 
  36. fly\image = LoadAnimImage ("fly.bmp",64,64,0,4)
  37.  
  38. ;create a starting frame value
  39. frame = 0
  40.  
  41. ;Create starting timer
  42. timerbegin = MilliSecs()
  43.  
  44. ;Create a variable that says the timer does not need to be reset
  45. timeractive = 1
  46.  
  47. ;MAIN LOOP
  48. While Not KeyDown (1)
  49.  
  50. ;Clear the screen
  51. Cls
  52.  
  53. Text 0,0,"Fly X: " + flyx
  54. Text 0,20,"Fly Y: " + flyy
  55. Text 0,40, "Current time remaining on timer: " + ( CHANGEDIRECTIONS - MilliSecs() + timerbegin )
  56.  
  57.  
  58.  
  59. ;If the counter has run through, update the fly's velocities
  60. If MilliSecs() >= timerbegin + CHANGEDIRECTIONS
  61.  
  62.     ;move the fly a random amount
  63.     fly\xv = fly\xv + Rand(-5,5)
  64.     fly\yv = fly\yv + Rand(-5,5)
  65.  
  66.     ;make sure timer is reset
  67.     timeractive = 0
  68. EndIf
  69.  
  70. ;If the timer is inactive, reset the timer
  71. If timeractive = 0
  72.     timerbegin = MilliSecs()
  73.     timeractive = 1
  74. EndIf
  75.  
  76.  
  77.  
  78.  
  79. ;Move the fly
  80. fly\x = fly\x + fly\xv
  81. fly\y = fly\y + fly\yv
  82.  
  83. ;Test if fly hit any walls
  84. If fly\x <= 0 Or fly\x > 800
  85.     fly\xv = -fly\xv
  86. EndIf
  87.  
  88. If fly\y <= 0 Or fly\y >= 600
  89.     fly\yv = - fly\yv
  90. EndIf 
  91.  
  92. ;Draw the fly on screen
  93. DrawImage fly\image,fly\x,fly\y,frame
  94.  
  95. ;increment the frame
  96. frame = frame + 1
  97.  
  98. ;If frame gets too large or small, reset it
  99. If frame > 3
  100.     frame = 0
  101. ElseIf frame < 0
  102.     frame = 3 
  103. EndIf
  104.  
  105. ;Flip the buffers
  106. Flip
  107.  
  108. ;Wait a little bit
  109. Delay 75
  110.  
  111. Wend   ;END OF MAIN LOOP