forums

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

12-21-2003 17:33

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

I've tried to understand the basics about shooting and i think i get it.. but i dont really know how to actually make it work!
the game is pretty simple. it's a Invaders clone. you are a tank at the bottom of the screen, shooting the ufo's at the top of the screen.
so... if someone could help me out a little, i'd be glad!

fade down
graphics mode hardware 640,480

hide mouse
set framerate 30

load images 128
load images 129
load sounds 128

'shot
set pen colour red
fill oval 0,0 to 2,2
copy image 0,0,2,2,white,true to 2

set pen colour black
paint canvas
draw picture 129


fade up

global int win = 0
global int playerx=300,speed=3
global int targetx,targety

global int shotstart=240
global int shotmax=20
global int shotx[shotmax]
global int shoty[shotmax]
global int shoton[shotmax]

repeat

'some pathetic bullet code!?!?
if pressed(49)
shoton[shotmax]=1
end if
if shoton[shotmax]=1
shotmax=shotmax-1
sprite 2,shotx[shotmax],shoty[shotmax],2
end if

' draw sprites
'player
sprite 0,playerx,410,1
'target
sprite 1,playerx+20,20,0
'shot


draw frame


'input
'move player
if pressed(123)'leftarrow
playerx=playerx-speed
playsnd=1
snd=001
else if pressed(124)'rightarrow
playerx=playerx+speed
Playsnd=1
snd=001
else
playerx=playerx
end if


until win=1

draw frame
wait mouse click

12-22-2003 02:43

Posted by:
Miles

Location:
Australia

Click Here to Email Miles   Find more posts by Miles

Try replacing this:

if pressed(49)
shoton[shotmax]=1
end if
if shoton[shotmax]=1
shotmax=shotmax-1
sprite 2,shotx[shotmax],shoty[shotmax],2
end if

With this:

***

if pressed(49)
if playerfires=false ' waits until the player has released the fire key
playerfires=true ' before being able to fire again
FireBullet(playerx,playery-10,-4) ' creates a bullet with the FireBullet
end if ' procedure.
else
playerfires=false
end if
DrawBullets() ' draws the bullets on-screen with the
' DrawBullets procedure. this should be called
' once per frame only.

Procedure FireBullet(int bulletx,int bullety,int bulletspd)
' the first value is the bullet's x pos, then ypos, then speed.
' 'speed' refers to how much the y position is modified per frame.
' shooting upwards would be a negative value, downwards a positive one.

int n=-1
repeat
n=n+1
until (n=shotmax-1) or (shoton[n]=false) ' repeats until a bullet slot
' not being used is found
shoton[n]=true
shotx[n]=bulletx ' adds the bullet data to that slot
shoty[n]=bullety
shotspd[n]=bulletspd
End Proc

Procedure DrawBullets()
int n
for n=0 to shotmax-1 ' goes through all the bullet indexes
if shoton[n]=true ' if one exists...
shoty[n]=shoty[n]+shotspd[n] ' then add the speed to the y pos...
Sprite 10+n,shotx[n],shoty[n],2 ' and draw it.
if not (point in rect ( shotx[n], shoty[n], 0,0 to 640,480))
shoton[n]=false ' if the bullet is no longer within the
end if ' bounds of the screen, then remove it.
end if
next
End Proc

***

It should, in theory, work... but I've probably made a mistake somewhere.

Anyway, the advantage of using a procedure to create object data (like a bullet with FireBullet) is you can easily change the values to create different objects without having to copy the code again. also, if you want to change something, you can just modify the procedure, and not every time it appears in the code.

So, if you wanted to make it so that the alien fires as well, all you'd have to do it add is:

FireBullet (ufo x,ufo y,4)

And to change the speed of the bullet, just modify the third value.

The DrawBullet procedure doesn't check for collisions with sprites yet. You'd probably want to add that at one point.

Oh yeah, and you'll need to add PlayerFires and ShotSpd[shotmax] on your list of globals.

12-26-2003 20:19

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

Thanks!

really helpful stuff!
i've debugged it some but it wont work!
it says
" Line# : 34
Code : global int shotmax=20
Error : -116
"
'--The code--
fade down
graphics mode hardware 640,480


hide mouse
set framerate 30

load images 128
load images 129
load sounds 128

'shot
set pen colour red
fill oval 0,0 to 2,2
copy image 0,0,2,2,white,true to 2

set pen colour black
paint canvas
draw picture 129

paint canvas
set pen colour black
frame rect 0,0 to 640,480

fade up

global int win = 0
global int playerx=300,playery,speed=3
global int targetx,targety


global int n=-1
global int playerfires
global int shotmax=20
global int shotx[n],shoty[n]
global float shoton[n]
global float shotspd[n]
'--------------------------

repeat
'input
'move player
if pressed(123)=true 'leftarrow
playerx=playerx-speed
else if pressed(124)=true 'rightarrow
playerx=playerx+speed
end if



'shooting
if pressed(49)
if playerfires=false ' waits until the player has released the fire key
playerfires=true ' before being able to fire again
FireBullet(playerx,playery-10,-4) ' creates a bullet with the FireBullet
end if ' procedure.
else
playerfires=false
end if
DrawBullets() ' draws the bullets on-screen with the
' DrawBullets procedure. this should be called
' once per frame only.

Procedure FireBullet(int bulletx,int bullety,int bulletspd)
' the first value is the bullet's x pos, then ypos, then speed.
' 'speed' refers to how much the y position is modified per frame.
' shooting upwards would be a negative value, downwards a positive one.


repeat
n=n+1
until (n=shotmax-1) or (shoton[n]=false) ' repeats until a bullet slot
' not being used is found
shoton[n]=true
shotx[n]=bulletx ' adds the bullet data to that slot
shoty[n]=bullety
shotspd[n]=bulletspd
End Proc

