Author Topic: help with bullets!  (Read 6408 times)

0 Members and 1 Guest are viewing this topic.

Offline saintrunner

  • Custom Spriter: You ask it! I'll Make it!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1787
  • Rating: +115/-11
  • Flogging Molly
    • View Profile
    • Jonny K Music
help with bullets!
« on: November 30, 2011, 06:12:57 pm »
So I'm working on a game, and I want to have bullets. I used bullets in Mario shotgun, but I want to do a better job of it this time, but I don't really know how. Any way I posted my source code below and I'd really appreciate any help with showing me where to put in a 'bullet' code, and what it should look like :) I already have the sprites for the bullets (picBL->bullet to the left) (picBR->to the right)



:.SEEKER
:DiagnosticOff
:[1824183CDB182466]→Pic1M
:[0000000707000000]→Pic1BR
:[000000C0C0000000]→Pic1BL
:2→X→A:50→Y→B:0→J
:ClrDraw
:Line(0,63,95,63)
:Line(0,0,95,0)
:Line(0,0,0,63)
:Line(95,0,95,63)
:Lbl 1
:Repeat getKey(15)
:¦ If getKey(2) and (X≠1)
:¦ X-1→X
:End
:If getKey(3) and (X+8≠95)
:X+1→X
:End
:If getKey(4) and (pxl-Test(X,Y+8)
:15→J
:End
:If (J>0)
:Y-1→Y
:J-1→J
:End
:If (pxl-Test(X,Y+8)=0) and (pxl-Test(X+7,Y+8)=0) and (J=0)
:Y+1→Y
:End
:Pause 25
:Pt-Change(X,Y,Pic1M)
:DispGraph
:Pt-Change(X,Y,Pic1M)
:End
My Sprites Thread   :Updated often :) for your viewing pleasure

GAMES:

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: help with bullets!
« Reply #1 on: November 30, 2011, 07:21:05 pm »


I originally typed this to help out hellninjas, but I decided to give it to you since I already took the time to type it :P

This is actually longer than the one I gave him.


My setup is when a bullet's tasks are done, the bullet check changes. For example,


When the bullet check = 0
That means a new bullet is ready to be shot


When the bullet check = 1
That means the bullet's coordinates and left/right need to be set.


When the bullet check = 2
That means to move the bullet.

Now, to start the real programming.

Pick a variable for your for loops. I used V.
Pick L1, or L2, or L3, etc. I used L5.

Im taking this mostly from my game. These bullets will be shooting left and right, not up and down, so if that's what your game needs, you will have to tweak this code.

Outside the main loop,


:.The sprite.
:[FCFC000000000000]-> Pic2
:For(V,0,5)
:0->{V*4+L5}
:End

In my for loop, I put 5 there to say what the max possible bullets on the screen would be.
{V*4+L5} will be my bullet check. That sets the bullet check to zero.
As you can see by what I previously wrote, when the bullet check is zero, it means the bullet is ready to be fired.


:25->S->T

Actually, this also checks if a bullet to be fired. If only {V*4+L5} the only check, they would all fire at once. S and T will be what spaces the bullets. T will gain 1 every time the loop finishes and when it reaches S, which is set to 25, a bullet will be ready to be fired.

So together {V*4+L5} and S/T are what checks if a bullet can be fired. If you'd like to make it shoot faster, lower S and T. But where it says
For(V,0,5)
If you speed the bullets up you'll have to increase the 5 so that L5 wont run out of bullets!


:Repeat getkey(15)
:If getKey(54)
:For(V,0,5)
:If T=S
:.Checks if T=S so that the bullets are spaced out
:!If {V*4+L5}
:.If the bullet check is 0...


Above, I have the calculator find a bullet that is ready to be fired.

Now that we found a bullet, you have to change the bullet check to 1 and reset the spacer.

:.Change bullet check
:1->{V*4+L5}
:.Turns T back to 0 and it will have to go back up to 25 (S) to fire again
:0->T
:.Exits this loop now since we already have our bullet
:Goto X
:End
:End
:End
:End
:Lbl X


As I said before, when the bullet check is 1 it means the bullet's coordinates and left/right need to be set.


