let tha shootsting begin!!
Author |
Message |
rollerdude
![](http://compsci.ca/v3/uploads/user_avatars/2012907293465dc2862c598.jpg)
|
Posted: 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
code: |
% this is a simple program that will "shoot" for point (1,1) to where the mouse is clicked
setscreen ("offscreenonly")
var pic1 := Pic.New (0, 0, maxx, maxy)
Pic.Draw (pic1, 0, 0, picCopy)
var x, y, b, prevx, prevy, mousex, mousey : int := 1
var linex, liney : int
var angle : real
var distance : int
process shoot (x, y : int)
if x not= 0 then
angle := arctand (y / x)
else
angle := 90
end if
distance := round (sqrt ((x ** 2) + (y ** 2)))
for i : 1 .. distance
linex := round (1 + cosd (angle) * i)
liney := round (1 + sind (angle) * i)
drawline (linex, liney, round (linex - cosd (angle) * 10), round (liney - sind (angle) * 10), black)
View.Update
delay (1)
Pic.Draw (pic1, 0, 0, picCopy)
end for
end shoot
loop
Mouse.Where (mousex, mousey, b)
if b not= 0 then
drawdot (mousex, mousey, b)
fork shoot (mousex, mousey)
end if
delay (10)
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DifinityRJ
![](http://compsci.ca/v3/uploads/user_avatars/1665843594662f4952a669.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: 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:
Turing: | type vector :
record
Sx, Sy, Ex, Ey, vX, vY : real %sx,sy are the star point of the bullet. likewise ex,ey are the endpoints
angle : real
end record
function angle (x1, y1, x2, y2 : real) : real%take in two points, and find their angle
result arctand ((y1 - y2 ) / (x1 - x2 )) %tan inverse of opposite(which is deltaX) / adjecent(which is deltaY)
end angle
var myBullet : flexible array 0 .. 0 of vector %flexible arrays are like arrays, but you can control the sieze during runtime
View.Set ("offscreenonly")
var mx, my, mb : int
loop
Mouse.Where (mx, my, mb )
if mb > 0 then %if clicked then....
new myBullet, upper (myBullet ) + 1 %create a new bullet
%start point of the bullet
myBullet (upper (myBullet )).Sx := 0
myBullet (upper (myBullet )).Sy := 0
%angle
myBullet (upper (myBullet )).angle := angle (0, 0, mx, my )
%the end point of a bullet
myBullet (upper (myBullet )).Ex := cosd (myBullet (upper (myBullet )).angle ) * 5 /*this is the length of the bullet*/ + myBullet (upper (myBullet )).Sx
myBullet (upper (myBullet )).Ey := sind (myBullet (upper (myBullet )).angle ) * 5 /*this is the length of the bullet*/ + myBullet (upper (myBullet )).Sy
%the change in coordinates
myBullet (upper (myBullet )).vX := cosd (myBullet (upper (myBullet )).angle ) * 3 %the three is the speed. increase it to make the bullet faster, lower it to slow it down
myBullet (upper (myBullet )).vY := sind (myBullet (upper (myBullet )).angle ) * 3
end if
for x : 1 .. upper (myBullet )
%moves the bullet
myBullet (x ).Sx + = myBullet (x ).vX
myBullet (x ).Sy + = myBullet (x ).vY
myBullet (x ).Ex + = myBullet (x ).vX
myBullet (x ).Ey + = myBullet (x ).vY
%Draws the xth bullet
Draw.Line (round (myBullet (x ).Sx ), round (myBullet (x ).Sy ), round (myBullet (x ).Ex ), round (myBullet (x ).Ey ), red)
end for
View.Update
cls
end loop
|
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. |
|
|
|
|
![](images/spacer.gif) |
DifinityRJ
![](http://compsci.ca/v3/uploads/user_avatars/1665843594662f4952a669.jpg)
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: 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). |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
rollerdude
![](http://compsci.ca/v3/uploads/user_avatars/2012907293465dc2862c598.jpg)
|
Posted: 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.... |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Marshmellow01
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Mazer
![](http://compsci.ca/v3/uploads/user_avatars/1323750815476d9f446d80c.png)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
rollerdude
![](http://compsci.ca/v3/uploads/user_avatars/2012907293465dc2862c598.jpg)
|
Posted: 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! |
|
|
|
|
![](images/spacer.gif) |
DIIST
![](http://compsci.ca/v3/uploads/user_avatars/248212014b7d9f60741cd.jpg)
|
Posted: 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:
Turing: |
function angle (x1, y1, x2, y2 : real) : real%take in two points, and find their angle
result arctand ((y1 - y2 ) / (x1 - x2 )) %tan inverse of opposite(which is deltaX) / adjecent(which is deltaY)
end angle
|
(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 ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
CodeMonkey2000
|
Posted: Thu Jun 07, 2007 3:04 pm Post subject: RE:let tha shootsting begin!! |
|
|
I know. It was fixed in my main game. |
|
|
|
|
![](images/spacer.gif) |
DIIST
![](http://compsci.ca/v3/uploads/user_avatars/248212014b7d9f60741cd.jpg)
|
Posted: 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. ![Embarassed Embarassed](http://compsci.ca/v3/images/smiles/icon_redface.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|