home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1990 / USERAU90.MSA / TEXT_STOS.DOC < prev    next >
Text File  |  1990-06-24  |  8KB  |  176 lines

  1.                            ON THE REBOUND
  2.  
  3.  
  4.             Phil Lawson continues his STOS BASIC tutorial and
  5.            introduces a clever way to get your sprites bouncing
  6.  
  7.  
  8. This month we'll continue to explore sprite commands and pay particular
  9. attention in getting objects to bounce around the screen. (I had hoped to get
  10. the sprite path definer completed for this month's issue, but several problems
  11. cropped up - including a duff disk drive - which means I'll have to postpone it
  12. for a while.)
  13.  
  14.     Anyone remotely interested in games playing will have heard of Breakout, or
  15. its famous 16 bit grand-child Arkanoid. These games are very simple in concept
  16. and the major programming headache is checking whether the ball has hit an
  17. obstacle and calculating the resulting direction when it has.
  18.  
  19.     To illustrate these points load and run STOS.BAS from the cover disk.
  20. You'll see a ball moving around a simple maze, with deflectors at each corner
  21. changing the ball's direction. The big question is how does the program work?
  22.  
  23. 10 REM Rebounding ball demo
  24. 20 MODE 0 : KEY OFF : CURS OFF : HIDE
  25. 30 DIM b(4,4),dx(4),dy(4)
  26. 40 dx(1)=-1 : dx(2)=0 : dx(3)=1 : dx(4)=0
  27. 50 dy(1)=0 : dy(2)=-1 : dy(3)=0 : dy(4)=1
  28. 60 b(1,1)=2 : b(1,4)=3 : b(2,1)=4 : b(2,2)=3
  29. 70 b(3,2)=1 : b(3,3)=4 : b(4,3)=2 : b(4,4)=1
  30. 80 RESERVE AS SCREEN 10 : UNPACK 5,10 : WAIT VBL
  31. 90 SCREEN COPY 10 TO BACK : WAIT VBL
  32. 100 SCREEN COPY 10 TO PHYSIC : WAIT VBL
  33. 110 SPRITE 1,303,160,1 : xp=303 : yp=160 : WAIT VBL
  34. 120 d=2 : REM 1=left,2=up,3=right,4=down
  35. 140 WHILE-1
  36. 150 c=DETECT(1) : IF c>0 THEN GOSUB 190
  37. 160 xp=xp+dx(d) : yp=yp+dy(d)
  38. 170 SPRITE 1,xp,yp,1 : WAIT VBL
  39. 180 WEND
  40. 190 REM Find out the angle of the barrier
  41. 200 LOGIC=BACK : p1=POINT(xp-1,yp+1) : p2=POINT(xp-1,yp-1)
  42. 210 p3=POINT(xp+1,yp-1) : p4=POINT(xp+1,yp+1) : LOGIC=PHYSIC
  43. 220 number=0 : IF p4>0 THEN number=4
  44. 230 IF p3>0 THEN number=3
  45. 240 IF p2>0 THEN number=2
  46. 250 IF p1>0 THEN number=1
  47. 260 REM number is now 1,2,3 or 4
  48. 270 REM get the new direction by using number and the old direction
  49. 280 d=b(number,d)
  50. 290 RETURN
  51.  
  52.     This is a very simple demonstation which only allows the ball to move
  53. either vertically or horizontally. We'll cover the more complex diagonal
  54. movements in a future article.
  55.  
  56.     Lines 30 to 70 set up a series of arrays which contain all the directional
  57. information, dx(4) and dy(4) hold the x and y increments for each of the four
  58. possible directions - left, up, right and down. For instance, if the ball is to
  59. move right (3), all we'd have to do is keep adding dx(3) and dy(3) to the x and
  60. y values of the sprite. The array b(4,4) is used to calculate the new direction
  61. when the ball hits an obstacle and we'll examine this in more detail later.
  62.  
  63.     For this demonstration program I could have set up a zone at each of the
  64. corners and simply checked when the ball moved into one. However, this wouldn't
  65. take into account the original direction of the ball, which as Figure I shows,
  66. decides the new direction.
  67.  
  68. STOSPIC.PC1:<<SCREENSHOT>>
  69.  
  70.     Another problem that occurs when using zones is that depending on your
  71. screen design they may overlap. This doesn't sound serious until you realise
  72. that the ZONE function only returns the first zone number the sprite is in and
  73. ignores any others. So, the ZONE result returned may not be the one you want.
  74.  
  75.     A more precise way of testing whether a sprite has hit something is to
  76. place the hot-spot of the sprite at its centre and constantly check the colour
  77. of the background screen under the sprite with the DETECT command, the syntax
  78. of which is:
  79.  
  80. x = DETECT(sprite-number)
  81.  
  82.     The returned value of x will be the colour number directly under the
  83. hot-spot. Therefore, if we use a particular colour to draw the objects the ball
  84. should bounce off, we can test for it with DETECT and change direction when the
  85. right colour is found.
  86.     Try adding the following line to the demonstration program to see the small
  87. points of colour which I've used to determine when the ball should change
  88. direction.
  89.  
  90. 105 COLOUR 7,$777
  91.  
  92.     Just looking for the colour won't tell us the angle which the ball hits the
  93. object, and therefore we cannot determine the new direction. This is where a
  94. little mathematics is used.
  95.  
  96.     Figure II shows a ball hitting an obstacle, with the centre point just
  97. touching the edge. Around the centre are four other points - P1,P2,P3 and P4.
  98. By testing the colours under these four points we can work out the angle of
  99. impact.
  100.  
  101. STOSPIC.PC1:<<SCREENSHOT>>
  102.  
  103.     In the first only P1 will be set, no matter which direction the ball is
  104. travelling in, and the other three points will be zero. The same is true for
  105. P2, P3 and P4 in the other examples. To see this for yourself try adding the
  106. folowing line:
  107.  
  108. 255 LOCATE 0,0 : PRINT p1,p2,p3,p4
  109.  
  110.     If x and y represent the centre coordinates of the ball, the positions of
  111. P1, P2, P3 and P4 are:
  112.  
  113. P1 is at x-1,y+1
  114. P2 is at x-1,y-1
  115. P3 is at x+1,y-1
  116. P4 is at x+1,y+1
  117.  
  118.     We can now check the background colour under these four points with the
  119. POINT command as shown in lines 200 and 210. Testing each point in turn will
  120. give us a value which represents the angle of the obstacle. This is where the
  121. array B(4,4) is used.
  122.  
  123.     The following table allows us to cross-reference the value obtained above
  124. with the original direction to find which way the ball should bounce.
  125.  
  126.                      original direction
  127.               left(1)   up(2)   right(3)   down(4)
  128.          1      2        0         0          3
  129. obtained 2      4        3         0          0
  130. value    3      0        1         4          0
  131.          4      0        0         2          1
  132.  
  133. Table I: Cross referencing the obtained value with the original direction.
  134.  
  135.     For example, if the obtained value was 2 this indicates the ball has hit a
  136. type-2 obstacle - shown in Figure III.
  137.  
  138. STOSPIC.PC1:<<SCREENSHOT>>
  139.  
  140.     There are only two possible directions the ball could have been travelling
  141. in to hit this type, namely left or up. If it was moving left, the table now
  142. shows that the resulting direction will be downwards. On the other hand, if it
  143. was moving up it will now start to move right.
  144.  
  145.     If you're still confused, which wouldn't be all that surprising since this
  146. concept can be difficult until you get used to it, try comparing Table I with
  147. the array in lines 60 and 70.
  148.  
  149.     You should now be able to see how the program works and understand the
  150. principle of using points around the centre position to determine the new
  151. direction. Next month I'll take these concepts further and get the ball to
  152. bounce in eight instead of four directions.
  153.  
  154.  
  155.  
  156. =========================================================================
  157.                                HINTS AND TIPS
  158. =========================================================================
  159.  
  160.     This month I'll clear up a small point which seems to be giving several
  161. readers a few problems. When using the COMPACT.ACB accessory to pack a screen,
  162. a message is displayed indicating that the compacted screen is to be used in
  163. bank 5. In fact, once it has been saved, you can load it into any of the
  164. available banks with the command:
  165.  
  166. LOAD "filename.mbk",bank-number
  167.  
  168.     The reason the message states bank 5, is that this is one normally used to
  169. store packed screen data and it's the bank that COMPACT.ACB uses. There is no
  170. reason why you cannot use any of the others to store your compacted screens.
  171.  
  172.  
  173. HARDWARE: ALL STs, COLOUR ONLY
  174.  
  175.  
  176.