Help! Need help with bullet and enemy collision
Posted: Mon Oct 08, 2018 10:21 pm
Hello , I am trying to make a working collision detection and response for my players bullets hitting the enemy ships. I would prefer to do this the simple way without using physics since this is a very simple game. I have looked at all the other threads talking about this and trust me its all over complicated non sense this is my first real project so thank you to anyone that can help.
Heres my code:
paused = 'true'
time = 0
timeinbetweenbullets = 0.4
spawntimer = 1
enemies = {}
function love.load()
player = {}
player.x = 20
player.y = 20
player.width = 200
player.height = 100
player.speed = 250
bullet = 0
player.bullets = {}
function player.fire()
bullet = {}
bullet.x = player.x + 48
bullet.y = player.y
bullet.hitbox = 10
table.insert(player.bullets,bullet)
end
player_sprite = love.graphics.newImage('gfx/SpaceShip.png')
background = love.graphics.newImage('gfx/Space.png')
enemy_sprite = love.graphics.newImage('gfx/alien.png')
end
function spawnEnemies()
local enemy = {}
enemy.x = math.random(800)
enemy.y = math.random(50,100)
hitbox = {}
hitbox.x = 50
hitbox.y = 60
table.insert(enemies,enemy)
end
function love.update(dt)
time = time + dt
if love.keyboard.isDown('d') and player.x + player.width < 900 then
player.x = player.x + player.speed*dt
end
if love.keyboard.isDown('a') and player.x > 0 then
player.x = player.x - player.speed*dt
end
if love.keyboard.isDown('w') and player.y > 0 then
player.y = player.y - player.speed*dt
end
if love.keyboard.isDown('s') and player.y + player.height < 600 then
player.y = player.y + player.speed*dt
end
if love.keyboard.isDown('escape') then
love.event.quit()
end
if love.keyboard.isDown('enter') then
paused = 'false'
end
if love.keyboard.isDown('space') and
time > timeinbetweenbullets then
time = 0
player.fire()
end
for _,b in pairs(player.bullets) do
b.y = b.y - 5
end
spawntimer = spawntimer - dt
if spawntimer <= 0 then
spawnEnemies()
local leftover = math.abs(spawntimer)
spawntimer = 1 + leftover
end
function love.draw()
for i = 0, love.graphics.getWidth() / background:getWidth() do
for j = 0, love.graphics.getHeight() / background:getHeight() do
love.graphics.draw(background, i * background:getWidth(), j * background:getHeight())
love.graphics.draw(player_sprite,player.x,player.y)
for _,e in pairs(enemies) do
love.graphics.draw(enemy_sprite,e.x,e.y)
end
for _,h in pairs(enemies) do
love.graphics.rectangle('line',h.x+26,h.y+32,50,38)
end
for _,s in pairs(player.bullets) do
love.graphics.rectangle('line',s.x,s.y,bullet.hitbox,bullet.hitbox)
for _,b in pairs(player.bullets) do
love.graphics.rectangle('fill',b.x,b.y,5,12)
end
end
end
end
end
end
Heres my code:
paused = 'true'
time = 0
timeinbetweenbullets = 0.4
spawntimer = 1
enemies = {}
function love.load()
player = {}
player.x = 20
player.y = 20
player.width = 200
player.height = 100
player.speed = 250
bullet = 0
player.bullets = {}
function player.fire()
bullet = {}
bullet.x = player.x + 48
bullet.y = player.y
bullet.hitbox = 10
table.insert(player.bullets,bullet)
end
player_sprite = love.graphics.newImage('gfx/SpaceShip.png')
background = love.graphics.newImage('gfx/Space.png')
enemy_sprite = love.graphics.newImage('gfx/alien.png')
end
function spawnEnemies()
local enemy = {}
enemy.x = math.random(800)
enemy.y = math.random(50,100)
hitbox = {}
hitbox.x = 50
hitbox.y = 60
table.insert(enemies,enemy)
end
function love.update(dt)
time = time + dt
if love.keyboard.isDown('d') and player.x + player.width < 900 then
player.x = player.x + player.speed*dt
end
if love.keyboard.isDown('a') and player.x > 0 then
player.x = player.x - player.speed*dt
end
if love.keyboard.isDown('w') and player.y > 0 then
player.y = player.y - player.speed*dt
end
if love.keyboard.isDown('s') and player.y + player.height < 600 then
player.y = player.y + player.speed*dt
end
if love.keyboard.isDown('escape') then
love.event.quit()
end
if love.keyboard.isDown('enter') then
paused = 'false'
end
if love.keyboard.isDown('space') and
time > timeinbetweenbullets then
time = 0
player.fire()
end
for _,b in pairs(player.bullets) do
b.y = b.y - 5
end
spawntimer = spawntimer - dt
if spawntimer <= 0 then
spawnEnemies()
local leftover = math.abs(spawntimer)
spawntimer = 1 + leftover
end
function love.draw()
for i = 0, love.graphics.getWidth() / background:getWidth() do
for j = 0, love.graphics.getHeight() / background:getHeight() do
love.graphics.draw(background, i * background:getWidth(), j * background:getHeight())
love.graphics.draw(player_sprite,player.x,player.y)
for _,e in pairs(enemies) do
love.graphics.draw(enemy_sprite,e.x,e.y)
end
for _,h in pairs(enemies) do
love.graphics.rectangle('line',h.x+26,h.y+32,50,38)
end
for _,s in pairs(player.bullets) do
love.graphics.rectangle('line',s.x,s.y,bullet.hitbox,bullet.hitbox)
for _,b in pairs(player.bullets) do
love.graphics.rectangle('fill',b.x,b.y,5,12)
end
end
end
end
end
end