forums

TNT Basic Forums > Programming Q&A
Need help
< Last Thread     Next Thread >
Author
Thread        Post A Reply

06-20-2006 19:23

Posted by:
MT86

Location:
US

Find more posts by MT86

hi
Does anyone know how to make multiple projectiles move at once? like if i was making a space invaiders game
(i used to have some of my code but i got a new mac and i cant find it)

thanks

06-20-2006 20:51

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

i don,t know how good you are at programming, so i'll assume you can make a single moving bullet (if you don't udnerstand what i say, just tell me and i'll explain more)


you'll need to use arrays

to move one bullet, you need to know its location:


int bullet_x=0, bullet_y=200

while true
bullet=bullet+1
sprite 1, bullet_x, bullet_y, image
wend


to have many bullets, you'll need many variables. assuming i have 10 bullets, bullet_x becomes bullet_x[10]

bullet_x[10] is the same as having bullet_x_1, bullet_x_2, and so on but quicker


int bullet_x[10], bullet_y[10]
int n 'we will do one bullet one after the other, n will store which bullet we're currently doing

' set initial location
for n=0 to 9
bullet_x[n]=0
bullet_y[n]=n*50 'so that bullets don't start at the same place
next

while true
for n=0 to 9
bullet[n]=bullet[n]+1
sprite 1+n, bullet_x[n], bullet_y[n], image
next
wend


i haven,t tested the code, but it should be a reliable skeleton

06-21-2006 21:14

Posted by:
MT86

Location:
US

Find more posts by MT86

ok i have been messing with the code you gave me


int bullet_x[10], bullet_y[10]
int sx=640,sy=400
int fired=false
int n,g

Window mode sx, sy
Load images 128
Set pen color make color(50, 120, 255)
Paint canvas

g=0
while true
If space
g=g+1
fired=true
end if
If fired=true
for n=0 to g
bullet_y[n]=bullet_Y[n]+5
sprite n+1, bullet_x[n], bullet_y[n],000
draw frame
next
end if
If bullet_y[n]>650 then n=n-1
wend

i understand programming but when there are lots of things to keep track of i start to get confused
this worked for a bit but it gave be the "array out of bounds" message after a bit

If anyone could tell me what i did wrong or how i could code it better than would be great

thanks again

06-21-2006 23:42

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

array index out of bounds means you've crossed the limit, e.g. you created 10 variables but try to access the 11th. Don't forget, if you create x[10], that they will range from x[0] to x[9] (0 to 9 makes 10, accessing x[10] itself will fail)

i tried looking at your code and i'm not sure what variable 'g' stands for. i think it's so that everytime you hit space a new bullet is created, right? however, you have int bullet_x[10], bullet_y[10], that means afte 10 bullets it will cross the limit and try to access the 11th, that doesn't exist.

there are many ways to solve this, but here's a suggestion:
you use a single 'fired' variable. however, there are 10 independant bullets and they not necessarly will be shot at the same time, so every bullet needs his own 'fired' variable

int fired[10]

then you'll need to set the 10 of them to false, and everytime the user pressed space, you'll need to go through 'fired' variables and find an available slot. you'll also need to 'free' variables that have gone out of screen bounds.

this is what you probably tried to do with:
If bullet_y[n]>650 then n=n-1

but this has no use, because of 'for n=0 to g'
no matter what you do to n before or after 'for', everytime it hits for n goes back to zero. in fact, 'for' means something like "i want to check shots 0 to 9, and remember in n which one is currently being done"

instead, you'd need, every loop, to check for every shot

pseudo-code for your main loop (in leave initialisation for you to try):

int time_between_shots

while true

for n=0 to 9

if fired[n] ' same as if fire=true but nicer

bullet_y[n]=bullet_y[n]+5
sprite n+1, bullet_x[n], bullet_y[n],000

end if

next

'------------ shooting ----------

' here i use a variable time_between_shots to ensure not
' all shots are shot at once when user holds down 'space'
' when shooting, time_between_shots is set to 100 and will
' decrease every frame. i won't be able to shoot again
' until it's equal or lower than zero

time_between_shots=time_between_shots-1

if space and time_between_shots<0

for n=0 to 9
if not fired[n]
' we have found a shot that's currenttly not used!
' we'll setup variables for this shot
fired[n]=true
bullet_x[n]=(some x, depending on your game)
bullet_y[n]=(some y, depending on your game)

time_between_shots=100
end if
next

end if

' you don't need draw frame inside the for block
' draw frame must be down only once per frame, when
' all your sprites are ready
' calling it too often will slow things down and make
' animation less smooth
draw frame

wend




I have NOT ran the code, it's just to give an idea, it might as well be full of mistakes

if you need help, don't hesitate to post again! if you need it, i'll make real code and post it

06-21-2006 23:47

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

oops i forgot a few things...

when space is pressed...

' we have found a shot that's currenttly not used!
' we'll setup variables for this shot
fired[n]=true
bullet_x[n]=(some x, depending on your game)
bullet_y[n]=(some y, depending on your game)

'once we have found an available shot we musn't continue
'looking, we must exit 'for', with command 'break'

