LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Shooting 360 degrees ?!

Root / Programming Questions / [.]

PixelStudioCreated:
Hello everyone I'm making a top down shooter zombie game and I need a bit help implementing bullets. My method the bullet spawns at players X and Y but just stays there ?? any help how? PS. I also want to know how to do 360 degree shooting...

When I made my shooter (terrible shooter) I used arrays for the bullets coordinates and direction and if they're alive or not. Really complicated method I used. I also used the buttons as if they were dual stick. Anyways, for your "bullets moving" problem, maybe the premade samples could be at your service. My method is really garbage.

Hmm ok

I would still like to check out your code, PS the only garbage code is one that's empty

You should store a vector for each of your bullet (magnitude, angle). Also, the angle you're facing with your character(Get it using atan(vy/vx) ). Then, when you create the bullet, give as argument the angle of the player. Finally, when you render the bullet you should calculate the next position of the bullet with this: x=x+magnitude*cos(angle) y=y+magnitude*sin(angle)

Hmm OK, mind making a tutorial and posting the key? If you have time though

I'm going to upload an example on weekend.

OK thanks

OK, I made an example with 190 lines.(A lot of them are comentaries). It's a ship that can shoot one bullet. It can turn 360 degrees and the bullet travel foward from it. It only can shoot one bullet at time because I didn't want to make a pool. Here is the key 433EPXG3 If you have any question, ask them on this thread.

OK I will and thanks

OK, I made an example with 190 lines.(A lot of them are comentaries). It's a ship that can shoot one bullet. It can turn 360 degrees and the bullet travel foward from it. It only can shoot one bullet at time because I didn't want to make a pool. Here is the key 433EPXG3 If you have any question, ask them on this thread.
YOUR KEY WONT WORK\( >A<)/

Here is a demo I made back when SmileBasic was young. No key, you will have to type it in yourself. I typed this back into the computer manually, so there may be type-os or bugs, let me know.
OPTION STRICT

VAR SCREEN_W% = 400, SCREEN_H% = 240
VAR HERO%, T1%, T2%, DT%, GAME_OVER% = FALSE

ACLS

HERO% = CREATE_HERO(SCREEN_W% / 2, SCREEN_H% / 2, 0.0)

LOCATE 0, 0
PRINT "B = EXIT - Y = THROW SHOVEL - SLIDE PAD = ROTATE"

