Computer Science Canada Help with shooting |
Author: | mike200015 [ Sat Apr 30, 2005 3:13 pm ] | ||
Post subject: | Help with shooting | ||
im working on an addition to my blockout game, when you press space, bullets shoot from the paddle, and i want them to destroy 1 block and then dissapear, and i want rapid fire. I did this so far, but i'm not sure how to get more bullets going at the same time, right now when you press Space, 1 bullet goes, and when you press Space again, the bullet dissapears and another one shoots. I think i have to do something with flexible arrays, but not quite sure what exactly. Heres the code:
|
Author: | Cervantes [ Sat Apr 30, 2005 8:44 pm ] |
Post subject: | |
Yep, flexible arrays is the key here. Whenever you hit the fire button, you create a new element of the flexible array. The flexible array should be of a record that contains things such as x and y positions, and x and y velocities or velocity and angle etc. In your main loop, you have a for loop from 1 .. upper (bullets) to update the position of all the bullets. You'll also have to check for collisions with them, and also you'll probably want to put a range limit on them. Say, if they go 500 pixels, delete them. That would be done by using startX and startY variables. You could then Math.Distance, though that's rather slow. Or you could just run a few if statements. In any case, check the flexible arrays tutorial, if you don't know how to use them. If you do, check through the code I've included; it's a good example as to how to do this kind of thing. |
Author: | mike200015 [ Sat Apr 30, 2005 10:55 pm ] | ||
Post subject: | |||
ok.. thanx.. i got the flexible array to work, but now i have 2 problems: 1. The bullet draws a line sort of, cuz it stretches is when i hold the space.? 2. I have a problem with the collision, i cant figure out how to do it, im using Math.DistancePointLine but i dunno where to put it cuz i gotta check the array of blocks and the array of bullets at the same time but they have 2 different upper values, so i cant really do it in a for loop?? ![]() And 1 more question, how can i get it so i can hold space and it will delay like 1 second and shoot the next bullet so i dont havto keep pressing space? Heres my new code:
|
Author: | Cervantes [ Sun May 01, 2005 7:53 am ] | ||
Post subject: | |||
1. What I think you're asking is the same as your "And 1 more question" question. I think the problem is that you push space and a bunch of bullets fire off at the same time. You want a cooldown for your weapon. Once again, check the code in the flexible array tutorial; it has an example of this as well. 2. You could use two for loops:
But let's think about that for a minute. We probably don't care about anything other than the first few bullets, depending on your cooldown time. We could maybe use upper (bullet) - 1 .. upper (bullet). Also, we only need to check collisions with the outside blocks. Using a 2D array for your blocks would make this much easier. A question: are the blocks going to ever move around in this game? Or do they stay in a fixed position? If the stay fixed, you could only deal with the upper few bullets and only deal with the outside blocks. You also have to consider the question: do your blocks have spaces between them? If so, and if they move, then you'd have to check collisions for ALL the blocks. Follow my logic? |
Author: | mike200015 [ Sun May 01, 2005 11:36 am ] |
Post subject: | |
yea.. im thinking that i can use an if statement around the second for loop, and only check the blocks, if the bullet in the for loop is actually moving, i have a hits record for each bullet and set it to 1 when a bullet hits a block, so i could make an if about if the bullet is onscreen and has not hit a block, then go to the seconf for loop |
Author: | mike200015 [ Sun May 01, 2005 11:42 am ] | ||
Post subject: | |||
ok.. so this is what i did for the detection, the only thing, it makes the game run very very slow, and one more thing, the bullet doesnt stop after hitting 1 bullet?? And, How would i make it go faster?? Side Note: Cervantes, i read your tutorial, and it says you cant use flexible arrays with types, but in my game i did, is that bad? EDIT -- In your tutorial, Cervantes, you have that timeDelay function , do you mind if i use that in my game with my bullets?, i tried it out in my game and it worked to only shoot one bullet at a time, and it gets rid of the problem of hitting more than 1 block. Code:
|
Author: | Cervantes [ Sun May 01, 2005 3:58 pm ] | ||
Post subject: | |||
Yep, that does go very, very slowly. What kind of graphic are you going to use for the bullet in the final product? Ovals, lines? box? If you use lines or a box, you could ditch the whole Math.DistancePointLine, because you'd only care about what's above the bullet, not anything to the side or below. Also, don't compare with all the blocks, only the outside ones. Try to switch your blocks to use a 2D array. About flexible arrays and types: what I meant is you can't do this:
About the timeDelay function: absolutely! ![]() I hope you have no intention of using that process. ![]() |
Author: | mike200015 [ Sun May 01, 2005 5:05 pm ] | ||
Post subject: | |||
I'm planning on using a cirlce, like what i have now, thats y im using the Math.DistancePointLine, if i switch to a line or square, would it help speed up the game?? And 2ndly, how can i only check the outside blocks? ![]() And last, what would making the blocks a 2D array do? Heres my updated code: EDIT -- I switched my circle to a Draw.ThickLine and it looks pretty good, and helps it from lagging 2 much. What do you guys think?.. looks ok? Also, how do i get the line not to show up all the time, i only want it to show when u press space, right now you can see the bullet constantly, ontop of the paddle.?
|
Author: | Cervantes [ Sun May 01, 2005 7:48 pm ] |
Post subject: | |
Yes, looks like switching away from ovals sped the game up. Check only the outside blocks by making the blocks into a 2D array. That way, you only need to check the upper element of each column. Provided your blocks will always remain stationary, you won't even need to check the side blocks. As for seeing the bullet constantly, I'm not sure (haven't looked at code), but I'll run over the basis: your flexible array is from 1 to 0 when there's no bullets on the screen. Whenever the fire key is pressed (and also when cooldown is true) create a new element of the flexible array. Drawing should be as simple as a for loop from 1 to upper (bullet) and a draw command (think line, in this case). |
Author: | mike200015 [ Sun May 01, 2005 8:14 pm ] |
Post subject: | |
at the begining, my flexible array is 1..1 cuz if i do 1..0 then it gives me an error of array subscript out of range, run my game if u can, and change the 1..1 to 1..0 and ull c wat i mean. And as for the 2d array, i should scrap wat i have now about how my blocks are drawn, and change it? ![]() |
Author: | mike200015 [ Mon May 02, 2005 5:35 pm ] | ||
Post subject: | |||
ok.. i got my flexible array thing fixed, and now it doesnt show all the time. The next thing i was trying to do, is what you have, Cervantes, in your little program you made for flexible arrays, to delete the upper array after the bullet hits a block. It works, but only if 1 bullet is shot, if you shoot 2 bullets, once the 1st bullet hits a block, i get an error that array subscript is out of range. Could you check it out, and see what i did wrong? Heres the new code: ( I also added missiles to the game, and that is Shift to shoot them)
|
Author: | Cervantes [ Mon May 02, 2005 6:09 pm ] | ||
Post subject: | |||
The reason for this is as follows: We are going to examine this section of code:
Let's assume all those if statements are true. We go all the way into it, and we delete that particular bullet. Then we exit the for loop that checks the blocks, and restart at the top of the bullet's for loop. But now we've got one less bullet, and yet x has still increased. Thus, x is now > upper (bullet), because we decreased upper (bullet). To fix this, check out the tut about Removing an Element from a Flexible Array within a For Loop. It's not a great tutorial, mind you, because it was really a question I posted in Help that got kinda got transformed into a tutorial. I don't even know the best solution to the problem: as you'll soon find out, I took Bacchus idea, and fixed it so it would work in a few more situations than his original idea did. But by doing this I also used lots of cpu processing, which is really bad in your case. In any case, read the tutorial, and maybe you can spend some time on the problem and post the best solution. ![]() |
Author: | mike200015 [ Mon May 02, 2005 9:06 pm ] |
Post subject: | |
k.. ill check out the tutorial, thanx! And also, about the 2D array for the blocks that you said to do, how should i change my blocks now to make it like you suggested? ![]() |
Author: | mike200015 [ Mon May 02, 2005 9:37 pm ] | ||
Post subject: | |||
i took out the line
it prevents the error of array subscript out of range coming up, but then the last bullet of the array doesnt stop, it destroys all the blocks in its row. |
Author: | mike200015 [ Mon May 02, 2005 9:54 pm ] | ||
Post subject: | |||
nevermind, i got it to work, i jus added the suggestion that Bacchus gave in your question, i added in:
![]() The only thing still, is that i thought by doing the whole thing with deleting the upper, it would prevent my game from slowing down, but it doesnt really, i dont think. After i shoot alot of bullets, then it slows down my game permanently. How would i prevent this from happening? |
Author: | Bacchus [ Tue May 03, 2005 5:54 am ] |
Post subject: | |
prevent them from shooting that much ![]() |
Author: | Cervantes [ Tue May 03, 2005 9:39 am ] |
Post subject: | |
I don't know about the game slowing down. I don't think you can use the free command with flexible arrays (only with classes and collections). I would have thought that Turing would automatically free some memory up when you delete an element of the array. Are you absolutely certain that this is the cause of your program slowing down? |
Author: | mike200015 [ Tue May 03, 2005 4:18 pm ] |
Post subject: | |
i realized the game didnt slow down after shooting alot of bullets, ![]() ![]() Anyways, I needed to delete bullets on screen that havent hit anything yet, at another point during my game, and i used the free command and it worked to delete it from the screen. I jus wanted to know if the free actually deletes the bullet from the array. For example, my array is at 1..0 at beginning, and i shoot a bullet, and my array is at 1..1, if i use free, will it now make my array 1..0 again? |
Author: | Bacchus [ Tue May 03, 2005 6:03 pm ] |
Post subject: | |
use new again to altar the array back, think it should delete everything |
Author: | mike200015 [ Tue May 03, 2005 7:21 pm ] | ||
Post subject: | |||
what i need to do is set my array again to 0, so i should do:
can i set the upper to a certain number using new?.. or is new only for adding / subtracting? Cuz i dunno how many to subtract, so i need to just set it to 0 |
Author: | mike200015 [ Tue May 03, 2005 7:24 pm ] | ||
Post subject: | |||
oo.. do i do this, to set upper as something: ?
|
Author: | Cervantes [ Tue May 03, 2005 7:27 pm ] |
Post subject: | |
That's correct. |
Author: | mike200015 [ Tue May 03, 2005 8:58 pm ] |
Post subject: | |
wats better to do.. the free command, or the new command? Also, im attaching my game in a zip, which is the blockout game with bullets and ball.. Please post your comments about what you think of it, as well.. if anyone knows how to fix the ball and paddle collision, so that the ball doesnt slide sometimes on the paddle, I'd really appreciate it. |
Author: | jamonathin [ Wed May 04, 2005 7:00 am ] |
Post subject: | |
Game looks good. I never ran into that paddle slide thing problem. Ball moves pretty fast though, or maybe Im just bad at pong ![]() ![]() |
Author: | mike200015 [ Wed May 04, 2005 4:33 pm ] |
Post subject: | |
![]() |
Author: | mike200015 [ Wed May 04, 2005 4:34 pm ] |
Post subject: | |
Question : How can i check the full circumference of the ball, right now it just checks the top bottom and sides.?? |
Author: | Cervantes [ Wed May 04, 2005 7:42 pm ] |
Post subject: | |
Check the full circumfrence? Use the formula for a circle: (x - h)^2 + (y - k)^2 = r^2. Of course, you don't want EVERY point. You probably really only want four points. It's probably easiest to just do a few more if statements. |
Author: | mike200015 [ Wed May 04, 2005 7:45 pm ] |
Post subject: | |
i need to check more than the four points that i'm checking now, because, when the ball hits the paddle at an angle, on the corner of the paddle, the ball wont bounce off, it just sort of goes through the paddle, and hits the bottom of the screen. And also with the blocks, when the ball hits the corner of a block, it just goes through it. |
Author: | Chaos_Simon [ Sat May 07, 2005 7:43 pm ] |
Post subject: | ... |
very inerestng...but for some reason it dosenst work well on my computer...why? |