break



I also forgot to check if the shot goes out of the screen. though,i think you can do it, it'll be something like:

if bullet_y[n]>600 then fired[n]=false

06-21-2006 23:52

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

one last thing... (dammit can't we have editable posts on these boards)

i ran the code and it works fine, it's just that time_between_shots=100 is too big, time_between_shots=10 work better

FULL CODE:


int bullet_x[10], bullet_y[10]
int sx=640,sy=400
int fired[10]
int n

for n=0 to 9
fired[n]=false
next

Window mode sx, sy
Load images 128
Set pen color make color(50, 120, 255)
Paint canvas


int time_between_shots

while true

for n=0 to 9

if fired[n] ' same as if fire=true but nicer

bullet_y[n]=bullet_y[n]+5
sprite n+1, bullet_x[n], bullet_y[n],0
if bullet_y[n]>600 then fired[n]=false

end if

next

'------------ shooting ----------

' here i use a variable time_between_shots to ensure not
' all shots are shot at once when user holds down 'space'
' when shooting, time_between_shots is set to 100 and will
' decrease every frame. i won't be able to shoot again
' until it's equal or lower than zero

time_between_shots=time_between_shots-1

if space and time_between_shots<0

for n=0 to 9
if not fired[n]
' we have found a shot that's currenttly not used!
' we'll setup variables for this shot
fired[n]=true
bullet_x[n]=200
bullet_y[n]=0

time_between_shots=10
break

end if
next

end if

' you don't need draw frame inside the for block
' draw frame must be down only once per frame, when
' all your sprites are ready
' calling it too often will slow things down and make
' animation less smooth
draw frame

wend

06-22-2006 04:00

Posted by:
MT86

Location:
US

Find more posts by MT86

It works! woot!

Thanks for the help
but i have few questions

The line that is bold below:

for n=0 to 9
If fired[n]
bullet_y[n]=bullet_y[n]+5
sprite n+1,bullet_x[n],bullet_y[n],0
if bullet_y[n]>600 then fired[n]=false
end if
next

does that mean i need 9 image banks because n is changing the image bank right?

and what does 'break' do?
just curious

thanks again!

06-22-2006 04:22

Posted by:
MT86

Location:
US

Find more posts by MT86

i just found another problem

I made 128 the bullet and 130 the ship.
But when i press space it shoots the ship not the bullets
i have had this problem with many other programs i make in TNT and i usally just swap or move around the 'load image' statements. But that didn't work

if you can help me out again and that would be great
thanks

[I will bold the statements that involve the player (hope it helps)]

int bullet_x[10], bullet_y[10]
int sx=640,sy=400
int fired[10]
int n

int charX,charY,speed=10 'I declared the variables for ship here

for n=0 to 9
fired[n]=false
next

Window mode sx, sy

Set pen color make color(50, 120, 255)
Paint canvas
Load images 128
load images 130 'This is where i loaded images

int time_between_shots

while true

for n=0 to 9
If fired[n]
bullet_y[n]=bullet_y[n]+5
sprite n+1,bullet_x[n],bullet_y[n],0
if bullet_y[n]>600 then fired[n]=false
end if
next

'Shooting
'--------

time_between_shots=time_between_shots-1

if left then charx=charx-speed
if right then charx=charx+speed 'here is movement

if space and time_between_shots<0

for n=0 to 9
if not fired[n]
fired[n]=true
bullet_x[n]=200
bullet_y[n]=0

time_between_shots=10
break

end if

next

end if
sprite 130,charx,chary,0 'And finally here is where i draw the sprite
draw frame

wend

06-22-2006 14:41

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

> sprite n+1,bullet_x[n],bullet_y[n],0

i think you misunderstood the first parameter of 'sprite'

it is not the ID of the image bank or of the image, it is the ID of the sprite. each sprite has a unique ID... for instance, you could decide your space ship has an ID of 0, and your bullets have IDs of 1 to 10, etc. this ID is connected in no way to the image of the sprite, the image of the sprite is give in the last parameter

you should put all your images in a single bank, it's easier... image banks exist in case you make a game so big all images don't fit in memory at the same time, or would take very long to load, or for organisation of long animations. as your game is still very small, i strongly recommend you put all images it the same bank, and you can then replace the 0 in the end by 1,2,3 etc. by the appropriate image number, solving your issues

break exits for... look at this code snippet:

int n
for n=0 to 10

print n
if n=5 then break

next

print "for is over"


if you run it it will output:
0
1
2
3
4
5
for is over



you should use the command reference, available via the help menu - you should use it, it's of great help! it describes all commands, including 'sprite', which you misunderstood

06-22-2006 14:45

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

i forgot to mention... in this case 'break' exits for, but if it was placed in 'while', 'repeat' or another block construct it would exit it either (well i haven't tested them but that's supposed to ;)

06-22-2006 16:20

Posted by:
MT86

Location:
US

Find more posts by MT86

Its working great! I'm going to try to add enemies and keep score. Thanks for all the help and good luck with your programming projects

06-22-2006 17:20

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

glad i could help you :) good look with your project and if you have more questions, don't hesitate!

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.