forums

TNT Basic Forums > Programming Q&A
Help with Arrays
< Last Thread     Next Thread >
Author
Thread        Post A Reply

05-19-2008 17:58

Posted by:
shepherd150

Click Here to Email shepherd150   Find more posts by shepherd150

I cant make my space ship fire more than one bullet at a time!
I have been trying to use arrays but i cannot make it work can someone please help!

global float width=600, height=600
global float yposship= height/2,xposship=width/2
global float shipspeed
global float shippic=0, rotationship
global float rotateship=0
global float shipxdirect, shipydirect, shipspeed=3
global float bulletx[999], bullety [999]
global float bullspeed=6
global float bullrange=300
global int bullmove
global int bullnum, bullid[999]

window mode hardware width, height
load images 128
load sounds 128

playgame()

'mainscreen()






'procedure mainscreen ()
'int value= Poll Button Click
'
'new button 0, (0+width/4), height/2, 4, 5, 5, 128
'
'if value= 0
'
'wait button click
'
'
'clear buttons
'
'playgame ()
'
'end if
'
'draw frame
'wait mouse click
'end proc()





procedure playgame ()
while not mouse button
sprite 1, xposship, yposship, shippic

if left
rotateship= rotateship-2
set sprite rotation 1,rotateship

else if right
rotateship= rotateship+2
set sprite rotation 1,rotateship

end if

rotationship= sprite rotation (1)
shipxdirect= Cos(rotationship)
shipydirect= sin(rotationship)


if up

yposship= yposship-(shipydirect*-shipspeed)
xposship= xposship+(shipxdirect*shipspeed)

end if

if xposship<0
xposship= width
else if xposship>width
xposship=0
end if

if yposship<0
yposship= height
else if yposship>height
yposship= 0
end if

if space
bullnum= bullnum+1
shoot()
end if

if bullnum>999 then bullnum = 0




draw frame


wend

end proc ()

procedure shoot ()
int soundplaying
soundplaying= sound playing (0)
If soundplaying= false then play sound 0


bulletx[bullnum]=xposship
bullety[bullnum]=yposship
sprite bullid[bullnum], bulletx[bullnum] ,bullety[bullnum] , 1
move sprite bullid[bullnum], (shipxdirect*bullrange), (shipydirect*bullrange), bullspeed




'for bullnum=0 to 9
'bullmove= sprite moving (bullid[bullnum])
'
'if bullmove=0
' sprite off bullid[bullnum]
'end if





end proc ()

05-20-2008 00:54

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Your approach with 'bullnum' is probably not optimal

