forums

TNT Basic Forums > Programming Q&A
put multiple enemy in 1 var
< Last Thread     Next Thread >
Author
Thread        Post A Reply

01-29-2005 17:41

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

Hi im making a game which they are a lot of computer enemies i dont really understand the array fonction...
can you help me to code something as simple as that:

i have a lot of enemies i want each to have
their own x and y and sprite #

so its e1x,e1y, and 1 for enemy#1
and e2x,e2y and 2 for enemy#2
but thats make too much variable (because i have 30 enemies ) i want to do all the sprite collision and some other things

How can i do for exemple : ex[30] (<-- 30 enemies "x")
so i have only to do one sprite collision and that work for all enemies with the arrays ??

Thanks !

Leorik

01-30-2005 01:25

Posted by:
isthaq

Location:
Kansas City

Click Here to Email isthaq   Find more posts by isthaq

I didn't understand arrays at first either.

An array of "x[3]" would contain 3 variables: x[0], x[1], and x[2].

You would assign values to and read values from these variables by referring to them as x[0], x[1] and x[2].

A 2 dimensional array of y[3,4] would contain 3 arrays of 4 variables each: y[0,0]
y[0,1]
y[0,2]
y[0,3]
y[1,0]
y[1,1]
y[1,2]
...and so on.
So if you wanted an array of 30 monsters with 3 variables each, it would be:
monsters[30,3]

What I don't understand is a quick way to assign values to all the variables in an array without actually typing out
monsters[0,0]=5
monsters[0,1]=8
monsters[0,2]=0...

I think it can be done with the "data" and "read" things, but I don't understand how to do those yet.

01-30-2005 17:06

Posted by:
Fischgurkensenf

Location:
Bern, Switzerland

Click Here to Email Fischgurkensenf   Find more posts by Fischgurkensenf

Use a simple "for" loop. :-)

Here is an example:

int n
for n=0 to 29 'this is for array with 30 segments
variable[n]=123
anothervariable[n]=456
next n

and so on... with the enemy x and y positions this would be:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
graphics mode 640,480

load images 128 'or any other image bank

float ex[30],ey[30]
float exspeed[30],eyspeed[30]

'set up all variables
int n
for n=0 to 29
ex[n]=320 'can be anything you like
ey[n]=240 'dito
'set speeds so your sprites move
exspeed[n]=random(-20,20)/10
eyspeed[n]=random(-20,20)/10
next n

while not mouse button 'press the mouse button to quit

'check for collisions and update positions
for n=0 to 29
ex[n]=ex[n]+exspeed[n] 'update positions
ey[n]=ey[n]+eyspeed[n]
if ex[n]>640 or ex[n]<0 then exspeed[n]=-exspeed[n]'check for collisions with screen bounds
if ey[n]>480 or ey[n]<0 then eyspeed[n]=-eyspeed[n]
sprite n,ex[n],ey[n],0 'or put another image instead of 0
next n

draw frame 'draw all sprites on screen

wend
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
just copy this text into the hieroglyph coding window and add an image with number 0 in image bank 128 and try it out!

Hope I could help you a bit :-)

-IBD

01-30-2005 18:05

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

Thank you

That was the exactly answer i was looking for !

I understand now , thans you guys

Cheers,

Leorik

01-30-2005 18:37

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

Arrays out of bound...

I have a problem in my code that say "arrays out of bound" when I try to run it here the piece of wrong code:


repeat
int n
for n=1 to 30

if px>ex[n] then ex[n]=ex[n]-random(0.3,2)
' here start the errors arrays out of bounds

if px<ex[n] then ex[n]=ex[n]-random(0.3,2)

if py>ey[n] then ey[n]=ey[n]+random(0.3,2)
if py<ey[n] then ey[n]=ey[n]-random(0.3,2)
sprite n,ex[n],ey[n],eimage
next n
draw frame
until mouse button

how can i fix that ?? thanks
Leorik

01-30-2005 18:51

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

Never mind

I fixed the problem !

01-31-2005 05:16

Posted by:
Fischgurkensenf

Location:
Bern, Switzerland

Click Here to Email Fischgurkensenf   Find more posts by Fischgurkensenf

:-)

I guess your problem was in the line:

for n=1 to 30

:-)

because an array, e.g "ex[5]" has the elements

ex[0]
ex[1]
ex[2]
ex[3]
ex[4]

and if you go for

for n=1 to 5

it's a bug because you're accessing "ex[5]" which doesn't exist. I had this problem, too. It's good you fixed the problem by yourself. ;-)

-IBD

01-31-2005 05:26

Posted by:
Fischgurkensenf

Location:
Bern, Switzerland

Click Here to Email Fischgurkensenf   Find more posts by Fischgurkensenf

Oh, and if I take a look at your code...

it's possible this doesn't work right for you:
if px>ex[n] then ex[n]=ex[n]-random(0.3,2)
because it's impossible to get numbers smaller than 1 and bigger than -1 with the built-in random function...
(Hope I'm right with that, at least it seems so for me :-)

use
if px>ex[n] then ex[n]=ex[n]-random(3,20)/10
instead

and make sure you're using floats for the variables because integers are numbers like 3,6 or 200 while floats are numbers like 2.125, 6.453 or 200.000 .

I'll send you the car racing example soon! I have no complete versions on my harddisk, you will have to get an incomplete version, sorry.

-IBD

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.