Computer Science Canada let tha shootsting begin!! |
Author: | rollerdude [ Fri Jun 01, 2007 9:13 am ] | ||
Post subject: | let tha shootsting begin!! | ||
well... i tried to make a program thats shoots bullet thingys(which are really just lines) fine, so i got just one going... fine, but when i tried to make mutiple bullets at the same time... it screws up... here's mine code
|
Author: | DifinityRJ [ Fri Jun 01, 2007 9:24 am ] |
Post subject: | Re: let tha shootsting begin!! |
I know of a really primative way to do , but i think you can perhaps make it better when your coding. Make 2 arrays to hold your bullet x pos and bullet y pos, or just make a 2d array. (Your choice) Then make it so when you click or press a button on your keyboard, it goes into a procedure. In the procedure you need a variable, lets call it 'changer' and you add 1 to it, per how many seconds (the delay of the shooting). You will also need a boolean to know if that certain bullet is shot. So then it will look a little like this bulletshot(changer):=true Then somewhere down your program you will have if bulletshot(a) = true then add the velocity to your bullet x and or bullet y. so it might look like this bulletx(a)+=Velocity_X(a) bullety(a)+=Velocity_Y(a) end if Note: this is incomplete, you will need to reset your bulletx once its hit something or its gone to far. And as well make bulletshot = false again. I am sure there are many easier and much more efficient ways to do it, but i can't think of any at the moment, haven't had much experience with it. You can probably play with it and come up with better ways. I quickly made this. Code : proc GunCache if chars (KEY_CTRL) then Shooting := true shootallowed += 1 if shootallowed > 0 and shootallowed < 2 then changer += 1 vx (changer) := Rand.Real + Velocity vy (changer) := Rand.Int (0, 2) bulletshot (changer) := true if changer = NumOfBullets then changer := 0 end if elsif shootallowed > 4 then shootallowed := 0 end if else Shooting := false end if for a : 1 .. NumOfBullets if bulletshot (a) = true then bulletcachex (a) += vx (a) bulletcachey (a) += vy (a) end if if bulletshot (a) = false then bulletcachex (a) := x bulletcachey (a) := y end if if bulletcachex (a) > maxx or bulletcachex (a) < 0 then bulletshot (a) := false bulletcachex (a) := x vx (a) := 0 vy(a):=0 end if end for end GunCache |
Author: | CodeMonkey2000 [ Fri Jun 01, 2007 5:17 pm ] | ||
Post subject: | Re: let tha shootsting begin!! | ||
It helps if you are experienced with vectors and trig. First create a type of record, called vector. Now what kind of things would a vector need? You need a start point point, let's call them sX and Sy. You need an end point, call them eX and eY. You need the velocety, call that vX and vY. And finally you need an angle. (note: the length of each bullet will be 5 and speed is 3 ) Now we need to figure out the angle between two points. You need to know trig for this. Make this a function, so we can keep using it later. Now take in the mouse coordinates, and check if the user has clicked the mouse. If they did, then create a new bullet. Let the new bullet's start point be at the origin (0,0). Now figure out the angle. The end points of the bullet will be 5 units away from the start point, and looking towards the mouse (use trig to figure out the end position). We know that the speed is 3, but it is in the direction of the mouse (again use trig to figure out vX,vY) Now we move the bullet, then it and repeat the process. Here's an example:
Now we have a problem, there is no way of getting rid of the bullets once they go off screen. I'm not going to tell you how to do that, you'll need to figure that out on your own. |
Author: | DifinityRJ [ Fri Jun 01, 2007 6:24 pm ] |
Post subject: | Re: let tha shootsting begin!! |
I just started learning Trig, and no idea about vectors. But one question, if you change the start (x) and the start (y) to 100 both. And you aim your mouse in the bottom left corner, it doesn't shoot the bullet there. It only shoots to the upper right corner. Can you explain please? |
Author: | CodeMonkey2000 [ Fri Jun 01, 2007 6:41 pm ] |
Post subject: | RE:let tha shootsting begin!! |
We haven't learned trig in school yet either. I just googled sine, cos and tan. As for that error, I forgot to include it in my post. I'm not going to tell you how to solve it. You should be able to solve it yourself (even if you don't know trig, google it). |
Author: | Cervantes [ Fri Jun 01, 2007 7:16 pm ] |
Post subject: | Re: let tha shootsting begin!! |
DifinityRJ @ Fri Jun 01, 2007 9:24 am wrote: Make 2 arrays to hold your bullet x pos and bullet y pos, or just make a 2d array. (Your choice) Nooooo! Use records for this. It makes so much more sense. If you do it your way, what have you got? You've got all the data you need, but there is little structure to it. You could easily flip things around or swap only parts of bullets.... With a record, things are much more well defined. If I understand your question correctly, rollerdude, you should check out the Flexible Arrays tutorial. THere's an example of just this located there. |
Author: | rollerdude [ Mon Jun 04, 2007 12:09 pm ] |
Post subject: | Re: let tha shootsting begin!! |
*sigh*, i can make the bullet fire, and disappear and all that good stuf, just how do make multiple ones without having do like it does in my code?.... processes just dont seem to work.... |
Author: | Mazer [ Mon Jun 04, 2007 1:45 pm ] |
Post subject: | RE:let tha shootsting begin!! |
Processes could work, but it's best that you avoid them anyways. (if, however, you don't give a crap anyways you could try searching for a tutorial about "drawcheck" from about four years ago). Ideally you'll have your flexible array of records representing the bullets (or even better, classes, but whatever) which you'll iterate through in each iteration of the main loop. For each bullet, you'll move it a little bit in whatever direction it's going , see if it collided with anything, and draw it as necessary. You sould only be using View.Update once in your main loop. |
Author: | Marshmellow01 [ Tue Jun 05, 2007 10:09 pm ] |
Post subject: | RE:let tha shootsting begin!! |
i am also stuck on making the bullets thing i can create a bullet but it just stays on the ship it wont move i used Inpu.KeyDown to triger the bullet to spawn but how do i make it move up XD i been stuck on this for about 3 days and project due in 2 days T_T some one help me XD |
Author: | Mazer [ Tue Jun 05, 2007 10:16 pm ] |
Post subject: | RE:let tha shootsting begin!! |
The same way you make anything move. Represent the location of the object with x y coordinates. Change the x and y values by some small amount each iteration of your loop. Movement. You'd be better off making your own topic, though. Also, spending more time explaining your problem and less on emoticons. "it wont move" tells me almost nothing about what you've done wrong. |
Author: | rollerdude [ Thu Jun 07, 2007 8:07 am ] |
Post subject: | Re: let tha shootsting begin!! |
oh i see, make a proc that not so much makes the bullet do the entire movement, just to move it to the next position... i see thnx a lot, i shall try und see if this works! |
Author: | DIIST [ Thu Jun 07, 2007 12:28 pm ] | ||
Post subject: | Re: let tha shootsting begin!! | ||
CodeMonkey2000, your sample code crashes when you point directly up, 90 degrees from the x axis. Note:
(x1-x2)->0 when your pointing up. This will cause a division by zero error. You need to take that into account when you write your function ![]() |
Author: | CodeMonkey2000 [ Thu Jun 07, 2007 3:04 pm ] |
Post subject: | RE:let tha shootsting begin!! |
I know. It was fixed in my main game. |
Author: | DIIST [ Thu Jun 07, 2007 5:06 pm ] |
Post subject: | Re: RE:let tha shootsting begin!! |
CodeMonkey2000 @ June 7th 2007 wrote: I know. It was fixed in my main game. Sorry, didn't notice. ![]() |