Thre may be as few ways to approach this, but one way I know to work is to (in pseudo-code, you'll need to adapt) :

- Create an array that indicates whether the shot is on or not

int shot_on[10]
(you most likely don't need 999 bullets on the screen at once... 10 at once is often enough, maybe a bit more but never 999)

- Before the game loop starts, when loading the level for instance, set them all to false

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

- When the user presses a key, check through the 10 bullets to find one that is currently unused

' I added a dummy time_since_last_bullet variable
' cause we probably don't want to shoot all bullets
' at once
if space and time_since_last_bullet>50
for n=0 to 9
if shot_on[n]=false
' great, we found a slot that is currently unused
' init this shot with appropriate values
shot_on[n]=true
shot_x[n] = main_character_x
shot_y[n] = main_character_y
' we have found one, no need to continue
' searching. so we can end this 'for' loop
break
end if
next

end if

- Then the code that manages the shots might look like this

for n=0 to 9
if shot_on[n]=true
' make shot move
shot_x[n] = shot_x[n] + 2
' display it
sprite 1000+n,shot_x[n],shot_y[n],6

' check if the shot left the screen
' if it did, disable the shot so that
' this slot can be re-used
if shot_x[n]<0 or shot_y[n]<0 or shot_x[n]>640 or shot_y[n]>480
' shot left screen, disable it
shot_on[n]=false
end if
end if
next

hope it helps
there is also a sample in the repository that does shooting

05-21-2008 00:16

Posted by:
shepherd150

Click Here to Email shepherd150   Find more posts by shepherd150

Thanks but

i used the sample code you showed me but i still cannot make it work!
i have no idea why

global float width=600, height=600
global float yposship= height/2,xposship=width/2
global float shipspeed
global float shippic=0, rotationship
global float rotateship=0
global float shipxdirect, shipydirect, shipspeed=3
global float bulletx[10], bullety [10]
global float bullspeed=60
global int bullmove, bull_on[10], bulltimer
global int bullid[10], n=0
global int soundplaying

while not mouse button

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

sprite 1, xposship, yposship, shippic

if left
rotateship= rotateship-2
set sprite rotation 1,rotateship

else if right
rotateship= rotateship+2
set sprite rotation 1,rotateship

end if

rotationship= sprite rotation (1)
shipxdirect= Cos(rotationship)
shipydirect= sin(rotationship)


if up

yposship= yposship-(shipydirect*-shipspeed)
xposship= xposship+(shipxdirect*shipspeed)

end if

if xposship<0
xposship= width
else if xposship>width
xposship=0
end if

if yposship<0
yposship= height
else if yposship>height
yposship= 0
end if










if space and bulltimer> 50

soundplaying= sound playing (0)
If soundplaying= false then play sound 0
for n=0 to 9
if bull_on[n]=false
bull_on[n]=true
bulletx[n] = xposship
bullety[n] = yposship
n=0
end if
break
next

end if




for n= 0 to 9
if bull_on[n]= true
sprite 900+n, bulletx[n], bullety[n], 1
bulletx[n]= bulletx[n] +(shipxdirect*bullspeed)
bullety[n]= bullety[n] -(shipydirect*-bullspeed)

end if
next

bulltimer= bulltimer+2



draw frame


wend

05-21-2008 15:23

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I fixed it and noted corrections in comments

window mode hardware 800,600
load images 128

global float width=800, height=600
global float yposship= height/2,xposship=width/2
global float shipspeed
global float shippic=0, rotationship
global float rotateship=0
global float shipxdirect, shipydirect, shipspeed=3
global float bulletx[10], bullety [10]
global float bullspeed=60
global int bullmove, bull_on[10], bulltimer
global int bullid[10], n=0
global int soundplaying

'init the shots *before* the main loop
' not on each frame
for n= 0 to 9
bull_on[n]=false
next


while not mouse button

sprite 1, xposship, yposship, shippic

if left
rotateship= rotateship-2
set sprite rotation 1,rotateship

else if right
rotateship= rotateship+2
set sprite rotation 1,rotateship

end if

rotationship= sprite rotation (1)
shipxdirect= Cos(rotationship)
shipydirect= sin(rotationship)


if up

yposship= yposship-(shipydirect*-shipspeed)
xposship= xposship+(shipxdirect*shipspeed)

end if

if xposship<0
xposship= width
else if xposship>width
xposship=0
end if

if yposship<0
yposship= height
else if yposship>height
yposship= 0
end if





if space and bulltimer> 50

'you need to reset the timer when shooting
bulltimer = 0


soundplaying= sound playing (0)
If soundplaying= false then play sound 0
for n=0 to 9
if bull_on[n]=false
bull_on[n]=true
bulletx[n] = xposship
bullety[n] = yposship
n=0
' break must be inside the if
' we must stop searching only when we inited a shot
break

end if
next

end if




for n= 0 to 9
if bull_on[n]= true
sprite 900+n, bulletx[n], bullety[n], 1
bulletx[n]= bulletx[n] +(shipxdirect*bullspeed)
bullety[n]= bullety[n] -(shipydirect*-bullspeed)

' you need to check if the bullet exits the screen otherwise you'll soon
' run out of shot slots
if bulletx[n] < -150 then bull_on[n] = false
if bullety[n] < -150 then bull_on[n] = false
if bulletx[n] > width+150 then bull_on[n] = false
if bullety[n] > height+150 then bull_on[n] = false

end if
next

bulltimer= bulltimer+2




draw frame


wend

05-25-2008 18:22

Posted by:
shepherd150

Click Here to Email shepherd150   Find more posts by shepherd150

Thanks

thanks soo much I really appreciate it

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.