Need some help manipulating my bullets...
Posted: Thu Mar 10, 2016 3:51 am
Hello everyone. I'm trying to make my (first) fan game. I've dabbled in C++ a little before but I'm sort of new to programming in general and lua. I spent a while looking up guides and tutorials on both lua and love 2d but I thought it was finally time to register here and come to you guys for help.
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.
My biggest problem is that I can't seem to find any way for the bullets to interact with the enemies unless I run a for loop for one of the tables inside of the other. If I do that while the bullets are moving vertically, it screws things up. And even then, whatever code I write doesn't do what I want it to (that is, I can't check to see if the bullets and the enemies line up on the x-axis by saying "if bullet.y = enemies.y then...")
I've yammered on a lot, but any help would be appreciated. I really hope I'm not a lost cause here.
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.