Discord
Login
Community
DARK THEME

How do I add bullets

Hi id like to add bullets for my game where if you press space or click, my sprite shoots bullets in the direction it's facing. My sprite moves and turns on angles smoothly so idk how hard it is to add bullets for it but it's too advanced for me. Here is my player file:

initPlayer = function() player = object x = 0 y = 0 rotation = 0 speed = 2 rotation_speed = 4 end level_width = 1024 level_height = 512 end

updatePlayer = function()

if keyboard.LEFT then player.rotation += player.rotation_speed end

if keyboard.RIGHT then player.rotation -= player.rotation_speed end

if keyboard.UP then local direction = angleToVector(player.rotation) player.x += direction[0] * player.speed player.y += direction[1] * player.speed end

if keyboard.DOWN then local direction = angleToVector(player.rotation) player.x -= direction[0] * player.speed * 0.2 player.y -= direction[1] * player.speed * 0.2 end

player.x = clamp(player.x, - level_width/2 + 20, + level_width/2 - 20) player.y = clamp(player.y, - level_height/2 + 20, + level_height/2 - 20)

end

drawPlayer = function()

screen.setDrawRotation(player.rotation) screen.drawSprite("ship", player.x - camera.x,player.y - camera.y,15) screen.setDrawRotation(0)

end

I have a bullets file that is empty from trial and error and I have a bullet sprite called "bullet". Any help is greatly appreciated : )

Add an object/class called Bullet.

init = function()
  bulletList = []
  updateCount = 1000
  //create a bullet object so you can create a new bullet whenever a player fires
  Bullet = object
    constructor = function(x, y, velocity, direction, size, index)
      this.x = x
      this.y = y
      this.velocity = velocity
      this.direction = direction
      this.size = size
      this.index = index
    end
    
    //because all active bullets are stored in a list, we need to know which index it is so when it's removed we know which index in the list to remove.
    //when we remove a bullet, we also need to change the index of every proceeding bullet in the list.
    removeBullet = function()
      bulletList.removeAt(index)
      for i in bulletList
        if i.index > index then
          i.index -= 1
        end
      end
    end
    
    drawBullet = function()
      screen.setRotation(direction)
      screen.drawSprite("bullet", x, y, size, size)
    end
    
    moveBullet = function()
      x += velocity*cos(direction*180/pi)
      y += velocity*sin(direction*180/pi)
      //delete it if it goes offscreen. you should also do this if it collides with an enemy
      if abs(x)>screen.width/2 and abs(y)>screen.height/2 then
        removeBullet()
      end
    end
  end
end

update = function()
  updateCount += 1
  if updateCount > 5 then
    updateCount = 0
    //always add bullets to the end of the list and set the index to the bulletList length
    //this is because if we add it to the start, all of their indexes will be 0.
    bulletList.push(new Bullet(0, 0, random.nextInt(3)+0.5, random.nextInt(361), random.nextInt(6)+10, bulletList.length-1))
  end
  
  for v in bulletList
    v.moveBullet()
  end
end

draw = function()
  screen.clear()
  screen.setColor("#FFF")
  for v in bulletList
    v.drawBullet()
  end
  screen.setRotation(0)
end

There's a lot of extra code for display purposes, but basically, you create a bullet object and, whenever you create a new bullet, store it in a list. when you no longer need the bullet, you delete it -- that is, when it hits an enemy or something. In this case, its just deleting when it goes off-screen, because the code shouldn't run if its off-screen (BIG memory leak if you don't), but because we're messing with a list, we need to store the index inside the bullet so it knows which one it is, so it can delete it from the list. Because we're deleting things from a list, we have to tell each object in the list that we're doing that, or else it'll delete the wrong object (for example, if we deleted the 1st bullet, but didnt tell that to every other bullet, the bullet that thinks it has an index of 2 now actually has an index of 1 in the list, so when we try and delete that bullet, it tells the program to delete the bullet at index 2, when its actually at index 1 in the list. As you can imagine, that causes problems.)

The actual bullet functionality is pretty simple. Store the x, y, velocity, direction, size, and index of the bullet inside of itself. then, draw the bullet using it's x, y, and size, and set its rotation to be the rotation provided. when moving the bullet, we move it in it's direction times the velocity it has (using some simple trig, though oddly its in radians rather than degrees, even though screen.rotation is in degrees; we have to account for that, which is that extra math in the degrees), and then we remove the bullet from the list using the earlier-described method if it's offscreen. Adding a new bullet is as simple as creating a new object in the list like I did, and adding new functionality to it (such as enemy collision) is as simple as adding a new function.

You should also add a class for enemys, or really object in your game that you'll repeatedly add. They're useful for other things as well but that's the main thing -- they're pretty easy once you get the hang of em, like anything else.

Thank you for this, it partially works because the bullets fly out in all directions indefinitely.Is there a workaround for this so it fires one at a time if I hold down space or press space. I get this is hard to do, or at least to my understanding, and I'm very grateful for the help but I can't seem to understand how to make it work? Again, the help is much appreciated and a fix for it would be grand : )

https://microstudio.io/i/Loginus/bullet/

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community