T2% = MILLISEC - 15
WHILE !GAME_OVER%
 T1% = T2%
 T2% = MILLISEC
 DT% = T2% - T1%
 CALL SPRITE
 VSYNC
 GAME_OVER% = ((BUTTON() AND #B) != 0)
WEND

END

DEF CREATE_HERO(X%, Y%, HEADING#)
 VAR T_HERO% = 682, SP%

 SPSET T_HERO% OUT SP%
 IF SP% != -1 THEN
  SPOFS SP%, X%, Y%
  SPHOME SP%, 8, 8
  SPROT SP%, DEG(HEADING#)
  SPVAR SP%, 0, 0 'COOL DOWN
  SPFUNC SP%, "UPDATE_HERO"
 ENDIF
 RETURN SP%
END

DEF CREATE_SHOVEL(X%, Y%, HEADING#)
 VAR T_SHOVEL% = 117, SHOVEL_SPEED# = 350.0

 SPSET T_SHOVEL% OUT SP%
 IF SP% != -1 THEN
  SPVAR SP%, 0, X%
  SPVAR SP%, 1, Y%
  SPVAR SP%, 2, HEADING#
  SPVAR SP%, 3, SHOVEL_SPEED#

  SPHOME SP%, 8, 8
  SPOFS SP%, X%, Y%
  SPROT SP%, DEG(HEADING#)
  SPFUNC SP%, "UPDATE_SHOVEL"
 ENDIF
 
 RETURN SP%
END

DEF UPDATE_HERO
 VAR COOLDOWN_MAX% = 100, X%, Y%, SP%
 VAR B%, SX#, SY#, COOLDOWN%, HEADING#

 COOLDOWN% = SPVAR(CALLIDX, 0)
 COOLDOWN% = MAX(0, COOLDOWN% - DT%)

 B% = BUTTON()
 STICK OUT SX#, SY#
 HEADING# = ATAN(-SY#, SX#)
 SPROT CALLIDX DEG(HEADING#)

 IF (B% AND #Y) != 0 AND COOLDOWN% <= 0 THEN
  COOLDOWN% = COOLDOWN_MAX%
  SPOFS  CALLIDX OUT X%, Y%
  SP% = CREATE_SHOVEL(X%, Y%, HEADING#)
 ENDIF
 SPVAR CALLIDX, 0, COOLDOWN%
END

DEF UPDATE_SHOVEL
 VAR X# = SPVAR(CALLIDX, 0)
 VAR Y# = SPVAR(CALLIDX, 1)
 VAR HEADING# = SPVAR(CALLIDX, 2)
 VAR SPEED# = SPVAR(CALLIDX, 3)
 VAR STEP# = ((SPEED# * DT%) / 1000.0)
  X# = X# + (STEP# * COS(HEADING#))
  Y# = Y# + (STEP# * SIN(HEADING#))
 IF X# < 0 OR X# > SCREEN_W% OR Y# < 0 OR Y# > SCREEN_H% THEN
  SPCLR CALLIDX
 ELSE
  SPVAR CALLIDX, 0,  X#
  SPVAR CALLIDX, 1,  Y#
  SPOFS CALLIDX, X#, Y#
 ENDIF
END
Ok, let's talk about the code for a bit. First off lets reiterate what was posted previously. For each projectile, we are going to want a pair of 2d vectors one cartesian and one polar. The cartesian vector (x, y) is for the projectile's current position on screen. The second polar vector (angle, magnitiude) will be which direction the projectile is headed, and how quickly (in pixels/per second for this example). You get change in position by adding magnitude * cos(heading) for the x-coordinate and magnitue * sin(heading) for the y coordinate. (We are in geometry/trigonometry class at this point, but if you just want to remember that part, you are good for now). When we apply that we can have our projectile move in a straight line at any angle. You may notice, that I don't have an array for all of the projectiles. I am using two nice smilebasic features to get around that. The first is sprite variables (SPVAR) and the other is sprite functions (SPFUNC). Each sprite gets 7 (0-6) floating point variables to use as desired. For this example 0, and 1 are the x,y coordinate of the projectile, and 2, and 3 are the heading and speed. A sprite callback function should have no parameters. You get the ID of the sprite the function is called for by using the system global variable CALLIDX (call index). The other thing you need to do is in the main game loop put in the command CALL SPRITE. Call sprite will call all of the call back functions for each sprite that has one. Also note, that we only have so many sprites, so I check to see if SPSET returned -1 as an error code and only set up the sprite if it didn't. I also set the home position for all the sprites at 8, 8 so they rotate around their centers. The code is old so I filled it with % modifiers for integer variables and # for floating point variables. It should work if you leave them off. You will also note that COS, and SIN want radians, and SPROT wants degrees so I do a fair amount of setting things back and forth. I suppose I should note that I am trying to get the time between each frame and storing/calculating it with T1 (Time 1,), T2 (Time 2), and DT (Delta Time). I them multiply the speed of the projectile by Delta Time/1000 (milliseconds in a second). That way, if one frame is slower or faster than the next, things should still move as smoothly as I can get them to. I forgot to mention the cool down, things get silly pretty quickly if you fire out a projectile each frame, so I put in a cool down timer for the shoot function. It goes down by DT (delta time) milliseconds each frame. When you fire you set it to maximum cooldown, when it reaches 0 or less you can fire again. It can be fun to play with that variable. So, in your own code, you can use a lot of the same code. You will probably want to add in an option to pass in which image to use for the projectile based on if it is was fired by the hero or an enemy. You should probably also add a flag to say which type of shot it is. That way when you add collision detection, you can tell if something should cause damage or not. Don't want your hero/enemy character(s) being killed by their own fire power/friendly fire. Adding in collision detection also means setting up SPCOL in the CREATE_SHOVEL function. Collision does work better if specify ranges so you still may want to go with the array solution so you can use a custom sprite ID allocator so that all friendly and enemy shots are in a continuous range. That should require less collision checks overall. Anyway, good luck and let me know if you have any questions.