Enemy Bullet Error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Sonic
Prole
Posts: 11
Joined: Tue Jan 02, 2018 6:25 pm
Contact:

Enemy Bullet Error

Post by Sonic »

Hello, I am having trouble coding my game so the enemy will shoot bullets towards the player. I have looked through videos and the forum to no avail. If you could help me out, I'd appreciate it.

Here is my code:

Code: Select all

platform                   = {}
player                     = {}
enemy                      = {}
enemies                    = {}
enemies_controller         = {}
enemies_controller.enemies = {}

function love.load()
  
  background = love.graphics.newImage('background.png')
  
	platform.width = love.graphics.getWidth()
	platform.height = love.graphics.getHeight()
 
	platform.x = 0
	platform.y = platform.height / 2
 
	player.x = love.graphics.getWidth() / 2
	player.y = love.graphics.getHeight() / 2.05
 
	player.speed = 200
 
	player.img = love.graphics.newImage('sprite.png')
 
	player.ground = player.y
 
	player.y_velocity = 0
 
	player.jump_height = -300
	player.gravity = -500
  
  enemy.x = love.graphics.getWidth() / 2.05
  enemy.y = love.graphics.getHeight() / 2.05
  
  enemy.img = love.graphics.newImage('sprite2.png')
  
  enemy.ground = player.y
 
  enemy.y_velocity = 0
  
  enemy.bullets  = {}
  enemy.cooldown = 30
  enemy.fire     = function()
      bullet = {}
      bullet.x = enemy.x
      bullet.y = 243
      table.insert(enemy.bullets, bullet)
    end
end
 
function love.update(dt)
  
	if love.keyboard.isDown('d') then
		if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
			player.x = player.x + (player.speed * dt)
		end
	elseif love.keyboard.isDown('a') then
		if player.x > 0 then 
			player.x = player.x - (player.speed * dt)
		end
	end
 
	if love.keyboard.isDown('space') then
		if player.y_velocity == 0 then
			player.y_velocity = player.jump_height
		end
	end
 
	if player.y_velocity ~= 0 then
		player.y = player.y + player.y_velocity * dt
		player.y_velocity = player.y_velocity - player.gravity * dt
	end
 
	if player.y > player.ground then
		player.y_velocity = 0
    	player.y = player.ground
	end
end

 if love.keyboard.isDown('e') then
   enemy.fire()
 end
 
function love.draw()
  
  love.graphics.setColor(255, 255, 255)
  love.graphics.draw(background, 1, 1, 0, 1.6, 1.2, 3)
  
	love.graphics.setColor(50, 100, 150)
	love.graphics.rectangle('fill', platform.x, platform.y, platform.width, platform.height)
 
  love.graphics.setColor(255, 255, 255)
	love.graphics.draw(player.img, player.x, player.y, 0, 1, 1.4, 0, 32)
  
  love.graphics.draw(enemy.img, 10, 243, 0, 1.1, 1.5)
  
  -- draw bullets
  love.graphics.setColor(255, 255, 255)
  for _,v in pairs(enemy.bullets) do
    love.graphics.rectangle("fill", v.x, v.y, 10, 5)
  end
end
Thanks in advance.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Enemy Bullet Error

Post by Sir_Silver »

I've pulled up your code in an editor and after messing with it for a little while, I found more questions than answers. On of the first things I noticed was that you needed to put

Code: Select all

 if love.keyboard.isDown('e') then
   enemy.fire()
 end
inside of love.update.

That begs the next question though, why is the player getting to tell the enemy when to fire? It's not my game, maybe there's a good reason for it, but it doesn't seem right.

The next thing I noticed is that when the bullets spawn, they spawn at enemy.x but they seemingly come out of nowhere, and that's because your rendering of the enemy, and its actual position in the world are not the same - they probably should be.

So this line needs some work

Code: Select all

love.graphics.draw(enemy.img, 10, 243, 0, 1.1, 1.5)
But to answer the question you had, you need to determine the angle between the player and the enemy when you create a bullet, and then make it so that the bullets move in the direction given by the angle by a given speed, one way to do that would be something like this:

Code: Select all

enemy.fire = function()
    bullet = {
        x = enemy.x,
        y = enemy.y,
        angle = math.atan2(player.y - enemy.y, player.x - enemy.x),
        speed = 20
    }

    table.insert(enemy.bullets, bullet)
end
This gives the bullets an angle and a speed, now they know what direction to travel in and how quickly to travel in that direction. Finally, we need to update the bullets so that they move.

Code: Select all

function love.update(dt)
	--your existing code
    for _, bullet in pairs(enemy.bullets) do
        bullet.x = bullet.x + math.cos(bullet.angle) * bullet.speed
        bullet.y = bullet.y + math.sin(bullet.angle) * bullet.speed
    end
Here is a .love which includes the changes I've mentioned (as well as my own sprites because I didn't have the sprites you had [which is why .love files are important])

game.love
(1.61 KiB) Downloaded 128 times

Image
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests