LoginLogin

How To Make Multiple enemies generate.

Root / Programming Questions / [.]

Z_E_R_OCreated:
I need to know how to make multiple enemies.

Easy. Just create a bunch of sprites and use a bunch of vars for their positions.

FOR I=0 TO WHATEVER
 SPSET I,0
 SPOFS I,EX[I],EY[I]
 NEXT
'Now do whatever to the X/Y arrays

Thank you!

it says type mitchmatch on spofs

Maybe try this?
VAR ENEMY_COUNT=25
DIM EX[ENEMY_COUNT]
DIM EY[ENEMY_COUNT]
VAR I 'declare vars like normal

FOR I=0 TO ENEMY_COUNT-1 'This will set the coordinates of each sprite to a random position on-screen.
 EX[I]=RND(400)
 EY[I]=RND(240)
 NEXT

WHILE TRUE 'People often prefer while loops for their game loops. This isn't really necessary here.
 FOR I=0 TO ENEMY_COUNT-1
  INC EX[I],(RND(10)-RND(10)) 'Should generate random positive or negative numbers. There are probably better ways of doing this.
  INC EY[I],(RND(10)-RND(10))
  SPSET I,0
  SPOFS I,EX[I],EY[I]
  NEXT
 VSYNC
 WEND
Also arrays can be declared with VAR instead of DIM. Variables can be declared with DIM too.

It worked thank you.

It worked thank you.
You're welcome.

So this Is a while after. But how would I make them react to the BG like I do the character throu bgcheck?

You BGCHECK with their sprites, just like the character. You can cycle through all of them in one frame (one pass of the game loop) using a FOR loop.

You BGCHECK with their sprites, just like the character. You can cycle through all of them in one frame (one pass of the game loop) using a FOR loop.
Thanks again!! Now the only situation I have is I'm trying to make their color separate and it won't budge.

You're trying to color each sprite a different color and make certain sprites not move as in at all or on the bg (or is that something else?)?

I was just trying to make a different type of enemy. And I'm still getting the hang of arrays. I want multiple types of enemy with the same collision and movement.

There are a few ways of doing this. One way is to use one multi-dimensional array (but separate arrays allow more unique attributes and can be easier to use at times because they can be resized) for all the enemies and one loop to manage them all. The following code uses multiple, single dimension arrays to create and manage multiple enemies of different types.
DIM ESP%[10] 'Enemy sprite
DIM EX%[10] 'Enemy X coord
DIM EY%[10] 'Enemy Y coord
DIM ETYPE%[10] 'Enemy AI/etc. type

FOR I=0 TO 9
 SPSET I,ESP%[I] 'Of course, you will have to set the array data. For instance ESP%[0] can be sprite 16 while ESP%[whatever] can be whatever other sprite.
 NEXT

WHILE 1 'Main loop, can be whatever loop type
 FOR I=0 TO 9
  SPOFS I,EX%[I],EY%[I]
  IF ETYPE%[I]==0 THEN
   'Enemy type code such as AI etc.
  ELSEIF ETYPE%[I]==1 THEN
   'Same thing, continue for how many types you want. Be sure to include the type in your array.
  ENDIF
  'You can also do other conditionals for the sprite, X/Y, etc.
  NEXT
 
 'Game code.
 WEND
I'll post again later if you still need help with arrays.

Thank you! Youve been really nice even though I spam you with questions. Everything is working now.

No problem. Question "spam" is what keeps the forums alive, just as long as they aren't repetitive questions.

You can use SPFUNC to have sprites handle themselves, and use SPVAR for the sprite specific variables. This example doesn't have sprite or background interaction but maybe it will help. If you get it right you just set up the sprite and let it go.
VAR MAXX = 400, MAXY = 240, I '0-6 = FRUIT
DEF BOUNCE
 VAR X, Y, XDIR, YDIR
 SPOFS CALLIDX OUT X, Y
 XDIR = SPVAR(CALLIDX, 0)
 YDIR = SPVAR(CALLIDX, 1)
 INC X, XDIR
 INC Y, YDIR
 IF X < 0 OR X >= MAXX THEN
  SPVAR CALLIDX, O, XDIR * -1
 ENDIF
 IF Y < 0 OR Y >= MAXY THEN
  SPVAR CALLIDX, 1, YDIR * -1
 ENDIF
 SPOFS CALLIDX, X, Y
END

ACLS : PRINT "PRESS [B] TO EXIT"
FOR I = 0 TO 13
 SPSET I, I MOD 7
 SPOFS I, RND(MAXX), RND(MAXY)
 SPVAR I, 0, RND(9) - 4
 SPVAR I, 1, RND(9) - 4
 SPFUNC I, "BOUNCE"
NEXT I

REPEAT
 CALL SPRITE
 VSYNC
UNTIL (BUTTON() AND #B) != 0


Thank you 😁 I have a code working now. But I like yours too.