Sprite Demonstration - Discussion

2018/07/30 18:05 by rave
Bug in code
This example has a bug resulting in a sprite to bounce between two sprites or the wall indefinitely, without other sprites moving.

Here is a simple fix:

MODE 3                                        ' Set video mode
LOADBMP"backdrop"                             ' Load backdrop image
SPRITE LOAD "sprites"                         ' Load sprite definitions

DIM p(8,4)                                    ' Define array size

FOR n=1 TO 8                                  ' Set all 8 sprites data
  p(n,1)=n*25+125                             '   X co-ordinate
  p(n,2)=216                                  '   Y co-ordinate
  p(n,3)=FIX(RND*4)+1                         '   Random 4 direction
  SPRITE on n,n*25+125,216                    ' Turn on sprite
NEXT n

GAMELOOP:

  FOR n=1 TO 8                                ' Start of main game loop

    MOVEGHOST:
    ON p(n,3) GOTO LEFT,RIGHT,UP,DOWN         ' Branch to direction

      LEFT:                                   ' Move sprite left
      SPRITE copy 11+b TO n                   ' Copy sprite buffer graphics
      p(n,1)=p(n,1)-1                         ' Decrement X co-ordinate
      GOTO REDRAW                             ' Branch to sprite redraw

      RIGHT:                                  ' Move sprite right
      SPRITE copy 9+b TO n                    ' Copy sprite buffer graphics
      p(n,1)=p(n,1)+1                         ' Increment X co-ordinate
      GOTO REDRAW                             ' Branch to sprite redraw

      UP:                                     ' Move sprite up
      SPRITE copy 13+b TO n                   ' Copy sprite buffer graphics
      p(n,2)=p(n,2)-1                         ' Decrement Y co-ordinate
      GOTO REDRAW                             ' Branch to sprite redraw

      DOWN:                                   ' Move sprite down
      SPRITE copy 15+b TO n                   ' Copy sprite buffer graphics
      p(n,2)=p(n,2)+1                         ' Increment Y co-ordinate

    REDRAW:

      SPRITE move n,p(n,1),p(n,2)             ' Move current sprite

    IF COLLISION(n,edge)>0 THEN               ' Test for edge of screen
      GOSUB BOUNCE

    ELSEIF COLLISION(n,sprite)>0 THEN         ' Test for another sprite
      GOSUB BOUNCE

    ELSEIF RND>.99 THEN                       ' Randomly decide to turn
      p(n,3)=FIX(RND*4)+1

    ENDIF

    NEXT n                                    ' Loop back for next sprite

c=c+1                                         ' Counter for sprite animate
IF c>8 THEN b=b XOR 1:c=0                     ' Sets speed of animation

GOTO GAMELOOP                                 ' Do it all again


BOUNCE:                                       ' reverse sprite direction
  p(n,3)=p(n,3)+1                             ' Reverse direction
  IF p(n,3)=3 THEN p(n,3)=1
  IF p(n,3)=5 THEN p(n,3)=3
  RETURN                                      ' Go back to redraw sprite