0

So I implemented a shoot function into my game where the spaceship can shoot a bullet every time I press space but the problem is that whenever the bullet is shot out if I move my character at the point where i shot the bullets follow the x coordinate of my player but I want them to just go straight when I shoot it

this is how it looks so far.

enter image description here

this is my code:

function love.load()
  my_background = love.graphics.newImage('images/space-background.png')

  player = {}
  player.x = love.graphics.getWidth()/2 - 30
  player.y = love.graphics.getHeight()*(7/8)
  player.speed = 5
  player.image = love.graphics.newImage('images/space_ship.png')

  bullet_speed = -250
  bullets = {}

  score=0
  lives=3

end

function love.update(dt)
  for i,v in ipairs(bullets) do
    v.y = v.y + (v.dy * dt)
  end
end

function love.draw()

  love.graphics.setColor(1,1,1,1)
  love.graphics.draw(my_background)

  love.graphics.draw(player.image, player.x, player.y, 0, 1)

  if love.keyboard.isDown("up") then
    player.y = player.y - 5
  end

  if love.keyboard.isDown("down") then
    player.y = player.y + 5
  end

  if love.keyboard.isDown("left") then
    player.x = player.x - 5
  end

  if love.keyboard.isDown("right") then
    player.x = player.x + 5
  end

  love.graphics.setColor(255, 100, 0)
  for i,v in ipairs(bullets) do
    love.graphics.circle("fill", player.x+30, v.y, 3)
  end
end

function love.keypressed(key, scancode, isrepeat)
    if key == "space" then
        local start_y = player.y + 49 / 2
        local bullet_y = bullet_speed
        table.insert(bullets, {y = start_y,dy =  bullet_y})
    end
end
1
  • please share the bullet class as well, I would want to see the update function in it, but it would be better if you share the whole thing. Apr 2 at 4:00

You must log in to answer this question.

Browse other questions tagged .