:For(V,0,5)
:If {V*4+L5}=1
:.If bullet check = 1
:.Set the coordnates of the player to the bullet
:X->{V*4+L5+1}
:Y->{V*4+L5+2}
:.Put it on the screen
:Pt-On({V*4+L5+1},{V*4+L5+2},Pic2
:.Turn on the bullet check to 2 now.
:2->{V*4+L5}
:.You should have a variable that tells if the character is left or right. Save that to the bullet like as shown here
:R->{V*4+L5+3}
:End
:End



If you dont have a variable that says if the character is facing left or right, add one!
Also where I have said
X->{V*4+L5+1}
Y->{V*4+L5+2}
Tweak that to exactly where you want the bullet to start, or it'll just start at the top left of your player.

Here, this will move the bullet, which is when the bullet check equals 2.

:For(V,0,5)
:If {V*4+L5}=2
:.Checks if the bullet check is 2...
:If {V*4+L5+3}
:.If facing right..
:{V*4+L5+1}+2->{V*4+L5+1}
:Else
:.If its not,
:{V*4+L5+1}-2->{V*4+L5+1}
:End
:.Show the bullet
:Pt-On({V*4+L5+1},{V*4+L5+2},Pic2)
:End
:End


Here, I have what checks if the bullet is off the screen and then resets the bullet.


:For(V,0,5)
:If {V*4+L5+1}>90
:.If the bullet is off the screen (Works for both left and right side)
:.Set the bullet checks back to zero and put the coordnates off the screen to not interfere
:0->{V*4+L5}+100->{V*5+L5+1}->{V*5+L5+2}
:.Now the bullet check is back to zero to start the cycle over again.
:Return
:End
:End


Don't forget to make T change so that bullet spacing will work!


:.Make T move up to S for the bullet spacing
:If T < S
:T++
:End


And that's it for bullet moving. Here's some collisions now!
In this example, A and B are the enemy's X and Y coordinates.


:For(V,0,5)
:.Collision detect
:If abs({V*4+L5+1}-A)<6
:If abs({V*4+L5+2}-B)<6
:. I'll use C for the enemy's health
:C--
:0->{V*4+L5}+100->{V*5+L5+1}->{V*5+L5+2}
:. The same thing as before, to reset the bullet
:End
:End
:End


It's the same idea for multiple enemies
Here, P and L1 are for the enemies and
{P*2+L1) = X coordinate
{P*2+L1+1) = Y coordinate


:For(V,0,5)
:For(P,0,3)
:. Has to check a For( for the enemies and the bullets
:. Collision detect
If abs({V*4+L5+1}-{P*2+L1})<6
If abs({V*4+L5+2}-{P*2+L1+1})<6
:. I'll use P+L2 as the enemy's health
:{P+L2}--
:. Remove one from the health
:0->{V*4+L5}+100->{V*5+L5+1}->{V*5+L5+2}
:. Bullet reset
End
End
End
End
End


OK! Now here's the whole code.
The only thing it's missing is the player.
Add the player and you'll be good to go!
Dont forget to tweak
X->{V*4+L5+1}
Y->{V*4+L5+2}


:[FCFC000000000000]->Pic2
:For(V,0,5)
:0->{V*4+L5}
:End
:25->S->T
:Repeat getkey(15)
:ClrDraw
:If getKey(54)
:For(V,0,5)
:If T=S
:!If {V*4+L5}
:1->{V*4+L5}
:0->T
:Goto X
:End
:End
:End
:End
:Lbl X
:For(V,0,5)
:If {V*4+L5}=1
:X->{V*4+L5+1}
:Y->{V*4+L5+2}
:Pt-On({V*4+L5+1},{V*4+L5+2},Pic2
:2->{V*4+L5}
:R->{V*4+L5+3}
:End
:End
:For(V,0,5)
:If {V*4+L5}=2
:If {V*4+L5+3}
:{V*4+L5+1}+2->{V*4+L5+1}
:Else
:{V*4+L5+1}-2->{V*4+L5+1}
:End
:Pt-On({V*4+L5+1},{V*4+L5+2},Pic2
:End
:End
:For(V,0,5)
:If {V*4+L5+1}>90
:0->{V*4+L5}+100->{V*5+L5+1}->{V*5+L5+2}
:Return
:End
:End
:If T < S
:T++
:End
:For(V,0,5)
:For(P,0,3)
If abs({V*4+L5+1}-{P*2+L1})<6
If abs({V*4+L5+2}-{P*2+L1+1})<6
:{P+L2}--
:0->{V*4+L5}+100->{V*5+L5+1}->{V*5+L5+2}
End
End
End
End
End
DispGraph
End



« Last Edit: November 30, 2011, 07:22:03 pm by epic7 »

Offline saintrunner

  • Custom Spriter: You ask it! I'll Make it!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1787
  • Rating: +115/-11
  • Flogging Molly
    • View Profile
    • Jonny K Music
Re: help with bullets!
« Reply #2 on: November 30, 2011, 07:26:32 pm »
Thanks Epic, that helps :)

edit: Wait, can you help me with this?

The way I did mario shotgun had the bullets like the code below, but If you've played it you'd realize that when you shoot, then shoot again the bullet jumps back to the gun. I want to make it so when you shoot twice, it shoots 2 bullets seperate. Is there a way to modify THIS code to do that?

:.SEEKER
:DiagnosticOff
:[1824183CDB182466]→Pic1M
:[0000000303000000]→Pic1BR
:[000000C0C0000000]→Pic1BL
:2→X:50→Y:0→J
:ClrDraw
:Line(0,63,95,63)
:Line(0,0,95,0)
:Line(0,0,0,63)
:Line(95,0,95,63)
:Line(0,50,30,50)
:Lbl 1
:Repeat getKey(15)
:If getKey(54)
:X→A:Y→B:1→S
:End
:If (S=1)
:A-2→A
:End
:If (S=1)
:Pt-Change(A,B,Pic1BL)
:End
:If getKey(55)
:X→D:Y→E:1→T
:End
:If (T=1)
: D+2→D
:End
:If (T=1)
:Pt-Change(D,E,Pic1BR)
:End
:If getKey(2) and (X≠1)
:X--
:End
:If getKey(3) and (X+8≠95)
:X++
:End
:If getKey(4) and (pxl-Test(X,Y+8)
:15→J
:End
:If (J>0)
:Y--
:J--
:End
:If (pxl-Test(X,Y+8)=0) and (pxl-Test(X+7,Y+8)=0) and (J=0) or (pxl-Test(X+3,Y)=1)
:0→J
:Y++
:End
:Pause 25
:Pt-Change(X,Y,Pic1M)
:DispGraph
:Pt-Change(X,Y,Pic1M)
:If (S=1)
:Pt-Change(A,B,Pic1BL)
:End
:If (T=1)
:Pt-Change(D,E,Pic1BR)
:End
:If (A≥95)
:0→S
:End
:If (D≥95)
:0→T
:End
:End


Edit2: Nevermind I fixed it, And for the record my code is a lot shorter then yours I think, considering mine includes character movement and jumping lol, but thanks again 8)
« Last Edit: November 30, 2011, 08:22:14 pm by saintrunner »
My Sprites Thread   :Updated often :) for your viewing pleasure

GAMES:

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: help with bullets!
« Reply #3 on: November 30, 2011, 08:24:36 pm »
Are a and d both for different bullets?

Offline saintrunner

  • Custom Spriter: You ask it! I'll Make it!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1787
  • Rating: +115/-11
  • Flogging Molly
    • View Profile
    • Jonny K Music
Re: help with bullets!
« Reply #4 on: November 30, 2011, 08:25:22 pm »
Yeah, and I know how to optimize that to like just A, but I'll do that later
My Sprites Thread   :Updated often :) for your viewing pleasure

GAMES:

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: help with bullets!
« Reply #5 on: November 30, 2011, 08:31:17 pm »
My code is longer because it controls a lot of  bullets and has collision detects.

But I bet someone could still omptimize it :P
« Last Edit: November 30, 2011, 08:42:20 pm by epic7 »

Offline saintrunner

  • Custom Spriter: You ask it! I'll Make it!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1787
  • Rating: +115/-11
  • Flogging Molly
    • View Profile
    • Jonny K Music
Re: help with bullets!
« Reply #6 on: November 30, 2011, 08:42:04 pm »
True, and mine has unlimited bullets also, I fixed that, and I don't need collision yet :) But I o appreciate the help!
My Sprites Thread   :Updated often :) for your viewing pleasure

GAMES:

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: help with bullets!
« Reply #7 on: November 30, 2011, 09:19:15 pm »
Yes, bullets=difficult :P

Offline saintrunner

  • Custom Spriter: You ask it! I'll Make it!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1787
  • Rating: +115/-11
  • Flogging Molly
    • View Profile
    • Jonny K Music
Re: help with bullets!
« Reply #8 on: November 30, 2011, 09:30:52 pm »
But not impossible! And they aren't really that hard, just depends on what you want them to do :)
My Sprites Thread   :Updated often :) for your viewing pleasure

GAMES:

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: help with bullets!
« Reply #9 on: November 30, 2011, 09:32:12 pm »
I thought they were pretty easy. I just figured it out and then I was fine :P :P