forums

TNT Basic Forums > Programming Q&A
How to shoot
< Last Thread     Next Thread >
Author
Thread        Post A Reply

08-25-2003 22:42

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

I got a problem. I have some code and I'm trying to make a spaceship shoot with just a simple if statement like this. Im not done, but, I'm just trying to get one direction first. This is the code:
if pressed (49)
sprite 3,shot1x,shot1y,8
if player1a = 1 ' if player is facing left
shot1x = shot1x-3
if shot1x <= 0
sprite off
end if
shot1x = player1x
shot1y = player1y
end if
if player1a = 3
shot1x = shot1x +3
if shot1x >=800
sprite off
end if
sprite off
shot1x = player1x
shot1y = player1y
end if
end if
Can you tell me what I should do instead? Or how to fix it.
Thanks
Alex

08-25-2003 23:48

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

sorry, I forgot to say the problem. When i shoot, it doesn't move unless i hold the button, and stay in the direction I was facing. Also, it won't really turn off at the end. And, how do I make multiple shots. Just change the index or what. How do I continue to make it go after I let go of the space bar etc. Thanks for your help.

08-26-2003 05:58

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

quote:
When i shoot, it doesn't move unless i hold the button

That's because the code only executes if you're pressing the spacebar. What you need to do is make a variable that tells if the bullet is on. If you press the spacebar and the bullet isn't already on, you turn the bullet variable on and create the bullet sprite. In your game loop, check if the bullet is on. If it is, move it.


quote:
and stay in the direction I was facing.

The way you have it now, I assume the bullet goes in the direction you face, even if it was going the other way before.
To make the bullet go in different directions, just make a bullet velocity variable that tells the amount it moves on the x axis every frame. If they are facing right and shoot it, make the velocity positive. If they are facing left and shoot it, make the velocity negative. Only set the velocity when you shoot.

quote:
Also, it won't really turn off at the end.

I don't know what you mean, but I noticed you are using sprite off without passing a sprite id, meaning it turns off all the sprites. Is that what you want?

quote:
And, how do I make multiple shots. Just change the index or what.

Yeah pretty much. Just make all of your shot variables into arrays so you can keep track of each one individually.

Now here is another thread that already went over shooting bullets:
http://www.tntbasic.com/community/forums/showthread.php?threadid=515

Hope this helps

08-26-2003 13:31

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

Thanks. Sorry I wasn't more clear. I did add the sprite off, but the thing was, if I turned in another direction, the same bullet would go in that direction. Thanks for the help and the link. I've been doing some modifications so I have to try it out now.

08-26-2003 18:45

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

I'm still having problems. Here is my whole code:

Graphics Mode 800,600
load images 128
Load Input 128
int player1x = 300
int player1y = 200
int player1direction = 0 ' which way the player 1 is facing, basically, which graphic
int player2x = 100
int player2y = 100
int player2direction = 4 ' which way the player 2 is facing, basically, which graphic
int shot1x = player1x
int shot1y = player1y
int fire = false,shot1x,shot1y
int DirectionOfShot1
while not mouse button
Sprite 0,player1x,player1y,player1direction
sprite 1,player2x,player2y,player2direction
if pressed (124) ' move player 1 right
player1direction = 3
player1x=player1x + 5
draw frame
end if
if pressed (2) ' move player 2 right
player2direction = 7
player2x = player2x + 5
draw frame
end if
if pressed (125) ' move player 1 down
player1direction = 2
player1y=player1y + 5
draw frame
end if
if pressed (1) ' move player 2 down
player2direction = 6
player2y = player2y + 5
draw frame
end if
if pressed (123) ' move player 1 left
player1direction = 1
player1x=player1x - 5
draw frame
end if
if pressed (0) ' move player 2 left
player2direction = 5
player2x = player2x - 4
draw frame
end if
if pressed (126) ' move player 1 up
player1direction = 0
player1y=player1y-4
draw frame
end if
if pressed (13) ' move player 2 up
player2direction = 4
player2y = player2y - 5
draw frame
end if
if pressed (49) ' if the space is pressed
fire = true
shot1x = player1x
shot1y = player1y
sprite 4,shot1x,shot1y,DirectionOfShot1
if player1a = 3 then directionOfShot1 = 9
if player1a = 1 then DirectionOfShot1 = 9
if player1a = 0 then DirectionOfShot1 = 8
if player1a = 2 then DirectionOfShot1 = 8
end if
if fire = true
if player1a = 1' if facing left
shot1x = shot1x - 6 'move it
end if
if player1a = 3' if facing right
shot1x = shot1x + 6 ' move it
end if
if player1a = 0 ' if facing up
shot1y = shot1y - 6 ' move it
end if
if player1a = 2 ' if facing down
shot1y = shot1y = 6 ' move it
end if
if shot1x <= 0
sprite off 9
end if
if shot1x >= 800
sprite off 9
end if
if shot1y <= 0
sprite off 8
end if
if shot1y >= 600
sprite off 8
end if
end if