Procedure DrawBullets()
int n
for n=0 to shotmax-1 ' goes through all the bullet indexes
if shoton[n]=true ' if one exists...
shoty[n]=shoty[n]+shotspd[n] ' then add the speed to the y pos...
Sprite 10+n,shotx[n],shoty[n],2 ' and draw it.
'if not (point in rect ( shotx[n], shoty[n], 0,0 to 640,480))
shoton[n]=false ' if the bullet is no longer within the
end if ' bounds of the screen, then remove it.
'end if
next
End Proc




draw frame


until win=1
draw frame
wait mouse click



12-29-2003 13:19

Posted by:
-wyvern

Location:
Bedroom

Find more posts by -wyvern

This sometimes happens; you get an invincible bug. If nothing else works you should create a new file, and manually code the angering bit again. If you can't be bothered then i've got a shooting tutorial in the directory, your welcome to it.

12-30-2003 01:21

Posted by:
Miles

Location:
Australia

Click Here to Email Miles   Find more posts by Miles

Umm, I think it's because you've declared shotx, shoty, etc. with an index of 'n', and 'n' was declared as -1.

You can't have a variable array declared with a negative number of indexes! ^_^

12-30-2003 01:53

Posted by:
Miles

Location:
Australia

Click Here to Email Miles   Find more posts by Miles

Shotx, shoty etc., should be declared with 'shotmax', shouldn't they?

12-30-2003 22:17

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

AArgh!...

Thanks for all the help! but i really cant seem to get it right!... can someone post a way to make this work or a working code!?.. i tried to set the ints to
global int shotx[shotmax],shoty[shotmax]
global int shoton[shotmax]
global int shotspd[shotmax]

but all i get is a screen in the same color as the 'bullet'

here is the newest version of the code...




fade down
graphics mode hardware 640,480


hide mouse
set framerate 30

load images 128
load images 129

'shot
set pen colour blue
fill oval 0,0 to 2,2
copy image 0,0,2,2,white,true to 2

'set pen colour black
'paint canvas
draw picture 129
paint canvas

fade up


global int win=1
global int playerx=300,playery,speed=3
global int targetx,targety
global int shotmax=20
global int n
global int fires
global int shotx[shotmax],shoty[shotmax]
global int shoton[shotmax]
global int shotspd[shotmax]

repeat
'input
'move player
if pressed(123)=true 'leftarrow
playerx=playerx-speed
else if pressed(124)=true 'rightarrow
playerx=playerx+speed
end if

draw frame

'shooting
if pressed(49)
if fires=false ' waits until the player has released the fire key
fires=true ' before being able to fire again
FireBullet(playerx,playery-10,-4) ' creates a bullet with the FireBullet
end if ' procedure.
else
fires=false
end if
DrawBullets() ' draws the bullets on-screen with the
' DrawBullets procedure. this should be called
' once per frame only.

Procedure FireBullet(int bulletx,int bullety,int bulletspd)
' the first value is the bullet's x pos, then ypos, then speed.
' 'speed' refers to how much the y position is modified per frame.
' shooting upwards would be a negative value, downwards a positive one.


repeat
n=n+1
until (n=shotmax-1) or (shoton[n]=false) ' repeats until a bullet slot
' not being used is found
shoton[n]=true
shotx[n]=bulletx ' adds the bullet data to that slot
shoty[n]=bullety
shotspd[n]=bulletspd
End Proc

Procedure DrawBullets()
for n=0 to shotmax-1 ' goes through all the bullet indexes
if shoton[n]=true ' if one exists...
shoty[n]=shoty[n]+shotspd[n] ' then add the speed to the y pos...
Sprite 10+n,shotx[n],shoty[n],2 ' and draw it.
'if not (point in rect ( shotx[n], shoty[n], 0,0 to 640,480))
shoton[n]=false ' if the bullet is no longer within the
end if ' bounds of the screen, then remove it.
'end if
next
End Proc



draw frame


until win=1
draw frame
wait mouse click

12-31-2003 12:05

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

it all seems very long winded, i use a for loop and a repeat, one to put bullets in their starting positions, and one to move them and takes 20 lines of code to do, i know you probably want to figure it out your self, but if you get dead stuck i could post it for you

good luck
charlie

12-31-2003 12:08

Posted by:
fjerkon

Location:
Sweden

Find more posts by fjerkon

Oyeah! i just cant find what's wrong!!! it would be great if you posted me your example! =)

12-31-2003 13:32

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

right, this code is from a shooter i started and never finished, ill include all the variables etc.

global int bulletexists[16],i,keyup,n,bs=20,hitsprite=false,bx[16],by[16]

if space and keyup=true
repeat
n=random(0,15)'you can change the number of shots here, just be sure to change the arrays
until bulletexists[n]=false
bulletexists[n]=true
bx[n]=x 'your main sprites x position
by[n]=y 'your main sprites y position

keyup=false
end if
if not space then keyup=true
for o=0 to 15
if bulletexists[ o ]=true
by[o]=by[o]-bs
sprite o,bx[o],by[o],2
if bx[o]>480 or by[o]<0
sprite off o
bulletexists[o]=false
end if
end if
next

theres not much by way of comments, but i figure youll work out whats what, also this was made at 640,480 res, you may want to change the height at which the bullets are turned off

and remember to change the number at the end of sprite o,bx[o],by[o],2
to the number of your bullet sprite

good luck
charlie

01-01-2004 08:35

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

I have a shooting example in the TNT directory if you want to take a look at it. I don't know how different it is from everyone else's examples but I commented it so it should be easy to adapt.

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.