forums

TNT Basic Forums > Programming Q&A
sprite col and sprite off
< Last Thread     Next Thread >
Author
Thread        Post A Reply

11-25-2005 02:21

Posted by:
marty samuels

Click Here to Email marty samuels   Find more posts by marty samuels

Hello

I'm trying to do a space invaders type game. I have the aliens
in a formation on the top of the screen. I have a mouse controlled
ship that shoots a bullet when the mouse button is pressed. When
I press the mouse button, before the bullet even hits the aliens,
two aliens are removed from the formation, but are still on the screen.
I can't figure out how the mouse button causes a collision and how
aliens = aliens - 1 causes two aliens to be removed from the formation
and remain on the screen. I have included my code. Could someone
please look at it and tell me what I am doing wrong?

Thanks

hide mouse
graphics mode 800,600
draw picture 129
load images 128
global int bulletx=mouse x, bullety=mouse y,fire=false,speedx=3
global int shipx,shipy,alieny[16],alienx[16],a,n,aliens=15
createaliens()
while pressed(12) = false

'*****shots******
if mouse button and fire = false
fire=true
bulletx=mouse x+14:bullety=mouse y+10
end if
if fire =true
sprite 1,bulletx,bullety,1
bullety=bullety-10
if bullety<-20 then fire = false
end if

'********* aliens ********
for n=0to aliens
a=n + 2

alienx[n]=alienx[n]+speedx
sprite a,alienx[n],alieny[n],2

next n
for n=0 to aliens
if alienx[n] then if alienx[n] > 780 then speedx=-3
if alienx[n] then if alienx[n] < 20 then speedx=3
next n
'********** collisions ***********
for n = 0 to aliens
if sprite col (1,n) <> -1
sprite off n
sprite off 1
aliens = aliens - 1
end if
next


'********* ship ********
shipx=mouse x
if shipx>750 then shipx=750
shipy=mouse y
if shipy>550 then shipy=550
sprite 0, shipx,shipy,0
draw frame
wend

gameover()
procedure createaliens() '**********

for n=0 to aliens
a=n + 2
if n<8
alienx[n]=n*40
alieny[n]=65
else
alienx[n]=(n-8)*40
alieny[n]=90
end if

sprite a,alienx[n],alieny[n],2
next n

end proc
procedure gameover() '**************

text mode
show mouse
end proc

11-25-2005 07:15

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Got it...

quote:

'********** collisions ***********
for n = 0 to aliens
if sprite col (1,n) <> -1
sprite off n
sprite off 1
aliens = aliens - 1
end if
next



You are testing sprites 0 through aliens for a collision with sprite 1, but sprite 0 is the mouse and sprite 1 is the bullet... Put your 'a=n+2' thing back (as you do when you draw the aliens) and you should be fine...

Danny (nods)

11-25-2005 14:51

Posted by:
marty samuels

Click Here to Email marty samuels   Find more posts by marty samuels

Thanks for the help. It is easy to overlook something
and it helps having a second person look at it.


Thanks

11-25-2005 15:32

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

No worries...

You get that a lot, just needs a fresh pair of eyes on it. You could always try doing "for n=2 to aliens+2", makes it a bit clearer and takes a calculation out of your loop. Speaking of which, you perform 3 for loops to control the aliens, you could compress that down to a single loop as well, would speed things up and make it easier on the eye:

'********* aliens ********

for n=2 to aliens+2

alienx[n]=alienx[n]+speedx
sprite a,alienx[n],alieny[n],2

if alienx[n] > 780 then speedx=-3
if alienx[n] < 20 then speedx=3

if sprite col (1,n) <> -1
sprite off n
sprite off 1
aliens = aliens - 1
end if

next


Just a thought...

Danny (nods)

11-26-2005 01:00

Posted by:
marty samuels

Click Here to Email marty samuels   Find more posts by marty samuels

Thanks for the tip. I took out two of the for-next loops.
I also made the aliens 0-15 to make things easier,the
ship is 16 and the bullet 17. I used an array a[16] for
the aliens. Now I can't get the collisions to work, so I'd
like to ask you to look at my code again and see why
my collisions are not working. I have made several
changes to my code so I am posting it again.

hide mouse
graphics mode 800,600
draw picture 129
load images 128

global int shipx=mouse x,shipy=mouse y,alieny[16],alienx[16],n
global int bulletx, bullety,fire=false,speedx=3,a[16]


createaliens()

while pressed(12) = false


'*****shots******
if mouse button and fire = false
fire=true
bulletx=mouse x+14:bullety=mouse y+10
end if
if fire =true
sprite 17,bulletx,bullety,1
bullety=bullety-10
if bullety<-20 then fire = false
end if

'********* aliens ********
for n=0to 15

alienx[n]=alienx[n]+speedx
sprite a[n],alienx[n],alieny[n],2

if alienx[n] > 780 then speedx=-3
if alienx[n] < 20 then speedx=3

'********** collisions ***********
if sprite col (17,a[n]) <> -1
sprite off a[n]
sprite off 17

end if
next


'********* ship ********
shipx=mouse x
if shipx>750 then shipx=750
shipy=mouse y
if shipy>550 then shipy=550
sprite 16, shipx,shipy,0


draw frame

wend

gameover()
procedure createaliens() '**********


for n=0 to 15
a[n]=n
if n<8
alienx[n]=n*40
alieny[n]=65
else
alienx[n]=(n-8)*40
alieny[n]=90
end if

sprite a[n],alienx[n],alieny[n],2

next n

end proc

procedure gameover() '**************

text mode
show mouse
end proc

11-26-2005 01:12

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

No probs...

quote:

'********** collisions ***********
if sprite col (17,a[n]) <> -1
sprite off a[n]
sprite off 17



Think you lost the plot a bit here. What's 'a[n]'? Well you don't want to check it whatever it is, just try 'if sprite col (17, n) <> -1' and I think you'll have it sussed. Probably just a 'typo' again...

Danny (nods)

11-26-2005 01:24

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Re-read the code...

Just lose 'a[n]', you don't need it. a[n] is always equal to n. No point keeping it around...

Danny (nods)

11-26-2005 13:38

Posted by:
marty samuels

Click Here to Email marty samuels   Find more posts by marty samuels

Thanks again. You have been a big help.

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.