if pressed (12) ' quit the game
end
end if
if player1y >= 600 ' check to see if player 1 has hit the bottom side and move to top
player1y = 1
end if
if player2y >= 600 ' check to see if player 2 has hit the bottom side and move to top
player2y = 1
end if
if player1x >= 800 ' check to see if player 1 has hit the right side and move to left
player1x = 1
end if
if player2x >= 800 ' check to see if player 2 has hit the right side and move to left
player2x = 1
end if
if player1x <= 0 ' check to see if player 1 has hit the left side and move to right
player1x = 799
end if
if player2x <= 0 ' check to see if player 2 has hit the left side and move to right
player2x = 799
end if
if player1y <= 0 ' check to see if player 1 has hit the top side and move to bottom
player1y = 599
end if
if player2y <= 0
player2y = 599
end if
draw frame
wend

PLease help and thank you in advance

Alex

08-26-2003 22:14

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

First, you only need one draw frame in the loop. You don't need to draw the frame once when a key is pressed and again at the end of the loop.

Second, you haven't declared player1a. I assume that it is supposed to be player1direction right?

When you check to see which direction you should move the shot, check the shot direction, not the player direction, because once the shot has been fired the player can turn around and the shot will go in that direction. Once the shot is fired, the bullet's direction should not change.

I can't tell what you are trying to do in your code. It seems like there are two different bullets: one that travels on the x axis and one that travels on the y axis. I think you would be better off making it the same bullet.

I'll post a sample up for you later.

08-27-2003 01:08

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

I sent an example to the guys at TNT and hopefully they will approve of hosting it for me.

08-27-2003 13:25

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

If they can't, send it to me: on all my FTP accounts I think I have about 175 mb of free space!

08-27-2003 18:01

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

One, there are 2 bullets. Sorry about player1a. I modified my code so that player1a was the direction so it would be easier to understand and I guess I missed a few. Yes, there are 2 bullets because the bullets are rectangles and if you shoot up, the bullet iis longer in the y axis then the x. So it is with the x except x is bigger. The point is just a simple game where two people going around trying to hit the other person with a buller.

Alex

08-27-2003 18:03

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

One more thing. THe shot direction IS the plaer direction until it leaves, so how would I do that?

08-27-2003 18:04

Posted by:
EvolPenguin

Location:
Chicago

Click Here to Email EvolPenguin   Find more posts by EvolPenguin

Oh, I see what you mean. Ignore last post.

08-28-2003 05:37

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Mathew: I haven't recieved a reply yet, so I'll email you the file now for EvolPenguin's sake. It's only 16k compressed!

Thanks

Eric

08-28-2003 14:20

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

OK, look at http://TNTbasic_files.site.voila.fr/ . The download link should work. I haven't tested it, due to some conexion problem.

08-28-2003 14:21

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

> haven't tested it, due to some conexion problem.

Never mind, it works.

08-28-2003 17:31

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Cool. Hopefully it will answer all those questions I see about shooting stuff.

10-12-2003 08:11

Posted by:
grymmius

Location:
Uppsala, Sweden

Click Here to Email grymmius   Find more posts by grymmius

I'm also doing a thing with shooting, I created two variables, numberof1bullets and numberof2bullets, then I check their size. if they are more than 0, I run a moan command.

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.