Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: hellninjas on October 19, 2011, 02:43:10 pm

Title: Bullet Coding
Post by: hellninjas on October 19, 2011, 02:43:10 pm
OK I need help with adding a code into my source for shooting bullets out of a ship on the screen using the 2nd button... Help please!
Title: Re: Bullet Coding
Post by: shmibs on October 19, 2011, 02:55:03 pm
what language are you using?
Title: Re: Bullet Coding
Post by: hellninjas on October 19, 2011, 02:55:33 pm
Axe :D
Title: Re: Bullet Coding
Post by: chattahippie on October 19, 2011, 03:00:45 pm
To create an array of say, 30 bullets, use
Code: [Select]
For(A,0,29)
0->{L1+A}->{L1+1+A}->{L1+2+A}->{L1+3+A}
End
This creates thirty bullets with x,y of 0,0 and are not moving
Then, you can store better coordinates to the x,y, and the x and y velocities

To move them, simply add the x velocity, whether or not it is negative, to the x coordinate, and the same for the y
Code: [Select]
For(A,0,29)
{L1+A}+{L1+2+A}->{L1+A}
{L1+1+A}+{L1+3+A}->{L1+1+A}
End

Hope this helps, and be warned, while messing around with this, you may crash your calc, so archive stuff you don't want deleted :P

Title: Re: Bullet Coding
Post by: Keoni29 on October 19, 2011, 03:11:34 pm
create bullet
Code: [Select]
:Lbl CREATE
:For(r3,0,9  //maximum of 10bullets on screen
:!If {r3*3+L1}
:1->{r3*3+L1}
:r1->{r3*3+L1+1}
:r2->{r3*3+L1+2}
:10->r3       //escape from loop
:End
:End

destroy bullet
Code: [Select]
:Lbl DESTR
:0->{r1+L1}

update
Code: [Select]
:Lbl UPDAT
:For(r1,0,9
:If {r1*3+L1}
:{r1*3+L1+1}++  //when it moves horizontally
:If 96          //outside the screen borders
:Sub(DESTR,r1
:End
:End
:End

You will have to come up with your own collision code since we are no magicians.
Title: Re: Bullet Coding
Post by: Darl181 on October 19, 2011, 03:27:27 pm
What's kind of tricky about bullets in general is removing them from the array safely--when a bullet in the middle of the array needs to be removed.  What I ended up doing for Essence was a Copy() to bring the data for the bullets after the one being deleted.  Also you'll probably want some sort of variable to tell how many bullets there are.
I'm not the best at explaining things :P
There's some code that does it here (http://piratepad.net/ep/pad/view/gSHUMP/rev.1684) and a tutorial explaining a different method which is pretty helpful to read through here (http://www.omnimaga.org/index.php?action=articles;sa=view;article=61) and here (http://www.omnimaga.org/index.php?action=articles;sa=view;article=60).
Hope this helps :)
Title: Re: Bullet Coding
Post by: Yeong on October 19, 2011, 03:33:28 pm
I thought DT had a tutorial for this
Title: Re: Bullet Coding
Post by: Darl181 on October 19, 2011, 03:34:05 pm
Both of the tutorials are his :P
Title: Re: Bullet Coding
Post by: calc84maniac on October 19, 2011, 03:46:00 pm
What's kind of tricky about bullets in general is removing them from the array safely--when a bullet in the middle of the array needs to be removed.  What I ended up doing for Essence was a Copy() to bring the data for the bullets after the one being deleted.
Actually, it's a lot easier to copy the final bullet into the position of the deleted bullet and then decrease the bullet count :D
Title: Re: Bullet Coding
Post by: Deep Toaster on October 19, 2011, 03:51:23 pm
Moved to the correct forum.

And calc84maniac, that would make the code smaller and faster, but if you're currently looping through the array for other purposes, it could cause problems. On the other hand all you would have to do would be to stay at the same position in the array after copying the last element back, but honestly I didn't think of that XD
Title: Re: Bullet Coding
Post by: AngelFish on October 19, 2011, 04:12:41 pm
Here's another way to do bullets in Axe. I assumed that the array would be stored in L1, but L1 can be changed to the pointer of your choice pretty trivially. Note that 0,0 is assumed to be an impossible coordinate. That can likewise be changed to -1 or something if it conflicts with your program.

Initialize array:
Code: [Select]
:Lbl INI
:Fill({L1},2*<max number of bullets>+1,0)

Add bullet
Code: [Select]
:Lbl ADD
:!If {L1}-10
:.10 is max number of bullets
:X->{L1+{L1}+1}
:Y->{L1+{L1}+2}
:End

Destroy bullet
Code: [Select]
:Lbl DESTR
:Copy(L1+{L1},Ptr to cell,2)
:{L1+{L1}}-1->{L1}
:Return

Update bullet array and print bullets onscreen.
Code: [Select]
:Lbl UPD
:.Store X Increment in U
.Y increment in V
:For(A,1,{L1}
:{L1+A}+U->{L1+A}
:{L1+A+1}+V->{L1+A+1}
:Pt-ON({L1+A},{L1+A+1},<Sprite data>)
:End
:DispGraph^rr
:Return
Title: Re: Bullet Coding
Post by: LincolnB on October 19, 2011, 04:44:22 pm
What's kind of tricky about bullets in general is removing them from the array safely--when a bullet in the middle of the array needs to be removed.

What I do is just go ahead and remove it, regardless where it is in the array. Then, when the user presses [2ND], I search through the array and put a new one wherever it fits. So every iteration of the main game loop, I move the bullets and display them and do collision checks with objects and such.
Title: Re: Bullet Coding
Post by: Deep Toaster on October 19, 2011, 05:21:22 pm
Yeah, there are several ways to do an array in Axe.
Title: Re: Bullet Coding
Post by: Builderboy on October 19, 2011, 05:53:29 pm
What's kind of tricky about bullets in general is removing them from the array safely--when a bullet in the middle of the array needs to be removed.

What I do is just go ahead and remove it, regardless where it is in the array. Then, when the user presses [2ND], I search through the array and put a new one wherever it fits. So every iteration of the main game loop, I move the bullets and display them and do collision checks with objects and such.

This is also very very useful if you use the address of the bullets elsewhere, since with this method the address of the bullets never change
Title: Re: Bullet Coding
Post by: calc84maniac on October 19, 2011, 07:31:45 pm
What's kind of tricky about bullets in general is removing them from the array safely--when a bullet in the middle of the array needs to be removed.

What I do is just go ahead and remove it, regardless where it is in the array. Then, when the user presses [2ND], I search through the array and put a new one wherever it fits. So every iteration of the main game loop, I move the bullets and display them and do collision checks with objects and such.

This is also very very useful if you use the address of the bullets elsewhere, since with this method the address of the bullets never change
Same with my method :)

Edit: Wait a second, never mind. The bullet at the end of the list moves.
Title: Re: Bullet Coding
Post by: C0deH4cker on October 20, 2011, 04:46:56 pm
Would the new tables coming out in axe be useful for this?
Title: Re: Bullet Coding
Post by: Deep Toaster on October 20, 2011, 06:52:59 pm
This would be pretty much what's it's best for :o