The game I'm working on is supposed to be a curtain shoot em up. I didn't want to get too far ahead of myself so everything I've done so far is in line with the guides and tutorials I've read / watched. My intention is to eventually have multiple playable characters and different types of bullets for each character as well. I have no idea how to get homing bullets to work.
So I tried something simple. By default, bullets will travel towards the top of the screen when you fire them. I wanted the to stop traveling vertically when they lined up with an enemy on the x-axis and instead, travel horizontally. But I can't get that to work either. Here is part of what I have so far.
Code: Select all
canShoot = true
canShootTimerMax = 0.2
canShootTimer = canShootTimerMax --lines 3,4,5 solve my infinite bullet problem
BrontoBTimerMax = 0.4
BrontoBTimer = BrontoBTimerMax
isAlive = true
function love.load()
Score = 0
hero = {} -- new table for the hero
hero.x = 300 -- x,y coordinates of the hero
hero.y = 450
hero.width = 30
hero.height = 15
hero.speed = 180
hero.bullets = {} -- table for hero's shots
hero.bullets.BWidth = 2
hero.bullets.BHeight = 5
enemies = {} --table for enemies.
enemies.width = 40
enemies.height = 40
end
Code: Select all
canShootTimer = canShootTimer - (1 * dt)
if canShootTimer < 0 then
canShoot = true
end
if love.keyboard.isDown("z") and canShoot and isAlive then
K_shoot()
end
for i,v in ipairs(hero.bullets) do
v.y = v.y - dt * 400
if v.y < 0 then
table.remove (hero.bullets, i)
end
end
BrontoBTimer = BrontoBTimer - (1 * dt)
if BrontoBTimer < 0 then
BrontoBTimer = BrontoBTimerMax
randomNumber = math.random(10, love.graphics.getWidth()-10)
Bronto_Burt ={x = randomNumber, y =(-10)}
table.insert(enemies, Bronto_Burt)
end
--Enemy position
for i, e in ipairs(enemies) do
e.y = e.y + (200 * dt)
if e.y > 770 then
table.remove(enemies, i)
end
end
--collision between enemies and bullets
for i, e in ipairs(enemies) do
for j, v in ipairs(hero.bullets) do
if CheckCollision(e.x, e.y, enemies.width, enemies.height, v.x, v.y, hero.bullets.BWidth, hero.bullets.BHeight) then
table.remove(hero.bullets, j)
table.remove(enemies, i)
Score = Score + 10
end
end
Code: Select all
function K_shoot()
local shot = {}
shot.x = hero.x + hero.width/2
shot.y = hero.y
table.insert(hero.bullets,shot)
canShoot = false
canShootTimer = canShootTimerMax
end
function CheckCollision (x1, y1, w1, h1, x2, y2, w2, h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
I've yammered on a lot, but any help would be appreciated. I really hope I'm not a lost cause here.